Skip to content

Commit d8abac4

Browse files
committed
Fix docker registry idempotency and add windows acceptance tests
1 parent 359b7a6 commit d8abac4

File tree

5 files changed

+94
-46
lines changed

5 files changed

+94
-46
lines changed

Diff for: manifests/registry.pp

+28-23
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,19 @@
8383
}
8484

8585
$docker_auth = "${title}${auth_environment}${auth_cmd}${local_user}"
86-
if $receipt {
8786

88-
# server may be an URI, which can contain /
89-
$server_strip = regsubst($server, '/', '_', 'G')
87+
if $auth_environment != '' {
88+
$exec_env = concat($exec_environment, $auth_environment, "docker_auth=${docker_auth}")
89+
} else {
90+
$exec_env = concat($exec_environment, "docker_auth=${docker_auth}")
91+
}
92+
93+
if $receipt {
9094

9195
if $::osfamily != 'windows' {
96+
# server may be an URI, which can contain /
97+
$server_strip = regsubst($server, '/', '_', 'G')
98+
9299
# no - with pw_hash
93100
$local_user_strip = regsubst($local_user, '-', '', 'G')
94101

@@ -104,34 +111,32 @@
104111
notify => Exec["${title} auth"],
105112
}
106113
} else {
107-
$_auth_command = $auth_cmd
108-
$pw_hash_path = 'C:/Windows/Temp/compute_hash.ps1'
114+
# server may be an URI, which can contain /
115+
$server_strip = regsubst($server, '[/:]', '_', 'G')
109116
$passfile = "C:/Windows/Temp/registry-auth-puppet_receipt_${server_strip}_${local_user}"
110-
file{ $pw_hash_path:
111-
ensure => present,
112-
force => true,
113-
content => template('docker/windows/compute_hash.ps1.erb'),
114-
notify => Exec['compute-hash']
115-
}
116-
exec { 'compute-hash':
117-
command => "& ${pw_hash_path}",
118-
provider => $exec_provider,
119-
refreshonly => true,
120-
logoutput => true,
121-
notify => Exec["${title} auth"],
117+
$_auth_command = "if (-not (${auth_cmd})) { Remove-Item -Path ${passfile} -Force -Recurse -EA SilentlyContinue; exit 0 } else { exit 0 }"
118+
119+
if $ensure == 'absent' {
120+
file { $passfile:
121+
ensure => $ensure,
122+
notify => Exec["${title} auth"],
123+
}
124+
} elsif $ensure == 'present' {
125+
exec { 'compute-hash':
126+
command => template('docker/windows/compute_hash.ps1.erb'),
127+
environment => $exec_env,
128+
provider => $exec_provider,
129+
logoutput => true,
130+
unless => template('docker/windows/check_hash.ps1.erb'),
131+
notify => Exec["${title} auth"],
122132
}
133+
}
123134
}
124135
}
125136
else {
126137
$_auth_command = $auth_cmd
127138
}
128139

129-
if $auth_environment != '' {
130-
$exec_env = concat($exec_environment, $auth_environment, "docker_auth=${docker_auth}")
131-
} else {
132-
$exec_env = concat($exec_environment, "docker_auth=${docker_auth}")
133-
}
134-
135140
exec { "${title} auth":
136141
environment => $exec_env,
137142
command => $_auth_command,

Diff for: spec/acceptance/docker_spec.rb

+43-22
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,43 @@
22

33
broken = false
44

5+
registry_port = 5000
6+
57
if fact('osfamily') == 'windows'
6-
puts "Not implemented on Windows"
8+
win_host = only_host_with_role(hosts, 'default')
9+
@windows_ip = win_host.ip
10+
docker_arg = "docker_ee => true, extra_parameters => '\"insecure-registries\": [ \"#{@windows_ip}:5000\" ]'"
11+
docker_registry_image = 'stefanscherer/registry-windows'
12+
docker_network = 'nat'
13+
registry_host = @windows_ip
14+
config_file = '/cygdrive/c/Users/Administrator/.docker/config.json'
15+
root_dir = "C:/Windows/Temp"
16+
server_strip = "#{registry_host}_#{registry_port}"
17+
bad_server_strip = "#{registry_host}_5001"
718
broken = true
8-
elsif fact('osfamily') == 'RedHat'
9-
docker_args = "repo_opt => '--enablerepo=localmirror-extras'"
19+
else
20+
if fact('osfamily') == 'RedHat'
21+
docker_args = "repo_opt => '--enablerepo=localmirror-extras'"
22+
else
23+
docker_arg = ''
24+
end
25+
docker_registry_image = 'registry'
26+
docker_network = 'bridge'
27+
registry_host = '127.0.0.1'
28+
server_strip = "#{registry_host}:#{registry_port}"
29+
bad_server_strip = "#{registry_host}:5001"
30+
config_file = '/root/.docker/config.json'
31+
root_dir = "/root"
1032
end
1133

12-
describe 'docker', :win_broken => broken do
34+
describe 'docker' do
1335
package_name = 'docker-ce'
1436
service_name = 'docker'
1537
command = 'docker'
1638

17-
context 'When adding system user' do
39+
context 'When adding system user', :win_broken => broken do
1840
let(:pp) {"
19-
class { 'docker':
41+
class { 'docker': #{docker_arg}
2042
docker_users => ['user1']
2143
}
2244
"}
@@ -28,7 +50,7 @@ class { 'docker':
2850
end
2951
end
3052

31-
context 'with default parameters' do
53+
context 'with default parameters', :win_broken => broken do
3254
let(:pp) {"
3355
class { 'docker':
3456
docker_users => [ 'testuser' ],
@@ -97,7 +119,7 @@ class { 'docker':
97119
end
98120
end
99121

100-
context "When asked to have the latest image of something" do
122+
context "When asked to have the latest image of something", :win_broken => broken do
101123
let(:pp) {"
102124
class { 'docker':
103125
docker_users => [ 'testuser' ]
@@ -112,7 +134,7 @@ class { 'docker':
112134
end
113135
end
114136

115-
context "When registry_mirror is set" do
137+
context "When registry_mirror is set", :win_broken => broken do
116138
let(:pp) {"
117139
class { 'docker':
118140
registry_mirror => 'http://testmirror.io'
@@ -131,23 +153,22 @@ class { 'docker':
131153

132154
context 'registry' do
133155
before(:all) do
134-
registry_host = 'localhost'
135-
registry_port = 5000
136156
@registry_address = "#{registry_host}:#{registry_port}"
137157
@registry_bad_address = "#{registry_host}:5001"
138158
# @registry_email = '[email protected]'
139-
@config_file = '/root/.docker/config.json'
140159
@manifest = <<-EOS
141-
class { 'docker': }
160+
class { 'docker': #{docker_arg}}
142161
docker::run { 'registry':
143-
image => 'registry',
162+
image => '#{docker_registry_image}',
144163
pull_on_start => true,
164+
restart => 'always',
165+
net => '#{docker_network}',
145166
ports => '#{registry_port}:#{registry_port}',
146-
volumes => '/tmp/registry-dev',
147167
}
148168
EOS
149-
150-
apply_manifest(@manifest, :catch_failures=>true)
169+
retry_on_error_matching(60, 5, /connection failure running/) do
170+
apply_manifest(@manifest, :catch_failures=>true)
171+
end
151172
# avoid a race condition with the registry taking time to start
152173
# on some operating systems
153174
sleep 10
@@ -161,8 +182,8 @@ class { 'docker': }
161182
}
162183
EOS
163184
apply_manifest(manifest, :catch_failures=>true)
164-
shell("grep #{@registry_address} #{@config_file}", :acceptable_exit_codes => [0])
165-
shell("test -e \"/root/registry-auth-puppet_receipt_#{@registry_address}_root\"", :acceptable_exit_codes => [0])
185+
shell("grep #{@registry_address} #{config_file}", :acceptable_exit_codes => [0])
186+
shell("test -e \"#{root_dir}/registry-auth-puppet_receipt_#{server_strip}_root\"", :acceptable_exit_codes => [0])
166187
end
167188

168189
it 'should be able to logout from the registry' do
@@ -172,7 +193,7 @@ class { 'docker': }
172193
}
173194
EOS
174195
apply_manifest(manifest, :catch_failures=>true)
175-
shell("grep #{@registry_address} #{@config_file}", :acceptable_exit_codes => [1,2])
196+
shell("grep #{@registry_address} #{config_file}", :acceptable_exit_codes => [1,2])
176197
# shell("grep #{@registry_email} #{@config_file}", :acceptable_exit_codes => [1,2])
177198
end
178199

@@ -184,8 +205,8 @@ class { 'docker': }
184205
}
185206
EOS
186207
apply_manifest(manifest, :catch_failures=>true)
187-
shell("grep #{@registry_bad_address} #{@config_file}", :acceptable_exit_codes => [1,2])
188-
shell("test -e \"/root/registry-auth-puppet_receipt_#{@registry_bad_address}_root\"", :acceptable_exit_codes => [1])
208+
shell("grep #{@registry_bad_address} #{config_file}", :acceptable_exit_codes => [1,2])
209+
shell("test -e \"#{root_dir}/registry-auth-puppet_receipt_#{bad_server_strip}_root\"", :acceptable_exit_codes => [1])
189210
end
190211

191212
end

Diff for: spec/spec_helper_acceptance.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ def retry_on_error_matching(max_retry_count = 3, retry_wait_interval_secs = 5, e
130130
end
131131

132132
if fact_on(host, 'osfamily') == 'windows'
133-
apply_manifest_on(host, "class { 'docker': docker_ee => true }")
133+
win_host = only_host_with_role(hosts, 'default')
134+
@windows_ip = win_host.ip
135+
apply_manifest_on(host, "class { 'docker': docker_ee => true, extra_parameters => '\"insecure-registries\": [ \"#{@windows_ip}:5000\" ]' }")
134136
docker_path = "/cygdrive/c/Program Files/Docker"
135137
host.add_env_var('PATH', docker_path)
136138
puts "Waiting for box to come online"

Diff for: templates/windows/check_hash.ps1.erb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#file computes the 512SHA for a given string and writes it to a file
2+
3+
$String = $env:docker_auth
4+
$HashName = "SHA512"
5+
$StringBuilder = New-Object System.Text.StringBuilder
6+
[System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))|%{
7+
[Void]$StringBuilder.Append($_.ToString("x2"))
8+
}
9+
10+
if([System.IO.File]::Exists("<%= @passfile %>")){
11+
$CurrentContent = Get-Content -Path "<%= @passfile %>"
12+
if($CurrentContent -eq $StringBuilder.ToString()){
13+
exit 0
14+
}
15+
}
16+
17+
exit 1

Diff for: templates/windows/config/daemon.json.erb

+3
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
<% if @bridge %>"bridge": "<%= @bridge %>",<% end -%>
1515
<% if @fixed_cidr %>"fixed-cidr": "<%= @fixed_cidr %>",<% end -%>
1616
<% if @registry_mirror %>"registry-mirrors": ["<%= @registry_mirror%>"], <% end -%>
17+
<% if @extra_parameters %><% @extra_parameters_array.each do |param| %>
18+
<%= param %> ,<% end %>
19+
<% end -%>
1720
"labels": <%= @labels_array.to_json %>
1821
}

0 commit comments

Comments
 (0)