kitchen-pester 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8604ba8a6d0f832c3eeeea3d763051ed16991eb5bac45501b6dc8e734b99c85b
4
- data.tar.gz: ee0b7ff38dfce855537e30510bca1f974db227bd0549716f07c6b82ce2574a5b
3
+ metadata.gz: e16e5c70ac65acec4f9178b9a790eb0679c0bc998eb6a0a140e8ab8c1fd9e40b
4
+ data.tar.gz: 7b6797461f5f7344b8055e733d66cd7b0b425f13a63c463342a7821412fa9990
5
5
  SHA512:
6
- metadata.gz: 58e9f72997c8c22a5a66a93b53c522951d738de6a85e056386586b08be4fb235333017be25e421c37a3150b07e651ff686605535b7b5e0a7e7048674dadfcd57
7
- data.tar.gz: '08a0a52e8df83182bd7376ff4412080a805ba7d0969be706d97f81f2df2d2e9024482c38a9a7d86a623e885f41dc860d8f1c6af885d1f3c39aa762df73f6d068'
6
+ metadata.gz: 7f826e3d30c87bfc1727c8812d5780f77318d20faeab508dbe9f18a1e98ba425151f59d73a8638e389d0a9231ac4575a6cec33ab61c5abb6c047131c4e548efa
7
+ data.tar.gz: f6c8c78499282f19c86b8abab1537650c6d560b7fc1a9bbad47a8768a41763b4d7d51082525f0e75c0e7f452c5bfd5f7ffdbab5c5815a4f675e98b5874f7719a
@@ -12,9 +12,9 @@ Gem::Specification.new do |spec|
12
12
  spec.summary = "Test-Kitchen verifier for Pester."
13
13
  spec.description = "Skip all that Busser stuff and jump right into Pester."
14
14
  spec.homepage = "https://github.com/test-kitchen/kitchen-pester"
15
- spec.license = "Apache 2"
15
+ spec.license = "Apache-2.0"
16
16
 
17
- spec.files = %w{LICENSE kitchen-pester.gemspec Gemfile Rakefile lib/support/powershell/PesterUtil/PesterUtil.psm1}
17
+ spec.files = %w(LICENSE kitchen-pester.gemspec Gemfile Rakefile) + Dir.glob('lib/**/*')
18
18
  spec.require_paths = ["lib"]
19
19
 
20
20
  spec.add_development_dependency "bundler"
@@ -0,0 +1,335 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Author:: Steven Murawski (<steven.murawski@gmail.com>)
4
+ #
5
+ # Copyright (C) 2015, Steven Murawski
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require "pathname"
20
+ require "kitchen/verifier/base"
21
+ require "kitchen/verifier/pester_version"
22
+
23
+ module Kitchen
24
+
25
+ module Verifier
26
+
27
+ class Pester < Kitchen::Verifier::Base
28
+
29
+ kitchen_verifier_api_version 1
30
+
31
+ plugin_version Kitchen::Verifier::PESTER_VERSION
32
+
33
+ default_config :restart_winrm, false
34
+ default_config :test_folder
35
+ default_config :use_local_pester_module, false
36
+
37
+ # Creates a new Verifier object using the provided configuration data
38
+ # which will be merged with any default configuration.
39
+ #
40
+ # @param config [Hash] provided verifier configuration
41
+ def initialize(config = {})
42
+ init_config(config)
43
+ end
44
+
45
+ # Creates a temporary directory on the local workstation into which
46
+ # verifier related files and directories can be copied or created. The
47
+ # contents of this directory will be copied over to the instance before
48
+ # invoking the verifier's run command. After this method completes, it
49
+ # is expected that the contents of the sandbox is complete and ready for
50
+ # copy to the remote instance.
51
+ #
52
+ # **Note:** any subclasses would be well advised to call super first when
53
+ # overriding this method, for example:
54
+ #
55
+ # @example overriding `#create_sandbox`
56
+ #
57
+ # class MyVerifier < Kitchen::Verifier::Base
58
+ # def create_sandbox
59
+ # super
60
+ # # any further file copies, preparations, etc.
61
+ # end
62
+ # end
63
+ def create_sandbox
64
+ super
65
+ prepare_powershell_modules
66
+ prepare_pester_tests
67
+ prepare_helpers
68
+ end
69
+
70
+ # Generates a command string which will install and configure the
71
+ # verifier software on an instance. If no work is required, then `nil`
72
+ # will be returned.
73
+ #
74
+ # @return [String] a command string
75
+ def install_command
76
+ return if local_suite_files.empty?
77
+ return if config[:use_local_pester_module]
78
+
79
+ really_wrap_shell_code(install_command_script)
80
+ end
81
+
82
+ # Generates a command string which will perform any data initialization
83
+ # or configuration required after the verifier software is installed
84
+ # but before the sandbox has been transferred to the instance. If no work
85
+ # is required, then `nil` will be returned.
86
+ #
87
+ # @return [String] a command string
88
+ def init_command
89
+ restart_winrm_service if config[:restart_winrm]
90
+ end
91
+
92
+ # Generates a command string which will perform any commands or
93
+ # configuration required just before the main verifier run command but
94
+ # after the sandbox has been transferred to the instance. If no work is
95
+ # required, then `nil` will be returned.
96
+ #
97
+ # @return [String] a command string
98
+ def prepare_command; end
99
+
100
+ # Generates a command string which will invoke the main verifier
101
+ # command on the prepared instance. If no work is required, then `nil`
102
+ # will be returned.
103
+ #
104
+ # @return [String] a command string
105
+ def run_command
106
+ return if local_suite_files.empty?
107
+
108
+ really_wrap_shell_code(run_command_script)
109
+ end
110
+
111
+ # private
112
+ def run_command_script
113
+ <<-CMD
114
+ $TestPath = "#{config[:root_path]}";
115
+ import-module Pester -force;
116
+ $result = invoke-pester -path $testpath -passthru ;
117
+ $result |
118
+ export-clixml (join-path $testpath 'result.xml');
119
+ $host.setshouldexit($result.failedcount)
120
+ CMD
121
+ end
122
+
123
+ def really_wrap_shell_code(code)
124
+ wrap_shell_code(Util.outdent!(use_local_powershell_modules(code)))
125
+ end
126
+
127
+ def use_local_powershell_modules(script)
128
+ <<-EOH
129
+ set-executionpolicy unrestricted -force;
130
+ $global:ProgressPreference = 'SilentlyContinue'
131
+ $env:psmodulepath += ";$(join-path (resolve-path $env:temp).path 'verifier/modules')";
132
+ #{script}
133
+ EOH
134
+ end
135
+
136
+ def install_command_script
137
+ <<-EOH
138
+ [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
139
+ function directory($path){
140
+ if (test-path $path) {(resolve-path $path).providerpath}
141
+ else {(resolve-path (mkdir $path)).providerpath}
142
+ }
143
+ $VerifierModulePath = directory $env:temp/verifier/modules
144
+ $VerifierTestsPath = directory $env:temp/verifier/pester
145
+
146
+ $env:psmodulepath += ";$VerifierModulePath"
147
+ function test-module($module){
148
+ (get-module $module -list) -ne $null
149
+ }
150
+ if (-not (test-module pester)) {
151
+ if (test-module PowerShellGet){
152
+ import-module PowerShellGet -force
153
+ import-module PackageManagement -force
154
+ get-packageprovider -name NuGet -force | out-null
155
+ install-module Pester -force
156
+ }
157
+ else {
158
+ if (-not (test-module PsGet)){
159
+ $wc = New-Object -TypeName Net.WebClient
160
+
161
+ if($env:http_Proxy){
162
+ if($env:no_proxy){
163
+ Write-Output "Creating WebProxy with 'http_proxy' and 'no_proxy' environment variables.
164
+ $webproxy = New-Object System.Net.WebProxy($env:http_Proxy,$true,$env:no_proxy)
165
+ }else{
166
+ Write-Output "Creating WebProxy with 'http_proxy' environment variable.
167
+ $webproxy = New-Object -TypeName System.Net.WebProxy -ArgumentList ($env:http_Proxy)
168
+ }
169
+
170
+ $wc.proxy = $webproxy
171
+ }
172
+
173
+ Invoke-Expression -Command $wc.DownloadString('http://bit.ly/GetPsGet')
174
+ }
175
+ try {
176
+ import-module psget -force -erroraction stop
177
+ Install-Module Pester
178
+ }
179
+ catch {
180
+ Write-Output "Installing from Github"
181
+ $zipfile = join-path(resolve-path "$env:temp/verifier") "pester.zip"
182
+ if (-not (test-path $zipfile)){
183
+ $source = 'https://github.com/pester/Pester/archive/3.3.14.zip'
184
+ $wc = New-Object -TypeName Net.WebClient
185
+
186
+ if($env:http_Proxy){
187
+ if($env:no_proxy){
188
+ Write-Output "Creating WebProxy with 'http_proxy' and 'no_proxy' environment variables."
189
+ $webproxy = New-Object System.Net.WebProxy($env:http_Proxy,$true,$env:no_proxy)
190
+ }else{
191
+ Write-Output "Creating WebProxy with 'http_proxy' environment variable."
192
+ $webproxy = New-Object -TypeName System.Net.WebProxy -ArgumentList ($env:http_Proxy)
193
+ }
194
+
195
+ $wc.proxy = $webproxy
196
+ }
197
+
198
+ [byte[]]$bytes = $wc.DownloadData($source)
199
+ [IO.File]::WriteAllBytes($zipfile, $bytes)
200
+ $bytes = $null
201
+ [gc]::collect()
202
+ write-output "Downloaded Pester.zip"
203
+ }
204
+ write-output "Creating Shell.Application COM object"
205
+ $shellcom = new-object -com shell.application
206
+ Write-Output "Creating COM object for zip file."
207
+ $zipcomobject = $shellcom.namespace($zipfile)
208
+ Write-Output "Creating COM object for module destination."
209
+ $destination = $shellcom.namespace($VerifierModulePath)
210
+ Write-Output "Unpacking zip file."
211
+ $destination.CopyHere($zipcomobject.Items(), 0x610)
212
+ rename-item (join-path $VerifierModulePath "Pester-3.3.14") -newname 'Pester' -force
213
+ }
214
+ }
215
+ }
216
+ if (-not (test-module Pester)) {
217
+ throw "Unable to install Pester. Please include Pester in your base image or install during your converge."
218
+ }
219
+ EOH
220
+ end
221
+
222
+ def restart_winrm_service
223
+ cmd = "schtasks /Create /TN restart_winrm /TR " \
224
+ '"powershell -command restart-service winrm" ' \
225
+ "/SC ONCE /ST 00:00 "
226
+ wrap_shell_code(Util.outdent!(<<-CMD
227
+ #{cmd}
228
+ schtasks /RUN /TN restart_winrm
229
+ CMD
230
+ ))
231
+ end
232
+
233
+ # Returns an Array of test suite filenames for the related suite currently
234
+ # residing on the local workstation. Any special provisioner-specific
235
+ # directories (such as a Chef roles/ directory) are excluded.
236
+ #
237
+ # @return [Array<String>] array of suite files
238
+ # @api private
239
+
240
+ def suite_test_folder
241
+ @suite_test_folder ||= File.join(test_folder, config[:suite_name])
242
+ end
243
+
244
+ def suite_level_glob
245
+ Dir.glob(File.join(suite_test_folder, "*"))
246
+ end
247
+
248
+ def suite_verifier_level_glob
249
+ Dir.glob(File.join(suite_test_folder, "*/**/*"))
250
+ end
251
+
252
+ def local_suite_files
253
+ suite = suite_level_glob
254
+ suite_verifier = suite_verifier_level_glob
255
+ (suite << suite_verifier).flatten!.reject do |f|
256
+ File.directory?(f)
257
+ end
258
+ end
259
+
260
+ def sandboxify_path(path)
261
+ File.join(sandbox_path, path.sub(%r{#{suite_test_folder}/}i, ""))
262
+ end
263
+
264
+ # Returns an Array of common helper filenames currently residing on the
265
+ # local workstation.
266
+ #
267
+ # @return [Array<String>] array of helper files
268
+ # @api private
269
+ def helper_files
270
+ glob = Dir.glob(File.join(test_folder, "helpers", "*/**/*"))
271
+ glob.reject { |f| File.directory?(f) }
272
+ end
273
+
274
+ # Copies all common testing helper files into the suites directory in
275
+ # the sandbox.
276
+ #
277
+ # @api private
278
+ def prepare_helpers
279
+ base = File.join(test_folder, "helpers")
280
+
281
+ helper_files.each do |src|
282
+ dest = File.join(sandbox_path, src.sub("#{base}/", ""))
283
+ debug("Copying #{src} to #{dest}")
284
+ FileUtils.mkdir_p(File.dirname(dest))
285
+ FileUtils.cp(src, dest, preserve: true)
286
+ end
287
+ end
288
+
289
+ # Copies all test suite files into the suites directory in the sandbox.
290
+ #
291
+ # @api private
292
+ def prepare_pester_tests
293
+ info("Preparing to copy files from #{suite_test_folder} to the SUT.")
294
+
295
+ local_suite_files.each do |src|
296
+ dest = sandboxify_path(src)
297
+ debug("Copying #{src} to #{dest}")
298
+ FileUtils.mkdir_p(File.dirname(dest))
299
+ FileUtils.cp(src, dest, preserve: true)
300
+ end
301
+ end
302
+
303
+ def prepare_powershell_module(name)
304
+ FileUtils.mkdir_p(File.join(sandbox_path, "modules/#{name}"))
305
+ FileUtils.cp(
306
+ File.join(File.dirname(__FILE__), "../../support/powershell/#{name}/#{name}.psm1"),
307
+ File.join(sandbox_path, "modules/#{name}/#{name}.psm1"),
308
+ preserve: true
309
+ )
310
+ end
311
+
312
+ def prepare_powershell_modules
313
+ info("Preparing to copy supporting powershell modules.")
314
+ %w{PesterUtil}.each do |module_name|
315
+ prepare_powershell_module module_name
316
+ end
317
+ end
318
+
319
+ def test_folder
320
+ return config[:test_base_path] if config[:test_folder].nil?
321
+
322
+ absolute_test_folder
323
+ end
324
+
325
+ def absolute_test_folder
326
+ path = (Pathname.new config[:test_folder]).realpath
327
+ integration_path = File.join(path, "integration")
328
+ return path unless Dir.exist?(integration_path)
329
+
330
+ integration_path
331
+ end
332
+
333
+ end
334
+ end
335
+ end
@@ -0,0 +1,5 @@
1
+ module Kitchen
2
+ module Verifier
3
+ PESTER_VERSION = "0.9.1".freeze
4
+ end
5
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kitchen-pester
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Murawski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-16 00:00:00.000000000 Z
11
+ date: 2019-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -103,10 +103,12 @@ files:
103
103
  - LICENSE
104
104
  - Rakefile
105
105
  - kitchen-pester.gemspec
106
+ - lib/kitchen/verifier/pester.rb
107
+ - lib/kitchen/verifier/pester_version.rb
106
108
  - lib/support/powershell/PesterUtil/PesterUtil.psm1
107
109
  homepage: https://github.com/test-kitchen/kitchen-pester
108
110
  licenses:
109
- - Apache 2
111
+ - Apache-2.0
110
112
  metadata: {}
111
113
  post_install_message:
112
114
  rdoc_options: []