kitchen-docker 2.9.0 → 2.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,111 @@
1
+ #
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+
14
+ require 'kitchen'
15
+
16
+ require_relative '../docker/container/linux'
17
+ require_relative '../docker/container/windows'
18
+
19
+ require_relative '../docker/helpers/inspec_helper'
20
+
21
+ require_relative '../../docker/version.rb'
22
+ require_relative '../../train/docker.rb'
23
+
24
+ module Kitchen
25
+ module Transport
26
+ class Docker < Kitchen::Transport::Base
27
+ class DockerFailed < TransportFailed; end
28
+
29
+ kitchen_transport_api_version 1
30
+ plugin_version Kitchen::VERSION
31
+
32
+ default_config :binary, 'docker'
33
+ default_config :env_variables, nil
34
+ default_config :interactive, false
35
+ default_config :privileged, false
36
+ default_config :tls, false
37
+ default_config :tls_cacert, nil
38
+ default_config :tls_cert, nil
39
+ default_config :tls_key, nil
40
+ default_config :tls_verify, false
41
+ default_config :tty, false
42
+ default_config :working_dir, nil
43
+
44
+ default_config :socket do |transport|
45
+ socket = 'unix:///var/run/docker.sock'
46
+ socket = 'npipe:////./pipe/docker_engine' if transport.windows_os?
47
+ ENV['DOCKER_HOST'] || socket
48
+ end
49
+
50
+ default_config :temp_dir do |transport|
51
+ if transport.windows_os?
52
+ '$env:TEMP'
53
+ else
54
+ '/tmp'
55
+ end
56
+ end
57
+
58
+ default_config :username do |transport|
59
+ # Return an empty string to prevent username from being added to Docker
60
+ # command line args for Windows if a username was not specified
61
+ if transport.windows_os?
62
+ nil
63
+ else
64
+ 'kitchen'
65
+ end
66
+ end
67
+
68
+ def connection(state, &block)
69
+ options = config.to_hash.merge(state)
70
+ options[:platform] = instance.platform.name
71
+
72
+ # Set value for DOCKER_HOST environment variable for the docker-api gem
73
+ # This allows Windows systems to use the TCP socket for the InSpec verifier
74
+ # See the lib/docker.rb file here: https://github.com/swipely/docker-api/blob/master/lib/docker.rb
75
+ # default_socket_url is set to a Unix socket and env_url requires an environment variable to be set
76
+ ENV['DOCKER_HOST'] = options[:socket] if !options[:socket].nil? && ENV['DOCKER_HOST'].nil?
77
+
78
+ Kitchen::Transport::Docker::Connection.new(options, &block)
79
+ end
80
+
81
+ class Connection < Kitchen::Transport::Docker::Connection
82
+ # Include the InSpec patches to be able to execute tests on Windows containers
83
+ include Kitchen::Docker::Helpers::InspecHelper
84
+
85
+ def execute(command)
86
+ return if command.nil?
87
+
88
+ debug("[Docker] Executing command: #{command}")
89
+ info("[Docker] Executing command on container")
90
+
91
+ container.execute(command)
92
+ rescue => e
93
+ raise DockerFailed, "Docker failed to execute command on container. Error Details: #{e}"
94
+ end
95
+
96
+ def upload(locals, remote)
97
+ container.upload(locals, remote)
98
+ end
99
+
100
+ def container
101
+ @container ||= if @options[:platform].include?('windows')
102
+ Kitchen::Docker::Container::Windows.new(@options)
103
+ else
104
+ Kitchen::Docker::Container::Linux.new(@options)
105
+ end
106
+ @container
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,125 @@
1
+ #
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+
14
+ # Monkey patched Docker train transport to support running the InSpec verifier on Windows
15
+ begin
16
+ # Requires train gem with a minimum version of 2.1.0
17
+ require 'train'
18
+
19
+ module Train::Transports
20
+ # Patched train transport with Windows support for InSpec verifier
21
+ class Docker < Train.plugin(1)
22
+ name 'docker'
23
+
24
+ include_options Train::Extras::CommandWrapper
25
+ option :host, required: true
26
+
27
+ def connection(state = {}, &block)
28
+ opts = merge_options(options, state || {})
29
+ validate_options(opts)
30
+
31
+ if @connection && @connection_options == opts
32
+ reuse_connection(&block)
33
+ else
34
+ create_new_connection(opts, &block)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ # Creates a new Docker connection instance and save it for potential future
41
+ # reuse.
42
+ #
43
+ # @param options [Hash] connection options
44
+ # @return [Docker::Connection] a Docker connection instance
45
+ # @api private
46
+ def create_new_connection(options, &block)
47
+ if @connection
48
+ logger.debug("[Docker] shutting previous connection #{@connection}")
49
+ @connection.close
50
+ end
51
+
52
+ @connection_options = options
53
+ @connection = Connection.new(options, &block)
54
+ end
55
+
56
+ # Return the last saved Docker connection instance.
57
+ #
58
+ # @return [Docker::Connection] a Docker connection instance
59
+ # @api private
60
+ def reuse_connection
61
+ logger.debug("[Docker] reusing existing connection #{@connection}")
62
+ yield @connection if block_given?
63
+ @connection
64
+ end
65
+ end
66
+ end
67
+
68
+ class Train::Transports::Docker
69
+ class Connection < BaseConnection
70
+ def initialize(conf)
71
+ super(conf)
72
+ @id = options[:host]
73
+ @container = ::Docker::Container.get(@id) ||
74
+ fail("Can't find Docker container #{@id}")
75
+ @cmd_wrapper = nil
76
+ @cmd_wrapper = CommandWrapper.load(self, @options)
77
+ self
78
+ end
79
+
80
+ def uri
81
+ if @container.nil?
82
+ "docker://#{@id}"
83
+ else
84
+ "docker://#{@container.id}"
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def file_via_connection(path)
91
+ if os.aix?
92
+ Train::File::Remote::Aix.new(self, path)
93
+ elsif os.solaris?
94
+ Train::File::Remote::Unix.new(self, path)
95
+ elsif os.windows?
96
+ Train::File::Remote::Windows.new(self, path)
97
+ else
98
+ Train::File::Remote::Linux.new(self, path)
99
+ end
100
+ end
101
+
102
+ def platform_specific_cmd(cmd)
103
+ return cmd if @container.info.nil?
104
+ if @container.info['Platform'] == 'windows'
105
+ return ['cmd.exe', '/c', cmd]
106
+ else
107
+ return ['/bin/sh', '-c', cmd]
108
+ end
109
+ end
110
+
111
+ def run_command_via_connection(cmd, &_data_handler)
112
+ cmd = @cmd_wrapper.run(cmd) unless @cmd_wrapper.nil?
113
+ stdout, stderr, exit_status = @container.exec(platform_specific_cmd(cmd))
114
+ CommandResult.new(stdout.join, stderr.join, exit_status)
115
+ rescue ::Docker::Error::DockerError => _
116
+ raise
117
+ rescue => _
118
+ # @TODO: differentiate any other error
119
+ raise
120
+ end
121
+ end
122
+ end
123
+ rescue LoadError => e
124
+ logger.debug("[Docker] train gem not found for InSpec verifier. #{e}")
125
+ end
@@ -15,7 +15,7 @@
15
15
  #
16
16
 
17
17
  require 'serverspec'
18
- set :backend, :exec
18
+ require 'spec_helper'
19
19
 
20
20
  # Just make sure the image launched and is reachable.
21
21
  describe command('true') do
@@ -0,0 +1,21 @@
1
+ #
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ #
14
+
15
+ case RbConfig::CONFIG['host_os']
16
+ when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
17
+ set :backend, :cmd
18
+ set :os, :family => 'windows'
19
+ else
20
+ set :backend, :exec
21
+ end
@@ -15,6 +15,12 @@
15
15
  #
16
16
 
17
17
  # Just make sure the image launched and is reachable.
18
- describe command('true') do
19
- its(:exit_status) { is_expected.to eq 0 }
18
+ if os[:family] == 'windows'
19
+ describe command('echo 1') do
20
+ its(:exit_status) { is_expected.to eq 0 }
21
+ end
22
+ else
23
+ describe command('true') do
24
+ its(:exit_status) { is_expected.to eq 0 }
25
+ end
20
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kitchen-docker
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.9.0
4
+ version: 2.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Porter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-15 00:00:00.000000000 Z
11
+ date: 2020-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-kitchen
@@ -176,14 +176,34 @@ dependencies:
176
176
  requirements:
177
177
  - - "~>"
178
178
  - !ruby/object:Gem::Version
179
- version: '0.14'
179
+ version: '1.1'
180
180
  type: :development
181
181
  prerelease: false
182
182
  version_requirements: !ruby/object:Gem::Requirement
183
183
  requirements:
184
184
  - - "~>"
185
185
  - !ruby/object:Gem::Version
186
- version: '0.14'
186
+ version: '1.1'
187
+ - !ruby/object:Gem::Dependency
188
+ name: train
189
+ requirement: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '2.1'
194
+ - - "<"
195
+ - !ruby/object:Gem::Version
196
+ version: '4.0'
197
+ type: :development
198
+ prerelease: false
199
+ version_requirements: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: '2.1'
204
+ - - "<"
205
+ - !ruby/object:Gem::Version
206
+ version: '4.0'
187
207
  description: A Docker Driver for Test Kitchen
188
208
  email:
189
209
  - portertech@gmail.com
@@ -193,6 +213,7 @@ extra_rdoc_files: []
193
213
  files:
194
214
  - ".cane"
195
215
  - ".gitignore"
216
+ - ".kitchen.windows.yml"
196
217
  - ".kitchen.yml"
197
218
  - ".tailor"
198
219
  - ".travis.yml"
@@ -201,13 +222,26 @@ files:
201
222
  - LICENSE
202
223
  - README.md
203
224
  - Rakefile
225
+ - docker.ps1
204
226
  - kitchen-docker.gemspec
227
+ - lib/docker/version.rb
228
+ - lib/kitchen/docker/container.rb
229
+ - lib/kitchen/docker/container/linux.rb
230
+ - lib/kitchen/docker/container/windows.rb
231
+ - lib/kitchen/docker/docker_version.rb
232
+ - lib/kitchen/docker/erb_context.rb
233
+ - lib/kitchen/docker/helpers/cli_helper.rb
234
+ - lib/kitchen/docker/helpers/container_helper.rb
235
+ - lib/kitchen/docker/helpers/file_helper.rb
236
+ - lib/kitchen/docker/helpers/image_helper.rb
237
+ - lib/kitchen/docker/helpers/inspec_helper.rb
205
238
  - lib/kitchen/driver/docker.rb
206
- - lib/kitchen/driver/docker/erb.rb
207
- - lib/kitchen/driver/docker_version.rb
239
+ - lib/kitchen/transport/docker.rb
240
+ - lib/train/docker.rb
208
241
  - test/Dockerfile
209
242
  - test/integration/capabilities/serverspec/capabilities_drop_spec.rb
210
243
  - test/integration/default/serverspec/default_spec.rb
244
+ - test/integration/default/serverspec/spec_helper.rb
211
245
  - test/integration/inspec/inspec_spec.rb
212
246
  - test/spec/docker_spec.rb
213
247
  - test/spec/spec_helper.rb
@@ -230,7 +264,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
230
264
  - !ruby/object:Gem::Version
231
265
  version: '0'
232
266
  requirements: []
233
- rubygems_version: 3.0.3
267
+ rubygems_version: 3.1.2
234
268
  signing_key:
235
269
  specification_version: 4
236
270
  summary: A Docker Driver for Test Kitchen
@@ -238,6 +272,7 @@ test_files:
238
272
  - test/Dockerfile
239
273
  - test/integration/capabilities/serverspec/capabilities_drop_spec.rb
240
274
  - test/integration/default/serverspec/default_spec.rb
275
+ - test/integration/default/serverspec/spec_helper.rb
241
276
  - test/integration/inspec/inspec_spec.rb
242
277
  - test/spec/docker_spec.rb
243
278
  - test/spec/spec_helper.rb