-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocker_steps.rb
More file actions
126 lines (104 loc) · 3.51 KB
/
Copy pathdocker_steps.rb
File metadata and controls
126 lines (104 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require 'digest/sha1'
class DockerMockInterface < Rascal::Docker::Interface
include RSpec::Mocks::ExampleMethods
attr_reader :history, :images, :containers, :volumes
attr_accessor :attached_exit_code
def initialize(*)
@attached_exit_code = 0
@history = []
@images = Hash.new { |h, k| h[k] = {} }
@containers = Hash.new { |h, k| h[k] = {} }
@volumes = Hash.new { |h, k| h[k] = {} }
@counter = 0
super
end
def flat_history
@history.map { |e| e.join(' ') }
end
private
def spawn(*command)
@history << command
exit_status(@attached_exit_code.zero?, @attached_exit_code)
end
def popen3(*command, &block)
@history << command
stdin_reader, stdin_writer = IO.pipe
stdout_reader, stdout_writer = IO.pipe
stderr_reader, stderr_writer = IO.pipe
thread = Thread.new do
sleep 0.01
stdin_reader.close
output = output_for(*command)
stdout_writer.puts(output)
stdout_writer.close
stderr_writer.close
exit_status(output != :fail)
end
block.call(stdin_writer, stdout_reader, stderr_reader, thread)
end
def output_for(*command)
case command.join(' ')
when /image inspect (\S*)/
@images[$1].to_json
when /pull (\S*)/
"[Docker mock] Pulling #{$1}"
when /network ls.*name=\^(.*)\$/
"deadbeef"
when /volume ls.*name=\^(.*)\$/
@volumes[$1][:id]
when /container ps.*name=\^\/(.*)\$/
@containers[$1][:id]
when /container create --name (\S*)/
new_id = "%08x" % (@counter += 1)
@containers[$1] = { id: new_id }
new_id
when /container inspect (.*)/
container = @containers.values.detect { |c| c[:id] == $1 }
if container
[container].to_json
else
:fail
end
end
end
def exit_status(success = true, code = nil)
instance_double(Process::Status,
success?: success,
exitstatus: code || (success ? 0 : 1),
termsig: nil,
)
end
end
Before do
Rascal::Docker.interface = DockerMockInterface.new
end
Then("docker {command} should have been called") do |command|
history = Rascal::Docker.interface.flat_history
expect(history).to include("docker #{command}"), "expected to find #{command} in:\n#{history.join("\n")}"
end
Then("docker {regexp} should have been called") do |regexp|
history = Rascal::Docker.interface.flat_history.join("\n")
expect(history).to match(/docker #{regexp}/), "expected to find #{regexp} in:\n#{history}"
end
Then("docker {command} should not have been called") do |command|
expect(Rascal::Docker.interface.flat_history).not_to include("docker #{command}")
end
Then("docker {regexp} should not have been called") do |regexp|
history = Rascal::Docker.interface.flat_history.join("\n")
expect(history).not_to match(/docker #{regexp}/)
end
Given("the container command will exit with {int}") do |code|
Rascal::Docker.interface.attached_exit_code = code
end
Given("the docker image {string} exists") do |name|
Rascal::Docker.interface.images[name] = { id: Digest::SHA1.hexdigest("image-#{name}") }
end
Given("the container {string} exists") do |name|
Rascal::Docker.interface.containers[name] = { id: Digest::SHA1.hexdigest("container-#{name}") }
end
Given("the container {string} is running") do |name|
Rascal::Docker.interface.containers[name] = { id: Digest::SHA1.hexdigest("container-#{name}"), State: { Running: true } }
end
Given("the volume {string} exists") do |name|
Rascal::Docker.interface.volumes[name] = { id: Digest::SHA1.hexdigest("volume-#{name}") }
end