Class: Kitchen::Verifier::Pester

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/verifier/pester.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Pester

Creates a new Verifier object using the provided configuration data which will be merged with any default configuration.

Parameters:

  • config (Hash) (defaults to: {})

    provided verifier configuration



40
41
42
# File 'lib/kitchen/verifier/pester.rb', line 40

def initialize(config = {})
  init_config(config)
end

Instance Method Details

#absolute_test_folderObject



186
187
188
189
190
191
# File 'lib/kitchen/verifier/pester.rb', line 186

def absolute_test_folder
  path = (Pathname.new config[:test_folder]).realpath
  integration_path = File.join(path, 'integration')
  return path unless Dir.exist?(integration_path)
  integration_path
end

#create_sandboxObject

Creates a temporary directory on the local workstation into which verifier related files and directories can be copied or created. The contents of this directory will be copied over to the instance before invoking the verifier’s run command. After this method completes, it is expected that the contents of the sandbox is complete and ready for copy to the remote instance.

Note: any subclasses would be well advised to call super first when overriding this method, for example:

Examples:

overriding #create_sandbox


class MyVerifier < Kitchen::Verifier::Base
  def create_sandbox
    super
    # any further file copies, preparations, etc.
  end
end


62
63
64
65
# File 'lib/kitchen/verifier/pester.rb', line 62

def create_sandbox
  super
  prepare_pester_tests
end

#init_commandString

Generates a command string which will perform any data initialization or configuration required after the verifier software is installed but before the sandbox has been transferred to the instance. If no work is required, then nil will be returned.

Returns:

  • (String)

    a command string



100
101
102
# File 'lib/kitchen/verifier/pester.rb', line 100

def init_command
  restart_winrm_service if config[:restart_winrm]
end

#install_commandString

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.

Returns:

  • (String)

    a command string



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/kitchen/verifier/pester.rb', line 72

def install_command
  return if local_suite_files.empty?

  cmd = "    set-executionpolicy unrestricted -force\n    if (-not (get-module -list pester)) {\n      if (get-module -list PowerShellGet){\n        import-module PowerShellGet -force\n        install-module Pester -force\n      }\n      else {\n        if (-not (get-module -list PsGet)){\n          iex (new-object Net.WebClient).DownloadString('http://bit.ly/GetPsGet')\n        }\n        import-module psget -force\n        Install-Module Pester\n      }\n    }\n  CMD\n  wrap_shell_code(Util.outdent!(cmd))\nend\n"

#local_suite_filesArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an Array of test suite filenames for the related suite currently residing on the local workstation. Any special provisioner-specific directories (such as a Chef roles/ directory) are excluded.

Returns:

  • (Array<String>)

    array of suite files



149
150
151
152
153
154
155
156
157
158
# File 'lib/kitchen/verifier/pester.rb', line 149

def local_suite_files
  base = File.join(test_folder, config[:suite_name])
  top_level_glob = File.join(base, "*")
  folder_glob = File.join(base, "*/**/*")
  top = Dir.glob(top_level_glob)
  nested = Dir.glob(folder_glob)
  (top << nested).flatten!.reject do |f|
    File.directory?(f)
  end
end

#prepare_commandString

Generates a command string which will perform any commands or configuration required just before the main verifier run command but after the sandbox has been transferred to the instance. If no work is required, then nil will be returned.

Returns:

  • (String)

    a command string



110
111
# File 'lib/kitchen/verifier/pester.rb', line 110

def prepare_command
end

#prepare_pester_testsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Copies all test suite files into the suites directory in the sandbox.



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/kitchen/verifier/pester.rb', line 163

def prepare_pester_tests
  base = File.join(test_folder, config[:suite_name])
  info("Preparing to copy files from #{base} to the SUT.")

  local_suite_files.each do |src|
    dest = File.join(sandbox_suites_dir, src.sub("#{base}/", ""))
    debug("Copying #{src} to #{dest}")
    FileUtils.mkdir_p(File.dirname(dest))
    FileUtils.cp(src, dest, preserve: true)
  end
end

#restart_winrm_serviceObject

private



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/kitchen/verifier/pester.rb', line 130

def restart_winrm_service

  cmd = 'schtasks /Create /TN restart_winrm /TR ' /
        '"powershell -command restart-service winrm" ' /
        '/SC ONCE /ST 00:00 '
  wrap_shell_code(Util.outdent!("    \#{cmd}\n    schtasks /RUN /TN restart_winrm\n  CMD\n  ))\nend\n"

#run_commandString

Generates a command string which will invoke the main verifier command on the prepared instance. If no work is required, then nil will be returned.

Returns:

  • (String)

    a command string



118
119
120
121
122
123
124
125
126
# File 'lib/kitchen/verifier/pester.rb', line 118

def run_command
  return if local_suite_files.empty?
  wrap_shell_code(Util.outdent!("    $global:ProgressPreference = 'SilentlyContinue'\n    $TestPath = \"\#{File.join(config[:root_path], 'suites')}\"\n    import-module Pester -force; invoke-pester -path $testpath -enableexit\n  CMD\n  ))\nend\n"

#sandbox_suites_dirString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns path to suites directory under sandbox path.

Returns:

  • (String)

    path to suites directory under sandbox path



177
178
179
# File 'lib/kitchen/verifier/pester.rb', line 177

def sandbox_suites_dir
  File.join(sandbox_path, "suites")
end

#test_folderObject



181
182
183
184
# File 'lib/kitchen/verifier/pester.rb', line 181

def test_folder
  return config[:test_base_path] if config[:test_folder].nil?
  absolute_test_folder
end