spec/functional/bootstrap_download_spec.rb in knife-windows-0.8.6 vs spec/functional/bootstrap_download_spec.rb in knife-windows-1.0.0.rc.0

- old
+ new

@@ -23,38 +23,38 @@ # These test cases exercise the Knife::Windows knife plugin's ability # to download a bootstrap msi as part of the bootstrap process on # Windows nodes. The test modifies the Windows batch file generated # from an erb template in the plugin source in order to enable execution # of only the download functionality contained in the bootstrap template. -# The test relies on knowledge of the fields of the template itself and +# The test relies on knowledge of the fields of the template itself and # also on knowledge of the contents and structure of the Windows batch # file generated by the template. # # Note that if the bootstrap template changes substantially, the tests # should fail and will require re-implementation. If such changes # occur, the bootstrap code should be refactored to explicitly expose # the download funcitonality separately from other tasks to make the # test more robust. -describe 'Knife::Windows::Core msi download functionality for knife Windows winrm bootstrap template' do +describe 'Knife::Windows::Core msi download functionality for knife Windows winrm bootstrap template' do before(:all) do # Since we're always running 32-bit Ruby, fix the # PROCESSOR_ARCHITECTURE environment variable. if ENV["PROCESSOR_ARCHITEW6432"] ENV["PROCESSOR_ARCHITECTURE"] = ENV["PROCESSOR_ARCHITEW6432"] end - + # All file artifacts from this test will be written into this directory @temp_directory = Dir.mktmpdir("bootstrap_test") # Location to which the download script will be modified to write # the downloaded msi @local_file_download_destination = "#{@temp_directory}/chef-client-latest.msi" - source_code_directory = File.dirname(__FILE__) - @template_file_path ="#{source_code_directory}/../../lib/chef/knife/bootstrap/windows-chef-client-msi.erb" + source_code_directory = File.dirname(__FILE__) + @template_file_path ="#{source_code_directory}/../../lib/chef/knife/bootstrap/windows-chef-client-msi.erb" end after(:all) do # Clear the temp directory upon exit if Dir.exists?(@temp_directory) @@ -69,11 +69,11 @@ before do # Stub the bootstrap context and prevent config related sections # from being populated, i.e. chef installation and first chef # run sections allow(mock_bootstrap_context).to receive(:validation_key).and_return("echo.validation_key") - allow(mock_bootstrap_context).to receive(:encrypted_data_bag_secret).and_return("echo.encrypted_data_bag_secret") + allow(mock_bootstrap_context).to receive(:secret).and_return("echo.encrypted_data_bag_secret") allow(mock_bootstrap_context).to receive(:config_content).and_return("echo.config_content") allow(mock_bootstrap_context).to receive(:start_chef).and_return("echo.echo start_chef_command") allow(mock_bootstrap_context).to receive(:run_list).and_return("echo.run_list") allow(mock_bootstrap_context).to receive(:install_chef).and_return("echo.echo install_chef_command") @@ -84,29 +84,23 @@ # Prevent password prompt during bootstrap process allow(mock_winrm).to receive(:get_password).and_return(nil) allow(Chef::Knife::Winrm).to receive(:new).and_return(mock_winrm) allow(Chef::Knife::Core::WindowsBootstrapContext).to receive(:new).and_return(mock_bootstrap_context) + Chef::Config[:knife] = {:winrm_transport => 'plaintext', :chef_node_name => 'foo.example.com', :winrm_authentication_protocol => 'negotiate'} end - it "downloads the chef-client MSI during winrm bootstrap" do + it "downloads the chef-client MSI from the default location during winrm bootstrap" do + run_download_scenario + end - clean_test_case - - winrm_bootstrapper = Chef::Knife::BootstrapWindowsWinrm.new([ "127.0.0.1" ]) - allow(winrm_bootstrapper).to receive(:wait_for_remote_response) - winrm_bootstrapper.config[:template_file] = @template_file_path - - # Execute the commands locally that would normally be executed via WinRM - allow(winrm_bootstrapper).to receive(:run_command) do |command| - system(command) + context "when provided a custom msi_url to fetch from" do + let(:mock_bootstrap_context) { Chef::Knife::Core::WindowsBootstrapContext.new( + { :msi_url => "file:///C:/Windows/System32/xcopy.exe" }, nil, { :knife => {} }) } + it "downloads the chef-client MSI from a custom path during winrm bootstrap" do + run_download_scenario end - - winrm_bootstrapper.run - - # Download should succeed - expect(download_succeeded?).to be true end end def download_succeeded? File.exists?(@local_file_download_destination) && ! File.zero?(@local_file_download_destination) @@ -116,7 +110,31 @@ def clean_test_case if File.exists?(@local_file_download_destination) File.delete(@local_file_download_destination) end end - -end + + def run_download_scenario + clean_test_case + + winrm_bootstrapper = Chef::Knife::BootstrapWindowsWinrm.new([ "127.0.0.1" ]) + + if chef_gte_12? + winrm_bootstrapper.client_builder = instance_double("Chef::Knife::Bootstrap::ClientBuilder", :run => nil, :client_path => nil) + elsif chef_lt_12? + allow(File).to receive(:exist?).with(File.expand_path(Chef::Config[:validation_key])).and_return(true) + end + + allow(winrm_bootstrapper).to receive(:wait_for_remote_response) + winrm_bootstrapper.config[:template_file] = @template_file_path + + # Execute the commands locally that would normally be executed via WinRM + allow(winrm_bootstrapper).to receive(:run_command) do |command| + system(command) + end + + winrm_bootstrapper.run + + # Download should succeed + expect(download_succeeded?).to be true + end +end