Skip to content

Commit

Permalink
Docker login Receipt is now removed if login fails and added acceptan…
Browse files Browse the repository at this point in the history
…ce tests (#182)

* Docker login Receipt is now removed if login fails and added acceptance tests

* Fix typo in test registry_spec

* Disable specific test case for normal spec test that are covered in acceptance tests
  • Loading branch information
sigsegv11 authored and davejrt committed Mar 12, 2018
1 parent c09412a commit 7183c7d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
6 changes: 5 additions & 1 deletion manifests/registry.pp
Expand Up @@ -77,17 +77,21 @@

# server may be an URI, which can contain /
$server_strip = regsubst($server, '/', '_', 'G')
$_auth_command = "${auth_cmd} || rm -f \"/root/registry-auth-puppet_receipt_${server_strip}_${local_user}\""

file { "/root/registry-auth-puppet_receipt_${server_strip}_${local_user}":
ensure => $ensure,
content => $_pass_hash,
notify => Exec["${title} auth"],
}
}
else {
$_auth_command = $auth_cmd
}

exec { "${title} auth":
environment => $auth_environment,
command => $auth_cmd,
command => $_auth_command,
user => $local_user,
cwd => '/root',
path => ['/bin', '/usr/bin'],
Expand Down
15 changes: 15 additions & 0 deletions spec/acceptance/docker_spec.rb
Expand Up @@ -110,6 +110,7 @@ class { 'docker':
registry_host = 'localhost'
registry_port = 5000
@registry_address = "#{registry_host}:#{registry_port}"
@registry_bad_address = "#{registry_host}:5001"
# @registry_email = 'user@example.com'
@config_file = '/root/.docker/config.json'
@manifest = <<-EOS
Expand Down Expand Up @@ -137,6 +138,7 @@ class { 'docker': }
EOS
apply_manifest(manifest, :catch_failures=>true)
shell("grep #{@registry_address} #{@config_file}", :acceptable_exit_codes => [0])
shell("test -e \"/root/registry-auth-puppet_receipt_#{@registry_address}_root\"", :acceptable_exit_codes => [0])
end

it 'should be able to logout from the registry' do
Expand All @@ -149,6 +151,19 @@ class { 'docker': }
shell("grep #{@registry_address} #{@config_file}", :acceptable_exit_codes => [1,2])
# shell("grep #{@registry_email} #{@config_file}", :acceptable_exit_codes => [1,2])
end

it 'should not create receipt if registry login fails' do
manifest = <<-EOS
docker::registry { '#{@registry_bad_address}':
username => 'username',
password => 'password',
}
EOS
apply_manifest(manifest, :catch_failures=>true)
shell("grep #{@registry_bad_address} #{@config_file}", :acceptable_exit_codes => [1,2])
shell("test -e \"/root/registry-auth-puppet_receipt_#{@registry_bad_address}_root\"", :acceptable_exit_codes => [1])
end

end

end
20 changes: 10 additions & 10 deletions spec/defines/registry_spec.rb
Expand Up @@ -10,51 +10,51 @@
:kernelrelease => '3.2.0-4-amd64',
:operatingsystemmajrelease => '8',
} }
let(:params) { { 'version' => '17.06', 'pass_hash' => 'test1234' } }
let(:params) { { 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false } }
it { should contain_exec('localhost:5000 auth') }

context 'with ensure => present' do
let(:params) { { 'ensure' => 'absent', 'version' => '17.06', 'pass_hash' => 'test1234' } }
let(:params) { { 'ensure' => 'absent', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false } }
it { should contain_exec('localhost:5000 auth').with_command('docker logout localhost:5000') }
end

context 'with ensure => present' do
let(:params) { { 'ensure' => 'present', 'version' => '17.06', 'pass_hash' => 'test1234' } }
let(:params) { { 'ensure' => 'present', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false } }
it { should contain_exec('localhost:5000 auth').with_command('docker login localhost:5000') }
end

context 'with ensure => present and username => user1' do
let(:params) { { 'ensure' => 'present', 'username' => 'user1', 'version' => '17.06', 'pass_hash' => 'test1234' } }
let(:params) { { 'ensure' => 'present', 'username' => 'user1', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false } }
it { should contain_exec('localhost:5000 auth').with_command('docker login localhost:5000') }
end

context 'with ensure => present and password => secret' do
let(:params) { { 'ensure' => 'present', 'password' => 'secret', 'version' => '17.06', 'pass_hash' => 'test1234' } }
let(:params) { { 'ensure' => 'present', 'password' => 'secret', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false } }
it { should contain_exec('localhost:5000 auth').with_command('docker login localhost:5000') }
end

context 'with ensure => present and email => user1@example.io' do
let(:params) { { 'ensure' => 'present', 'email' => 'user1@example.io', 'version' => '17.06', 'pass_hash' => 'test1234' } }
let(:params) { { 'ensure' => 'present', 'email' => 'user1@example.io', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false } }
it { should contain_exec('localhost:5000 auth').with_command('docker login localhost:5000') }
end

context 'with ensure => present and username => user1, and password => secret and email => user1@example.io' do
let(:params) { { 'ensure' => 'present', 'username' => 'user1', 'password' => 'secret', 'email' => 'user1@example.io', 'version' => '17.06', 'pass_hash' => 'test1234' } }
let(:params) { { 'ensure' => 'present', 'username' => 'user1', 'password' => 'secret', 'email' => 'user1@example.io', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false } }
it { should contain_exec('localhost:5000 auth').with_command("docker login -u 'user1' -p \"${password}\" localhost:5000").with_environment('password=secret') }
end

context 'with ensure => present and username => user1, and password => secret and email => user1@example.io and version < 1.11.0' do
let(:params) { { 'ensure' => 'present', 'username' => 'user1', 'password' => 'secret', 'email' => 'user1@example.io', 'version' => '1.9.0', 'pass_hash' => 'test1234' } }
let(:params) { { 'ensure' => 'present', 'username' => 'user1', 'password' => 'secret', 'email' => 'user1@example.io', 'version' => '1.9.0', 'pass_hash' => 'test1234', 'receipt' => false } }
it { should contain_exec('localhost:5000 auth').with_command("docker login -u 'user1' -p \"${password}\" -e 'user1@example.io' localhost:5000").with_environment('password=secret') }
end

context 'with username => user1, and password => secret' do
let(:params) { { 'username' => 'user1', 'password' => 'secret', 'version' => '17.06', 'pass_hash' => 'test1234' } }
let(:params) { { 'username' => 'user1', 'password' => 'secret', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false } }
it { should contain_exec('localhost:5000 auth').with_command("docker login -u 'user1' -p \"${password}\" localhost:5000").with_environment('password=secret') }
end

context 'with username => user1, and password => secret and local_user => testuser' do
let(:params) { { 'username' => 'user1', 'password' => 'secret', 'local_user' => 'testuser', 'version' => '17.06', 'pass_hash' => 'test1234' } }
let(:params) { { 'username' => 'user1', 'password' => 'secret', 'local_user' => 'testuser', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false } }
it { should contain_exec('localhost:5000 auth').with_command("docker login -u 'user1' -p \"${password}\" localhost:5000").with_user('testuser').with_environment('password=secret') }
end

Expand Down

0 comments on commit 7183c7d

Please sign in to comment.