Skip to content

Commit

Permalink
added multiple cookbook path, role path and databags path support
Browse files Browse the repository at this point in the history
  • Loading branch information
exceedhl committed Jun 14, 2012
1 parent bcdf08e commit 517a1db
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
toft (0.0.11)
toft (0.0.12)
net-ssh

GEM
Expand Down
1 change: 1 addition & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Vagrant::Config.run do |config|
config.vm.box = "centos6-x86_64"
config.vm.network :hostonly, "33.33.33.13"
config.vm.share_folder("v-root3", "/home/vagrant/code", ".", :nfs => true)
config.vm.boot_mode = :gui
end

end
10 changes: 9 additions & 1 deletion features/chef.feature
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Scenario: Toft should not deal with empty cookbook and role path
Then Running chef "recipe[test]" on node "n1" should succeed
When I set the cookbook path to empty
Then Running chef "recipe[test]" on node "n1" should fail
Scenario: Run chef recipe with json attributes file
Given I have a clean running node n1
When I run "recipe[test::attribute]" on node "n1" and overwrite attributes with json file "attributes.json"
Expand All @@ -65,6 +65,14 @@ Scenario: Run chef recipe with data bags
Then Node "n1" should have file or directory "/tmp/stub/bag2item1"
Then Node "n1" should have file or directory "/tmp/stub/bag2item2"

Scenario: Toft should deal with multiple cookbook, roles and databag paths
Given I have a clean running node n1
When I add another cookbook "cookbooks2"
And I add another role dir "roles2"
And I add another databag dir "data_bags2"
Then Running chef "role[test2]" on node "n1" should succeed
And Node "n1" should have file or directory "/tmp/test2/bag3item3"

Scenario: Run non-exist recipe

Scenario: Run non-exist role
Expand Down
12 changes: 12 additions & 0 deletions features/step_definitions/chef.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,15 @@
When /^I run "([^"]*)" on node "([^"]*)" and overwrite attributes with json file "([^"]*)" and chef attributes:$/ do |run_list, node, json_file, table|
find(node).run_chef run_list, {:json => CHEF_FIXTURE_PATH + '/attributes.json', :attributes => Toft::ChefAttributes.new(table)}
end

When /^I add another cookbook "(.*?)"$/ do |cookbook_path|
Toft.cookbook_path = [CHEF_FIXTURE_PATH + '/cookbooks', CHEF_FIXTURE_PATH + "/#{cookbook_path}"]
end

When /^I add another role dir "(.*?)"$/ do |role_path|
Toft.role_path = [CHEF_FIXTURE_PATH + '/roles', CHEF_FIXTURE_PATH + "/#{role_path}"]
end

When /^I add another databag dir "(.*?)"$/ do |databag_path|
Toft.data_bag_path = [CHEF_FIXTURE_PATH + '/data_bags', CHEF_FIXTURE_PATH + "/#{databag_path}"]
end
2 changes: 1 addition & 1 deletion features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
end

at_exit do
n1.destroy
# n1.destroy
end
5 changes: 5 additions & 0 deletions fixtures/chef/cookbooks2/test2/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
item = data_bag_item("bag3", "item3")
directory "/tmp/test2/#{item['value']}" do
action :create
recursive true
end
4 changes: 4 additions & 0 deletions fixtures/chef/data_bags2/bag3/item3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": "item3",
"value": "bag3item3"
}
3 changes: 3 additions & 0 deletions fixtures/chef/roles2/test2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name 'test'
description 'Test!'
run_list "recipe[test2]"
6 changes: 6 additions & 0 deletions lib/toft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ def blank?
empty?
end
end

class Array
def blank?
empty?
end
end
13 changes: 10 additions & 3 deletions lib/toft/chef/chef_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ def copy_chef_material
raise ArgumentError, "Toft.cookbook_path can not be empty!" if Toft.cookbook_path.blank?
rm_rf "#{@root_dir}#{DEST_CHEF_TMP}"
mkdir_p "#{@root_dir}#{DEST_CHEF_TMP}"
cp_r Toft.cookbook_path, "#{@root_dir}#{DEST_COOKBOOK_PATH}"
cp_r Toft.role_path, "#{@root_dir}#{DEST_ROLE_PATH}" unless roles_missing?
cp_r Toft.data_bag_path, "#{@root_dir}#{DEST_DATA_BAG_PATH}" unless data_bags_missing?
copy_resources Toft.cookbook_path, "#{@root_dir}#{DEST_COOKBOOK_PATH}"
copy_resources Toft.role_path, "#{@root_dir}#{DEST_ROLE_PATH}" unless roles_missing?
copy_resources Toft.data_bag_path, "#{@root_dir}#{DEST_DATA_BAG_PATH}" unless data_bags_missing?
end

def copy_resources(src_dir, dest_dir)
src_dir = [src_dir] if src_dir.is_a? String
src_dir.each do |dir|
cp_r dir + "/.", dest_dir
end
end

def roles_missing?
Expand Down
2 changes: 1 addition & 1 deletion lib/toft/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Toft
VERSION = "0.0.12"
VERSION = "0.0.13"
end
7 changes: 7 additions & 0 deletions spec/toft/chef/chef_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@
lambda { cf.run "run list" }.should raise_error RuntimeError
end

it "should throw exception if data bags not exist" do
Toft.cookbook_path = "#{PROJECT_ROOT}/fixtures/chef/cookbookse"
Toft.data_bag_path = "non-exist-data-bags"
cf = Toft::Chef::ChefRunner.new "#{PROJECT_ROOT}/tmp"
lambda { cf.run "run list" }.should raise_error RuntimeError
end

end

0 comments on commit 517a1db

Please sign in to comment.