diff options
author | AbhishekKr <abhikumar163@gmail.com> | 2016-07-31 07:42:10 +0530 |
---|---|---|
committer | AbhishekKr <abhikumar163@gmail.com> | 2016-07-31 07:42:10 +0530 |
commit | c3c6cfc156d0a0bfe7c7dd4f273a2c2bdc4b0281 (patch) | |
tree | cafccae761432edd05c952f5c8fdb452bfd30ad5 /spec | |
parent | 58e147a9dfa331c359df2c9a2bcc53a27a722ef5 (diff) | |
parent | 6b88d8c1bd89f9864478fc873352c2881441dbfe (diff) | |
download | chef-c3c6cfc156d0a0bfe7c7dd4f273a2c2bdc4b0281.tar.gz |
Merge branch 'master' of https://github.com/chef/chef
Diffstat (limited to 'spec')
37 files changed, 771 insertions, 142 deletions
diff --git a/spec/data/run_context/cookbooks/circular-dep1/attributes/default.rb b/spec/data/run_context/cookbooks/circular-dep1/attributes/default.rb index ef0967a4d2..e45e7d9f68 100644 --- a/spec/data/run_context/cookbooks/circular-dep1/attributes/default.rb +++ b/spec/data/run_context/cookbooks/circular-dep1/attributes/default.rb @@ -1,4 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "circular-dep1::default" - - +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "circular-dep1::default" diff --git a/spec/data/run_context/cookbooks/circular-dep2/attributes/default.rb b/spec/data/run_context/cookbooks/circular-dep2/attributes/default.rb index f2ef012aa1..37f396b1f9 100644 --- a/spec/data/run_context/cookbooks/circular-dep2/attributes/default.rb +++ b/spec/data/run_context/cookbooks/circular-dep2/attributes/default.rb @@ -1,3 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "circular-dep2::default" - +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "circular-dep2::default" diff --git a/spec/data/run_context/cookbooks/dependency1/attributes/aa_first.rb b/spec/data/run_context/cookbooks/dependency1/attributes/aa_first.rb index e818d36a9e..3059494198 100644 --- a/spec/data/run_context/cookbooks/dependency1/attributes/aa_first.rb +++ b/spec/data/run_context/cookbooks/dependency1/attributes/aa_first.rb @@ -1,2 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "dependency1::aa_first" +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "dependency1::aa_first" diff --git a/spec/data/run_context/cookbooks/dependency1/attributes/default.rb b/spec/data/run_context/cookbooks/dependency1/attributes/default.rb index 6875274e3f..a65a3345bc 100644 --- a/spec/data/run_context/cookbooks/dependency1/attributes/default.rb +++ b/spec/data/run_context/cookbooks/dependency1/attributes/default.rb @@ -1,2 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "dependency1::default" +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "dependency1::default" diff --git a/spec/data/run_context/cookbooks/dependency1/attributes/zz_last.rb b/spec/data/run_context/cookbooks/dependency1/attributes/zz_last.rb index 1a513b03d4..94ffb30133 100644 --- a/spec/data/run_context/cookbooks/dependency1/attributes/zz_last.rb +++ b/spec/data/run_context/cookbooks/dependency1/attributes/zz_last.rb @@ -1,3 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "dependency1::zz_last" - +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "dependency1::zz_last" diff --git a/spec/data/run_context/cookbooks/dependency2/attributes/default.rb b/spec/data/run_context/cookbooks/dependency2/attributes/default.rb index 526751f422..8917bf9730 100644 --- a/spec/data/run_context/cookbooks/dependency2/attributes/default.rb +++ b/spec/data/run_context/cookbooks/dependency2/attributes/default.rb @@ -1,3 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "dependency2::default" - +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "dependency2::default" diff --git a/spec/data/run_context/cookbooks/no-default-attr/attributes/server.rb b/spec/data/run_context/cookbooks/no-default-attr/attributes/server.rb index 3ad2b925aa..07294665b2 100644 --- a/spec/data/run_context/cookbooks/no-default-attr/attributes/server.rb +++ b/spec/data/run_context/cookbooks/no-default-attr/attributes/server.rb @@ -1,3 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "no-default-attr::server" - +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "no-default-attr::server" diff --git a/spec/data/run_context/cookbooks/test-with-circular-deps/attributes/default.rb b/spec/data/run_context/cookbooks/test-with-circular-deps/attributes/default.rb index cca56bc61f..77309462b1 100644 --- a/spec/data/run_context/cookbooks/test-with-circular-deps/attributes/default.rb +++ b/spec/data/run_context/cookbooks/test-with-circular-deps/attributes/default.rb @@ -1,3 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "test-with-circular-deps::default" - +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "test-with-circular-deps::default" diff --git a/spec/data/run_context/cookbooks/test-with-deps/attributes/default.rb b/spec/data/run_context/cookbooks/test-with-deps/attributes/default.rb index 4d71cc3cfe..c4cc8151a4 100644 --- a/spec/data/run_context/cookbooks/test-with-deps/attributes/default.rb +++ b/spec/data/run_context/cookbooks/test-with-deps/attributes/default.rb @@ -1,3 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "test-with-deps::default" - +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "test-with-deps::default" diff --git a/spec/functional/resource/package_spec.rb b/spec/functional/resource/package_spec.rb index 6dc55f7f01..0f01a751ec 100644 --- a/spec/functional/resource/package_spec.rb +++ b/spec/functional/resource/package_spec.rb @@ -260,7 +260,7 @@ describe Chef::Resource::Package, metadata do end before do - node.set[:preseed_value] = "FROM TEMPLATE" + node.normal[:preseed_value] = "FROM TEMPLATE" end it "preseeds the package, then installs it" do diff --git a/spec/functional/resource/template_spec.rb b/spec/functional/resource/template_spec.rb index f270043f2c..32529fbb0c 100644 --- a/spec/functional/resource/template_spec.rb +++ b/spec/functional/resource/template_spec.rb @@ -110,7 +110,7 @@ describe Chef::Resource::Template do context "using single helper syntax referencing @node" do before do - node.set[:helper_test_attr] = "value from helper method" + node.normal[:helper_test_attr] = "value from helper method" resource.helper(:helper_method) { "#{@node[:helper_test_attr]}" } end @@ -131,7 +131,7 @@ describe Chef::Resource::Template do context "using an inline block referencing @node" do before do - node.set[:helper_test_attr] = "value from helper method" + node.normal[:helper_test_attr] = "value from helper method" resource.helpers do def helper_method @@ -168,7 +168,7 @@ describe Chef::Resource::Template do end before do - node.set[:helper_test_attr] = "value from helper method" + node.normal[:helper_test_attr] = "value from helper method" resource.helpers(ExampleModuleReferencingATNode) end diff --git a/spec/functional/shell_spec.rb b/spec/functional/shell_spec.rb index fe2abdb12a..636162fb16 100644 --- a/spec/functional/shell_spec.rb +++ b/spec/functional/shell_spec.rb @@ -137,7 +137,7 @@ describe Shell do it "sets the override_runlist from the command line" do output, exitstatus = run_chef_shell_with("-o 'override::foo,override::bar'") do |out, keyboard| - show_recipes_code = %q[puts "#{node.recipes.inspect}"] + show_recipes_code = %q[puts "#{node["recipes"].inspect}"] keyboard.puts(show_recipes_code) read_until(out, show_recipes_code) end diff --git a/spec/integration/knife/diff_spec.rb b/spec/integration/knife/diff_spec.rb index b7d2f4d1c3..b3bd23f48e 100644 --- a/spec/integration/knife/diff_spec.rb +++ b/spec/integration/knife/diff_spec.rb @@ -72,7 +72,7 @@ EOM file "data_bags/x/y.json", {} file "environments/_default.json", { "description" => "The default Chef environment" } file "environments/x.json", {} - file "nodes/x.json", {} + file "nodes/x.json", { "normal" => { "tags" => [] } } file "roles/x.json", {} file "users/admin.json", { "admin" => true, "public_key" => ChefZero::PUBLIC_KEY } file "users/x.json", { "public_key" => ChefZero::PUBLIC_KEY } @@ -366,7 +366,7 @@ EOM file "data_bags/x/y.json", {} file "environments/_default.json", { "description" => "The default Chef environment" } file "environments/x.json", {} - file "nodes/x.json", {} + file "nodes/x.json", { "normal" => { "tags" => [] } } file "roles/x.json", {} file "users/admin.json", { "admin" => true, "public_key" => ChefZero::PUBLIC_KEY } file "users/x.json", { "public_key" => ChefZero::PUBLIC_KEY } diff --git a/spec/integration/knife/download_spec.rb b/spec/integration/knife/download_spec.rb index a924226ea2..be0fc9d708 100644 --- a/spec/integration/knife/download_spec.rb +++ b/spec/integration/knife/download_spec.rb @@ -76,7 +76,7 @@ EOM file "data_bags/x/y.json", {} file "environments/_default.json", { "description" => "The default Chef environment" } file "environments/x.json", {} - file "nodes/x.json", {} + file "nodes/x.json", { "normal" => { "tags" => [] } } file "roles/x.json", {} file "users/admin.json", { "admin" => true, "public_key" => ChefZero::PUBLIC_KEY } file "users/x.json", { "public_key" => ChefZero::PUBLIC_KEY } @@ -645,7 +645,7 @@ EOM file "data_bags/x/y.json", {} file "environments/_default.json", { "description" => "The default Chef environment" } file "environments/x.json", {} - file "nodes/x.json", {} + file "nodes/x.json", { "normal" => { "tags" => [] } } file "roles/x.json", {} file "users/admin.json", { "admin" => true, "public_key" => ChefZero::PUBLIC_KEY } file "users/x.json", { "public_key" => ChefZero::PUBLIC_KEY } @@ -1271,7 +1271,7 @@ EOM file "groups/x.json", {} file "invitations.json", [ "foo" ] file "members.json", [ "bar" ] - file "nodes/x.json", {} + file "nodes/x.json", { "normal" => { "tags" => [] } } file "org.json", { "full_name" => "Something" } file "policies/x-1.0.0.json", {} file "policies/blah-1.0.0.json", {} @@ -1295,7 +1295,7 @@ EOM file "environments/x.json", { "description" => "foo" } file "groups/x.json", { "description" => "foo" } file "groups/x.json", { "groups" => [ "admin" ] } - file "nodes/x.json", { "run_list" => [ "blah" ] } + file "nodes/x.json", { "normal" => { "tags" => [] }, "run_list" => [ "blah" ] } file "org.json", { "full_name" => "Something Else " } file "policies/x-1.0.0.json", { "run_list" => [ "blah" ] } file "policy_groups/x.json", { diff --git a/spec/integration/knife/raw_spec.rb b/spec/integration/knife/raw_spec.rb index 9078bf09a1..5e0d3a3d11 100644 --- a/spec/integration/knife/raw_spec.rb +++ b/spec/integration/knife/raw_spec.rb @@ -49,7 +49,9 @@ describe "knife raw", :workstation do }, "normal": { + "tags": [ + ] }, "default": { diff --git a/spec/integration/knife/upload_spec.rb b/spec/integration/knife/upload_spec.rb index 55c95b59b7..1db0ea8726 100644 --- a/spec/integration/knife/upload_spec.rb +++ b/spec/integration/knife/upload_spec.rb @@ -96,7 +96,7 @@ EOM file "data_bags/x/y.json", {} file "environments/_default.json", { "description" => "The default Chef environment" } file "environments/x.json", {} - file "nodes/x.json", {} + file "nodes/x.json", { "normal" => { "tags" => [] } } file "roles/x.json", {} file "users/admin.json", { "admin" => true, "public_key" => ChefZero::PUBLIC_KEY } file "users/x.json", { "public_key" => ChefZero::PUBLIC_KEY } @@ -802,7 +802,7 @@ EOM file "data_bags/x/y.json", {} file "environments/_default.json", { "description" => "The default Chef environment" } file "environments/x.json", {} - file "nodes/x.json", {} + file "nodes/x.json", { "normal" => { "tags" => [] } } file "roles/x.json", {} file "users/admin.json", { "admin" => true, "public_key" => ChefZero::PUBLIC_KEY } file "users/x.json", { "public_key" => ChefZero::PUBLIC_KEY } @@ -1313,7 +1313,7 @@ EOM file "invitations.json", [ "foo" ] file "members.json", [ "bar" ] file "org.json", { "full_name" => "wootles" } - file "nodes/x.json", {} + file "nodes/x.json", { "normal" => { "tags" => [] } } file "policies/x-1.0.0.json", {} file "policies/blah-1.0.0.json", {} file "policy_groups/x.json", { "policies" => { "x" => { "revision_id" => "1.0.0" }, "blah" => { "revision_id" => "1.0.0" } } } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2f6747c9af..dcbc34a157 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -66,6 +66,8 @@ require "chef/util/file_edit" require "chef/config" +require "chef/chef_fs/file_system_cache" + if ENV["CHEF_FIPS"] == "1" Chef::Config.init_openssl end @@ -152,6 +154,8 @@ RSpec.configure do |config| config.filter_run_excluding :unix_only => true unless unix? config.filter_run_excluding :aix_only => true unless aix? config.filter_run_excluding :debian_family_only => true unless debian_family? + config.filter_run_excluding :linux_only => true unless linux? + config.filter_run_excluding :non_linux_only => true if linux? config.filter_run_excluding :supports_cloexec => true unless supports_cloexec? config.filter_run_excluding :selinux_only => true unless selinux_enabled? config.filter_run_excluding :requires_root => true unless root? @@ -200,6 +204,8 @@ RSpec.configure do |config| config.before(:each) do Chef.reset! + Chef::ChefFS::FileSystemCache.instance.reset! + Chef::Config.reset # By default, treat deprecation warnings as errors in tests. diff --git a/spec/support/platform_helpers.rb b/spec/support/platform_helpers.rb index 9ba56a15e3..783429161a 100644 --- a/spec/support/platform_helpers.rb +++ b/spec/support/platform_helpers.rb @@ -138,6 +138,10 @@ def freebsd? !!(RUBY_PLATFORM =~ /freebsd/) end +def linux? + !!(RUBY_PLATFORM =~ /linux/) +end + def debian_family? !!(ohai[:platform_family] == "debian") end diff --git a/spec/support/shared/integration/knife_support.rb b/spec/support/shared/integration/knife_support.rb index 398d90dff0..01df9ab1a7 100644 --- a/spec/support/shared/integration/knife_support.rb +++ b/spec/support/shared/integration/knife_support.rb @@ -20,6 +20,7 @@ require "chef/knife" require "chef/application/knife" require "logger" require "chef/log" +require "chef/chef_fs/file_system_cache" module KnifeSupport DEBUG = ENV["DEBUG"] @@ -70,6 +71,9 @@ module KnifeSupport Chef::Config[:verbosity] = ( DEBUG ? 2 : 0 ) instance.config[:config_file] = File.join(CHEF_SPEC_DATA, "null_config.rb") + # Ensure the ChefFS cache is empty + Chef::ChefFS::FileSystemCache.instance.reset! + # Configure chef with a (mostly) blank knife.rb # We set a global and then mutate it in our stub knife.rb so we can be # extra sure that we're not loading someone's real knife.rb and then diff --git a/spec/support/shared/unit/provider/file.rb b/spec/support/shared/unit/provider/file.rb index cb539ffbc3..ee3438da70 100644 --- a/spec/support/shared/unit/provider/file.rb +++ b/spec/support/shared/unit/provider/file.rb @@ -683,6 +683,31 @@ shared_examples_for Chef::Provider::File do end end + context "do_resolv_conf_fixup" do + %w{/resolv.conf /etc/resolv.con /etc/foo/resolv.conf /c/resolv.conf}.each do |path| + context "when managing #{path}" do + let(:resource_path) { path } + it "does not reload the nameservers" do + expect(Resolv::DefaultResolver).not_to receive(:replace_resolvers) + provider.send(:do_resolv_conf_fixup) + end + end + end + context "when managing /etc/resolv.conf", linux_only: true do + let(:resource_path) { "/etc/resolv.conf" } + it "reloads the nameservers on linux" do + expect(Resolv::DefaultResolver).to receive(:replace_resolvers) + provider.send(:do_resolv_conf_fixup) + end + end + context "when managing /etc/resolv.conf", non_linux_only: true do + let(:resource_path) { "/etc/resolv.conf" } + it "does not reload the nameservers on non-linux" do + expect(Resolv::DefaultResolver).not_to receive(:replace_resolvers) + provider.send(:do_resolv_conf_fixup) + end + end + end end context "action delete" do diff --git a/spec/unit/application/solo_spec.rb b/spec/unit/application/solo_spec.rb index b1931414cc..686ae745d8 100644 --- a/spec/unit/application/solo_spec.rb +++ b/spec/unit/application/solo_spec.rb @@ -120,9 +120,10 @@ Enable chef-client interval runs by setting `:client_fork = true` in your config expect(app).to receive(:open).with("http://junglist.gen.nz/recipes.tgz").and_yield(tarfile) expect(File).to receive(:open).with("#{Dir.tmpdir}/chef-solo/recipes.tgz", "wb").and_yield(target_file) - shellout = instance_double("Mixlib::ShellOut", run_command: nil, error!: nil, stdout: "") + archive = double(Mixlib::Archive) - expect(app).to receive(:shell_out!).with("tar zxvf #{Dir.tmpdir}/chef-solo/recipes.tgz -C #{Dir.tmpdir}/chef-solo").and_return(shellout) + expect(Mixlib::Archive).to receive(:new).with("#{Dir.tmpdir}/chef-solo/recipes.tgz").and_return(archive) + expect(archive).to receive(:extract).with("#{Dir.tmpdir}/chef-solo", { perms: false, ignore: /^\.$/ }) app.reconfigure expect(target_file.string).to eq("remote_tarball_content") end @@ -136,11 +137,10 @@ Enable chef-client interval runs by setting `:client_fork = true` in your config Chef::Config[:cookbook_path] = "#{Dir.tmpdir}/chef-solo/cookbooks" expect(FileUtils).to receive(:mkdir_p).with("#{Dir.tmpdir}/chef-solo").and_return(true) - allow(Chef::Mixin::Command).to receive(:run_command).and_return(true) + archive = double(Mixlib::Archive) - shellout = instance_double("Mixlib::ShellOut", run_command: nil, error!: nil, stdout: "") - - expect(app).to receive(:shell_out!).with("tar zxvf #{Dir.tmpdir}/chef-solo/recipes.tgz -C #{Dir.tmpdir}/chef-solo").and_return(shellout) + expect(Mixlib::Archive).to receive(:new).with("#{Dir.tmpdir}/chef-solo/recipes.tgz").and_return(archive) + expect(archive).to receive(:extract).with("#{Dir.tmpdir}/chef-solo", { perms: false, ignore: /^\.$/ }) expect(app).to receive(:fetch_recipe_tarball).ordered expect(Chef::ConfigFetcher).to receive(:new).ordered.and_return(config_fetcher) app.reconfigure diff --git a/spec/unit/cookbook_version_spec.rb b/spec/unit/cookbook_version_spec.rb index 7def2f5749..81ea161bfe 100644 --- a/spec/unit/cookbook_version_spec.rb +++ b/spec/unit/cookbook_version_spec.rb @@ -120,8 +120,8 @@ describe Chef::CookbookVersion do # Used to test file-specificity related file lookups let(:node) do Chef::Node.new.tap do |n| - n.set[:platform] = "ubuntu" - n.set[:platform_version] = "13.04" + n.normal[:platform] = "ubuntu" + n.normal[:platform_version] = "13.04" n.name("testing") end end @@ -203,8 +203,8 @@ describe Chef::CookbookVersion do # Used to test file-specificity related file lookups let(:node) do Chef::Node.new.tap do |n| - n.set[:platform] = "ubuntu" - n.set[:platform_version] = "13.04" + n.normal[:platform] = "ubuntu" + n.normal[:platform_version] = "13.04" n.name("testing") end end diff --git a/spec/unit/data_collector/messages_spec.rb b/spec/unit/data_collector/messages_spec.rb index 686e500507..b0c7e692d2 100644 --- a/spec/unit/data_collector/messages_spec.rb +++ b/spec/unit/data_collector/messages_spec.rb @@ -18,6 +18,7 @@ # require "spec_helper" +require "ffi_yajl" require "chef/data_collector/messages/helpers" describe Chef::DataCollector::Messages do @@ -61,7 +62,8 @@ describe Chef::DataCollector::Messages do end describe '#run_end_message' do - let(:run_status) { Chef::RunStatus.new(Chef::Node.new, Chef::EventDispatch::Dispatcher.new) } + let(:node) { Chef::Node.new } + let(:run_status) { Chef::RunStatus.new(node, Chef::EventDispatch::Dispatcher.new) } let(:report1) { double("report1", report_data: { "status" => "updated" }) } let(:report2) { double("report2", report_data: { "status" => "skipped" }) } let(:reporter_data) do @@ -76,6 +78,20 @@ describe Chef::DataCollector::Messages do allow(run_status).to receive(:end_time).and_return(Time.now) end + it "includes a valid node object in the payload" do + message = Chef::DataCollector::Messages.run_end_message(reporter_data) + expect(message["node"]).to be_an_instance_of(Chef::Node) + end + + it "returns a sane JSON representation of the node object" do + node.chef_environment = "my_test_environment" + node.run_list.add("recipe[my_test_cookbook::default]") + message = FFI_Yajl::Parser.parse(Chef::DataCollector::Messages.run_end_message(reporter_data).to_json) + + expect(message["node"]["chef_environment"]).to eq("my_test_environment") + expect(message["node"]["run_list"]).to eq(["recipe[my_test_cookbook::default]"]) + end + context "when the run was successful" do let(:required_fields) do %w{ @@ -86,6 +102,7 @@ describe Chef::DataCollector::Messages do expanded_run_list message_type message_version + node node_name organization_name resources @@ -136,6 +153,7 @@ describe Chef::DataCollector::Messages do expanded_run_list message_type message_version + node node_name organization_name resources @@ -169,46 +187,4 @@ describe Chef::DataCollector::Messages do end end end - - describe '#node_update_message' do - let(:run_status) { Chef::RunStatus.new(Chef::Node.new, Chef::EventDispatch::Dispatcher.new) } - - let(:required_fields) do - %w{ - entity_name - entity_type - entity_uuid - id - message_type - message_version - organization_name - recorded_at - remote_hostname - requestor_name - requestor_type - run_id - service_hostname - source - task - user_agent - } - end - let(:optional_fields) { %w{data} } - - it "is not missing any required fields" do - missing_fields = required_fields.select do |key| - !Chef::DataCollector::Messages.node_update_message(run_status).key?(key) - end - - expect(missing_fields).to eq([]) - end - - it "does not have any extra fields" do - extra_fields = Chef::DataCollector::Messages.node_update_message(run_status).keys.select do |key| - !required_fields.include?(key) && !optional_fields.include?(key) - end - - expect(extra_fields).to eq([]) - end - end end diff --git a/spec/unit/data_collector_spec.rb b/spec/unit/data_collector_spec.rb index 831643347b..131d6b8df9 100644 --- a/spec/unit/data_collector_spec.rb +++ b/spec/unit/data_collector_spec.rb @@ -156,6 +156,10 @@ describe Chef::DataCollector::Reporter do let(:reporter) { described_class.new } let(:run_status) { Chef::RunStatus.new(Chef::Node.new, Chef::EventDispatch::Dispatcher.new) } + before do + Chef::Config[:data_collector][:server_url] = "http://my-data-collector-server.mycompany.com" + end + describe '#run_started' do before do allow(reporter).to receive(:update_run_status) @@ -490,7 +494,8 @@ describe Chef::DataCollector::Reporter do [ Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, EOFError, Net::HTTPBadResponse, - Net::HTTPHeaderSyntaxError, Net::ProtocolError, OpenSSL::SSL::SSLError ].each do |exception_class| + Net::HTTPHeaderSyntaxError, Net::ProtocolError, OpenSSL::SSL::SSLError, + Errno::EHOSTDOWN ].each do |exception_class| context "when the block raises a #{exception_class} exception" do it "disables the reporter" do expect(reporter).to receive(:disable_data_collector_reporter) @@ -515,4 +520,39 @@ describe Chef::DataCollector::Reporter do end end end + + describe '#validate_data_collector_server_url!' do + context "when server_url is empty" do + it "raises an exception" do + Chef::Config[:data_collector][:server_url] = "" + expect { reporter.send(:validate_data_collector_server_url!) }.to raise_error(Chef::Exceptions::ConfigurationError) + end + end + + context "when server_url is not empty" do + context "when server_url is an invalid URI" do + it "raises an exception" do + Chef::Config[:data_collector][:server_url] = "this is not a URI" + expect { reporter.send(:validate_data_collector_server_url!) }.to raise_error(Chef::Exceptions::ConfigurationError) + end + end + + context "when server_url is a valid URI" do + context "when server_url is a URI with no host" do + it "raises an exception" do + Chef::Config[:data_collector][:server_url] = "/file/uri.txt" + expect { reporter.send(:validate_data_collector_server_url!) }.to raise_error(Chef::Exceptions::ConfigurationError) + end + + end + + context "when server_url is a URI with a valid host" do + it "does not an exception" do + Chef::Config[:data_collector][:server_url] = "http://www.google.com/data-collector" + expect { reporter.send(:validate_data_collector_server_url!) }.not_to raise_error + end + end + end + end + end end diff --git a/spec/unit/dsl/declare_resource_spec.rb b/spec/unit/dsl/declare_resource_spec.rb index 6dd9317c21..692e57d2e1 100644 --- a/spec/unit/dsl/declare_resource_spec.rb +++ b/spec/unit/dsl/declare_resource_spec.rb @@ -235,6 +235,36 @@ describe Chef::ResourceCollection do ).to eql(resource) expect(run_context.resource_collection.all_resources.size).to eql(0) end + + it "removes pending delayed notifications" do + recipe.declare_resource(:zen_master, "one") + recipe.declare_resource(:zen_master, "two") do + notifies :win, "zen_master[one]" + end + recipe.delete_resource(:zen_master, "two") + resource = recipe.declare_resource(:zen_master, "two") + expect(resource.delayed_notifications).to eql([]) + end + + it "removes pending immediate notifications" do + recipe.declare_resource(:zen_master, "one") + recipe.declare_resource(:zen_master, "two") do + notifies :win, "zen_master[one]", :immediate + end + recipe.delete_resource(:zen_master, "two") + resource = recipe.declare_resource(:zen_master, "two") + expect(resource.immediate_notifications).to eql([]) + end + + it "removes pending before notifications" do + recipe.declare_resource(:zen_master, "one") + recipe.declare_resource(:zen_master, "two") do + notifies :win, "zen_master[one]", :before + end + recipe.delete_resource(:zen_master, "two") + resource = recipe.declare_resource(:zen_master, "two") + expect(resource.before_notifications).to eql([]) + end end describe "run_context helpers" do diff --git a/spec/unit/knife/cookbook_site_install_spec.rb b/spec/unit/knife/cookbook_site_install_spec.rb index d60443d779..1549245ea3 100644 --- a/spec/unit/knife/cookbook_site_install_spec.rb +++ b/spec/unit/knife/cookbook_site_install_spec.rb @@ -23,6 +23,7 @@ describe Chef::Knife::CookbookSiteInstall do let(:stdout) { StringIO.new } let(:stderr) { StringIO.new } let(:downloader) { Hash.new } + let(:archive) { double(Mixlib::Archive, extract: true) } let(:repo) { double(:sanity_check => true, :reset_to_default_state => true, :prepare_to_import => true, :finalize_updates_to => true, :merge_updates_from => true) } @@ -48,6 +49,7 @@ describe Chef::Knife::CookbookSiteInstall do allow(File).to receive(:unlink) allow(File).to receive(:rmtree) allow(knife).to receive(:shell_out!).and_return(true) + allow(Mixlib::Archive).to receive(:new).and_return(archive) # CookbookSiteDownload Stup allow(knife).to receive(:download_cookbook_to).and_return(downloader) diff --git a/spec/unit/knife/cookbook_site_share_spec.rb b/spec/unit/knife/cookbook_site_share_spec.rb index 9339114d2a..5ac48539e4 100644 --- a/spec/unit/knife/cookbook_site_share_spec.rb +++ b/spec/unit/knife/cookbook_site_share_spec.rb @@ -83,11 +83,11 @@ describe Chef::Knife::CookbookSiteShare do @knife.run end - it "should print error and exit when given only 1 argument and cannot determine category" do + it "should use a default category when given only 1 argument and cannot determine category" do @knife.name_args = ["cookbook_name"] - expect(@noauth_rest).to receive(:get).with("https://supermarket.chef.io/api/v1/cookbooks/cookbook_name").and_return(@bad_category_response) - expect(@knife.ui).to receive(:fatal) - expect { @knife.run }.to raise_error(SystemExit) + expect(@noauth_rest).to receive(:get).with("https://supermarket.chef.io/api/v1/cookbooks/cookbook_name") { raise Net::HTTPServerException.new("404 Not Found", OpenStruct.new(code: "404")) } + expect(@knife).to receive(:do_upload) + expect { @knife.run }.to_not raise_error end it "should print error and exit when given only 1 argument and Chef::ServerAPI throws an exception" do diff --git a/spec/unit/node/attribute_spec.rb b/spec/unit/node/attribute_spec.rb index 57ad3c0c25..e40f454c0b 100644 --- a/spec/unit/node/attribute_spec.rb +++ b/spec/unit/node/attribute_spec.rb @@ -218,7 +218,7 @@ describe Chef::Node::Attribute do end it "gives the value at each level of precedence for a path spec" do - expected = [["set_unless_enabled?", false], + expected = [ %w{default default}, %w{env_default env_default}, %w{role_default role_default}, @@ -417,12 +417,6 @@ describe Chef::Node::Attribute do expect(@attributes.normal["foo"]["bar"]).to eq(:baz) end - it "should optionally skip setting the value if one already exists" do - @attributes.set_unless_value_present = true - @attributes.normal["hostname"] = "bar" - expect(@attributes["hostname"]).to eq("latte") - end - it "does not support ||= when setting" do # This is a limitation of auto-vivification. # Users who need this behavior can use set_unless and friends @@ -493,6 +487,7 @@ describe Chef::Node::Attribute do end it "should return true if an attribute exists but is set to nil using dot notation" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false expect(@attributes.music.deeper.has_key?("gates_of_ishtar")).to eq(true) end @@ -533,10 +528,12 @@ describe Chef::Node::Attribute do describe "method_missing" do it "should behave like a [] lookup" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false expect(@attributes.music.mastodon).to eq("rocks") end it "should allow the last method to set a value if it has an = sign on the end" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false @attributes.normal.music.mastodon = %w{dream still shining} expect(@attributes.normal.music.mastodon).to eq(%w{dream still shining}) end @@ -577,7 +574,7 @@ describe Chef::Node::Attribute do it "should yield lower if we go deeper" do collect = Array.new - @attributes.one.keys.each do |k| + @attributes["one"].keys.each do |k| collect << k end expect(collect.include?("two")).to eq(true) @@ -587,7 +584,7 @@ describe Chef::Node::Attribute do end it "should not raise an exception if one of the hashes has a nil value on a deep lookup" do - expect { @attributes.place.keys { |k| } }.not_to raise_error + expect { @attributes["place"].keys { |k| } }.not_to raise_error end end @@ -1171,6 +1168,7 @@ describe Chef::Node::Attribute do end it "raises an error when using `attr=value`" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false expect { @attributes.new_key = "new value" }.to raise_error(Chef::Exceptions::ImmutableAttributeModification) end diff --git a/spec/unit/node/immutable_collections_spec.rb b/spec/unit/node/immutable_collections_spec.rb index f57ed459cd..fe4e50d1bd 100644 --- a/spec/unit/node/immutable_collections_spec.rb +++ b/spec/unit/node/immutable_collections_spec.rb @@ -95,6 +95,10 @@ describe Chef::Node::ImmutableMash do :replace, :select!, :shift, + :write, + :write!, + :unlink, + :unlink!, ].each do |mutator| it "doesn't allow mutation via `#{mutator}'" do expect { @immutable_mash.send(mutator) }.to raise_error(Chef::Exceptions::ImmutableAttributeModification) diff --git a/spec/unit/node/vivid_mash_spec.rb b/spec/unit/node/vivid_mash_spec.rb new file mode 100644 index 0000000000..5319ba4a35 --- /dev/null +++ b/spec/unit/node/vivid_mash_spec.rb @@ -0,0 +1,377 @@ +# +# Copyright:: Copyright 2016, Chef Software 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" +require "chef/node/attribute_collections" + +describe Chef::Node::VividMash do + class Root + attr_accessor :top_level_breadcrumb + end + + let(:root) { Root.new } + + let(:vivid) do + expect(root).to receive(:reset_cache).at_least(:once).with(nil) + Chef::Node::VividMash.new(root, + { "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil } + ) + end + + def with_breadcrumb(key) + expect(root).to receive(:top_level_breadcrumb=).with(nil).at_least(:once).and_call_original + expect(root).to receive(:top_level_breadcrumb=).with(key).at_least(:once).and_call_original + end + + context "#read" do + before do + # vivify the vividmash, then we're read-only so the cache should never be cleared afterwards + vivid + expect(root).not_to receive(:reset_cache) + end + + it "reads hashes deeply" do + with_breadcrumb("one") + expect(vivid.read("one", "two", "three")).to eql("four") + end + + it "does not trainwreck when hitting hash keys that do not exist" do + with_breadcrumb("one") + expect(vivid.read("one", "five", "six")).to eql(nil) + end + + it "does not trainwreck when hitting an array with an out of bounds index" do + with_breadcrumb("array") + expect(vivid.read("array", 5, "one")).to eql(nil) + end + + it "does not trainwreck when hitting an array with a string key" do + with_breadcrumb("array") + expect(vivid.read("array", "one", "two")).to eql(nil) + end + + it "does not trainwreck when traversing a nil" do + with_breadcrumb("nil") + expect(vivid.read("nil", "one", "two")).to eql(nil) + end + end + + context "#exist?" do + before do + # vivify the vividmash, then we're read-only so the cache should never be cleared afterwards + vivid + expect(root).not_to receive(:reset_cache) + end + + it "true if there's a hash key there" do + with_breadcrumb("one") + expect(vivid.exist?("one", "two", "three")).to be true + end + + it "true for intermediate hashes" do + with_breadcrumb("one") + expect(vivid.exist?("one")).to be true + end + + it "true for arrays that exist" do + with_breadcrumb("array") + expect(vivid.exist?("array", 1)).to be true + end + + it "true when the value of the key is nil" do + with_breadcrumb("nil") + expect(vivid.exist?("nil")).to be true + end + + it "false when attributes don't exist" do + with_breadcrumb("one") + expect(vivid.exist?("one", "five", "six")).to be false + end + + it "false when traversing a non-container" do + with_breadcrumb("one") + expect(vivid.exist?("one", "two", "three", "four")).to be false + end + + it "false when an array index does not exist" do + with_breadcrumb("array") + expect(vivid.exist?("array", 3)).to be false + end + + it "false when traversing a nil" do + with_breadcrumb("nil") + expect(vivid.exist?("nil", "foo", "bar")).to be false + end + end + + context "#read!" do + before do + # vivify the vividmash, then we're read-only so the cache should never be cleared afterwards + vivid + expect(root).not_to receive(:reset_cache) + end + + it "reads hashes deeply" do + with_breadcrumb("one") + expect(vivid.read!("one", "two", "three")).to eql("four") + end + + it "reads arrays deeply" do + with_breadcrumb("array") + expect(vivid.read!("array", 1)).to eql(1) + end + + it "throws an exception when attributes do not exist" do + with_breadcrumb("one") + expect { vivid.read!("one", "five", "six") }.to raise_error(Chef::Exceptions::NoSuchAttribute) + end + + it "throws an exception when traversing a non-container" do + with_breadcrumb("one") + expect { vivid.read!("one", "two", "three", "four") }.to raise_error(Chef::Exceptions::NoSuchAttribute) + end + + it "throws an exception when an array element does not exist" do + with_breadcrumb("array") + expect { vivid.read!("array", 3) }.to raise_error(Chef::Exceptions::NoSuchAttribute) + end + end + + context "#write" do + before do + vivid + expect(root).not_to receive(:reset_cache).with(nil) + end + + it "should write into hashes" do + with_breadcrumb("one") + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write("one", "five", "six") + expect(vivid["one"]["five"]).to eql("six") + end + + it "should deeply autovivify" do + with_breadcrumb("one") + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write("one", "five", "six", "seven", "eight", "nine", "ten") + expect(vivid["one"]["five"]["six"]["seven"]["eight"]["nine"]).to eql("ten") + end + + it "should raise an exception if you overwrite an array with a hash" do + with_breadcrumb("array") + expect(root).to receive(:reset_cache).at_least(:once).with("array") + vivid.write("array", "five", "six") + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => { "five" => "six" }, "nil" => nil }) + end + + it "should raise an exception if you traverse through an array with a hash" do + with_breadcrumb("array") + expect(root).to receive(:reset_cache).at_least(:once).with("array") + vivid.write("array", "five", "six", "seven") + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => { "five" => { "six" => "seven" } }, "nil" => nil }) + end + + it "should raise an exception if you overwrite a string with a hash" do + with_breadcrumb("one") + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write("one", "two", "three", "four", "five") + expect(vivid).to eql({ "one" => { "two" => { "three" => { "four" => "five" } } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should raise an exception if you traverse through a string with a hash" do + with_breadcrumb("one") + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write("one", "two", "three", "four", "five", "six") + expect(vivid).to eql({ "one" => { "two" => { "three" => { "four" => { "five" => "six" } } } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should raise an exception if you overwrite a nil with a hash" do + with_breadcrumb("nil") + expect(root).to receive(:reset_cache).at_least(:once).with("nil") + vivid.write("nil", "one", "two") + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => { "one" => "two" } }) + end + + it "should raise an exception if you traverse through a nil with a hash" do + with_breadcrumb("nil") + expect(root).to receive(:reset_cache).at_least(:once).with("nil") + vivid.write("nil", "one", "two", "three") + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => { "one" => { "two" => "three" } } }) + end + + it "writes with a block" do + with_breadcrumb("one") + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write("one", "five") { "six" } + expect(vivid["one"]["five"]).to eql("six") + end + end + + context "#write!" do + before do + vivid + expect(root).not_to receive(:reset_cache).with(nil) + end + + it "should write into hashes" do + with_breadcrumb("one") + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write!("one", "five", "six") + expect(vivid["one"]["five"]).to eql("six") + end + + it "should deeply autovivify" do + with_breadcrumb("one") + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write!("one", "five", "six", "seven", "eight", "nine", "ten") + expect(vivid["one"]["five"]["six"]["seven"]["eight"]["nine"]).to eql("ten") + end + + it "should raise an exception if you overwrite an array with a hash" do + with_breadcrumb("array") + expect(root).not_to receive(:reset_cache) + expect { vivid.write!("array", "five", "six") }.to raise_error(Chef::Exceptions::AttributeTypeMismatch) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should raise an exception if you traverse through an array with a hash" do + with_breadcrumb("array") + expect(root).not_to receive(:reset_cache) + expect { vivid.write!("array", "five", "six", "seven") }.to raise_error(Chef::Exceptions::AttributeTypeMismatch) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should raise an exception if you overwrite a string with a hash" do + with_breadcrumb("one") + expect(root).not_to receive(:reset_cache) + expect { vivid.write!("one", "two", "three", "four", "five") }.to raise_error(Chef::Exceptions::AttributeTypeMismatch) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should raise an exception if you traverse through a string with a hash" do + with_breadcrumb("one") + expect(root).not_to receive(:reset_cache) + expect { vivid.write!("one", "two", "three", "four", "five", "six") }.to raise_error(Chef::Exceptions::AttributeTypeMismatch) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should raise an exception if you overwrite a nil with a hash" do + with_breadcrumb("nil") + expect(root).not_to receive(:reset_cache) + expect { vivid.write!("nil", "one", "two") }.to raise_error(Chef::Exceptions::AttributeTypeMismatch) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should raise an exception if you traverse through a nil with a hash" do + with_breadcrumb("nil") + expect(root).not_to receive(:reset_cache) + expect { vivid.write!("nil", "one", "two", "three") }.to raise_error(Chef::Exceptions::AttributeTypeMismatch) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "writes with a block" do + with_breadcrumb("one") + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write!("one", "five") { "six" } + expect(vivid["one"]["five"]).to eql("six") + end + end + + context "#unlink" do + before do + vivid + expect(root).not_to receive(:reset_cache).with(nil) + end + + it "should return nil if the keys don't already exist" do + expect(root).to receive(:top_level_breadcrumb=).with(nil).at_least(:once).and_call_original + expect(root).not_to receive(:reset_cache) + expect(vivid.unlink("five", "six", "seven", "eight")).to eql(nil) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should unlink hashes" do + with_breadcrumb("one") + expect(root).to receive(:reset_cache).at_least(:once).with("one") + expect( vivid.unlink("one") ).to eql({ "two" => { "three" => "four" } }) + expect(vivid).to eql({ "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should unlink array elements" do + with_breadcrumb("array") + expect(root).to receive(:reset_cache).at_least(:once).with("array") + expect(vivid.unlink("array", 2)).to eql(2) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1 ], "nil" => nil }) + end + + it "should unlink nil" do + with_breadcrumb("nil") + expect(root).to receive(:reset_cache).at_least(:once).with("nil") + expect(vivid.unlink("nil")).to eql(nil) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ] }) + end + + it "should traverse a nil and safely do nothing" do + with_breadcrumb("nil") + expect(root).not_to receive(:reset_cache) + expect(vivid.unlink("nil", "foo")).to eql(nil) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + end + + context "#unlink!" do + before do + vivid + expect(root).not_to receive(:reset_cache).with(nil) + end + + it "should raise an exception if the keys don't already exist" do + expect(root).to receive(:top_level_breadcrumb=).with(nil).at_least(:once).and_call_original + expect(root).not_to receive(:reset_cache) + expect { vivid.unlink!("five", "six", "seven", "eight") }.to raise_error(Chef::Exceptions::NoSuchAttribute) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should unlink! hashes" do + with_breadcrumb("one") + expect(root).to receive(:reset_cache).at_least(:once).with("one") + expect( vivid.unlink!("one") ).to eql({ "two" => { "three" => "four" } }) + expect(vivid).to eql({ "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should unlink! array elements" do + with_breadcrumb("array") + expect(root).to receive(:reset_cache).at_least(:once).with("array") + expect(vivid.unlink!("array", 2)).to eql(2) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1 ], "nil" => nil }) + end + + it "should unlink! nil" do + with_breadcrumb("nil") + expect(root).to receive(:reset_cache).at_least(:once).with("nil") + expect(vivid.unlink!("nil")).to eql(nil) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ] }) + end + + it "should raise an exception if it traverses a nil" do + with_breadcrumb("nil") + expect(root).not_to receive(:reset_cache) + expect { vivid.unlink!("nil", "foo") }.to raise_error(Chef::Exceptions::NoSuchAttribute) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + end +end diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index 923b488f72..72731c927f 100644 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -233,60 +233,69 @@ describe Chef::Node do end it "should let you go deep with attribute?" do - node.set["battles"]["people"]["wonkey"] = true + node.normal["battles"]["people"]["wonkey"] = true expect(node["battles"]["people"].attribute?("wonkey")).to eq(true) expect(node["battles"]["people"].attribute?("snozzberry")).to eq(false) end it "does not allow you to set an attribute via method_missing" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false expect { node.sunshine = "is bright" }.to raise_error(Chef::Exceptions::ImmutableAttributeModification) end it "should allow you get get an attribute via method_missing" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false node.default.sunshine = "is bright" expect(node.sunshine).to eql("is bright") end describe "normal attributes" do it "should allow you to set an attribute with set, without pre-declaring a hash" do - node.set[:snoopy][:is_a_puppy] = true + node.normal[:snoopy][:is_a_puppy] = true expect(node[:snoopy][:is_a_puppy]).to eq(true) end + it "should allow you to set an attribute with set_unless with method_missing but emit a deprecation warning" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false + node.normal_unless.snoopy.is_a_puppy = false + expect(node[:snoopy][:is_a_puppy]).to eq(false) + end + it "should allow you to set an attribute with set_unless" do - node.set_unless[:snoopy][:is_a_puppy] = false + node.normal_unless[:snoopy][:is_a_puppy] = false expect(node[:snoopy][:is_a_puppy]).to eq(false) end it "should not allow you to set an attribute with set_unless if it already exists" do - node.set[:snoopy][:is_a_puppy] = true - node.set_unless[:snoopy][:is_a_puppy] = false + node.normal[:snoopy][:is_a_puppy] = true + node.normal_unless[:snoopy][:is_a_puppy] = false expect(node[:snoopy][:is_a_puppy]).to eq(true) end it "should allow you to set an attribute with set_unless if is a nil value" do node.attributes.normal = { snoopy: { is_a_puppy: nil } } - node.set_unless[:snoopy][:is_a_puppy] = false + node.normal_unless[:snoopy][:is_a_puppy] = false expect(node[:snoopy][:is_a_puppy]).to eq(false) end it "should allow you to set a value after a set_unless" do # this tests for set_unless_present state bleeding between statements CHEF-3806 - node.set_unless[:snoopy][:is_a_puppy] = false - node.set[:snoopy][:is_a_puppy] = true + node.normal_unless[:snoopy][:is_a_puppy] = false + node.normal[:snoopy][:is_a_puppy] = true expect(node[:snoopy][:is_a_puppy]).to eq(true) end it "should let you set a value after a 'dangling' set_unless" do # this tests for set_unless_present state bleeding between statements CHEF-3806 - node.set[:snoopy][:is_a_puppy] = "what" - node.set_unless[:snoopy][:is_a_puppy] - node.set[:snoopy][:is_a_puppy] = true + node.normal[:snoopy][:is_a_puppy] = "what" + node.normal_unless[:snoopy][:is_a_puppy] + node.normal[:snoopy][:is_a_puppy] = true expect(node[:snoopy][:is_a_puppy]).to eq(true) end it "auto-vivifies attributes created via method syntax" do - node.set.fuu.bahrr.baz = "qux" + Chef::Config[:treat_deprecation_warnings_as_errors] = false + node.normal.fuu.bahrr.baz = "qux" expect(node.fuu.bahrr.baz).to eq("qux") end @@ -295,6 +304,20 @@ describe Chef::Node do node.tag("three", "four") expect(node["tags"]).to eq(%w{one two three four}) end + + it "set is a deprecated alias for normal" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false + expect(Chef).to receive(:log_deprecation).with(/set is deprecated/) + node.set[:snoopy][:is_a_puppy] = true + expect(node[:snoopy][:is_a_puppy]).to eq(true) + end + + it "set_unless is a deprecated alias for normal_unless" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false + expect(Chef).to receive(:log_deprecation).with(/set_unless is deprecated/) + node.set_unless[:snoopy][:is_a_puppy] = false + expect(node[:snoopy][:is_a_puppy]).to eq(false) + end end describe "default attributes" do @@ -329,10 +352,41 @@ describe Chef::Node do expect(node[:snoopy][:is_a_puppy]).to eq(true) end + it "does not exhibit chef/chef/issues/5005 bug" do + node.env_default["a"]["r1"]["g"]["u"] = "u1" + node.default_unless["a"]["r1"]["g"]["r"] = "r" + expect(node["a"]["r1"]["g"]["u"]).to eql("u1") + end + it "auto-vivifies attributes created via method syntax" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false node.default.fuu.bahrr.baz = "qux" expect(node.fuu.bahrr.baz).to eq("qux") end + + it "default_unless correctly resets the deep merge cache" do + node.normal["tags"] = [] # this sets our top-level breadcrumb + node.default_unless["foo"]["bar"] = "NK-19V" + expect(node["foo"]["bar"]).to eql("NK-19V") + node.default_unless["foo"]["baz"] = "NK-33" + expect(node["foo"]["baz"]).to eql("NK-33") + end + + it "normal_unless correctly resets the deep merge cache" do + node.normal["tags"] = [] # this sets our top-level breadcrumb + node.normal_unless["foo"]["bar"] = "NK-19V" + expect(node["foo"]["bar"]).to eql("NK-19V") + node.normal_unless["foo"]["baz"] = "NK-33" + expect(node["foo"]["baz"]).to eql("NK-33") + end + + it "override_unless correctly resets the deep merge cache" do + node.normal["tags"] = [] # this sets our top-level breadcrumb + node.override_unless["foo"]["bar"] = "NK-19V" + expect(node["foo"]["bar"]).to eql("NK-19V") + node.override_unless["foo"]["baz"] = "NK-33" + expect(node["foo"]["baz"]).to eql("NK-33") + end end describe "override attributes" do @@ -368,6 +422,7 @@ describe Chef::Node do end it "auto-vivifies attributes created via method syntax" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false node.override.fuu.bahrr.baz = "qux" expect(node.fuu.bahrr.baz).to eq("qux") end @@ -453,8 +508,9 @@ describe Chef::Node do expect( node["mysql"]["server"][0]["port"] ).to be_nil end - it "does not have a horrible error message when mistaking arrays for hashes" do - expect { node.rm("mysql", "server", "port") }.to raise_error(TypeError, "Wrong type in index of attribute (did you use a Hash index on an Array?)") + it "when mistaking arrays for hashes, it considers the value removed and does nothing" do + node.rm("mysql", "server", "port") + expect(node["mysql"]["server"][0]["port"]).to eql(3456) end end end @@ -706,6 +762,7 @@ describe Chef::Node do # describe "deep merge attribute cache edge conditions" do it "does not error with complicated attribute substitution" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false node.default["chef_attribute_hell"]["attr1"] = "attribute1" node.default["chef_attribute_hell"]["attr2"] = "#{node.chef_attribute_hell.attr1}/attr2" expect { node.default["chef_attribute_hell"]["attr3"] = "#{node.chef_attribute_hell.attr2}/attr3" }.not_to raise_error @@ -720,6 +777,7 @@ describe Chef::Node do end it "method interpolation syntax also works" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false node.default["passenger"]["version"] = "4.0.57" node.default["passenger"]["root_path"] = "passenger-#{node['passenger']['version']}" node.default["passenger"]["root_path_2"] = "passenger-#{node.passenger['version']}" @@ -729,6 +787,7 @@ describe Chef::Node do end it "should raise an ArgumentError if you ask for an attribute that doesn't exist via method_missing" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false expect { node.sunshine }.to raise_error(NoMethodError) end @@ -744,6 +803,51 @@ describe Chef::Node do expect(seen_attributes["sunshine"]).to eq("is bright") expect(seen_attributes["canada"]).to eq("is a nice place") end + + describe "functional attribute API" do + # deeper functional testing of this API is in the VividMash spec tests + it "should have an exist? function" do + node.default["foo"]["bar"] = "baz" + expect(node.exist?("foo", "bar")).to be true + expect(node.exist?("bar", "foo")).to be false + end + + it "should have a read function" do + node.override["foo"]["bar"] = "baz" + expect(node.read("foo", "bar")).to eql("baz") + expect(node.read("bar", "foo")).to eql(nil) + end + + it "should have a read! function" do + node.override["foo"]["bar"] = "baz" + expect(node.read!("foo", "bar")).to eql("baz") + expect { node.read!("bar", "foo") }.to raise_error(Chef::Exceptions::NoSuchAttribute) + end + + it "delegates write(:level) to node.level.write()" do + node.write(:default, "foo", "bar", "baz") + expect(node.default["foo"]["bar"]).to eql("baz") + end + + it "delegates write!(:level) to node.level.write!()" do + node.write!(:default, "foo", "bar", "baz") + expect(node.default["foo"]["bar"]).to eql("baz") + node.default["bar"] = true + expect { node.write!(:default, "bar", "foo", "baz") }.to raise_error(Chef::Exceptions::AttributeTypeMismatch) + end + + it "delegates unlink(:level) to node.level.unlink()" do + node.default["foo"]["bar"] = "baz" + expect(node.unlink(:default, "foo", "bar")).to eql("baz") + expect(node.unlink(:default, "bar", "foo")).to eql(nil) + end + + it "delegates unlink!(:level) to node.level.unlink!()" do + node.default["foo"]["bar"] = "baz" + expect(node.unlink!(:default, "foo", "bar")).to eql("baz") + expect { node.unlink!(:default, "bar", "foo") }.to raise_error(Chef::Exceptions::NoSuchAttribute) + end + end end describe "consuming json" do @@ -789,8 +893,8 @@ describe Chef::Node do it "should add json attributes to the node" do node.consume_external_attrs(@ohai_data, { "one" => "two", "three" => "four" }) - expect(node.one).to eql("two") - expect(node.three).to eql("four") + expect(node["one"]).to eql("two") + expect(node["three"]).to eql("four") end it "should set the tags attribute to an empty array if it is not already defined" do @@ -824,17 +928,17 @@ describe Chef::Node do it "deep merges attributes instead of overwriting them" do node.consume_external_attrs(@ohai_data, "one" => { "two" => { "three" => "four" } }) - expect(node.one.to_hash).to eq({ "two" => { "three" => "four" } }) + expect(node["one"].to_hash).to eq({ "two" => { "three" => "four" } }) node.consume_external_attrs(@ohai_data, "one" => { "abc" => "123" }) node.consume_external_attrs(@ohai_data, "one" => { "two" => { "foo" => "bar" } }) - expect(node.one.to_hash).to eq({ "two" => { "three" => "four", "foo" => "bar" }, "abc" => "123" }) + expect(node["one"].to_hash).to eq({ "two" => { "three" => "four", "foo" => "bar" }, "abc" => "123" }) end it "gives attributes from JSON priority when deep merging" do node.consume_external_attrs(@ohai_data, "one" => { "two" => { "three" => "four" } }) - expect(node.one.to_hash).to eq({ "two" => { "three" => "four" } }) + expect(node["one"].to_hash).to eq({ "two" => { "three" => "four" } }) node.consume_external_attrs(@ohai_data, "one" => { "two" => { "three" => "forty-two" } }) - expect(node.one.to_hash).to eq({ "two" => { "three" => "forty-two" } }) + expect(node["one"].to_hash).to eq({ "two" => { "three" => "forty-two" } }) end end @@ -1036,10 +1140,10 @@ describe Chef::Node do end it "sets attributes from the files" do - expect(node.ldap_server).to eql("ops1prod") - expect(node.ldap_basedn).to eql("dc=hjksolutions,dc=com") - expect(node.ldap_replication_password).to eql("forsure") - expect(node.smokey).to eql("robinson") + expect(node["ldap_server"]).to eql("ops1prod") + expect(node["ldap_basedn"]).to eql("dc=hjksolutions,dc=com") + expect(node["ldap_replication_password"]).to eql("forsure") + expect(node["smokey"]).to eql("robinson") end it "gives a sensible error when attempting to load a missing attributes file" do @@ -1083,8 +1187,8 @@ describe Chef::Node do it "should load a node from a ruby file" do node.from_file(File.expand_path(File.join(CHEF_SPEC_DATA, "nodes", "test.rb"))) expect(node.name).to eql("test.example.com-short") - expect(node.sunshine).to eql("in") - expect(node.something).to eql("else") + expect(node["sunshine"]).to eql("in") + expect(node["something"]).to eql("else") expect(node.run_list).to eq(["operations-master", "operations-monitoring"]) end @@ -1562,4 +1666,19 @@ describe Chef::Node do end end + describe "method_missing handling" do + it "should have an #empty? method via Chef::Node::Attribute" do + node.default["foo"] = "bar" + expect(node.empty?).to be false + end + + it "it should correctly implement #respond_to?" do + expect(node.respond_to?(:empty?)).to be true + end + + it "it should correctly retrieve the method with #method" do + expect(node.method(:empty?)).to be_kind_of(Method) + end + end + end diff --git a/spec/unit/provider/package/aix_spec.rb b/spec/unit/provider/package/aix_spec.rb index 6940874a43..2b574bb7df 100644 --- a/spec/unit/provider/package/aix_spec.rb +++ b/spec/unit/provider/package/aix_spec.rb @@ -73,6 +73,19 @@ describe Chef::Provider::Package::Aix do expect(@new_resource.version).to eq("3.3.12.0") end + it "should warn if the package is not a fileset" do + info = "samba.base:samba.base.samples:3.3.12.0::COMMITTED:I:Samba for AIX: + /etc/objrepos:samba.base:3.3.12.0::COMMITTED:I:Samba for AIX:" + status = double("Status", :stdout => info, :exitstatus => 0) + expect(@provider).to receive(:shell_out).with("installp -L -d /tmp/samba.base", timeout: 900).and_return(status) + expect(@provider).to receive(:shell_out).with("lslpp -lcq samba.base", timeout: 900).and_return(@empty_status) + expect(Chef::Log).to receive(:warn).once.with(%r{bff package by product name}) + @provider.load_current_resource + + expect(@provider.current_resource.package_name).to eq("samba.base") + expect(@new_resource.version).to eq("3.3.12.0") + end + it "should return the current version installed if found by lslpp" do status = double("Status", :stdout => @bffinfo, :exitstatus => 0) @stdout = StringIO.new(@bffinfo) diff --git a/spec/unit/provider/package/zypper_spec.rb b/spec/unit/provider/package/zypper_spec.rb index df0a1da9a2..8838c26b71 100644 --- a/spec/unit/provider/package/zypper_spec.rb +++ b/spec/unit/provider/package/zypper_spec.rb @@ -72,7 +72,7 @@ describe Chef::Provider::Package::Zypper do provider.load_current_resource end - it "should set the installed version if zypper info has one" do + it "should set the installed version if zypper info has one (zypper version < 1.13.0)" do status = double(:stdout => "Version: 1.0\nInstalled: Yes\n", :exitstatus => 0) allow(provider).to receive(:shell_out!).and_return(status) @@ -80,7 +80,15 @@ describe Chef::Provider::Package::Zypper do provider.load_current_resource end - it "should set the candidate version if zypper info has one" do + it "should set the installed version if zypper info has one (zypper version >= 1.13.0)" do + status = double(:stdout => "Version : 1.0 \nInstalled : Yes \n", :exitstatus => 0) + + allow(provider).to receive(:shell_out!).and_return(status) + expect(current_resource).to receive(:version).with(["1.0"]).and_return(true) + provider.load_current_resource + end + + it "should set the candidate version if zypper info has one (zypper version < 1.13.0)" do status = double(:stdout => "Version: 1.0\nInstalled: No\nStatus: out-of-date (version 0.9 installed)", :exitstatus => 0) allow(provider).to receive(:shell_out!).and_return(status) @@ -88,6 +96,14 @@ describe Chef::Provider::Package::Zypper do expect(provider.candidate_version).to eql(["1.0"]) end + it "should set the candidate version if zypper info has one (zypper version >= 1.13.0)" do + status = double(:stdout => "Version : 1.0 \nInstalled : No \nStatus : out-of-date (version 0.9 installed)", :exitstatus => 0) + + allow(provider).to receive(:shell_out!).and_return(status) + provider.load_current_resource + expect(provider.candidate_version).to eql(["1.0"]) + end + it "should return the current resouce" do expect(provider.load_current_resource).to eql(current_resource) end diff --git a/spec/unit/provider/powershell_script_spec.rb b/spec/unit/provider/powershell_script_spec.rb index 5b5c783986..96869ff31c 100644 --- a/spec/unit/provider/powershell_script_spec.rb +++ b/spec/unit/provider/powershell_script_spec.rb @@ -43,8 +43,8 @@ describe Chef::Provider::PowershellScript, "action_run" do allow(Chef::Platform).to receive(:windows_nano_server?).and_return(true) allow(provider).to receive(:is_forced_32bit).and_return(false) os_info_double = double("os_info") - allow(provider.run_context.node.kernel).to receive(:os_info).and_return(os_info_double) - allow(os_info_double).to receive(:system_directory).and_return("C:\\Windows\\system32") + allow(provider.run_context.node["kernel"]).to receive(:[]).with("os_info").and_return(os_info_double) + allow(os_info_double).to receive(:[]).with("system_directory").and_return("C:\\Windows\\system32") end it "sets the -Command flag as the last flag" do @@ -58,8 +58,8 @@ describe Chef::Provider::PowershellScript, "action_run" do allow(Chef::Platform).to receive(:windows_nano_server?).and_return(false) allow(provider).to receive(:is_forced_32bit).and_return(false) os_info_double = double("os_info") - allow(provider.run_context.node.kernel).to receive(:os_info).and_return(os_info_double) - allow(os_info_double).to receive(:system_directory).and_return("C:\\Windows\\system32") + allow(provider.run_context.node["kernel"]).to receive(:[]).with("os_info").and_return(os_info_double) + allow(os_info_double).to receive(:[]).with("system_directory").and_return("C:\\Windows\\system32") end it "sets the -File flag as the last flag" do diff --git a/spec/unit/recipe_spec.rb b/spec/unit/recipe_spec.rb index 70164f0a9b..3c30f96b20 100644 --- a/spec/unit/recipe_spec.rb +++ b/spec/unit/recipe_spec.rb @@ -266,6 +266,27 @@ describe Chef::Recipe do end end + class Coerced < Chef::Resource + resource_name :coerced + provides :coerced + default_action :whatever + property :package_name, [String, Array], coerce: proc { |x| [x].flatten }, name_property: true + def after_created + Array(action).each do |action| + run_action(action) + end + end + action :whatever do + package_name # unlazy the package_name + end + end + + it "does not emit 3694 when the name_property is unlazied by running it at compile_time" do + recipe.coerced "string" + expect(Chef).to_not receive(:log_deprecation) + recipe.coerced "string" + end + it "validating resources via build_resource" do expect {recipe.build_resource(:remote_file, "klopp") do source Chef::DelayedEvaluator.new { "http://chef.io" } diff --git a/spec/unit/run_context_spec.rb b/spec/unit/run_context_spec.rb index 7b2a27e9f6..234cd3c9e1 100644 --- a/spec/unit/run_context_spec.rb +++ b/spec/unit/run_context_spec.rb @@ -153,8 +153,8 @@ describe Chef::RunContext do let(:chef_repo_path) { File.expand_path(File.join(CHEF_SPEC_DATA, "cookbooks")) } let(:node) { node = Chef::Node.new - node.set[:platform] = "ubuntu" - node.set[:platform_version] = "13.04" + node.normal[:platform] = "ubuntu" + node.normal[:platform_version] = "13.04" node.name("testing") node } diff --git a/spec/unit/shell/shell_session_spec.rb b/spec/unit/shell/shell_session_spec.rb index 22f3dc6f0c..259e6096a4 100644 --- a/spec/unit/shell/shell_session_spec.rb +++ b/spec/unit/shell/shell_session_spec.rb @@ -158,7 +158,7 @@ describe Shell::SoloSession do it "keeps json attribs and passes them to the node for consumption" do @session.node_attributes = { "besnard_lakes" => "are_the_dark_horse" } - expect(@session.node.besnard_lakes).to eq("are_the_dark_horse") + expect(@session.node["besnard_lakes"]).to eq("are_the_dark_horse") #pending "1) keep attribs in an ivar 2) pass them to the node 3) feed them to the node on reset" end |