Skip to content

Commit

Permalink
Merge pull request #74 from thedebugger/update_last_action
Browse files Browse the repository at this point in the history
Update last action so that notifications can work
  • Loading branch information
reset committed Oct 30, 2014
2 parents f987a54 + 6602231 commit 349383c
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Berksfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
source 'https://supermarket.getchef.com'

metadata

group :test do
cookbook "consul_spec", path: "spec/fixtures/cookbooks/consul_spec"
end
9 changes: 9 additions & 0 deletions libraries/matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if defined?(ChefSpec)
def create_consul_service_def(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:consul_service_def, :create, resource_name)
end

def delete_consul_service_def(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:consul_service_def, :delete, resource_name)
end
end
32 changes: 20 additions & 12 deletions providers/service_def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,31 @@
# limitations under the License.
#

use_inline_resources
use_inline_resources if defined? use_inline_resources

action :create do
file new_resource.path do
user node['consul']['service_user']
group node['consul']['service_group']
mode 0600
content new_resource.to_json
def set_updated
r = yield
new_resource.updated_by_last_action(r.updated_by_last_action?)
end

action :create
notifies :reload, 'service[consul]', :delayed
action :create do
set_updated do
file new_resource.path do
user node['consul']['service_user']
group node['consul']['service_group']
mode 0600
content new_resource.to_json
action :create
notifies :reload, 'service[consul]', :delayed
end
end
end

action :delete do
file new_resource.path do
action :delete
notifies :reload, 'service[consul]', :delayed
set_updated do
file new_resource.path do
action :delete
notifies :reload, 'service[consul]', :delayed
end
end
end
3 changes: 3 additions & 0 deletions spec/fixtures/cookbooks/consul_spec/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name 'consul_spec'

depends 'consul'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include_recipe "consul"
consul_service_def "dummy" do
id "uniqueid"
port 8888
tags ['releases', 'v1']
check :interval => "10s", :script => "curl http://localhost:8888/health"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include_recipe "consul"
consul_service_def "dummy" do
id "uniqueid"
action :delete
end
43 changes: 43 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
RSpec.configure do |config|
config.color = true
config.alias_example_group_to :describe_recipe, type: :recipe
config.alias_example_group_to :describe_resource, type: :resource

config.filter_run :focus
config.run_all_when_everything_filtered = true
Expand Down Expand Up @@ -44,3 +45,45 @@ def node_attributes
}
end
end

RSpec.shared_context "resource tests", type: :resource do
let(:chef_run) do
ChefSpec::SoloRunner.new(node_attributes.merge(step_into)).converge(example_recipe)
end

let(:example_recipe) do
fail %(
Please specify the name of the test recipe that executes your recipe:
let(:example_recipe) do
"consul_spec::service_def"
end
)
end

let(:node) { chef_run.node }

def node_attributes
{
platform: 'ubuntu',
version: '12.04'
}
end

let(:step_into) do
{ step_into: [cookbook_name] }
end

def cookbook_recipe_names
described_recipe.split("::", 2)
end

def cookbook_name
cookbook_recipe_names.first
end

def recipe_name
cookbook_recipe_names.last
end
end
31 changes: 31 additions & 0 deletions spec/unit/resources/service_def_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'
require 'chefspec/berkshelf'

describe_resource "consul_service_def" do
let(:service_def_path) { "/etc/consul.d/service-uniqueid.json" }

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

it "register the service" do
['"name": "dummy"', '"port": 8888',
'"script": "curl http://localhost:8888/health"'].each do |content|
expect(chef_run).to render_file(service_def_path)
.with_content(content)

expect(chef_run.file(service_def_path))
.to notify('service[consul]').to(:reload).delayed
end
end
end

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

it "de-register the service" do
expect(chef_run).to delete_file(service_def_path)
expect(chef_run.file(service_def_path))
.to notify('service[consul]').to(:reload).delayed
end
end
end

0 comments on commit 349383c

Please sign in to comment.