dockerspec 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +34 -0
- data/LICENSE +1 -1
- data/README.md +190 -24
- data/Rakefile +60 -6
- data/TODO.md +3 -2
- data/lib/dockerspec.rb +1 -2
- data/lib/dockerspec/builder.rb +13 -6
- data/lib/dockerspec/builder/config_helpers.rb +8 -3
- data/lib/dockerspec/builder/logger/ci.rb +2 -2
- data/lib/dockerspec/builder/matchers.rb +32 -51
- data/lib/dockerspec/configuration.rb +188 -0
- data/lib/dockerspec/docker_exception_parser.rb +2 -2
- data/lib/dockerspec/engine/base.rb +156 -0
- data/lib/dockerspec/engine/infrataster.rb +87 -0
- data/lib/dockerspec/engine/specinfra.rb +130 -0
- data/lib/dockerspec/engine/specinfra/backend.rb +163 -0
- data/lib/dockerspec/{serverspec/specinfra_hack.rb → engine/specinfra/backend_hack.rb} +17 -3
- data/lib/dockerspec/engine_list.rb +150 -0
- data/lib/dockerspec/exceptions.rb +13 -0
- data/lib/dockerspec/helper/multiple_sources_description.rb +3 -3
- data/lib/dockerspec/helper/rspec_example_helpers.rb +86 -6
- data/lib/dockerspec/infrataster.rb +26 -0
- data/lib/dockerspec/rspec.rb +21 -0
- data/lib/dockerspec/{rspec_configuration.rb → rspec/configuration.rb} +1 -1
- data/lib/dockerspec/rspec/resources.rb +602 -0
- data/lib/dockerspec/rspec/resources/its_container.rb +110 -0
- data/lib/dockerspec/{rspec_settings.rb → rspec/settings.rb} +5 -1
- data/lib/dockerspec/runner.rb +2 -357
- data/lib/dockerspec/runner/base.rb +367 -0
- data/lib/dockerspec/runner/compose.rb +322 -0
- data/lib/dockerspec/runner/docker.rb +302 -0
- data/lib/dockerspec/runner/serverspec.rb +21 -0
- data/lib/dockerspec/runner/serverspec/base.rb +185 -0
- data/lib/dockerspec/runner/serverspec/compose.rb +106 -0
- data/lib/dockerspec/runner/serverspec/docker.rb +116 -0
- data/lib/dockerspec/runner/serverspec/rspec.rb +20 -0
- data/lib/dockerspec/{serverspec/rspec_settings.rb → runner/serverspec/rspec/settings.rb} +1 -1
- data/lib/dockerspec/serverspec.rb +12 -2
- data/lib/dockerspec/version.rb +1 -1
- data/spec/spec_helper.rb +6 -2
- metadata +84 -15
- data/lib/dockerspec/rspec_assertions.rb +0 -54
- data/lib/dockerspec/rspec_resources.rb +0 -203
- data/lib/dockerspec/serverspec/rspec_resources.rb +0 -179
- data/lib/dockerspec/serverspec/runner.rb +0 -305
- data/lib/dockerspec/serverspec/specinfra_backend.rb +0 -128
@@ -1,54 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
#
|
3
|
-
# Author:: Xabier de Zuazo (<xabier@zuazo.org>)
|
4
|
-
# Copyright:: Copyright (c) 2015 Xabier de Zuazo
|
5
|
-
# License:: Apache License, Version 2.0
|
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
|
-
|
20
|
-
require 'dockerspec/exceptions'
|
21
|
-
|
22
|
-
module Dockerspec
|
23
|
-
#
|
24
|
-
# Some helper methods to check {Dockerspec::RSpecResources#docker_run}`
|
25
|
-
# arguments and output understandable error messages.
|
26
|
-
#
|
27
|
-
module RSpecAssertions
|
28
|
-
#
|
29
|
-
# A message with description on how to avoid the error in
|
30
|
-
# {Dockerspec::Serverspec::Runner#docker_run}
|
31
|
-
#
|
32
|
-
DOCKER_RUN_ALWAYS_MESSAGE = <<-EOE
|
33
|
-
|
34
|
-
Remember to include the Serverspec library:
|
35
|
-
|
36
|
-
require 'dockerspec'
|
37
|
-
require 'dockerspec/serverspec'
|
38
|
-
|
39
|
-
EOE
|
40
|
-
|
41
|
-
#
|
42
|
-
# Raises and exception with instructions on how to fix it.
|
43
|
-
#
|
44
|
-
# @raise [Dockerspec::DockerRunArgumentError] Raises this exception always.
|
45
|
-
#
|
46
|
-
# @return void
|
47
|
-
#
|
48
|
-
# @api private
|
49
|
-
#
|
50
|
-
def self.assert_docker_run!(_opts)
|
51
|
-
fail DockerRunArgumentError, DOCKER_RUN_ALWAYS_MESSAGE
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
@@ -1,203 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
#
|
3
|
-
# Author:: Xabier de Zuazo (<xabier@zuazo.org>)
|
4
|
-
# Copyright:: Copyright (c) 2015 Xabier de Zuazo
|
5
|
-
# License:: Apache License, Version 2.0
|
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
|
-
|
20
|
-
require 'dockerspec/builder'
|
21
|
-
require 'dockerspec/runner' # Not really necessary (currently unused)
|
22
|
-
require 'dockerspec/rspec_settings'
|
23
|
-
require 'dockerspec/rspec_assertions'
|
24
|
-
|
25
|
-
module Dockerspec
|
26
|
-
#
|
27
|
-
# Some resources included inside {RSpec::Core::ExampleGroup} to build and run
|
28
|
-
# Docker containers.
|
29
|
-
#
|
30
|
-
# ## RSpec Settings
|
31
|
-
#
|
32
|
-
# * `dockerfile_path`: The dockerfile path.
|
33
|
-
# * `rm_build`: Whether to remove the build after the run.
|
34
|
-
# * `log_level`: Log level to use by default.
|
35
|
-
#
|
36
|
-
# All the RSpec settings are optional.
|
37
|
-
#
|
38
|
-
# @example RSpec Settings
|
39
|
-
# RSpec.configure do |config|
|
40
|
-
# config.log_level = :silent
|
41
|
-
# end
|
42
|
-
#
|
43
|
-
module RSpecResources
|
44
|
-
#
|
45
|
-
# Builds a Docker image.
|
46
|
-
#
|
47
|
-
# The image can be build from a path or from a string.
|
48
|
-
#
|
49
|
-
# See the {Dockerspec::Builder::ConfigHelpers} documentation for more
|
50
|
-
# information about the available RSpec resource helpers.
|
51
|
-
#
|
52
|
-
# @example A Simple Example
|
53
|
-
# describe 'My Dockerfile' do
|
54
|
-
# describe docker_build('.') do
|
55
|
-
# it { should have_maintainer /John Doe/ }
|
56
|
-
# it { should have_cmd ['/bin/dash'] }
|
57
|
-
# it { should have_expose '80' }
|
58
|
-
# end
|
59
|
-
# end
|
60
|
-
#
|
61
|
-
# @example A Complete Example
|
62
|
-
# describe docker_build(path: '.') do
|
63
|
-
# it { should have_maintainer 'John Doe "john.doe@example.com"' }
|
64
|
-
# it { should have_maintainer(/John Doe/) }
|
65
|
-
# it { should have_cmd %w(2 2000) }
|
66
|
-
# it { should have_label 'description' }
|
67
|
-
# it { should have_label 'description' => 'My Container' }
|
68
|
-
# it { should have_expose '80' }
|
69
|
-
# it { should have_expose(/80$/) }
|
70
|
-
# it { should have_env 'container' }
|
71
|
-
# it { should have_env 'container' => 'docker' }
|
72
|
-
# it { should have_env 'CRACKER' => 'RANDOM;PATH=/tmp/bin:/sbin:/bin' }
|
73
|
-
# it { should have_entrypoint ['sleep'] }
|
74
|
-
# it { should have_volume '/volume1' }
|
75
|
-
# it { should have_volume %r{/vol.*2} }
|
76
|
-
# it { should have_user 'nobody' }
|
77
|
-
# it { should have_workdir '/opt' }
|
78
|
-
# it { should have_workdir %r{^/op} }
|
79
|
-
# it { should have_onbuild 'RUN echo onbuild' }
|
80
|
-
# it { should have_stopsignal 'SIGTERM' }
|
81
|
-
# end
|
82
|
-
#
|
83
|
-
# @example Checking the Attribute Values Using the `its` Method
|
84
|
-
# describe docker_build(path: '.') do
|
85
|
-
# its(:maintainer) { should eq 'John Doe "john.doe@example.com"' }
|
86
|
-
# its(:cmd) { should eq %w(2 2000) }
|
87
|
-
# its(:labels) { should include 'description' }
|
88
|
-
# its(:labels) { should include 'description' => 'My Container' }
|
89
|
-
# its(:exposes) { should include '80' }
|
90
|
-
# its(:env) { should include 'container' }
|
91
|
-
# its(:env) { should include 'container' => 'docker' }
|
92
|
-
# its(:entrypoint) { should eq ['sleep'] }
|
93
|
-
# its(:volumes) { should include '/volume1' }
|
94
|
-
# its(:user) { should eq 'nobody' }
|
95
|
-
# its(:workdir) { should eq '/opt' }
|
96
|
-
# its(:onbuilds) { should include 'RUN echo onbuild' }
|
97
|
-
# its(:stopsignal) { should eq 'SIGTERM' }
|
98
|
-
# end
|
99
|
-
#
|
100
|
-
# @example Checking Its Size and OS
|
101
|
-
# describe docker_build(path: '.') do
|
102
|
-
# its(:size) { should be < 20 * 2**20 } # 20M
|
103
|
-
# its(:arch) { should eq 'amd64' }
|
104
|
-
# its(:os) { should eq 'linux' }
|
105
|
-
# end
|
106
|
-
#
|
107
|
-
# @example Building from a File
|
108
|
-
# describe docker_build(path: '../dockerfiles/Dockerfile-nginx') do
|
109
|
-
# # [...]
|
110
|
-
# end
|
111
|
-
#
|
112
|
-
# @example Building from a Template
|
113
|
-
# describe docker_build(template: 'Dockerfile1.erb') do
|
114
|
-
# # [...]
|
115
|
-
# end
|
116
|
-
#
|
117
|
-
# @example Building from a Template with a Context
|
118
|
-
# describe docker_build(
|
119
|
-
# template: 'Dockerfile1.erb', context: {version: '8'}
|
120
|
-
# ) do
|
121
|
-
# it { should have_maintainer(/John Doe/) }
|
122
|
-
# it { should have_cmd %w(/bin/sh) }
|
123
|
-
# # [...]
|
124
|
-
# end
|
125
|
-
#
|
126
|
-
# @example Building from a String
|
127
|
-
# describe docker_build(string: "FROM nginx:1.9\n [...]") do
|
128
|
-
# # [...]
|
129
|
-
# end
|
130
|
-
#
|
131
|
-
# @example Building from a Docker Image ID
|
132
|
-
# describe docker_build(id: '07d362aea98d') do
|
133
|
-
# # [...]
|
134
|
-
# end
|
135
|
-
#
|
136
|
-
# @example Building from a Docker Image Name
|
137
|
-
# describe docker_build(id: 'nginx:1.9') do
|
138
|
-
# # [...]
|
139
|
-
# end
|
140
|
-
#
|
141
|
-
# @param opts [String, Hash] The `:path` or a list of options.
|
142
|
-
#
|
143
|
-
# @option opts [String] :path ('.') The directory or file that contains the
|
144
|
-
# *Dockerfile*. By default tries to read it from the `DOCKERFILE_PATH`
|
145
|
-
# environment variable and uses `'.'` if it is not set.
|
146
|
-
# @option opts [String] :string Use this string as *Dockerfile* instead of
|
147
|
-
# `:path`. Not set by default.
|
148
|
-
# @option opts [String] :template Use this [Erubis]
|
149
|
-
# (http://www.kuwata-lab.com/erubis/users-guide.html) template file as
|
150
|
-
# *Dockerfile*.
|
151
|
-
# @option opts [String] :id Use this Docker image ID instead of a
|
152
|
-
# *Dockerfile*.
|
153
|
-
# @option opts [Boolean] :rm Whether to remove the generated docker images
|
154
|
-
# after running the tests. By default only removes them if it is running
|
155
|
-
# on a CI machine.
|
156
|
-
# @option opts [Hash, Erubis::Context] :context ({}) Template *context*
|
157
|
-
# used when the `:template` source is used.
|
158
|
-
# @option opts [String] :tag Repository tag to be applied to the resulting
|
159
|
-
# image.
|
160
|
-
# @option opts [Fixnum, Symbol] :log_level Sets the docker library
|
161
|
-
# verbosity level. Possible values:
|
162
|
-
# `:silent` or `0` (no output),
|
163
|
-
# `:ci` or `1` (enables some outputs recommended for CI environments),
|
164
|
-
# `:info` or `2` (gives information about main build steps),
|
165
|
-
# `:debug` or `3` (outputs all the provided information in its raw
|
166
|
-
# original form).
|
167
|
-
#
|
168
|
-
# @return [Dockerspec::Builder] Builder object.
|
169
|
-
#
|
170
|
-
# @see Dockerspec::Builder::ConfigHelpers
|
171
|
-
#
|
172
|
-
# @api public
|
173
|
-
#
|
174
|
-
def docker_build(*opts)
|
175
|
-
builder = Dockerspec::Builder.new(*opts)
|
176
|
-
builder.build
|
177
|
-
end
|
178
|
-
|
179
|
-
#
|
180
|
-
# Runs a docker image.
|
181
|
-
#
|
182
|
-
# @param opts [Hash] List of options.
|
183
|
-
#
|
184
|
-
# @see Dockerspec::Serverspec::RSpecResources#docker_run
|
185
|
-
#
|
186
|
-
def docker_run(*opts)
|
187
|
-
RSpecAssertions.assert_docker_run!(opts)
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
#
|
193
|
-
# Add the Dockerspec resources to RSpec core.
|
194
|
-
#
|
195
|
-
RSpec::Core::ExampleGroup.class_eval do
|
196
|
-
extend Dockerspec::RSpecResources
|
197
|
-
include Dockerspec::RSpecResources
|
198
|
-
end
|
199
|
-
|
200
|
-
#
|
201
|
-
# Allow using #docker_build in the outermost example
|
202
|
-
#
|
203
|
-
extend Dockerspec::RSpecResources
|
@@ -1,179 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
#
|
3
|
-
# Author:: Xabier de Zuazo (<xabier@zuazo.org>)
|
4
|
-
# Copyright:: Copyright (c) 2015 Xabier de Zuazo
|
5
|
-
# License:: Apache License, Version 2.0
|
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
|
-
|
20
|
-
require 'dockerspec/serverspec/runner'
|
21
|
-
require 'dockerspec/serverspec/rspec_settings'
|
22
|
-
|
23
|
-
module Dockerspec
|
24
|
-
module Serverspec
|
25
|
-
#
|
26
|
-
# Some resources included inside {RSpec::Core::ExampleGroup} to build and
|
27
|
-
# run Docker containers with Serverspec.
|
28
|
-
#
|
29
|
-
# ## RSpec Settings
|
30
|
-
#
|
31
|
-
# * `family`: The OS family to use
|
32
|
-
#
|
33
|
-
# All the RSpec settings are optional.
|
34
|
-
#
|
35
|
-
# @example RSpec Settings
|
36
|
-
# RSpec.configure do |config|
|
37
|
-
# config.family = :debian
|
38
|
-
# end
|
39
|
-
#
|
40
|
-
module RSpecResources
|
41
|
-
#
|
42
|
-
# Runs a docker image and the Serverspec tests against it.
|
43
|
-
#
|
44
|
-
# See the [Serverspec Resource Types documentation]
|
45
|
-
# (http://serverspec.org/resource_types.html) to see the available
|
46
|
-
# resources.
|
47
|
-
#
|
48
|
-
# By default tries to detect the most appropriate Docker backend: native
|
49
|
-
# or LXC.
|
50
|
-
#
|
51
|
-
# @example A Basic Example to Test the HTTPd Service
|
52
|
-
# describe docker_build('.', tag: 'myapp') do
|
53
|
-
# describe docker_run('myapp') do
|
54
|
-
# describe service('httpd') do
|
55
|
-
# it { should be_enabled }
|
56
|
-
# it { should be_running }
|
57
|
-
# end
|
58
|
-
# # [...]
|
59
|
-
# end
|
60
|
-
# end
|
61
|
-
#
|
62
|
-
# @example Avoid Automatic OS Detection to Speed Up the Tests
|
63
|
-
# describe docker_build('.', tag: 'myapp') do
|
64
|
-
# describe docker_run('myapp', family: :debian) do
|
65
|
-
# # [...]
|
66
|
-
# end
|
67
|
-
# end
|
68
|
-
#
|
69
|
-
# @example Using Hash Format
|
70
|
-
# describe docker_build('.', tag: 'myapp') do
|
71
|
-
# describe docker_run(tag: 'myapp', family: :debian) do
|
72
|
-
# # [...]
|
73
|
-
# end
|
74
|
-
# end
|
75
|
-
#
|
76
|
-
# @example Force a Specific Docker Backend
|
77
|
-
# describe docker_build('.', tag: 'myapp') do
|
78
|
-
# describe docker_run('myapp', backend: :lxc) do
|
79
|
-
# # [...]
|
80
|
-
# end
|
81
|
-
# end
|
82
|
-
#
|
83
|
-
# @example Use a Backend Not Included by Default
|
84
|
-
# # specinfra-backend-docker_nsenter gem must be installed by hand
|
85
|
-
# require 'specinfra/backend/docker_nsenter'
|
86
|
-
# describe docker_build('.', tag: 'myapp') do
|
87
|
-
# describe docker_run('myapp', backend: :nsenter) do
|
88
|
-
# # [...]
|
89
|
-
# end
|
90
|
-
# end
|
91
|
-
#
|
92
|
-
# @example Running a Container Image Tag
|
93
|
-
# describe docker_run('debian:8') do
|
94
|
-
# # [...]
|
95
|
-
# end
|
96
|
-
#
|
97
|
-
# @example Testing `FROM` Dockerfile Instruction
|
98
|
-
# # FROM debian:8
|
99
|
-
# describe docker_run('myapp') do
|
100
|
-
# describe file('/etc/debian_version') do
|
101
|
-
# it { should be_file }
|
102
|
-
# its(:content) { should match /^8\./ }
|
103
|
-
# end
|
104
|
-
# # Another way to check it:
|
105
|
-
# describe command('lsb_release -ri') do
|
106
|
-
# its(:stdout) { should match /^Distributor ID:\s+Debian/ }
|
107
|
-
# its(:stdout) { should match /^Release:\s+8\./ }
|
108
|
-
# end
|
109
|
-
# end
|
110
|
-
#
|
111
|
-
# @example Testing `COPY` and `ADD` Dockerfile Instructions
|
112
|
-
# # COPY docker-entrypoint.sh /entrypoint.sh
|
113
|
-
# describe docker_run('myapp') do
|
114
|
-
# describe file('/entrypoint.sh') do
|
115
|
-
# it { should be_file }
|
116
|
-
# its(:content) { should match /^exec java -jar myapp\.jar/ }
|
117
|
-
# end
|
118
|
-
# end
|
119
|
-
#
|
120
|
-
# @example Testing `RUN` Dockerfile Instructions
|
121
|
-
# describe docker_run('myapp') do
|
122
|
-
# # RUN apt-get install -y wget
|
123
|
-
# describe package('wget') do
|
124
|
-
# it { should be_installed }
|
125
|
-
# end
|
126
|
-
# # RUN useradd -g myapp -d /opt/myapp myapp
|
127
|
-
# describe user('myapp') do
|
128
|
-
# it { should exist }
|
129
|
-
# it { should belong_to_group 'myapp' }
|
130
|
-
# it { should have_home_directory '/opt/myapp' }
|
131
|
-
# end
|
132
|
-
# end
|
133
|
-
#
|
134
|
-
# @param opts [String, Hash] The `:tag` or a list of options.
|
135
|
-
#
|
136
|
-
# @option opts [String] :tag The Docker image tag name to run.
|
137
|
-
# @option opts [String] :id The Docker container ID to use instead of
|
138
|
-
# starting a new container.
|
139
|
-
# @option opts [Boolean] :rm (calculated) Whether to remove the Docker
|
140
|
-
# container afterwards.
|
141
|
-
# @option opts [String] :path The environment `PATH` value of the
|
142
|
-
# container.
|
143
|
-
# @option opts [Hash, Array] :env Some `ENV` instructions to add to the
|
144
|
-
# container.
|
145
|
-
# @option opts [Symbol] :family (calculated) The OS family.
|
146
|
-
# It's automatically detected by default, but can be used to
|
147
|
-
# **speed up the tests**. Some possible values:
|
148
|
-
# `:alpine`, `:arch`, `:coreos`, `:debian`, `:gentoo`, `:nixos`,
|
149
|
-
# `:plamo`, `:poky`, `:redhat`, `:suse`.
|
150
|
-
# @option opts [Symbol] :backend (calculated) Docker backend to use:
|
151
|
-
# `:docker`, `:lxc`.
|
152
|
-
#
|
153
|
-
# @return [Dockerspec::ServerspecRunner] Runner object.
|
154
|
-
#
|
155
|
-
# @raise [Dockerspec::DockerRunArgumentError] Raises this exception when
|
156
|
-
# some required fields are missing.
|
157
|
-
#
|
158
|
-
# @api public
|
159
|
-
#
|
160
|
-
def docker_run(*opts)
|
161
|
-
runner = Dockerspec::Serverspec::Runner.new(*opts)
|
162
|
-
runner.run
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
#
|
169
|
-
# Add the Dockerspec::Serverspec resources to RSpec core.
|
170
|
-
#
|
171
|
-
RSpec::Core::ExampleGroup.class_eval do
|
172
|
-
extend Dockerspec::Serverspec::RSpecResources
|
173
|
-
include Dockerspec::Serverspec::RSpecResources
|
174
|
-
end
|
175
|
-
|
176
|
-
#
|
177
|
-
# Allow using #docker_run in the outermost example
|
178
|
-
#
|
179
|
-
extend Dockerspec::RSpecResources
|
@@ -1,305 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
#
|
3
|
-
# Author:: Xabier de Zuazo (<xabier@zuazo.org>)
|
4
|
-
# Copyright:: Copyright (c) 2015 Xabier de Zuazo
|
5
|
-
# License:: Apache License, Version 2.0
|
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
|
-
|
20
|
-
require 'serverspec'
|
21
|
-
require 'specinfra/backend/docker_lxc'
|
22
|
-
require 'dockerspec/runner'
|
23
|
-
require 'dockerspec/serverspec/specinfra_backend'
|
24
|
-
require 'dockerspec/helper/rspec_example_helpers'
|
25
|
-
require 'dockerspec/helper/docker'
|
26
|
-
require 'dockerspec/docker_exception_parser'
|
27
|
-
|
28
|
-
#
|
29
|
-
# Silence error: No backend type is specified. Fall back to :exec type.
|
30
|
-
#
|
31
|
-
Specinfra.configuration.backend(:base)
|
32
|
-
|
33
|
-
module Dockerspec
|
34
|
-
#
|
35
|
-
# Contains the classes related to running Serverspec in docker containers.
|
36
|
-
#
|
37
|
-
module Serverspec
|
38
|
-
#
|
39
|
-
# Runs a Docker container using [Serverspec](http://serverspec.org/).
|
40
|
-
#
|
41
|
-
class Runner < Dockerspec::Runner
|
42
|
-
#
|
43
|
-
# Constructs a Docker Serverspec runner class to run Docker images.
|
44
|
-
#
|
45
|
-
# @example From a Docker Container Image Tag
|
46
|
-
# Dockerspec::Serverspec::Runner.new('myapp')
|
47
|
-
# #=> #<Dockerspec::Serverspec::Runner:0x0124>
|
48
|
-
#
|
49
|
-
# @example From a Docker Container Image Tag Using Hash Format
|
50
|
-
# Dockerspec::Serverspec::Runner.new(tag: 'myapp')
|
51
|
-
# #=> #<Dockerspec::Serverspec::Runner:0x0124>
|
52
|
-
#
|
53
|
-
# @example From a Running Docker Container ID
|
54
|
-
# Dockerspec::Serverspec::Runner.new(id: 'c51f86c28340')
|
55
|
-
# #=> #<Dockerspec::Serverspec::Runner:0x0125>
|
56
|
-
#
|
57
|
-
# @param opts [String, Hash] The `:tag` or a list of options.
|
58
|
-
#
|
59
|
-
# @option opts [String] :tag The Docker image tag name to run.
|
60
|
-
# @option opts [String] :id The Docker container ID to use instead of
|
61
|
-
# starting a new container.
|
62
|
-
# @option opts [Boolean] :rm (calculated) Whether to remove the Docker
|
63
|
-
# container afterwards.
|
64
|
-
# @option opts [String] :path The environment `PATH` value of the
|
65
|
-
# container.
|
66
|
-
# @option opts [Hash, Array] :env Some `ENV` instructions to add to the
|
67
|
-
# container.
|
68
|
-
# @option opts [Symbol] :family (calculated) The OS family.
|
69
|
-
# It's automatically detected by default, but can be used to
|
70
|
-
# **speed up the tests**. Some possible values:
|
71
|
-
# `:alpine`, `:arch`, `:coreos`, `:debian`, `:gentoo`, `:nixos`,
|
72
|
-
# `:plamo`, `:poky`, `:redhat`, `:suse`.
|
73
|
-
# @option opts [Symbol] :backend (calculated) Docker backend to use:
|
74
|
-
# `:docker`, `:lxc`.
|
75
|
-
#
|
76
|
-
# @return [Dockerspec::Serverspec::Runner] Runner object.
|
77
|
-
#
|
78
|
-
# @api public
|
79
|
-
#
|
80
|
-
def initialize(*opts)
|
81
|
-
super
|
82
|
-
@specinfra_backend = nil
|
83
|
-
@backend = calculate_docker_backend_name
|
84
|
-
end
|
85
|
-
|
86
|
-
#
|
87
|
-
# Runs the Docker Container and sets the Specinfra configuration.
|
88
|
-
#
|
89
|
-
# @example
|
90
|
-
# builder = Dockerspec::Builder.new('.', tag: 'myapp')
|
91
|
-
# builder.build
|
92
|
-
# runner = Dockerspec::Serverspec::Runner.new('myapp')
|
93
|
-
# runner.run #=> #<Dockerspec::Serverspec::Runner:0x0123>
|
94
|
-
#
|
95
|
-
# @return [Dockerspec::Serverspec::Runner] Runner object.
|
96
|
-
#
|
97
|
-
# @api public
|
98
|
-
#
|
99
|
-
def run
|
100
|
-
specinfra_setup
|
101
|
-
run_container
|
102
|
-
specinfra_save
|
103
|
-
self
|
104
|
-
rescue ::Docker::Error::DockerError => e
|
105
|
-
DockerExceptionParser.new(e)
|
106
|
-
end
|
107
|
-
|
108
|
-
#
|
109
|
-
# Stops and deletes the Docker Container.
|
110
|
-
#
|
111
|
-
# Actually does nothing. Do no delete anything, let Specinfra do that.
|
112
|
-
#
|
113
|
-
# @return void
|
114
|
-
#
|
115
|
-
# @api public
|
116
|
-
#
|
117
|
-
def finalize
|
118
|
-
# Do not stop the container
|
119
|
-
end
|
120
|
-
|
121
|
-
#
|
122
|
-
# Restores the Docker running container instance in the Specinfra
|
123
|
-
# internal reference.
|
124
|
-
#
|
125
|
-
# Gets the correct {Runner} reference from the RSpec metadata.
|
126
|
-
#
|
127
|
-
# @example Restore Specinfra Backend
|
128
|
-
# RSpec.configure do |c|
|
129
|
-
# c.before(:each) do
|
130
|
-
# metadata = RSpec.current_example.metadata
|
131
|
-
# Dockerspec::Serverspec::Runner.restore(metadata)
|
132
|
-
# end
|
133
|
-
# end
|
134
|
-
#
|
135
|
-
# @param metadata [Hash] RSpec metadata.
|
136
|
-
#
|
137
|
-
# @return void
|
138
|
-
#
|
139
|
-
# @api public
|
140
|
-
#
|
141
|
-
# @see restore
|
142
|
-
#
|
143
|
-
def self.restore(metadata)
|
144
|
-
runner = Helper::RSpecExampleHelpers.search_object(metadata, self)
|
145
|
-
return if runner.nil?
|
146
|
-
runner.restore
|
147
|
-
end
|
148
|
-
|
149
|
-
#
|
150
|
-
# Restores the Specinfra backend instance to point to this object's
|
151
|
-
# container.
|
152
|
-
#
|
153
|
-
# This is used to avoid Serverspec running against the last started
|
154
|
-
# container if you are testing multiple containers at the same time.
|
155
|
-
#
|
156
|
-
# @return void
|
157
|
-
#
|
158
|
-
def restore
|
159
|
-
@specinfra_backend.restore
|
160
|
-
end
|
161
|
-
|
162
|
-
#
|
163
|
-
# Generates a description of the object.
|
164
|
-
#
|
165
|
-
# @example Running Against a Container Image Tag
|
166
|
-
# self.description #=> "Serverspec on tag: \"debian\""
|
167
|
-
#
|
168
|
-
# @example Running Against a Running Container ID
|
169
|
-
# self.description #=> "Serverspec on id: \"92cc98ab560a\""
|
170
|
-
#
|
171
|
-
# @return [String] The object description.
|
172
|
-
#
|
173
|
-
# @api private
|
174
|
-
#
|
175
|
-
def to_s
|
176
|
-
description('Serverspec on')
|
177
|
-
end
|
178
|
-
|
179
|
-
protected
|
180
|
-
|
181
|
-
#
|
182
|
-
# Gets the default options configured using `RSpec.configuration`.
|
183
|
-
#
|
184
|
-
# @example
|
185
|
-
# self.rspec_options #=> { :family => :debian }
|
186
|
-
#
|
187
|
-
# @return [Hash] The configuration options.
|
188
|
-
#
|
189
|
-
# @api private
|
190
|
-
#
|
191
|
-
def rspec_options
|
192
|
-
config = RSpec.configuration
|
193
|
-
super.tap do |opts|
|
194
|
-
opts[:family] = config.family if config.family?
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
#
|
199
|
-
# Sets the Specinfra configuration.
|
200
|
-
#
|
201
|
-
# - Resets the internal Specinfra backend reference.
|
202
|
-
# - Sets the `:family`.
|
203
|
-
# - Sets the `:docker_image` or `:docker_container`.
|
204
|
-
#
|
205
|
-
# @return void
|
206
|
-
#
|
207
|
-
# @api private
|
208
|
-
#
|
209
|
-
def specinfra_setup
|
210
|
-
@specinfra_backend = SpecinfraBackend.new(@backend)
|
211
|
-
@specinfra_backend.reset
|
212
|
-
if @options.key?(:family)
|
213
|
-
Specinfra.configuration.os(family: @options[:family])
|
214
|
-
end
|
215
|
-
if id.nil?
|
216
|
-
Specinfra.configuration.docker_image(image_id)
|
217
|
-
else
|
218
|
-
Specinfra.configuration.docker_container(id)
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
#
|
223
|
-
# Saves the Specinfra backend internal reference internally to restore
|
224
|
-
# it later.
|
225
|
-
#
|
226
|
-
# @return void
|
227
|
-
#
|
228
|
-
# @api private
|
229
|
-
#
|
230
|
-
def specinfra_save
|
231
|
-
@specinfra_backend.save
|
232
|
-
end
|
233
|
-
|
234
|
-
#
|
235
|
-
# Generates the correct Specinfra backend name to use from a name.
|
236
|
-
#
|
237
|
-
# @example
|
238
|
-
# self.generate_docker_backend_name(:docker) #=> :docker
|
239
|
-
# self.generate_docker_backend_name(:lxc) #=> :docker_lxc
|
240
|
-
# self.generate_docker_backend_name(:docker_lxc) #=> :docker_lxc
|
241
|
-
# self.generate_docker_backend_name(:native) #=> :docker
|
242
|
-
#
|
243
|
-
# @param name [String, Symbol] The backend short (without the `docker`
|
244
|
-
# prefix) or long name.
|
245
|
-
#
|
246
|
-
# @return [Symbol] The backend name.
|
247
|
-
#
|
248
|
-
# @api private
|
249
|
-
#
|
250
|
-
def generate_docker_backend_name(name)
|
251
|
-
return name.to_s.to_sym unless name.to_s.match(/^docker/).nil?
|
252
|
-
return :docker if name.to_s.to_sym == :native
|
253
|
-
"docker_#{name}".to_sym
|
254
|
-
end
|
255
|
-
|
256
|
-
#
|
257
|
-
# Calculates the correct docker Specinfra backend to use on the system.
|
258
|
-
#
|
259
|
-
# Returns the LXC driver instead of the native driver when required.
|
260
|
-
#
|
261
|
-
# Reads the driver from the configuration options if set.
|
262
|
-
#
|
263
|
-
# @example Docker with Native Execution Driver
|
264
|
-
# self.calculate_docker_backend_name #=> :docker
|
265
|
-
#
|
266
|
-
# @example Docker with LXC Execution Driver
|
267
|
-
# self.calculate_docker_backend_name #=> :docker_lxc
|
268
|
-
#
|
269
|
-
# @return [Symbol] The backend name.
|
270
|
-
#
|
271
|
-
# @api private
|
272
|
-
#
|
273
|
-
def calculate_docker_backend_name
|
274
|
-
if @options.key?(:backend)
|
275
|
-
generate_docker_backend_name(@options[:backend])
|
276
|
-
elsif Helper::Docker.lxc_execution_driver?
|
277
|
-
:docker_lxc
|
278
|
-
else
|
279
|
-
:docker
|
280
|
-
end
|
281
|
-
end
|
282
|
-
|
283
|
-
#
|
284
|
-
# Starts the Docker container.
|
285
|
-
#
|
286
|
-
# @return void
|
287
|
-
#
|
288
|
-
# @api private
|
289
|
-
#
|
290
|
-
def run_container
|
291
|
-
Specinfra.configuration.backend(@backend)
|
292
|
-
end
|
293
|
-
end
|
294
|
-
end
|
295
|
-
end
|
296
|
-
|
297
|
-
#
|
298
|
-
# Restore Specinfra backend:
|
299
|
-
#
|
300
|
-
RSpec.configure do |c|
|
301
|
-
c.before(:each) do
|
302
|
-
metadata = RSpec.current_example.metadata
|
303
|
-
Dockerspec::Serverspec::Runner.restore(metadata)
|
304
|
-
end
|
305
|
-
end
|