diff options
author | PrajaktaPurohit <prajakta@opscode.com> | 2012-12-11 17:23:19 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@opscode.com> | 2012-12-19 15:56:12 -0800 |
commit | 4bd6fb95f37f94402a8a5200e2018f36df5ca146 (patch) | |
tree | 27d0fec06382d8d75b0097012732a22b5dfd7188 | |
parent | 95cefeed2f69fa79c0cd05d0683289a1e48f9b1e (diff) | |
download | chef-4bd6fb95f37f94402a8a5200e2018f36df5ca146.tar.gz |
Adding new unit tests for the registry helper, fixing older tests and refactoring code
-rw-r--r-- | spec/unit/registry_helper_spec.rb | 186 |
1 files changed, 132 insertions, 54 deletions
diff --git a/spec/unit/registry_helper_spec.rb b/spec/unit/registry_helper_spec.rb index da5a542177..a83895ba20 100644 --- a/spec/unit/registry_helper_spec.rb +++ b/spec/unit/registry_helper_spec.rb @@ -18,19 +18,9 @@ require 'spec_helper' -module Win32 - class Registry - KEY_SET_VALUE = 0x0002 - KEY_QUERY_VALUE = 0x0001 - KEY_WRITE = 0x00020000 | 0x0002 | 0x0004 - KEY_READ = 0x00020000 | 0x0001 | 0x0008 | 0x0010 - end -end - describe Chef::Provider::RegistryKey do let(:value1) { { :name => "one", :type => :string, :data => "1" } } - #let(:value2) { { :name => "two", :type => :string, :data => "2" } } let(:key_path) { 'HKCU\Software\OpscodeNumbers' } let(:key) { 'Software\OpscodeNumbers' } let(:key_parent) { 'Software' } @@ -41,12 +31,41 @@ describe Chef::Provider::RegistryKey do before(:each) do Chef::Win32::Registry.any_instance.stub(:machine_architecture).and_return(:x86_64) @registry = Chef::Win32::Registry.new() + + #Making the values for registry constants available on unix + Object.send(:remove_const, 'Win32') if defined?(Win32) + Win32 = Module.new + Win32::Registry = Class.new + Win32::Registry::KEY_SET_VALUE = 0x0002 + Win32::Registry::KEY_QUERY_VALUE = 0x0001 + Win32::Registry::KEY_WRITE = 0x00020000 | 0x0002 | 0x0004 + Win32::Registry::KEY_READ = 0x00020000 | 0x0001 | 0x0008 | 0x0010 + + Win32::Registry::Error = Class.new(RuntimeError) + + @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") + @reg_mock = mock("reg") + end + + describe "get_values" do + it "gets all values for a key if the key exists" do + @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) + @registry.should_receive(:key_exists!).with(key_path).and_return(true) + @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_READ | @registry.registry_system_architecture).and_yield(@reg_mock) + @reg_mock.should_receive(:map) + @registry.get_values(key_path) + end + + it "throws an exception if key does not exist" do + @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) + @registry.should_receive(:key_exists!).with(key_path).and_raise(Chef::Exceptions::Win32RegKeyMissing) + lambda{@registry.get_values(key_path)}.should raise_error(Chef::Exceptions::Win32RegKeyMissing) + end end describe "set_value" do it "does nothing if key and hive and value exist" do @registry.should_receive(:key_exists!).with(key_path).and_return(true) - @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) @registry.should_receive(:value_exists?).with(key_path, value1).and_return(true) @registry.should_receive(:data_exists?).with(key_path, value1).and_return(true) @@ -55,12 +74,10 @@ describe Chef::Provider::RegistryKey do it "updates value if key and hive and value exist, but data is different" do @registry.should_receive(:key_exists!).with(key_path).and_return(true) - @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) @registry.should_receive(:value_exists?).with(key_path, value1).and_return(true) @registry.should_receive(:data_exists?).with(key_path, value1).and_return(false) - @reg_mock = mock("reg") - @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_SET_VALUE | ::Win32::Registry::KEY_QUERY_VALUE | @registry.registry_system_architecture).and_yield(@reg_mock) + @hive_mock.should_receive(:open).with(key, Win32::Registry::KEY_SET_VALUE | ::Win32::Registry::KEY_QUERY_VALUE | @registry.registry_system_architecture).and_yield(@reg_mock) @registry.should_receive(:get_type_from_name).with(:string).and_return(1) @reg_mock.should_receive(:write).with("one", 1, "1") @registry.set_value(key_path, value1) @@ -68,10 +85,8 @@ describe Chef::Provider::RegistryKey do it "creates value if the key exists and the value does not exist" do @registry.should_receive(:key_exists!).with(key_path).and_return(true) - @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) @registry.should_receive(:value_exists?).with(key_path, value1).and_return(false) - @reg_mock = mock("reg") @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_SET_VALUE | ::Win32::Registry::KEY_QUERY_VALUE | @registry.registry_system_architecture).and_yield(@reg_mock) @registry.should_receive(:get_type_from_name).with(:string).and_return(1) @reg_mock.should_receive(:write).with("one", 1, "1") @@ -87,23 +102,17 @@ describe Chef::Provider::RegistryKey do describe "delete_value" do it "deletes value if value exists" do @registry.should_receive(:value_exists?).with(key_path, value1).and_return(true) - @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) - @reg_mock = mock("reg") @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_SET_VALUE | @registry.registry_system_architecture).and_yield(@reg_mock) - @reg_mock.should_receive(:delete_value).with("one") + @reg_mock.should_receive(:delete_value).with("one").and_return(true) @registry.delete_value(key_path, value1) end - # it "raises an exception if the key does not exist" do - # @registry.should_receive(:value_exists?).with(key_path, value1).and_return(true) - # @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") - # @registry.should_receive(:get_hive_and_key).with(key_path).and_raise(Chef::Exceptions::Win32RegKeyMissing) - # @reg_mock = mock("reg") - # #@hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_SET_VALUE | @registry.registry_system_architecture).and_yield(@reg_mock) - # #@reg_mock.should_receive(:delete_value).with("one") - # @registry.delete_value(key_path, value1) - # end + it "raises an exception if the key does not exist" do + @registry.should_receive(:value_exists?).with(key_path, value1).and_return(true) + @registry.should_receive(:get_hive_and_key).with(key_path).and_raise(Chef::Exceptions::Win32RegKeyMissing) + @registry.delete_value(key_path, value1) + end it "does nothing if the value does not exist" do @registry.should_receive(:value_exists?).with(key_path, value1).and_return(false) @@ -116,7 +125,6 @@ describe Chef::Provider::RegistryKey do @registry.should_receive(:keys_missing?).with(key_path).and_return(true) @registry.should_receive(:create_missing).with(key_path) @registry.should_receive(:key_exists?).with(key_path).and_return(false) - @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) @hive_mock.should_receive(:create).with(key, ::Win32::Registry::KEY_WRITE | @registry.registry_system_architecture) @registry.create_key(key_path, true) @@ -137,7 +145,6 @@ describe Chef::Provider::RegistryKey do it "create key if intermediate keys not missing and recursive is set to false" do @registry.should_receive(:keys_missing?).with(key_path).and_return(false) @registry.should_receive(:key_exists?).with(key_path).and_return(false) - @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) @hive_mock.should_receive(:create).with(key, ::Win32::Registry::KEY_WRITE | @registry.registry_system_architecture) @registry.create_key(key_path, false) @@ -146,7 +153,6 @@ describe Chef::Provider::RegistryKey do it "create key if intermediate keys not missing and recursive is set to true" do @registry.should_receive(:keys_missing?).with(key_path).and_return(false) @registry.should_receive(:key_exists?).with(key_path).and_return(false) - @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) @hive_mock.should_receive(:create).with(key, ::Win32::Registry::KEY_WRITE | @registry.registry_system_architecture) @registry.create_key(key_path, true) @@ -156,10 +162,8 @@ describe Chef::Provider::RegistryKey do describe "delete_key" do it "deletes key if it has subkeys and recursive is set to true" do @registry.should_receive(:key_exists?).with(key_path).and_return(true) - @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) @registry.should_receive(:has_subkeys?).with(key_path).and_return(true) - @reg_mock = mock("reg") @hive_mock.should_receive(:open).with(key_parent, ::Win32::Registry::KEY_WRITE | @registry.registry_system_architecture).and_yield(@reg_mock) @reg_mock.should_receive(:delete_key).with(key_to_delete, true) @registry.delete_key(key_path, true) @@ -167,7 +171,6 @@ describe Chef::Provider::RegistryKey do it "raises an exception if it has subkeys but recursive is set to false" do @registry.should_receive(:key_exists?).with(key_path).and_return(true) - @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) @registry.should_receive(:has_subkeys?).with(key_path).and_return(true) lambda{@registry.delete_key(key_path, false)}.should raise_error(Chef::Exceptions::Win32RegNoRecursive) @@ -175,28 +178,27 @@ describe Chef::Provider::RegistryKey do it "deletes key if the key exists and has no subkeys" do @registry.should_receive(:key_exists?).with(key_path).and_return(true) - @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) @registry.should_receive(:has_subkeys?).with(key_path).and_return(false) - @reg_mock = mock("reg") @hive_mock.should_receive(:open).with(key_parent, ::Win32::Registry::KEY_WRITE | @registry.registry_system_architecture).and_yield(@reg_mock) @reg_mock.should_receive(:delete_key).with(key_to_delete) @registry.delete_key(key_path, true) end end - # describe "key_exists?" do - # it "returns true if key_exists" do - # @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") - # @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) - # @hive_mock.should_receive(:open).with(key_parent, ::Win32::Registry::KEY_WRITE | @registry.registry_system_architecture).and_yield(@reg_mock) - # @registry.key_exists?(key_path) - # end - - # it "returns false if key does not exist" do + describe "key_exists?" do + it "returns true if key_exists" do + @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) + @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_READ | @registry.registry_system_architecture).and_yield(@reg_mock) + @registry.key_exists?(key_path).should == true + end - # end - # end + it "returns false if key does not exist" do + @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) + @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_READ | @registry.registry_system_architecture).and_raise(::Win32::Registry::Error) + @registry.key_exists?(key_path).should == false + end + end describe "key_exists!" do it "throws an exception if the key_parent does not exist" do @@ -205,18 +207,99 @@ describe Chef::Provider::RegistryKey do end end + describe "hive_exists?" do + it "returns true if the hive exists" do + @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) + @registry.hive_exists?(key_path) == true + end + + it "returns false if the hive does not exist" do + @registry.should_receive(:get_hive_and_key).with(key_path).and_raise(Chef::Exceptions::Win32RegHiveMissing) + @registry.hive_exists?(key_path) == false + end + end + + describe "has_subkeys?" do + it "returns true if the key has subkeys" do + @registry.should_receive(:key_exists!).with(key_path).and_return(true) + @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) + @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_READ | @registry.registry_system_architecture).and_yield(@reg_mock) + @reg_mock.should_receive(:each_key).and_yield(key) + @registry.has_subkeys?(key_path) == true + end + + it "returns false if the key does not have subkeys" do + @registry.should_receive(:key_exists!).with(key_path).and_return(true) + @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) + @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_READ | @registry.registry_system_architecture).and_yield(@reg_mock) + @reg_mock.should_receive(:each_key).and_return(no_args()) + @registry.has_subkeys?(key_path).should == false + end + + it "throws an exception if the key does not exist" do + @registry.should_receive(:key_exists!).with(key_path).and_raise(Chef::Exceptions::Win32RegKeyMissing) + lambda {@registry.set_value(key_path, value1)}.should raise_error(Chef::Exceptions::Win32RegKeyMissing) + end + end + describe "get_subkeys" do it "returns the subkeys if they exist" do @registry.should_receive(:key_exists!).with(key_path).and_return(true) - @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) - @reg_mock = mock("reg") @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_READ | @registry.registry_system_architecture).and_yield(@reg_mock) @reg_mock.should_receive(:each_key).and_yield(sub_key) @registry.get_subkeys(key_path) end end + describe "value_exists?" do + it "throws an exception if the key does not exist" do + @registry.should_receive(:key_exists!).with(key_path).and_raise(Chef::Exceptions::Win32RegKeyMissing) + lambda {@registry.value_exists?(key_path, value1)}.should raise_error(Chef::Exceptions::Win32RegKeyMissing) + end + + it "returns true if the value exists" do + @registry.should_receive(:key_exists!).with(key_path).and_return(true) + @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) + @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_READ | @registry.registry_system_architecture).and_yield(@reg_mock) + @reg_mock.should_receive(:any?).and_yield("one") + @registry.value_exists?(key_path, value1) == true + end + + it "returns false if the value does not exist" do + @registry.should_receive(:key_exists!).with(key_path).and_return(true) + @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) + @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_READ | @registry.registry_system_architecture).and_yield(@reg_mock) + @reg_mock.should_receive(:any?).and_yield(no_args()) + @registry.value_exists?(key_path, value1) == false + end + end + + describe "data_exists?" do + it "throws an exception if the key does not exist" do + @registry.should_receive(:key_exists!).with(key_path).and_raise(Chef::Exceptions::Win32RegKeyMissing) + lambda {@registry.data_exists?(key_path, value1)}.should raise_error(Chef::Exceptions::Win32RegKeyMissing) + end + + it "returns true if the data exists" do + @registry.should_receive(:key_exists!).with(key_path).and_return(true) + @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) + @registry.should_receive(:get_type_from_name).with(:string).and_return(1) + @reg_mock.should_receive(:each).with(no_args()).and_yield("one", 1, "1") + @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_READ | @registry.registry_system_architecture).and_yield(@reg_mock) + @registry.data_exists?(key_path, value1).should == true + end + + it "returns false if the data does not exist" do + @registry.should_receive(:key_exists!).with(key_path).and_return(true) + @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) + @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_READ | @registry.registry_system_architecture).and_yield(@reg_mock) + @registry.should_receive(:get_type_from_name).with(:string).and_return(1) + @reg_mock.should_receive(:each).with(no_args()).and_yield("one", 1, "2") + @registry.data_exists?(key_path, value1).should == false + end + end + describe "value_exists!" do it "does nothing if the value exists" do @registry.should_receive(:value_exists?).with(key_path, value1).and_return(true) @@ -244,9 +327,7 @@ describe Chef::Provider::RegistryKey do describe "type_matches?" do it "returns true if type matches" do @registry.should_receive(:value_exists!).with(key_path, value1).and_return(true) - @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) - @reg_mock = mock("reg") @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_READ | @registry.registry_system_architecture).and_yield(@reg_mock) @registry.should_receive(:get_type_from_name).with(:string).and_return(1) @reg_mock.should_receive(:each).and_yield("one", 1) @@ -255,11 +336,8 @@ describe Chef::Provider::RegistryKey do it "returns false if type does not match" do @registry.should_receive(:value_exists!).with(key_path, value1).and_return(true) - @hive_mock = mock("::Win32::Registry::HKEY_CURRENT_USER") @registry.should_receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) - @reg_mock = mock("reg") @hive_mock.should_receive(:open).with(key, ::Win32::Registry::KEY_READ | @registry.registry_system_architecture).and_yield(@reg_mock) - # @registry.should_receive(:get_type_from_name).with(:string).and_return(2) @reg_mock.should_receive(:each).and_yield("two", 2) @registry.type_matches?(key_path, value1).should == false end |