kitchen-docker 2.15.0 → 3.0.0

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.
@@ -1,172 +1,176 @@
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 'erb'
15
- require 'json'
16
- require 'shellwords'
17
- require 'tempfile'
18
- require 'uri'
19
-
20
- require 'kitchen'
21
- require 'kitchen/configurable'
22
- require_relative '../erb_context'
23
- require_relative 'cli_helper'
24
-
25
- module Kitchen
26
- module Docker
27
- module Helpers
28
- module ContainerHelper
29
- include Configurable
30
- include Kitchen::Docker::Helpers::CliHelper
31
-
32
- def parse_container_id(output)
33
- container_id = output.chomp
34
-
35
- unless [12, 64].include?(container_id.size)
36
- raise ActionFailed, 'Could not parse Docker run output for container ID'
37
- end
38
-
39
- container_id
40
- end
41
-
42
- def dockerfile_template
43
- template = IO.read(File.expand_path(config[:dockerfile]))
44
- context = Kitchen::Docker::ERBContext.new(config.to_hash)
45
- ERB.new(template).result(context.get_binding)
46
- end
47
-
48
- def remote_socket?
49
- config[:socket] ? socket_uri.scheme == 'tcp' : false
50
- end
51
-
52
- def socket_uri
53
- URI.parse(config[:socket])
54
- end
55
-
56
- def dockerfile_path(file)
57
- config[:build_context] ? Pathname.new(file.path).relative_path_from(Pathname.pwd).to_s : file.path
58
- end
59
-
60
- def container_exists?(state)
61
- state[:container_id] && !!docker_command("top #{state[:container_id]}") rescue false
62
- end
63
-
64
- def container_exec(state, command)
65
- cmd = build_exec_command(state, command)
66
- docker_command(cmd)
67
- rescue => e
68
- raise "Failed to execute command on Docker container. #{e}"
69
- end
70
-
71
- def create_dir_on_container(state, path)
72
- path = replace_env_variables(state, path)
73
- cmd = "mkdir -p #{path}"
74
-
75
- if state[:platform].include?('windows')
76
- psh = "-Command if(-not (Test-Path \'#{path}\')) { New-Item -Path \'#{path}\' -Force }"
77
- cmd = build_powershell_command(psh)
78
- end
79
-
80
- cmd = build_exec_command(state, cmd)
81
- docker_command(cmd)
82
- rescue => e
83
- raise "Failed to create directory #{path} on container. #{e}"
84
- end
85
-
86
- def copy_file_to_container(state, local_file, remote_file)
87
- debug("Copying local file #{local_file} to #{remote_file} on container")
88
-
89
- remote_file = replace_env_variables(state, remote_file)
90
-
91
- remote_file = "#{state[:container_id]}:#{remote_file}"
92
- cmd = build_copy_command(local_file, remote_file)
93
- docker_command(cmd)
94
- rescue => e
95
- raise "Failed to copy file #{local_file} to container. #{e}"
96
- end
97
-
98
- def container_env_variables(state)
99
- # Retrieves all environment variables from inside container
100
- vars = {}
101
-
102
- if state[:platform].include?('windows')
103
- cmd = build_powershell_command('-Command [System.Environment]::GetEnvironmentVariables() ^| ConvertTo-Json')
104
- cmd = build_exec_command(state, cmd)
105
- stdout = docker_command(cmd, suppress_output: !logger.debug?).strip
106
- vars = ::JSON.parse(stdout)
107
- else
108
- cmd = build_exec_command(state, 'printenv')
109
- stdout = docker_command(cmd, suppress_output: !logger.debug?).strip
110
- stdout.split("\n").each { |line| vars[line.split('=')[0]] = line.split('=')[1] }
111
- end
112
-
113
- vars
114
- end
115
-
116
- def replace_env_variables(state, str)
117
- if str.include?('$env:')
118
- key = str[/\$env:(.*?)(\\|$)/, 1]
119
- value = container_env_variables(state)[key].to_s.strip
120
- str = str.gsub("$env:#{key}", value)
121
- elsif str.include?('$')
122
- key = str[/\$(.*?)(\/|$)/, 1]
123
- value = container_env_variables(state)[key].to_s.strip
124
- str = str.gsub("$#{key}", value)
125
- end
126
-
127
- str
128
- end
129
-
130
- def run_container(state, transport_port = nil)
131
- cmd = build_run_command(state[:image_id], transport_port)
132
- output = docker_command(cmd)
133
- parse_container_id(output)
134
- end
135
-
136
- def container_ip_address(state)
137
- cmd = "inspect --format '{{ .NetworkSettings.IPAddress }}'"
138
- cmd << " #{state[:container_id]}"
139
- docker_command(cmd).strip
140
- rescue
141
- raise ActionFailed, 'Error getting internal IP of Docker container'
142
- end
143
-
144
- def remove_container(state)
145
- container_id = state[:container_id]
146
- docker_command("stop -t 0 #{container_id}")
147
- docker_command("rm #{container_id}")
148
- end
149
-
150
- def dockerfile_proxy_config
151
- env_variables = ''
152
- if config[:http_proxy]
153
- env_variables << "ENV http_proxy #{config[:http_proxy]}\n"
154
- env_variables << "ENV HTTP_PROXY #{config[:http_proxy]}\n"
155
- end
156
-
157
- if config[:https_proxy]
158
- env_variables << "ENV https_proxy #{config[:https_proxy]}\n"
159
- env_variables << "ENV HTTPS_PROXY #{config[:https_proxy]}\n"
160
- end
161
-
162
- if config[:no_proxy]
163
- env_variables << "ENV no_proxy #{config[:no_proxy]}\n"
164
- env_variables << "ENV NO_PROXY #{config[:no_proxy]}\n"
165
- end
166
-
167
- env_variables
168
- end
169
- end
170
- end
171
- end
172
- end
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 'erb'
15
+ require 'json'
16
+ require 'shellwords'
17
+ require 'tempfile'
18
+ require 'uri'
19
+
20
+ require 'kitchen'
21
+ require 'kitchen/configurable'
22
+ require_relative '../erb_context'
23
+ require_relative 'cli_helper'
24
+
25
+ module Kitchen
26
+ module Docker
27
+ module Helpers
28
+ # rubocop:disable Metrics/ModuleLength, Style/Documentation
29
+ module ContainerHelper
30
+ include Configurable
31
+ include Kitchen::Docker::Helpers::CliHelper
32
+
33
+ def parse_container_id(output)
34
+ container_id = output.chomp
35
+
36
+ unless [12, 64].include?(container_id.size)
37
+ raise ActionFailed, 'Could not parse Docker run output for container ID'
38
+ end
39
+
40
+ container_id
41
+ end
42
+
43
+ def dockerfile_template
44
+ template = IO.read(File.expand_path(config[:dockerfile]))
45
+ context = Kitchen::Docker::ERBContext.new(config.to_hash)
46
+ ERB.new(template).result(context.get_binding)
47
+ end
48
+
49
+ def remote_socket?
50
+ config[:socket] ? socket_uri.scheme == 'tcp' : false
51
+ end
52
+
53
+ def socket_uri
54
+ URI.parse(config[:socket])
55
+ end
56
+
57
+ def dockerfile_path(file)
58
+ config[:build_context] ? Pathname.new(file.path).relative_path_from(Pathname.pwd).to_s : file.path
59
+ end
60
+
61
+ def container_exists?(state)
62
+ state[:container_id] && !!docker_command("top #{state[:container_id]}") rescue false
63
+ end
64
+
65
+ def container_exec(state, command)
66
+ cmd = build_exec_command(state, command)
67
+ docker_command(cmd)
68
+ rescue => e
69
+ raise "Failed to execute command on Docker container. #{e}"
70
+ end
71
+
72
+ def create_dir_on_container(state, path)
73
+ path = replace_env_variables(state, path)
74
+ cmd = "mkdir -p #{path}"
75
+
76
+ if state[:platform].include?('windows')
77
+ psh = "-Command if(-not (Test-Path \'#{path}\')) { New-Item -Path \'#{path}\' -Force }"
78
+ cmd = build_powershell_command(psh)
79
+ end
80
+
81
+ cmd = build_exec_command(state, cmd)
82
+ docker_command(cmd)
83
+ rescue => e
84
+ raise "Failed to create directory #{path} on container. #{e}"
85
+ end
86
+
87
+ def copy_file_to_container(state, local_file, remote_file)
88
+ debug("Copying local file #{local_file} to #{remote_file} on container")
89
+
90
+ remote_file = replace_env_variables(state, remote_file)
91
+
92
+ remote_file = "#{state[:container_id]}:#{remote_file}"
93
+ cmd = build_copy_command(local_file, remote_file)
94
+ docker_command(cmd)
95
+ rescue => e
96
+ raise "Failed to copy file #{local_file} to container. #{e}"
97
+ end
98
+
99
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
100
+ def container_env_variables(state)
101
+ # Retrieves all environment variables from inside container
102
+ vars = {}
103
+
104
+ if state[:platform].include?('windows')
105
+ cmd = build_powershell_command('-Command [System.Environment]::GetEnvironmentVariables() ^| ConvertTo-Json')
106
+ cmd = build_exec_command(state, cmd)
107
+ stdout = docker_command(cmd, suppress_output: !logger.debug?).strip
108
+ vars = ::JSON.parse(stdout)
109
+ else
110
+ cmd = build_exec_command(state, 'printenv')
111
+ stdout = docker_command(cmd, suppress_output: !logger.debug?).strip
112
+ stdout.split("\n").each { |line| vars[line.split('=')[0]] = line.split('=')[1] }
113
+ end
114
+
115
+ vars
116
+ end
117
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
118
+
119
+ def replace_env_variables(state, str)
120
+ if str.include?('$env:')
121
+ key = str[/\$env:(.*?)(\\|$)/, 1]
122
+ value = container_env_variables(state)[key].to_s.strip
123
+ str = str.gsub("$env:#{key}", value)
124
+ elsif str.include?('$')
125
+ key = str[/\$(.*?)(\/|$)/, 1]
126
+ value = container_env_variables(state)[key].to_s.strip
127
+ str = str.gsub("$#{key}", value)
128
+ end
129
+
130
+ str
131
+ end
132
+
133
+ def run_container(state, transport_port = nil)
134
+ cmd = build_run_command(state[:image_id], transport_port)
135
+ output = docker_command(cmd)
136
+ parse_container_id(output)
137
+ end
138
+
139
+ def container_ip_address(state)
140
+ cmd = "inspect --format '{{ .NetworkSettings.IPAddress }}'"
141
+ cmd << " #{state[:container_id]}"
142
+ docker_command(cmd).strip
143
+ rescue
144
+ raise ActionFailed, 'Error getting internal IP of Docker container'
145
+ end
146
+
147
+ def remove_container(state)
148
+ container_id = state[:container_id]
149
+ docker_command("stop -t 0 #{container_id}")
150
+ docker_command("rm #{container_id}")
151
+ end
152
+
153
+ def dockerfile_proxy_config
154
+ env_variables = ''
155
+ if config[:http_proxy]
156
+ env_variables << "ENV http_proxy #{config[:http_proxy]}\n"
157
+ env_variables << "ENV HTTP_PROXY #{config[:http_proxy]}\n"
158
+ end
159
+
160
+ if config[:https_proxy]
161
+ env_variables << "ENV https_proxy #{config[:https_proxy]}\n"
162
+ env_variables << "ENV HTTPS_PROXY #{config[:https_proxy]}\n"
163
+ end
164
+
165
+ if config[:no_proxy]
166
+ env_variables << "ENV no_proxy #{config[:no_proxy]}\n"
167
+ env_variables << "ENV NO_PROXY #{config[:no_proxy]}\n"
168
+ end
169
+
170
+ env_variables
171
+ end
172
+ end
173
+ # rubocop:enable Metrics/ModuleLength, Style/Documentation
174
+ end
175
+ end
176
+ end
@@ -14,10 +14,11 @@
14
14
  # limitations under the License.
15
15
  #
16
16
 
17
- require 'serverspec'
18
- set :backend, :exec
17
+ # Disable now busser-serever is gone.
18
+ # require 'serverspec'
19
+ # set :backend, :exec
19
20
 
20
- describe command('/sbin/ifconfig eth0 multicast') do
21
- its(:exit_status) { is_expected.to_not eq 0 }
22
- its(:stderr) { is_expected.to match /Operation not permitted/ }
23
- end
21
+ # describe command('/sbin/ifconfig eth0 multicast') do
22
+ # its(:exit_status) { is_expected.to_not eq 0 }
23
+ # its(:stderr) { is_expected.to match /Operation not permitted/ }
24
+ # end
@@ -14,10 +14,11 @@
14
14
  # limitations under the License.
15
15
  #
16
16
 
17
- require 'serverspec'
18
- require 'spec_helper'
17
+ # Disable now busser-serever is gone.
18
+ # require 'serverspec'
19
+ # require 'spec_helper'
19
20
 
20
- # Just make sure the image launched and is reachable.
21
- describe command('true') do
22
- its(:exit_status) { is_expected.to eq 0 }
23
- end
21
+ # # Just make sure the image launched and is reachable.
22
+ # describe command('true') do
23
+ # its(:exit_status) { is_expected.to eq 0 }
24
+ # end
@@ -12,10 +12,10 @@
12
12
  # limitations under the License.
13
13
  #
14
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
+ # 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
@@ -22,7 +22,7 @@ require 'simplecov'
22
22
  # Check for coverage stuffs
23
23
  formatters = []
24
24
 
25
- if ENV['CODECOV_TOKEN'] || ENV['TRAVIS']
25
+ if ENV['CODECOV_TOKEN'] || ENV['CI']
26
26
  require 'codecov'
27
27
  formatters << SimpleCov::Formatter::Codecov
28
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kitchen-docker
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.15.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Porter
@@ -170,6 +170,20 @@ dependencies:
170
170
  - - ">="
171
171
  - !ruby/object:Gem::Version
172
172
  version: 0.0.2
173
+ - !ruby/object:Gem::Dependency
174
+ name: chefstyle
175
+ requirement: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ type: :development
181
+ prerelease: false
182
+ version_requirements: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
173
187
  - !ruby/object:Gem::Dependency
174
188
  name: kitchen-inspec
175
189
  requirement: !ruby/object:Gem::Requirement
@@ -213,11 +227,11 @@ extra_rdoc_files: []
213
227
  files:
214
228
  - ".cane"
215
229
  - ".github/dependabot.yml"
230
+ - ".github/workflows/ci.yml"
216
231
  - ".gitignore"
217
- - ".kitchen.windows.yml"
218
- - ".kitchen.yml"
232
+ - ".rubocop.yml"
219
233
  - ".tailor"
220
- - ".travis.yml"
234
+ - ".yamllint"
221
235
  - CHANGELOG.md
222
236
  - Gemfile
223
237
  - LICENSE
@@ -225,6 +239,8 @@ files:
225
239
  - Rakefile
226
240
  - docker.ps1
227
241
  - kitchen-docker.gemspec
242
+ - kitchen.windows.yml
243
+ - kitchen.yml
228
244
  - lib/docker/version.rb
229
245
  - lib/kitchen/docker/container.rb
230
246
  - lib/kitchen/docker/container/linux.rb
@@ -241,9 +257,9 @@ files:
241
257
  - lib/kitchen/transport/docker.rb
242
258
  - lib/train/docker.rb
243
259
  - test/Dockerfile
244
- - test/integration/capabilities/serverspec/capabilities_drop_spec.rb
245
- - test/integration/default/serverspec/default_spec.rb
246
- - test/integration/default/serverspec/spec_helper.rb
260
+ - test/integration/capabilities/disabled/capabilities_drop_spec.rb
261
+ - test/integration/default/disabled/default_spec.rb
262
+ - test/integration/default/disabled/spec_helper.rb
247
263
  - test/integration/inspec/inspec_spec.rb
248
264
  - test/spec/docker_spec.rb
249
265
  - test/spec/spec_helper.rb
@@ -272,9 +288,9 @@ specification_version: 4
272
288
  summary: A Docker Driver for Test Kitchen
273
289
  test_files:
274
290
  - test/Dockerfile
275
- - test/integration/capabilities/serverspec/capabilities_drop_spec.rb
276
- - test/integration/default/serverspec/default_spec.rb
277
- - test/integration/default/serverspec/spec_helper.rb
291
+ - test/integration/capabilities/disabled/capabilities_drop_spec.rb
292
+ - test/integration/default/disabled/default_spec.rb
293
+ - test/integration/default/disabled/spec_helper.rb
278
294
  - test/integration/inspec/inspec_spec.rb
279
295
  - test/spec/docker_spec.rb
280
296
  - test/spec/spec_helper.rb
data/.kitchen.windows.yml DELETED
@@ -1,33 +0,0 @@
1
- <% # Make sure the local copy of the driver is loaded %>
2
- <% lib = File.expand_path('../lib', __FILE__) %>
3
- <% $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) %>
4
- ---
5
- driver:
6
- name: docker
7
- provision_command:
8
- - powershell -ExecutionPolicy Bypass -NoLogo -Command . { iwr -useb https://omnitruck.chef.io/install.ps1 } ^| iex; install
9
- - powershell -Command $path=$env:Path + ';c:\opscode\chef\embedded\bin'; Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\' -Name Path -Value $path
10
-
11
- transport:
12
- name: docker
13
- socket: tcp://localhost:2375
14
-
15
- provisioner:
16
- name: dummy
17
-
18
- platforms:
19
- - name: windows
20
- driver_config:
21
- image: mcr.microsoft.com/windows/servercore:1809
22
- platform: windows
23
-
24
- suites:
25
- - name: default
26
- - name: context
27
- driver:
28
- build_context: false
29
- - name: inspec
30
- driver:
31
- provision_command: echo 1
32
- verifier:
33
- name: inspec
data/.kitchen.yml DELETED
@@ -1,65 +0,0 @@
1
- <% # Make sure the local copy of the driver is loaded %>
2
- <% lib = File.expand_path('../lib', __FILE__) %>
3
- <% $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) %>
4
- ---
5
- driver:
6
- name: docker
7
- provision_command: curl -L https://www.chef.io/chef/install.sh | bash
8
-
9
- transport:
10
- name: docker
11
-
12
- provisioner:
13
- name: dummy
14
-
15
- platforms:
16
- - name: amazonlinux-2
17
- - name: ubuntu-18.04
18
- - name: ubuntu-20.04
19
- - name: fedora-latest
20
- driver:
21
- provision_command:
22
- - yum install libxcrypt-compat -y
23
- - curl -L https://www.chef.io/chef/install.sh | bash
24
- - name: centos-7
25
- - name: oraclelinux-7
26
- - name: rockylinux-8
27
- - name: debian-9
28
- - name: debian-10
29
- - name: opensuse-15
30
- driver:
31
- image: opensuse/leap:15
32
- - name: dockerfile
33
- driver:
34
- username: dockerfile
35
- password: dockerfile
36
- dockerfile: test/Dockerfile
37
- run_command: /sbin/init
38
-
39
- suites:
40
- - name: default
41
- excludes: [arch, debian-9]
42
- - name: context
43
- excludes: [arch, debian-9]
44
- driver:
45
- build_context: false
46
- - name: capabilities
47
- includes: [debian-10,ubuntu-18.04,ubuntu-20.04]
48
- driver:
49
- provision_command:
50
- - curl -L https://www.chef.io/chef/install.sh | bash
51
- - apt-get install -y net-tools
52
- cap_drop:
53
- - NET_ADMIN
54
- - name: arm64
55
- excludes: [debian-9]
56
- driver:
57
- docker_platform: linux/arm64
58
- - name: amd64
59
- driver:
60
- docker_platform: linux/amd64
61
- - name: inspec
62
- driver:
63
- provision_command: true
64
- verifier:
65
- name: inspec
data/.travis.yml DELETED
@@ -1,57 +0,0 @@
1
- matrix:
2
- include:
3
- - os: linux
4
- rvm: 2.4.9
5
- dist: xenial
6
- language: ruby
7
- cache: bundler
8
- script:
9
- - bundle exec docker version
10
- - bundle exec kitchen --version
11
- - bundle exec rake spec
12
- - bundle exec kitchen test -d always
13
- - os: linux
14
- rvm: 2.5.7
15
- dist: xenial
16
- language: ruby
17
- cache: bundler
18
- script:
19
- - bundle exec docker version
20
- - bundle exec kitchen --version
21
- - bundle exec rake spec
22
- - bundle exec kitchen test -d always
23
- - os: linux
24
- rvm: 2.6.5
25
- dist: xenial
26
- language: ruby
27
- cache: bundler
28
- script:
29
- - bundle exec docker version
30
- - bundle exec kitchen --version
31
- - bundle exec rake spec
32
- - bundle exec kitchen test -d always
33
- - os: windows
34
- language: bash
35
- install:
36
- - choco uninstall ruby
37
- - choco install ruby --version=2.6.5.1
38
- - export PATH=$(echo "$PATH" | sed -e 's/:\/c\/tools\/ruby27\/bin//')
39
- - export PATH=$PATH:/c/tools/ruby26/bin
40
- - choco install mingw
41
- - choco install msys2
42
- - ridk.cmd exec pacman -S --noconfirm --needed base-devel mingw-w64-x86_64-toolchain mingw-w64-x86_64-libxslt
43
- script:
44
- - if [[ $(tasklist | grep "gpg-agent") ]]; then taskkill -IM "gpg-agent.exe" -F; else echo "Process gpg-agent not found. Skipping."; fi
45
- - powershell -ExecutionPolicy Bypass -NoLogo -File docker.ps1
46
- - export KITCHEN_YAML=.kitchen.windows.yml
47
- - ruby -v
48
- - gem install bundler
49
- - bundle config build.nokogiri --use-system-libraries
50
- - bundle install
51
- - bundle exec docker version
52
- - bundle exec kitchen --version
53
- - bundle exec rake spec
54
- - bundle exec kitchen test -d always
55
-
56
- services:
57
- - docker