From 0d7aac70c39aae2456ca5eba76af2f80057324ad Mon Sep 17 00:00:00 2001 From: Aliasgar16 Date: Mon, 23 Jan 2017 21:12:37 +0530 Subject: Added module_version attribute for dsc_resource. (#5701) * Added module_version attribute for dsc_resource. Signed-off-by: aliasgar16 --- spec/unit/provider/dsc_resource_spec.rb | 175 ++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) (limited to 'spec/unit/provider') diff --git a/spec/unit/provider/dsc_resource_spec.rb b/spec/unit/provider/dsc_resource_spec.rb index 34eb9727f8..96356e5d73 100644 --- a/spec/unit/provider/dsc_resource_spec.rb +++ b/spec/unit/provider/dsc_resource_spec.rb @@ -165,4 +165,179 @@ describe Chef::Provider::DscResource do end end end + + describe "define_resource_requirements" do + let (:node) do + set_node_object + end + + context "module usage is valid" do + before do + allow(provider).to receive(:module_usage_valid?).and_return(true) + allow(provider).to receive(:action_run) + end + + [:run].each do |action| + context "action #{action}" do + it "does not raise the exception" do + expect { provider.run_action(action) }.not_to raise_error + end + end + end + end + + context "module usage is invalid" do + before do + allow(provider).to receive(:module_usage_valid?).and_return(false) + allow(provider).to receive(:action_run) + end + + [:run].each do |action| + context "action #{action}" do + it "raises the exception" do + expect { provider.run_action(action) }.to raise_error( + Chef::Exceptions::DSCModuleNameMissing + ) + end + end + end + end + end + + describe "module_usage_valid?" do + let (:node) do + set_node_object + end + + context "module_name and module_version both are not provided" do + before do + provider.instance_variable_set(:@module_name, nil) + provider.instance_variable_set(:@module_version, nil) + end + + it "returns true" do + response = provider.send(:module_usage_valid?) + expect(response).to be == true + end + end + + context "module_name and module_version both are provided" do + before do + provider.instance_variable_set(:@module_name, "my_module") + provider.instance_variable_set(:@module_version, "1.3") + end + + it "returns true" do + response = provider.send(:module_usage_valid?) + expect(response).to be == true + end + end + + context "module_name is given but module_version is not given" do + before do + provider.instance_variable_set(:@module_name, "my_module") + provider.instance_variable_set(:@module_version, nil) + end + + it "returns true" do + response = provider.send(:module_usage_valid?) + expect(response).to be == true + end + end + + context "module_name is not given but module_version is given" do + before do + provider.instance_variable_set(:@module_name, nil) + provider.instance_variable_set(:@module_version, "1.4.0.1") + end + + it "returns false" do + response = provider.send(:module_usage_valid?) + expect(response).to be == false + end + end + end + + describe "module_info_object" do + let (:node) do + set_node_object + end + + context "module_version is not given" do + before do + provider.instance_variable_set(:@module_version, nil) + allow(provider).to receive(:module_name).and_return("my_module") + end + + it "returns only name of the module" do + response = provider.send(:module_info_object) + expect(response).to be == "my_module" + end + end + + context "module_version is given" do + before do + provider.instance_variable_set(:@module_version, "1.3.1") + allow(provider).to receive(:module_name).and_return("my_module") + end + + it "returns the module info object" do + response = provider.send(:module_info_object) + expect(response).to be == "@{ModuleName='my_module';ModuleVersion='1.3.1'}" + end + end + end + + describe "invoke_resource" do + let (:node) do + set_node_object + end + + let(:cmdlet) { double(:run! => nil) } + + before(:each) do + allow(provider).to receive(:translate_type).and_return("my_properties") + provider.instance_variable_set(:@new_resource, double( + :properties => "my_properties", :resource => "my_resource", :timeout => 123 + )) + end + + context "when module_version is not given" do + before do + allow(provider).to receive(:module_info_object).and_return("my_module") + end + + it "invokes Invoke-DscResource command with module name" do + expect(Chef::Util::Powershell::Cmdlet).to receive(:new).with( + node, + "Invoke-DscResource -Method my_method -Name my_resource -Property my_properties -Module my_module -Verbose", + "my_output_format" + ).and_return(cmdlet) + provider.send(:invoke_resource, "my_method", "my_output_format") + end + end + + context "when module_version is given" do + before do + allow(provider).to receive(:module_info_object).and_return( + "@{ModuleName='my_module';ModuleVersion='my_module_version'}" + ) + end + + it "invokes Invoke-DscResource command with module info object" do + expect(Chef::Util::Powershell::Cmdlet).to receive(:new).with( + node, + "Invoke-DscResource -Method my_method -Name my_resource -Property my_properties -Module @{ModuleName='my_module';ModuleVersion='my_module_version'} -Verbose", + "my_output_format" + ).and_return(cmdlet) + provider.send(:invoke_resource, "my_method", "my_output_format") + end + end + end +end + +def set_node_object + node = Chef::Node.new + node.automatic[:languages][:powershell][:version] = "5.0.10586.0" + node end -- cgit v1.2.1