Skip to content

Commit

Permalink
Merge pull request #100 from tgwizard/use-id-in-consul-check-filename
Browse files Browse the repository at this point in the history
Prefer id over name consul check filename
  • Loading branch information
reset committed Dec 16, 2014
2 parents 8dc6de1 + 4ae75d2 commit 8d34e11
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 0 deletions.
5 changes: 5 additions & 0 deletions providers/check_def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
use_inline_resources

action :create do
file new_resource.path_with_name do
action :delete
only_if { new_resource.id }
end

file new_resource.path do
user node['consul']['service_user']
group node['consul']['service_group']
Expand Down
4 changes: 4 additions & 0 deletions resources/check_def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
attribute :notes, kind_of: String

def path
::File.join(node['consul']['config_dir'], "check-#{id || name}.json")
end

def path_with_name
::File.join(node['consul']['config_dir'], "check-#{name}.json")
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
include_recipe "consul"
consul_check_def "dummy name" do
id "uniqueid"
script "curl http://localhost:8888/health"
interval "10s"
ttl "50s"
notes "Blahblah"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include_recipe "consul"
consul_check_def "dummy name" do
id "uniqueid"
action :delete
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include_recipe "consul"
consul_check_def "dummy name" do
script "curl http://localhost:8888/health"
interval "10s"
ttl "50s"
notes "Blahblah"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include_recipe "consul"
consul_check_def "dummy name" do
action :delete
end
60 changes: 60 additions & 0 deletions spec/unit/resources/check_def_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'spec_helper'
require 'chefspec/berkshelf'

describe_resource "consul_check_def" do
describe "with id" do
let(:check_def_path) { "/etc/consul.d/check-uniqueid.json" }

describe "create" do
let(:example_recipe) { "consul_spec::check_def_with_id_create" }

it "removes any check previously registered by name" do
expect(chef_run).to delete_file("/etc/consul.d/check-dummy name.json")
end

it "register the check" do
['"name": "dummy name"', '"id": "uniqueid"', '"script": "curl http://localhost:8888/health"',
'"interval": "10s"', '"ttl": "50s"', '"notes": "Blahblah"'].each do |content|
expect(chef_run).to render_file(check_def_path)
.with_content(content)
end
end
end

describe "delete" do
let(:example_recipe) { "consul_spec::check_def_with_id_delete" }

it "de-register the check" do
expect(chef_run).to delete_file(check_def_path)
end
end
end

describe "without id" do
let(:check_def_path) { "/etc/consul.d/check-dummy name.json" }

describe "create" do
let(:example_recipe) { "consul_spec::check_def_without_id_create" }

it "does not remove checks previously registered by name" do
expect(chef_run).not_to delete_file("/etc/consul.d/check-dummy name.json")
end

it "register the check" do
['"name": "dummy name"', '"script": "curl http://localhost:8888/health"',
'"interval": "10s"', '"ttl": "50s"', '"notes": "Blahblah"'].each do |content|
expect(chef_run).to render_file(check_def_path)
.with_content(content)
end
end
end

describe "delete" do
let(:example_recipe) { "consul_spec::check_def_without_id_delete" }

it "de-register the check" do
expect(chef_run).to delete_file(check_def_path)
end
end
end
end

0 comments on commit 8d34e11

Please sign in to comment.