diff options
21 files changed, 1859 insertions, 0 deletions
diff --git a/lib/chef/knife/role_env_run_list_add.rb b/lib/chef/knife/role_env_run_list_add.rb new file mode 100644 index 0000000000..faf84ccfdb --- /dev/null +++ b/lib/chef/knife/role_env_run_list_add.rb @@ -0,0 +1,86 @@ +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: William Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2009 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/knife' + +class Chef + class Knife + class RoleEnvRunListAdd < Knife + + deps do + require 'chef/role' + require 'chef/json_compat' + end + + banner "knife role env_run_list add [ROLE] [ENVIRONMENT] [ENTRY[,ENTRY]] (options)" + + option :after, + :short => "-a ITEM", + :long => "--after ITEM", + :description => "Place the ENTRY in the run list after ITEM" + + def add_to_env_run_list(role, environment, entries, after=nil) + if after + nlist = [] + unless role.env_run_lists.key?(environment) + role.env_run_lists_add(environment => nlist) + end + role.run_list_for(environment).each do |entry| + nlist << entry + if entry == after + entries.each { |e| nlist << e } + end + end + role.env_run_lists_add(environment => nlist) + else + nlist = [] + unless role.env_run_lists.key?(environment) + role.env_run_lists_add(environment => nlist) + end + role.run_list_for(environment).each do |entry| + nlist << entry + end + entries.each { |e| nlist << e } + role.env_run_lists_add(environment => nlist) + end + end + + def run + role = Chef::Role.load(@name_args[0]) + role.name(@name_args[0]) + environment = @name_args[1] + + if @name_args.size > 2 + # Check for nested lists and create a single plain one + entries = @name_args[2..-1].map do |entry| + entry.split(',').map { |e| e.strip } + end.flatten + else + # Convert to array and remove the extra spaces + entries = @name_args[2].split(',').map { |e| e.strip } + end + + add_to_env_run_list(role, environment, entries, config[:after]) + role.save + config[:env_run_list] = true + output(format_for_display(role)) + end + + end + end +end diff --git a/lib/chef/knife/role_env_run_list_clear.rb b/lib/chef/knife/role_env_run_list_clear.rb new file mode 100644 index 0000000000..4304f29a38 --- /dev/null +++ b/lib/chef/knife/role_env_run_list_clear.rb @@ -0,0 +1,55 @@ +# +# Author:: Mike Fiedler (<miketheman@gmail.com>) +# Author:: William Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2013 Mike Fiedler +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/knife' + +class Chef + class Knife + class RoleEnvRunListClear < Knife + + deps do + require 'chef/role' + require 'chef/json_compat' + end + + banner "knife role env_run_list clear [ROLE] [ENVIRONMENT]" + def clear_env_run_list(role, environment) + nlist = [] + role.env_run_lists_add(environment => nlist) + end + + def run + if @name_args.size > 2 + ui.fatal "You must not supply an environment run list." + show_usage + exit 1 + end + role = Chef::Role.load(@name_args[0]) + role.name(@name_args[0]) + environment = @name_args[1] + + clear_env_run_list(role, environment) + role.save + config[:env_run_list] = true + output(format_for_display(role)) + end + + end + end +end diff --git a/lib/chef/knife/role_env_run_list_remove.rb b/lib/chef/knife/role_env_run_list_remove.rb new file mode 100644 index 0000000000..9ffc3f520e --- /dev/null +++ b/lib/chef/knife/role_env_run_list_remove.rb @@ -0,0 +1,57 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Copyright:: Copyright (c) 2009 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/knife' + +class Chef + class Knife + class RoleEnvRunListRemove < Knife + + deps do + require 'chef/role' + require 'chef/json_compat' + end + + banner "knife role env_run_list remove [ROLE] [ENVIRONMENT] [ENTRIES]" + + def remove_from_env_run_list(role, environment, item_to_remove) + nlist = [] + role.run_list_for(environment).each do |entry| + nlist << entry unless entry == item_to_remove + #unless entry == @name_args[2] + # nlist << entry + #end + end + role.env_run_lists_add(environment => nlist) + end + + def run + role = Chef::Role.load(@name_args[0]) + role.name(@name_args[0]) + environment = @name_args[1] + item_to_remove = @name_args[2] + + remove_from_env_run_list(role, environment, item_to_remove) + role.save + config[:env_run_list] = true + output(format_for_display(role)) + end + + end + end +end diff --git a/lib/chef/knife/role_env_run_list_replace.rb b/lib/chef/knife/role_env_run_list_replace.rb new file mode 100644 index 0000000000..146d0c4905 --- /dev/null +++ b/lib/chef/knife/role_env_run_list_replace.rb @@ -0,0 +1,59 @@ +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: William Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2009 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/knife' + +class Chef + class Knife + class RoleEnvRunListReplace < Knife + + deps do + require 'chef/role' + require 'chef/json_compat' + end + + banner "knife role env_run_list replace [ROLE] [ENVIRONMENT] [OLD_ENTRY] [NEW_ENTRY] " + + def replace_in_env_run_list(role, environment, old_entry, new_entry) + nlist = [] + role.run_list_for(environment).each do |entry| + if entry == old_entry + nlist << new_entry + else + nlist << entry + end + end + role.env_run_lists_add(environment => nlist) + end + + def run + role = Chef::Role.load(@name_args[0]) + role.name(@name_args[0]) + environment = @name_args[1] + old_entry = @name_args[2] + new_entry = @name_args[3] + + replace_in_env_run_list(role, environment, old_entry, new_entry) + role.save + config[:env_run_list] = true + output(format_for_display(role)) + end + + end + end +end diff --git a/lib/chef/knife/role_env_run_list_set.rb b/lib/chef/knife/role_env_run_list_set.rb new file mode 100644 index 0000000000..a1618516c0 --- /dev/null +++ b/lib/chef/knife/role_env_run_list_set.rb @@ -0,0 +1,70 @@ +# +# Author:: Mike Fiedler (<miketheman@gmail.com>) +# Author:: William Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2013 Mike Fiedler +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/knife' + +class Chef + class Knife + class RoleEnvRunListSet < Knife + + deps do + require 'chef/role' + require 'chef/json_compat' + end + + banner "knife role env_run_list set [ROLE] [ENVIRONMENT] [ENTRIES]" + + # Clears out any existing env_run_list_items and sets them to the + # specified entries + def set_env_run_list(role, environment, entries) + nlist = [] + unless role.env_run_lists.key?(environment) + role.env_run_lists_add(environment => nlist) + end + entries.each { |e| nlist << e } + role.env_run_lists_add(environment => nlist) + end + + def run + role = Chef::Role.load(@name_args[0]) + role.name(@name_args[0]) + environment = @name_args[1] + if @name_args.size < 2 + ui.fatal "You must supply both a role name and an environment run list." + show_usage + exit 1 + elsif @name_args.size > 2 + # Check for nested lists and create a single plain one + entries = @name_args[2..-1].map do |entry| + entry.split(',').map { |e| e.strip } + end.flatten + else + # Convert to array and remove the extra spaces + entries = @name_args[2].split(',').map { |e| e.strip } + end + + set_env_run_list(role, environment, entries ) + role.save + config[:env_run_list] = true + output(format_for_display(role)) + end + + end + end +end diff --git a/lib/chef/knife/role_run_list_add.rb b/lib/chef/knife/role_run_list_add.rb new file mode 100644 index 0000000000..c9d7785bb7 --- /dev/null +++ b/lib/chef/knife/role_run_list_add.rb @@ -0,0 +1,86 @@ +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: William Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2009 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/knife' + +class Chef + class Knife + class RoleRunListAdd < Knife + + deps do + require 'chef/role' + require 'chef/json_compat' + end + + banner "knife role run_list add [ROLE] [ENTRY[,ENTRY]] (options)" + + option :after, + :short => "-a ITEM", + :long => "--after ITEM", + :description => "Place the ENTRY in the run list after ITEM" + + def add_to_env_run_list(role, environment, entries, after=nil) + if after + nlist = [] + unless role.env_run_lists.key?(environment) + role.env_run_lists_add(environment => nlist) + end + role.run_list_for(environment).each do |entry| + nlist << entry + if entry == after + entries.each { |e| nlist << e } + end + end + role.env_run_lists_add(environment => nlist) + else + nlist = [] + unless role.env_run_lists.key?(environment) + role.env_run_lists_add(environment => nlist) + end + role.run_list_for(environment).each do |entry| + nlist << entry + end + entries.each { |e| nlist << e } + role.env_run_lists_add(environment => nlist) + end + end + + def run + role = Chef::Role.load(@name_args[0]) + role.name(@name_args[0]) + environment = "_default" + + if @name_args.size > 1 + # Check for nested lists and create a single plain one + entries = @name_args[1..-1].map do |entry| + entry.split(',').map { |e| e.strip } + end.flatten + else + # Convert to array and remove the extra spaces + entries = @name_args[1].split(',').map { |e| e.strip } + end + + add_to_env_run_list(role, environment, entries, config[:after]) + role.save + config[:env_run_list] = true + output(format_for_display(role)) + end + + end + end +end diff --git a/lib/chef/knife/role_run_list_clear.rb b/lib/chef/knife/role_run_list_clear.rb new file mode 100644 index 0000000000..ed6225d83d --- /dev/null +++ b/lib/chef/knife/role_run_list_clear.rb @@ -0,0 +1,55 @@ +# +# Author:: Mike Fiedler (<miketheman@gmail.com>) +# Author:: William Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2013 Mike Fiedler +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/knife' + +class Chef + class Knife + class RoleRunListClear < Knife + + deps do + require 'chef/role' + require 'chef/json_compat' + end + + banner "knife role run_list clear [ROLE]" + def clear_env_run_list(role, environment) + nlist = [] + role.env_run_lists_add(environment => nlist) + end + + def run + if @name_args.size > 2 + ui.fatal "You must not supply an environment run list." + show_usage + exit 1 + end + role = Chef::Role.load(@name_args[0]) + role.name(@name_args[0]) + environment = "_default" + + clear_env_run_list(role, environment) + role.save + config[:env_run_list] = true + output(format_for_display(role)) + end + + end + end +end diff --git a/lib/chef/knife/role_run_list_remove.rb b/lib/chef/knife/role_run_list_remove.rb new file mode 100644 index 0000000000..d783b34ab4 --- /dev/null +++ b/lib/chef/knife/role_run_list_remove.rb @@ -0,0 +1,57 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Copyright:: Copyright (c) 2009 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/knife' + +class Chef + class Knife + class RoleRunListRemove < Knife + + deps do + require 'chef/role' + require 'chef/json_compat' + end + + banner "knife role run_list remove [ROLE] [ENTRY]" + + def remove_from_env_run_list(role, environment, item_to_remove) + nlist = [] + role.run_list_for(environment).each do |entry| + nlist << entry unless entry == item_to_remove + #unless entry == @name_args[2] + # nlist << entry + #end + end + role.env_run_lists_add(environment => nlist) + end + + def run + role = Chef::Role.load(@name_args[0]) + role.name(@name_args[0]) + environment = "_default" + item_to_remove = @name_args[1] + + remove_from_env_run_list(role, environment, item_to_remove) + role.save + config[:env_run_list] = true + output(format_for_display(role)) + end + + end + end +end diff --git a/lib/chef/knife/role_run_list_replace.rb b/lib/chef/knife/role_run_list_replace.rb new file mode 100644 index 0000000000..1ab94df111 --- /dev/null +++ b/lib/chef/knife/role_run_list_replace.rb @@ -0,0 +1,59 @@ +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: William Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2009 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/knife' + +class Chef + class Knife + class RoleRunListReplace < Knife + + deps do + require 'chef/role' + require 'chef/json_compat' + end + + banner "knife role run_list replace [ROLE] [OLD_ENTRY] [NEW_ENTRY] " + + def replace_in_env_run_list(role, environment, old_entry, new_entry) + nlist = [] + role.run_list_for(environment).each do |entry| + if entry == old_entry + nlist << new_entry + else + nlist << entry + end + end + role.env_run_lists_add(environment => nlist) + end + + def run + role = Chef::Role.load(@name_args[0]) + role.name(@name_args[0]) + environment = "_default" + old_entry = @name_args[1] + new_entry = @name_args[2] + + replace_in_env_run_list(role, environment, old_entry, new_entry) + role.save + config[:env_run_list] = true + output(format_for_display(role)) + end + + end + end +end diff --git a/lib/chef/knife/role_run_list_set.rb b/lib/chef/knife/role_run_list_set.rb new file mode 100644 index 0000000000..b9675c70dd --- /dev/null +++ b/lib/chef/knife/role_run_list_set.rb @@ -0,0 +1,70 @@ +# +# Author:: Mike Fiedler (<miketheman@gmail.com>) +# Author:: William Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2013 Mike Fiedler +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/knife' + +class Chef + class Knife + class RoleRunListSet < Knife + + deps do + require 'chef/role' + require 'chef/json_compat' + end + + banner "knife role run_list set [ROLE] [ENTRIES]" + + # Clears out any existing env_run_list_items and sets them to the + # specified entries + def set_env_run_list(role, environment, entries) + nlist = [] + unless role.env_run_lists.key?(environment) + role.env_run_lists_add(environment => nlist) + end + entries.each { |e| nlist << e } + role.env_run_lists_add(environment => nlist) + end + + def run + role = Chef::Role.load(@name_args[0]) + role.name(@name_args[0]) + environment = "_default" + if @name_args.size < 1 + ui.fatal "You must supply both a role name and an environment run list." + show_usage + exit 1 + elsif @name_args.size > 1 + # Check for nested lists and create a single plain one + entries = @name_args[1..-1].map do |entry| + entry.split(',').map { |e| e.strip } + end.flatten + else + # Convert to array and remove the extra spaces + entries = @name_args[1].split(',').map { |e| e.strip } + end + + set_env_run_list(role, environment, entries ) + role.save + config[:env_run_list] = true + output(format_for_display(role)) + end + + end + end +end diff --git a/lib/chef/role.rb b/lib/chef/role.rb index 4b4669f4b5..2f174116cf 100644 --- a/lib/chef/role.rb +++ b/lib/chef/role.rb @@ -103,6 +103,16 @@ class Chef alias :env_run_list :env_run_lists + def env_run_lists_add(env_run_lists=nil) + if (!env_run_lists.nil?) + env_run_lists.each { |k,v| @env_run_lists[k] = Chef::RunList.new(*Array(v))} + end + @env_run_lists + end + + alias :env_run_list_add :env_run_lists_add + + def default_attributes(arg=nil) set_or_return( :default_attributes, diff --git a/spec/unit/knife/role_env_run_list_add_spec.rb b/spec/unit/knife/role_env_run_list_add_spec.rb new file mode 100644 index 0000000000..1e8ddfc1ca --- /dev/null +++ b/spec/unit/knife/role_env_run_list_add_spec.rb @@ -0,0 +1,217 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Will Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' + +describe Chef::Knife::RoleEnvRunListAdd do + before(:each) do +# Chef::Config[:role_name] = "websimian" +# Chef::Config[:env_name] = "QA" + @knife = Chef::Knife::RoleEnvRunListAdd.new + @knife.config = { + :after => nil + } + @knife.name_args = [ "will", "QA", "role[monkey]" ] + @knife.stub!(:output).and_return(true) + @role = Chef::Role.new() + @role.stub!(:save).and_return(true) + Chef::Role.stub!(:load).and_return(@role) + end + + describe "run" do + +# it "should display all the things" do +# @knife.run +# @role.to_json.should == 'show all the things' +# end + + it "should have an empty default run list" do + @knife.run + @role.run_list[0].should be_nil + end + + it "should have a QA environment" do + @knife.run + @role.active_run_list_for('QA').should == 'QA' + end + + it "should load the role named will" do + Chef::Role.should_receive(:load).with("will") + @knife.run + end + + it "should be able to add an environment specific run list" do + @knife.run + @role.run_list_for('QA')[0].should == 'role[monkey]' + end + + it "should save the role" do + @role.should_receive(:save) + @knife.run + end + + it "should print the run list" do + @knife.should_receive(:output).and_return(true) + @knife.run + end + + describe "with -a or --after specified" do + it "should not create a change if the specified 'after' never comes" do + @role.run_list_for("_default") << "role[acorns]" + @role.run_list_for("_default") << "role[barn]" + @knife.config[:after] = "role[acorns]" + @knife.name_args = [ "will", "QA", "role[pad]" ] + @knife.run + @role.run_list_for("QA")[0].should be_nil + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should == "role[barn]" + @role.run_list[2].should be_nil + end + + it "should add to the run list after the specified entries in the QA run list" do + #Setup + @role.run_list_for("_default") << "role[acorns]" + @role.run_list_for("_default") << "role[barn]" + @knife.run + @role.run_list_for("QA") << "role[pencil]" + @role.run_list_for("QA") << "role[pen]" + #Configuration we are testing + @knife.config[:after] = "role[pencil]" + @knife.name_args = [ "will", "QA", "role[pad]", "role[whackadoo]" ] + @knife.run + #The actual tests + @role.run_list_for("QA")[0].should == "role[monkey]" + @role.run_list_for("QA")[1].should == "role[pencil]" + @role.run_list_for("QA")[2].should == "role[pad]" + @role.run_list_for("QA")[3].should == "role[whackadoo]" + @role.run_list_for("QA")[4].should == "role[pen]" + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should == "role[barn]" + @role.run_list[2].should be_nil + end + end + + describe "with more than one role or recipe" do + it "should add to the QA run list all the entries" do + @knife.name_args = [ "will", "QA", "role[monkey],role[duck]" ] + @role.run_list_for("_default") << "role[acorns]" + @knife.run + @role.run_list_for("QA")[0].should == "role[monkey]" + @role.run_list_for("QA")[1].should == "role[duck]" + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should be_nil + end + end + + describe "with more than one role or recipe with space between items" do + it "should add to the run list all the entries" do + @knife.name_args = [ "will", "QA", "role[monkey], role[duck]" ] + @role.run_list_for("_default") << "role[acorns]" + @knife.run + @role.run_list_for("QA")[0].should == "role[monkey]" + @role.run_list_for("QA")[1].should == "role[duck]" + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should be_nil + end + end + + describe "with more than one role or recipe as different arguments" do + it "should add to the run list all the entries" do + @knife.name_args = [ "will", "QA", "role[monkey]", "role[duck]" ] + @role.run_list_for("_default") << "role[acorns]" + @knife.run + @role.run_list_for("QA")[0].should == "role[monkey]" + @role.run_list_for("QA")[1].should == "role[duck]" + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should be_nil + end + end + + describe "with more than one role or recipe as different arguments and list separated by comas" do + it "should add to the run list all the entries" do + @knife.name_args = [ "will", "QA", "role[monkey]", "role[duck],recipe[bird::fly]" ] + @role.run_list_for("_default") << "role[acorns]" + @knife.run + @role.run_list_for("QA")[0].should == "role[monkey]" + @role.run_list_for("QA")[1].should == "role[duck]" + @role.run_list_for("QA")[2].should == "recipe[bird::fly]" + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should be_nil + end + end + + describe "Recipe with version number is allowed" do + it "should add to the run list all the entries including the versioned recipe" do + @knife.name_args = [ "will", "QA", "role[monkey]", "role[duck],recipe[bird::fly@1.1.3]" ] + @role.run_list_for("_default") << "role[acorns]" + @knife.run + @role.run_list_for("QA")[0].should == "role[monkey]" + @role.run_list_for("QA")[1].should == "role[duck]" + @role.run_list_for("QA")[2].should == "recipe[bird::fly@1.1.3]" + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should be_nil + end + end + + describe "with one role or recipe but with an extraneous comma" do + it "should add to the run list one item" do + @role.run_list_for("_default") << "role[acorns]" + @knife.name_args = [ "will", "QA", "role[monkey]," ] + @knife.run + @role.run_list_for("QA")[0].should == "role[monkey]" + @role.run_list_for("QA")[1].should be_nil + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should be_nil + end + end + + describe "with more than one command" do + it "should be able to the environment run list by running multiple knife commands" do + @knife.name_args = [ "will", "QA", "role[blue]," ] + @knife.run + @knife.name_args = [ "will", "QA", "role[black]," ] + @knife.run + @role.run_list_for("QA")[0].should == "role[blue]" + @role.run_list_for("QA")[1].should == "role[black]" + @role.run_list[0].should be_nil + end + end + + describe "with more than one environment" do + it "should add to the run list a second environment in the specific run list" do + @role.run_list_for("_default") << "role[acorns]" + @knife.name_args = [ "will", "QA", "role[blue]," ] + @knife.run + @role.run_list_for("QA") << "role[walnuts]" + + @knife.name_args = [ "will", "PRD", "role[ball]," ] + @knife.run + @role.run_list_for("PRD") << "role[pen]" + + @role.run_list_for("QA")[0].should == "role[blue]" + @role.run_list_for("PRD")[0].should == "role[ball]" + @role.run_list_for("QA")[1].should == "role[walnuts]" + @role.run_list_for("PRD")[1].should == "role[pen]" + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should be_nil + end + end + + end +end diff --git a/spec/unit/knife/role_env_run_list_clear_spec.rb b/spec/unit/knife/role_env_run_list_clear_spec.rb new file mode 100644 index 0000000000..39e57b1055 --- /dev/null +++ b/spec/unit/knife/role_env_run_list_clear_spec.rb @@ -0,0 +1,100 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Will Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' + +describe Chef::Knife::RoleEnvRunListClear do + before(:each) do + Chef::Config[:role_name] = "will" + Chef::Config[:env_name] = "QA" + @setup = Chef::Knife::RoleEnvRunListAdd.new + @setup.name_args = [ "will", "QA", "role[monkey]", "role[person]" ] + + @knife = Chef::Knife::RoleEnvRunListClear.new + @knife.config = { + :print_after => nil + } + @knife.name_args = [ "will", "QA" ] + @knife.stub!(:output).and_return(true) + + @role = Chef::Role.new() + @role.name("will") + @role.stub!(:save).and_return(true) + + @knife.ui.stub!(:confirm).and_return(true) + Chef::Role.stub!(:load).and_return(@role) + + end + + + + describe "run" do + + +# it "should display all the things" do +# @knife.run +# @role.to_json.should == 'show all the things' +# end + + it "should load the node" do + Chef::Role.should_receive(:load).with("will").and_return(@role) + @knife.run + end + + it "should remove the item from the run list" do + @setup.run + @knife.run + @role.run_list_for('QA')[0].should be_nil + @role.run_list[0].should be_nil + end + + it "should save the node" do + @role.should_receive(:save).and_return(true) + @knife.run + end + + it "should print the run list" do + @knife.should_receive(:output).and_return(true) + @knife.config[:print_after] = true + @setup.run + @knife.run + end + + describe "should clear an environmental run list of roles and recipes" do + it "should remove the items from the run list" do + @setup.name_args = [ "will", "QA", "recipe[orange::chicken]", "role[monkey]", "recipe[duck::type]", "role[person]", "role[bird]", "role[town]" ] + @setup.run + @setup.name_args = [ "will", "PRD", "recipe[orange::chicken]", "role[monkey]", "recipe[duck::type]", "role[person]", "role[bird]", "role[town]" ] + @setup.run + @knife.name_args = [ 'will', 'QA' ] + @knife.run + @role.run_list_for('QA')[0].should be_nil + @role.run_list_for('PRD')[0].should == 'recipe[orange::chicken]' + @role.run_list_for('PRD')[1].should == 'role[monkey]' + @role.run_list_for('PRD')[2].should == 'recipe[duck::type]' + @role.run_list_for('PRD')[3].should == 'role[person]' + @role.run_list_for('PRD')[4].should == 'role[bird]' + @role.run_list_for('PRD')[5].should == 'role[town]' + end + end + end +end + + + diff --git a/spec/unit/knife/role_env_run_list_remove_spec.rb b/spec/unit/knife/role_env_run_list_remove_spec.rb new file mode 100644 index 0000000000..53f0f76427 --- /dev/null +++ b/spec/unit/knife/role_env_run_list_remove_spec.rb @@ -0,0 +1,108 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Will Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' + +describe Chef::Knife::RoleEnvRunListRemove do + before(:each) do + Chef::Config[:role_name] = "will" + Chef::Config[:env_name] = "QA" + @setup = Chef::Knife::RoleEnvRunListAdd.new + @setup.name_args = [ "will", "QA", "role[monkey]", "role[person]" ] + + @knife = Chef::Knife::RoleEnvRunListRemove.new + @knife.config = { + :print_after => nil + } + @knife.name_args = [ "will", "QA", "role[monkey]" ] + @knife.stub!(:output).and_return(true) + + @role = Chef::Role.new() + @role.name("will") + @role.stub!(:save).and_return(true) + + @knife.ui.stub!(:confirm).and_return(true) + Chef::Role.stub!(:load).and_return(@role) + + end + + + + describe "run" do + + +# it "should display all the things" do +# @knife.run +# @role.to_json.should == 'show all the things' +# end + + it "should load the node" do + Chef::Role.should_receive(:load).with("will").and_return(@role) + @knife.run + end + + it "should remove the item from the run list" do + @setup.run + @knife.run + @role.run_list_for('QA')[0].should_not == 'role[monkey]' + @role.run_list_for('QA')[0].should == 'role[person]' + @role.run_list[0].should be_nil + end + + it "should save the node" do + @role.should_receive(:save).and_return(true) + @knife.run + end + + it "should print the run list" do + @knife.should_receive(:output).and_return(true) + @knife.config[:print_after] = true + @setup.run + @knife.run + end + + describe "run with a list of roles and recipes" do + it "should remove the items from the run list" do + @setup.name_args = [ "will", "QA", "recipe[orange::chicken]", "role[monkey]", "recipe[duck::type]", "role[person]", "role[bird]", "role[town]" ] + @setup.run + @setup.name_args = [ "will", "PRD", "recipe[orange::chicken]", "role[monkey]", "recipe[duck::type]", "role[person]", "role[bird]", "role[town]" ] + @setup.run + @knife.name_args = [ 'will', 'QA', 'role[monkey]' ] + @knife.run + @knife.name_args = [ 'will', 'QA', 'recipe[duck::type]' ] + @knife.run + @role.run_list_for('QA').should_not include('role[monkey]') + @role.run_list_for('QA').should_not include('recipe[duck::type]') + @role.run_list_for('QA')[0].should == 'recipe[orange::chicken]' + @role.run_list_for('QA')[1].should == 'role[person]' + @role.run_list_for('QA')[2].should == 'role[bird]' + @role.run_list_for('QA')[3].should == 'role[town]' + @role.run_list_for('PRD')[0].should == 'recipe[orange::chicken]' + @role.run_list_for('PRD')[1].should == 'role[monkey]' + @role.run_list_for('PRD')[2].should == 'recipe[duck::type]' + @role.run_list_for('PRD')[3].should == 'role[person]' + @role.run_list_for('PRD')[4].should == 'role[bird]' + @role.run_list_for('PRD')[5].should == 'role[town]' + end + end + end +end + + + diff --git a/spec/unit/knife/role_env_run_list_replace_spec.rb b/spec/unit/knife/role_env_run_list_replace_spec.rb new file mode 100644 index 0000000000..0978e3e566 --- /dev/null +++ b/spec/unit/knife/role_env_run_list_replace_spec.rb @@ -0,0 +1,108 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Will Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' + +describe Chef::Knife::RoleEnvRunListReplace do + before(:each) do + Chef::Config[:role_name] = "will" + Chef::Config[:env_name] = "QA" + @setup = Chef::Knife::RoleEnvRunListAdd.new + @setup.name_args = [ "will", "QA", "role[monkey]", "role[dude]", "role[fixer]" ] + + @knife = Chef::Knife::RoleEnvRunListReplace.new + @knife.config = { + :print_after => nil + } + @knife.name_args = [ "will", "QA", "role[dude]", "role[person]" ] + @knife.stub!(:output).and_return(true) + + @role = Chef::Role.new() + @role.name("will") + @role.stub!(:save).and_return(true) + + @knife.ui.stub!(:confirm).and_return(true) + Chef::Role.stub!(:load).and_return(@role) + + end + + + + describe "run" do + + +# it "should display all the things" do +# @knife.run +# @role.to_json.should == 'show all the things' +# end + + it "should load the node" do + Chef::Role.should_receive(:load).with("will").and_return(@role) + @knife.run + end + + it "should remove the item from the run list" do + @setup.run + @knife.run + @role.run_list_for('QA')[1].should_not == 'role[dude]' + @role.run_list_for('QA')[1].should == 'role[person]' + @role.run_list[0].should be_nil + end + + it "should save the node" do + @role.should_receive(:save).and_return(true) + @knife.run + end + + it "should print the run list" do + @knife.should_receive(:output).and_return(true) + @knife.config[:print_after] = true + @setup.run + @knife.run + end + + describe "run with a list of roles and recipes" do + it "should replace the items from the run list" do + @setup.name_args = [ "will", "QA", "recipe[orange::chicken]", "role[monkey]", "recipe[duck::type]", "role[person]", "role[bird]", "role[town]" ] + @setup.run + @setup.name_args = [ "will", "PRD", "recipe[orange::chicken]", "role[monkey]", "recipe[duck::type]", "role[person]", "role[bird]", "role[town]" ] + @setup.run + @knife.name_args = [ 'will', 'QA', 'role[monkey]', 'role[gibbon]' ] + @knife.run + @knife.name_args = [ 'will', 'QA', 'recipe[duck::type]', 'recipe[duck::mallard]' ] + @knife.run + @role.run_list_for('QA').should_not include('role[monkey]') + @role.run_list_for('QA').should_not include('recipe[duck::type]') + @role.run_list_for('QA')[0].should == 'recipe[orange::chicken]' + @role.run_list_for('QA')[1].should == 'role[gibbon]' + @role.run_list_for('QA')[2].should == 'recipe[duck::mallard]' + @role.run_list_for('QA')[3].should == 'role[person]' + @role.run_list_for('QA')[4].should == 'role[bird]' + @role.run_list_for('QA')[5].should == 'role[town]' + @role.run_list_for('PRD')[0].should == 'recipe[orange::chicken]' + @role.run_list_for('PRD')[1].should == 'role[monkey]' + @role.run_list_for('PRD')[2].should == 'recipe[duck::type]' + @role.run_list_for('PRD')[3].should == 'role[person]' + @role.run_list_for('PRD')[4].should == 'role[bird]' + @role.run_list_for('PRD')[5].should == 'role[town]' + @role.run_list[0].should be_nil + end + end + end +end diff --git a/spec/unit/knife/role_env_run_list_set_spec.rb b/spec/unit/knife/role_env_run_list_set_spec.rb new file mode 100644 index 0000000000..150579c0ab --- /dev/null +++ b/spec/unit/knife/role_env_run_list_set_spec.rb @@ -0,0 +1,102 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Will Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' + +describe Chef::Knife::RoleEnvRunListSet do + before(:each) do + Chef::Config[:role_name] = "will" + Chef::Config[:env_name] = "QA" + @setup = Chef::Knife::RoleEnvRunListAdd.new + @setup.name_args = [ "will", "QA", "role[monkey]", "role[person]", "role[bucket]" ] + + @knife = Chef::Knife::RoleEnvRunListSet.new + @knife.config = { + :print_after => nil + } + @knife.name_args = [ "will", "QA", "role[owen]", "role[mauntel]" ] + @knife.stub!(:output).and_return(true) + + @role = Chef::Role.new() + @role.name("will") + @role.stub!(:save).and_return(true) + + @knife.ui.stub!(:confirm).and_return(true) + Chef::Role.stub!(:load).and_return(@role) + + end + + + + describe "run" do + + +# it "should display all the things" do +# @knife.run +# @role.to_json.should == 'show all the things' +# end + + it "should load the node" do + Chef::Role.should_receive(:load).with("will").and_return(@role) + @knife.run + end + + it "should replace all the items in the runlist with what is specified" do + @setup.run + @knife.run + @role.run_list_for('QA')[0].should == "role[owen]" + @role.run_list_for('QA')[1].should == "role[mauntel]" + @role.run_list_for('QA')[2].should be_nil + @role.run_list[0].should be_nil + end + + it "should save the node" do + @role.should_receive(:save).and_return(true) + @knife.run + end + + it "should print the run list" do + @knife.should_receive(:output).and_return(true) + @knife.config[:print_after] = true + @setup.run + @knife.run + end + + describe "should clear an environmental run list of roles and recipes" do + it "should remove the items from the run list" do + @setup.name_args = [ "will", "QA", "recipe[orange::chicken]", "role[monkey]", "recipe[duck::type]", "role[person]", "role[bird]", "role[town]" ] + @setup.run + @setup.name_args = [ "will", "PRD", "recipe[orange::chicken]", "role[monkey]", "recipe[duck::type]", "role[person]", "role[bird]", "role[town]" ] + @setup.run + @knife.name_args = [ "will", "QA", "role[coke]", "role[pepsi]" ] + @knife.run + @role.run_list_for('QA')[0].should == "role[coke]" + @role.run_list_for('QA')[1].should == "role[pepsi]" + @role.run_list_for('QA')[2].should be_nil + @role.run_list_for('PRD')[0].should == 'recipe[orange::chicken]' + @role.run_list_for('PRD')[1].should == 'role[monkey]' + @role.run_list_for('PRD')[2].should == 'recipe[duck::type]' + @role.run_list_for('PRD')[3].should == 'role[person]' + @role.run_list_for('PRD')[4].should == 'role[bird]' + @role.run_list_for('PRD')[5].should == 'role[town]' + @role.run_list[0].should be_nil + end + end + end +end diff --git a/spec/unit/knife/role_run_list_add_spec.rb b/spec/unit/knife/role_run_list_add_spec.rb new file mode 100644 index 0000000000..57e0eb0553 --- /dev/null +++ b/spec/unit/knife/role_run_list_add_spec.rb @@ -0,0 +1,179 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Will Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' + +describe Chef::Knife::RoleRunListAdd do + before(:each) do +# Chef::Config[:role_name] = "websimian" +# Chef::Config[:env_name] = "QA" + @knife = Chef::Knife::RoleRunListAdd.new + @knife.config = { + :after => nil + } + @knife.name_args = [ "will", "role[monkey]" ] + @knife.stub!(:output).and_return(true) + @role = Chef::Role.new() + @role.stub!(:save).and_return(true) + Chef::Role.stub!(:load).and_return(@role) + end + + describe "run" do + +# it "should display all the things" do +# @knife.run +# @role.to_json.should == 'show all the things' +# end + + it "should have a run list with the monkey role" do + @knife.run + @role.run_list[0].should == "role[monkey]" + end + + it "should load the role named will" do + Chef::Role.should_receive(:load).with("will") + @knife.run + end + + it "should save the role" do + @role.should_receive(:save) + @knife.run + end + + it "should print the run list" do + @knife.should_receive(:output).and_return(true) + @knife.run + end + + describe "with -a or --after specified" do + it "should not create a change if the specified 'after' never comes" do + @role.run_list_for("_default") << "role[acorns]" + @role.run_list_for("_default") << "role[barn]" + @knife.config[:after] = "role[tree]" + @knife.name_args = [ "will", "role[pad]" ] + @knife.run + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should == "role[barn]" + @role.run_list[2].should be_nil + end + + it "should add to the run list after the specified entries in the default run list" do + #Setup + @role.run_list_for("_default") << "role[acorns]" + @role.run_list_for("_default") << "role[barn]" + #Configuration we are testing + @knife.config[:after] = "role[acorns]" + @knife.name_args = [ "will", "role[pad]", "role[whackadoo]" ] + @knife.run + #The actual tests + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should == "role[pad]" + @role.run_list[2].should == "role[whackadoo]" + @role.run_list[3].should == "role[barn]" + @role.run_list[4].should be_nil + end + end + + describe "with more than one role or recipe" do + it "should add to the QA run list all the entries" do + @knife.name_args = [ "will", "role[monkey],role[duck]" ] + @role.run_list_for("_default") << "role[acorns]" + @knife.run + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should == "role[monkey]" + @role.run_list[2].should == "role[duck]" + @role.run_list[3].should be_nil + end + end + + describe "with more than one role or recipe with space between items" do + it "should add to the run list all the entries" do + @knife.name_args = [ "will", "role[monkey], role[duck]" ] + @role.run_list_for("_default") << "role[acorns]" + @knife.run + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should == "role[monkey]" + @role.run_list[2].should == "role[duck]" + @role.run_list[3].should be_nil + end + end + + describe "with more than one role or recipe as different arguments" do + it "should add to the run list all the entries" do + @knife.name_args = [ "will", "role[monkey]", "role[duck]" ] + @role.run_list_for("_default") << "role[acorns]" + @knife.run + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should == "role[monkey]" + @role.run_list[2].should == "role[duck]" + @role.run_list[3].should be_nil + end + end + + describe "with more than one role or recipe as different arguments and list separated by comas" do + it "should add to the run list all the entries" do + @knife.name_args = [ "will", "role[monkey]", "role[duck],recipe[bird::fly]" ] + @role.run_list_for("_default") << "role[acorns]" + @knife.run + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should == "role[monkey]" + @role.run_list[2].should == "role[duck]" + @role.run_list[3].should == "recipe[bird::fly]" + @role.run_list[4].should be_nil + end + end + + describe "Recipe with version number is allowed" do + it "should add to the run list all the entries including the versioned recipe" do + @knife.name_args = [ "will", "role[monkey]", "role[duck],recipe[bird::fly@1.1.3]" ] + @role.run_list_for("_default") << "role[acorns]" + @knife.run + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should == "role[monkey]" + @role.run_list[2].should == "role[duck]" + @role.run_list[3].should == "recipe[bird::fly@1.1.3]" + @role.run_list[4].should be_nil + end + end + + describe "with one role or recipe but with an extraneous comma" do + it "should add to the run list one item" do + @role.run_list_for("_default") << "role[acorns]" + @knife.name_args = [ "will", "role[monkey]," ] + @knife.run + @role.run_list[0].should == "role[acorns]" + @role.run_list[1].should == "role[monkey]" + @role.run_list[2].should be_nil + end + end + + describe "with more than one command" do + it "should be able to the environment run list by running multiple knife commands" do + @knife.name_args = [ "will", "role[blue]," ] + @knife.run + @knife.name_args = [ "will", "role[black]," ] + @knife.run + @role.run_list[0].should == "role[blue]" + @role.run_list[1].should == "role[black]" + @role.run_list[2].should be_nil + end + end + + end +end diff --git a/spec/unit/knife/role_run_list_clear_spec.rb b/spec/unit/knife/role_run_list_clear_spec.rb new file mode 100644 index 0000000000..7ec8dce769 --- /dev/null +++ b/spec/unit/knife/role_run_list_clear_spec.rb @@ -0,0 +1,90 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Will Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' + +describe Chef::Knife::RoleRunListClear do + before(:each) do + Chef::Config[:role_name] = "will" + @setup = Chef::Knife::RoleRunListAdd.new + @setup.name_args = [ "will", "role[monkey]", "role[person]" ] + + @knife = Chef::Knife::RoleRunListClear.new + @knife.config = { + :print_after => nil + } + @knife.name_args = [ "will" ] + @knife.stub!(:output).and_return(true) + + @role = Chef::Role.new() + @role.name("will") + @role.stub!(:save).and_return(true) + + @knife.ui.stub!(:confirm).and_return(true) + Chef::Role.stub!(:load).and_return(@role) + + end + + + + describe "run" do + + +# it "should display all the things" do +# @knife.run +# @role.to_json.should == 'show all the things' +# end + + it "should load the node" do + Chef::Role.should_receive(:load).with("will").and_return(@role) + @knife.run + end + + it "should remove the item from the run list" do + @setup.run + @knife.run + @role.run_list[0].should be_nil + end + + it "should save the node" do + @role.should_receive(:save).and_return(true) + @knife.run + end + + it "should print the run list" do + @knife.should_receive(:output).and_return(true) + @knife.config[:print_after] = true + @setup.run + @knife.run + end + + describe "should clear an environmental run list of roles and recipes" do + it "should remove the items from the run list" do + @setup.name_args = [ "will", "recipe[orange::chicken]", "role[monkey]", "recipe[duck::type]", "role[person]", "role[bird]", "role[town]" ] + @setup.run + @knife.name_args = [ 'will' ] + @knife.run + @role.run_list[0].should be_nil + end + end + end +end + + + diff --git a/spec/unit/knife/role_run_list_remove_spec.rb b/spec/unit/knife/role_run_list_remove_spec.rb new file mode 100644 index 0000000000..0ff329e2eb --- /dev/null +++ b/spec/unit/knife/role_run_list_remove_spec.rb @@ -0,0 +1,98 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Will Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' + +describe Chef::Knife::RoleRunListRemove do + before(:each) do + Chef::Config[:role_name] = "will" + @setup = Chef::Knife::RoleRunListAdd.new + @setup.name_args = [ "will", "role[monkey]", "role[person]" ] + + @knife = Chef::Knife::RoleRunListRemove.new + @knife.config = { + :print_after => nil + } + @knife.name_args = [ "will", "role[monkey]" ] + @knife.stub!(:output).and_return(true) + + @role = Chef::Role.new() + @role.name("will") + @role.stub!(:save).and_return(true) + + @knife.ui.stub!(:confirm).and_return(true) + Chef::Role.stub!(:load).and_return(@role) + + end + + + + describe "run" do + + +# it "should display all the things" do +# @knife.run +# @role.to_json.should == 'show all the things' +# end + + it "should load the node" do + Chef::Role.should_receive(:load).with("will").and_return(@role) + @knife.run + end + + it "should remove the item from the run list" do + @setup.run + @knife.run + @role.run_list[0].should == 'role[person]' + @role.run_list[1].should be_nil + end + + it "should save the node" do + @role.should_receive(:save).and_return(true) + @knife.run + end + + it "should print the run list" do + @knife.should_receive(:output).and_return(true) + @knife.config[:print_after] = true + @setup.run + @knife.run + end + + describe "run with a list of roles and recipes" do + it "should remove the items from the run list" do + @setup.name_args = [ "will", "recipe[orange::chicken]", "role[monkey]", "recipe[duck::type]", "role[person]", "role[bird]", "role[town]" ] + @setup.run + @knife.name_args = [ 'will', 'role[monkey]' ] + @knife.run + @knife.name_args = [ 'will', 'recipe[duck::type]' ] + @knife.run + @role.run_list.should_not include('role[monkey]') + @role.run_list.should_not include('recipe[duck::type]') + @role.run_list[0].should == 'recipe[orange::chicken]' + @role.run_list[1].should == 'role[person]' + @role.run_list[2].should == 'role[bird]' + @role.run_list[3].should == 'role[town]' + end + end + end +end + + + diff --git a/spec/unit/knife/role_run_list_replace_spec.rb b/spec/unit/knife/role_run_list_replace_spec.rb new file mode 100644 index 0000000000..118a4316ba --- /dev/null +++ b/spec/unit/knife/role_run_list_replace_spec.rb @@ -0,0 +1,101 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Will Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' + +describe Chef::Knife::RoleRunListReplace do + before(:each) do + Chef::Config[:role_name] = "will" + @setup = Chef::Knife::RoleRunListAdd.new + @setup.name_args = [ "will", "role[monkey]", "role[dude]", "role[fixer]" ] + + @knife = Chef::Knife::RoleRunListReplace.new + @knife.config = { + :print_after => nil + } + @knife.name_args = [ "will", "role[dude]", "role[person]" ] + @knife.stub!(:output).and_return(true) + + @role = Chef::Role.new() + @role.name("will") + @role.stub!(:save).and_return(true) + + @knife.ui.stub!(:confirm).and_return(true) + Chef::Role.stub!(:load).and_return(@role) + + end + + + + describe "run" do + + +# it "should display all the things" do +# @knife.run +# @role.to_json.should == 'show all the things' +# end + + it "should load the node" do + Chef::Role.should_receive(:load).with("will").and_return(@role) + @knife.run + end + + it "should remove the item from the run list" do + @setup.run + @knife.run + @role.run_list[0].should == 'role[monkey]' + @role.run_list[1].should_not == 'role[dude]' + @role.run_list[1].should == 'role[person]' + @role.run_list[2].should == 'role[fixer]' + @role.run_list[3].should be_nil + end + + it "should save the node" do + @role.should_receive(:save).and_return(true) + @knife.run + end + + it "should print the run list" do + @knife.should_receive(:output).and_return(true) + @knife.config[:print_after] = true + @setup.run + @knife.run + end + + describe "run with a list of roles and recipes" do + it "should replace the items from the run list" do + @setup.name_args = [ "will", "recipe[orange::chicken]", "role[monkey]", "recipe[duck::type]", "role[person]", "role[bird]", "role[town]" ] + @setup.run + @knife.name_args = [ 'will', 'role[monkey]', 'role[gibbon]' ] + @knife.run + @knife.name_args = [ 'will', 'recipe[duck::type]', 'recipe[duck::mallard]' ] + @knife.run + @role.run_list.should_not include('role[monkey]') + @role.run_list.should_not include('recipe[duck::type]') + @role.run_list[0].should == 'recipe[orange::chicken]' + @role.run_list[1].should == 'role[gibbon]' + @role.run_list[2].should == 'recipe[duck::mallard]' + @role.run_list[3].should == 'role[person]' + @role.run_list[4].should == 'role[bird]' + @role.run_list[5].should == 'role[town]' + @role.run_list[6].should be_nil + end + end + end +end diff --git a/spec/unit/knife/role_run_list_set_spec.rb b/spec/unit/knife/role_run_list_set_spec.rb new file mode 100644 index 0000000000..04362c4d0d --- /dev/null +++ b/spec/unit/knife/role_run_list_set_spec.rb @@ -0,0 +1,92 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Will Albenzi (<walbenzi@gmail.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' + +describe Chef::Knife::RoleRunListSet do + before(:each) do + Chef::Config[:role_name] = "will" + @setup = Chef::Knife::RoleRunListAdd.new + @setup.name_args = [ "will", "role[monkey]", "role[person]", "role[bucket]" ] + + @knife = Chef::Knife::RoleRunListSet.new + @knife.config = { + :print_after => nil + } + @knife.name_args = [ "will", "role[owen]", "role[mauntel]" ] + @knife.stub!(:output).and_return(true) + + @role = Chef::Role.new() + @role.name("will") + @role.stub!(:save).and_return(true) + + @knife.ui.stub!(:confirm).and_return(true) + Chef::Role.stub!(:load).and_return(@role) + + end + + + + describe "run" do + + +# it "should display all the things" do +# @knife.run +# @role.to_json.should == 'show all the things' +# end + + it "should load the node" do + Chef::Role.should_receive(:load).with("will").and_return(@role) + @knife.run + end + + it "should replace all the items in the runlist with what is specified" do + @setup.run + @knife.run + @role.run_list[0].should == "role[owen]" + @role.run_list[1].should == "role[mauntel]" + @role.run_list[2].should be_nil + end + + it "should save the node" do + @role.should_receive(:save).and_return(true) + @knife.run + end + + it "should print the run list" do + @knife.should_receive(:output).and_return(true) + @knife.config[:print_after] = true + @setup.run + @knife.run + end + + describe "should clear an environmental run list of roles and recipes" do + it "should remove the items from the run list" do + @setup.name_args = [ "will", "recipe[orange::chicken]", "role[monkey]", "recipe[duck::type]", "role[person]", "role[bird]", "role[town]" ] + @setup.run + @knife.name_args = [ "will", "role[coke]", "role[pepsi]" ] + @knife.run + @role.run_list[0].should == "role[coke]" + @role.run_list[1].should == "role[pepsi]" + @role.run_list[2].should be_nil + @role.run_list[3].should be_nil + end + end + end +end |