lib/kitchen/verifier/pester.rb in kitchen-pester-0.7.1 vs lib/kitchen/verifier/pester.rb in kitchen-pester-0.8.0

- old
+ new

@@ -30,11 +30,10 @@ plugin_version Kitchen::Verifier::PESTER_VERSION default_config :restart_winrm, false default_config :test_folder - default_config :run_as_scheduled_task, false default_config :use_local_pester_module, false # Creates a new Verifier object using the provided configuration data # which will be merged with any default configuration. # @@ -63,10 +62,11 @@ # end def create_sandbox super prepare_powershell_modules prepare_pester_tests + prepare_helpers end # Generates a command string which will install and configure the # verifier software on an instance. If no work is required, then `nil` # will be returned. @@ -103,18 +103,11 @@ # will be returned. # # @return [String] a command string def run_command return if local_suite_files.empty? - - cmd = if config[:run_as_scheduled_task] - wrap_scheduled_task('verify-run', run_command_script) - else - run_command_script - end - - really_wrap_shell_code(cmd) + really_wrap_shell_code(run_command_script) end #private def run_command_script <<-CMD @@ -133,96 +126,98 @@ def use_local_powershell_modules(script) <<-EOH set-executionpolicy unrestricted -force; $global:ProgressPreference = 'SilentlyContinue' - #{"$VerbosePreference = 'Continue'" if instance.logger.logdev.level == 0} $env:psmodulepath += ";$(join-path (resolve-path $env:temp).path 'verifier/modules')"; - # $env:psmodulepath -split ';' | % {write-output "PSModulePath contains:"} {write-output "`t$_"} #{script} EOH end - def random_string - (0...8).map { (65 + rand(26)).chr }.join - end - - def wrap_scheduled_task (name, script) - randomized_name = "#{name}-#{random_string}" + def install_command_script <<-EOH - import-module NamedPipes, ScheduledTaskRunner, PesterUtil - $Action = @' -#{script} -'@ - $ScriptBlock = [scriptblock]::Create($action) - Add-ScheduledTaskCommand -name #{randomized_name} -Action $ScriptBlock - Invoke-ScheduledTaskCommand -name #{randomized_name} - $ExitCode = Get-ScheduledTaskExitCode -name #{randomized_name} - Remove-ScheduledTaskCommand -name #{randomized_name} - $TestResultPath = "#{File.join(config[:root_path], 'pester/result.xml')}" - $TestResults = import-clixml $TestResultPath - Write-Host - ConvertFrom-PesterOutputObject $TestResults - Write-Host - $host.SetShouldExit($ExitCode) - EOH - end + function directory($path){ + if (test-path $path) {(resolve-path $path).providerpath} + else {(resolve-path (mkdir $path)).providerpath} + } + $VerifierModulePath = directory $env:temp/verifier/modules + $VerifierTestsPath = directory $env:temp/verifier/pester - def install_command_script - <<-EOH - function directory($path){ - if (test-path $path) {(resolve-path $path).providerpath} - else {(resolve-path (mkdir $path)).providerpath} - } - $VerifierModulePath = directory $env:temp/verifier/modules - $VerifierTestsPath = directory $env:temp/verifier/pester - - function test-module($module){ - (get-module $module -list) -ne $null - } - if (-not (test-module pester)) { - if (test-module PowerShellGet){ - import-module PowerShellGet -force - import-module PackageManagement -force - get-packageprovider -name NuGet -force | out-null - install-module Pester -force + $env:psmodulepath += ";$VerifierModulePath" + function test-module($module){ + (get-module $module -list) -ne $null } - else { - if (-not (test-module PsGet)){ - iex (new-object Net.WebClient).DownloadString('http://bit.ly/GetPsGet') + if (-not (test-module pester)) { + if (test-module PowerShellGet){ + import-module PowerShellGet -force + import-module PackageManagement -force + get-packageprovider -name NuGet -force | out-null + install-module Pester -force } - try { - import-module psget -force -erroraction stop - Install-Module Pester - } - catch { - Write-Output "Installing from Github" - $zipfile = join-path(resolve-path "$env:temp/verifier") "pester.zip" - if (-not (test-path $zipfile)){ - $source = 'https://github.com/pester/Pester/archive/3.3.14.zip' - [byte[]]$bytes = (new-object System.net.WebClient).DownloadData($source) - [IO.File]::WriteAllBytes($zipfile, $bytes) - $bytes = $null - [gc]::collect() - write-output "Downloaded Pester.zip" + else { + if (-not (test-module PsGet)){ + $wc = New-Object -TypeName Net.WebClient + + if($env:http_Proxy){ + if($env:no_proxy){ + Write-Output "Creating WebProxy with 'http_proxy' and 'no_proxy' environment variables. + $webproxy = New-Object System.Net.WebProxy($env:http_Proxy,$true,$env:no_proxy) + }else{ + Write-Output "Creating WebProxy with 'http_proxy' environment variable. + $webproxy = New-Object -TypeName System.Net.WebProxy -ArgumentList ($env:http_Proxy) + } + + $wc.proxy = $webproxy } - write-output "Creating Shell.Application COM object" - $shellcom = new-object -com shell.application - Write-Output "Creating COM object for zip file." - $zipcomobject = $shellcom.namespace($zipfile) - Write-Output "Creating COM object for module destination." - $destination = $shellcom.namespace($VerifierModulePath) - Write-Output "Unpacking zip file." - $destination.CopyHere($zipcomobject.Items(), 0x610) - rename-item (join-path $VerifierModulePath "Pester-3.3.14") -newname 'Pester' -force + + Invoke-Expression -Command $wc.DownloadString('http://bit.ly/GetPsGet') + } + try { + import-module psget -force -erroraction stop + Install-Module Pester + } + catch { + Write-Output "Installing from Github" + $zipfile = join-path(resolve-path "$env:temp/verifier") "pester.zip" + if (-not (test-path $zipfile)){ + $source = 'https://github.com/pester/Pester/archive/3.3.14.zip' + $wc = New-Object -TypeName Net.WebClient + + if($env:http_Proxy){ + if($env:no_proxy){ + Write-Output "Creating WebProxy with 'http_proxy' and 'no_proxy' environment variables." + $webproxy = New-Object System.Net.WebProxy($env:http_Proxy,$true,$env:no_proxy) + }else{ + Write-Output "Creating WebProxy with 'http_proxy' environment variable." + $webproxy = New-Object -TypeName System.Net.WebProxy -ArgumentList ($env:http_Proxy) + } + + $wc.proxy = $webproxy + } + + [byte[]]$bytes = $wc.DownloadData($source) + [IO.File]::WriteAllBytes($zipfile, $bytes) + $bytes = $null + [gc]::collect() + write-output "Downloaded Pester.zip" + } + write-output "Creating Shell.Application COM object" + $shellcom = new-object -com shell.application + Write-Output "Creating COM object for zip file." + $zipcomobject = $shellcom.namespace($zipfile) + Write-Output "Creating COM object for module destination." + $destination = $shellcom.namespace($VerifierModulePath) + Write-Output "Unpacking zip file." + $destination.CopyHere($zipcomobject.Items(), 0x610) + rename-item (join-path $VerifierModulePath "Pester-3.3.14") -newname 'Pester' -force + } } } - } - if (-not (test-module Pester)) { - throw "Unable to install Pester. Please include Pester in your base image or install during your converge." - } - EOH + if (-not (test-module Pester)) { + throw "Unable to install Pester. Please include Pester in your base image or install during your converge." + } + EOH end def restart_winrm_service cmd = 'schtasks /Create /TN restart_winrm /TR ' \ @@ -261,13 +256,38 @@ File.directory?(f) end end def sandboxify_path(path) - File.join(sandbox_path, path.sub("#{suite_test_folder}/", "")) + File.join(sandbox_path, path.sub(/#{suite_test_folder}\//i, "")) end + # Returns an Array of common helper filenames currently residing on the + # local workstation. + # + # @return [Array<String>] array of helper files + # @api private + def helper_files + glob = Dir.glob(File.join(test_folder, "helpers", "*/**/*")) + glob.reject { |f| File.directory?(f) } + end + + # Copies all common testing helper files into the suites directory in + # the sandbox. + # + # @api private + def prepare_helpers + base = File.join(test_folder, "helpers") + + helper_files.each do |src| + dest = File.join(sandbox_path, src.sub("#{base}/", "")) + debug("Copying #{src} to #{dest}") + FileUtils.mkdir_p(File.dirname(dest)) + FileUtils.cp(src, dest, preserve: true) + end + end + # Copies all test suite files into the suites directory in the sandbox. # # @api private def prepare_pester_tests info("Preparing to copy files from #{suite_test_folder} to the SUT.") @@ -280,15 +300,19 @@ end end def prepare_powershell_module(name) FileUtils.mkdir_p(File.join(sandbox_path, "modules/#{name}")) - FileUtils.cp(File.join(File.dirname(__FILE__), "../../support/powershell/#{name}/#{name}.psm1"), File.join(sandbox_path, "modules/#{name}/#{name}.psm1"), preserve: true) + FileUtils.cp( + File.join(File.dirname(__FILE__), "../../support/powershell/#{name}/#{name}.psm1"), + File.join(sandbox_path, "modules/#{name}/#{name}.psm1"), + preserve: true + ) end def prepare_powershell_modules info("Preparing to copy supporting powershell modules.") - %w[NamedPipes ScheduledTaskRunner PesterUtil].each do |module_name| + %w[PesterUtil].each do |module_name| prepare_powershell_module module_name end end