-
Notifications
You must be signed in to change notification settings - Fork 321
/
Copy pathdocker_params_changed_spec.rb
149 lines (131 loc) · 4.82 KB
/
docker_params_changed_spec.rb
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# frozen_string_literal: true
require 'spec_helper_acceptance'
if os[:family] == 'windows'
os_name = run_shell('systeminfo | findstr /R /C:"OS Name"')
raise 'Could not retrieve systeminfo for Windows box' if os_name.exit_code != 0
os_name = if os_name.stdout.split(%r{\s}).include?('2016')
'win-2016'
elsif os_name.stdout.split(%r{\s}).include?('2019')
'win-2019'
else
'win-2022'
end
docker_args = 'docker_ee => true'
docker_network = 'nat'
volume_location = 'C:\\'
docker_image = if os_name == 'win-2016'
'stefanscherer/nanoserver:sac2016'
else
'stefanscherer/nanoserver:10.0.17763.1040'
end
else
docker_args = ''
docker_network = 'bridge'
volume_location = '/opt/'
docker_image = 'hello-world:linux'
end
describe 'docker trigger parameters change', if: fetch_puppet_version > 5 do
before(:all) do
if os[:family] != 'windows'
install_pp = "class { 'docker': #{docker_args}}"
apply_manifest(install_pp)
end
run_shell("mkdir #{volume_location}volume_1")
run_shell("mkdir #{volume_location}volume_2")
end
context 'when image is changed' do
image_changed = if os[:family] == 'windows'
if os_name == 'win-2016'
'stefanscherer/nanoserver:10.0.14393.2551'
else
'stefanscherer/nanoserver:1809'
end
else
'hello-world:latest'
end
let(:pp1) do
"
class {'docker': #{docker_args}}
docker::run {'servercore': image => '#{docker_image}', restart => 'always', net => '#{docker_network}' }
"
end
let(:pp2) do
"
class {'docker': #{docker_args}}
docker::run {'servercore': image => '#{image_changed}', restart => 'always', net => '#{docker_network}' }
"
end
it 'creates servercore with first image' do
expect(docker_run_idempotent_apply(pp1)).to be true
end
it 'detect image change and apply the change' do
apply_manifest(pp2, catch_failures: true)
run_shell('docker inspect --format="{{ .Config.Image }}" servercore') do |r|
expect(r.stdout).to match(%r{#{image_changed}})
end
end
end
context 'when volumes parameter is changed' do
if os[:family] == 'windows'
volumes1 = "volumes => ['volume-1:C:\\volume_1']"
volumes2 = "volumes => ['volume-1:C:\\volume_1', 'volume-2:C:\\volume_2']"
else
volumes1 = "volumes => ['volume-1:#{volume_location}volume_1']"
volumes2 = "volumes => ['volume-1:#{volume_location}volume_1', 'volume-2:#{volume_location}volume_2']"
end
let(:pp1) do
"
class {'docker': #{docker_args}}
docker::run {'servercore': image => '#{docker_image}', restart => 'always', net => '#{docker_network}', #{volumes1}}
"
end
let(:pp2) do
"
class {'docker': #{docker_args}}
docker::run {'servercore': image => '#{docker_image}', restart => 'always', net => '#{docker_network}', #{volumes2}}
"
end
it "creates servercore with #{volumes1}" do
expect(docker_run_idempotent_apply(pp1)).to be true
end
it "creates servercore with #{volumes2}" do
apply_manifest(pp2, catch_failures: true)
run_shell('docker inspect servercore --format="{{ json .Mounts }}"') do |r|
inspect_result = JSON.parse(r.stdout)
inspect_result = inspect_result.map { |item| item['Name'] }.sort
expect(inspect_result).to eq(['volume-1', 'volume-2'])
end
end
end
context 'when ports parameter is changed' do
ports1 = "ports => ['4444']"
ports2 = "ports => ['4444', '4445']"
let(:pp1) do
"
class {'docker': #{docker_args}}
docker::run {'servercore': image => '#{docker_image}', restart => 'always', net => '#{docker_network}', #{ports1}}
"
end
let(:pp2) do
"
class {'docker': #{docker_args}}
docker::run {'servercore': image => '#{docker_image}', restart => 'always', net => '#{docker_network}', #{ports2}}
"
end
it 'creates servercore with ports => ["4444"]' do
expect(docker_run_idempotent_apply(pp1)).to be true
end
it 'creates servercore with ports => ["4444", "4445"]' do
apply_manifest(pp2, catch_failures: true)
run_shell('docker inspect servercore --format="{{ json .HostConfig.PortBindings }}"') do |r|
inspect_result = JSON.parse(r.stdout)
inspect_result = inspect_result.keys.map { |item| item.split('/')[0] }.sort
expect(inspect_result).to eq(['4444', '4445'])
end
end
end
after(:all) do
run_shell("rm -r #{volume_location}volume_1")
run_shell("rm -r #{volume_location}volume_2")
end
end