From 4b57d04dc54397845e98134a66e874474630d23f Mon Sep 17 00:00:00 2001 From: "James T. Lee" Date: Wed, 20 Dec 2017 14:38:29 -0500 Subject: [PATCH] Add service_provider parameter to docker::run This module already has the code to support all systemd-based distributions, yet it is artificially locked to just Red Hat and Ubuntu/Debian. By exposing the `service_provider` parameter in the `docker::run` defined type (a parameter that is also exposed in the main `docker` class), this module becomes usable on most other distributions. Other minor fixes have been added to set missing variables when running under a non-Debian or non-RedHat operating system. --- manifests/init.pp | 15 ++- manifests/install.pp | 2 +- manifests/run.pp | 30 +++-- manifests/service.pp | 4 +- spec/defines/run_spec.rb | 229 +++++++++++++++++++-------------------- 5 files changed, 152 insertions(+), 128 deletions(-) diff --git a/manifests/init.pp b/manifests/init.pp index eb48a626..1ccbbc0c 100755 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -502,13 +502,14 @@ Optional[Boolean] $service_hasstatus = $docker::params::service_hasstatus, Optional[Boolean] $service_hasrestart = $docker::params::service_hasrestart, Optional[String] $registry_mirror = $docker::params::registry_mirror, + Boolean $acknowledge_unsupported_os = false, # Windows specific parameters Optional[String] $docker_msft_provider_version = $docker::params::docker_msft_provider_version, Optional[String] $nuget_package_provider_version = $docker::params::nuget_package_provider_version, ) inherits docker::params { - if $::osfamily { + if $::osfamily and !$acknowledge_unsupported_os { assert_type(Pattern[/^(Debian|RedHat|windows)$/], $::osfamily) |$a, $b| { fail(translate('This module only works on Debian, Red Hat or Windows based systems.')) } @@ -620,7 +621,11 @@ 'windows': { fail(translate('This module only work for Docker Enterprise Edition on Windows.')) } - default: {} + default: { + $package_location = $docker_package_location + $package_key_source = $docker_package_key_source + $package_key_check_source = $docker_package_key_check_source + } } $docker_start_command = $docker_ce_start_command $docker_package_name = $docker_ce_package_name @@ -640,7 +645,11 @@ $package_key_source = $docker_package_key_source $package_key_check_source = $docker_package_key_check_source } - default : {} + default : { + $package_location = $docker_package_location + $package_key_source = $docker_package_key_source + $package_key_check_source = $docker_package_key_check_source + } } $docker_start_command = $docker_engine_start_command $docker_package_name = $docker_engine_package_name diff --git a/manifests/install.pp b/manifests/install.pp index 542320e7..eb26b86b 100755 --- a/manifests/install.pp +++ b/manifests/install.pp @@ -30,7 +30,7 @@ ) { $docker_start_command = $docker::docker_start_command - if $::osfamily { + if $::osfamily and !::docker::acknowledge_unsupported_os { assert_type(Pattern[/^(Debian|RedHat|windows)$/], $::osfamily) |$a, $b| { fail(translate('This module only works on Debian, RedHat or Windows.')) } diff --git a/manifests/run.pp b/manifests/run.pp index 1f01f65a..d5f339fc 100755 --- a/manifests/run.pp +++ b/manifests/run.pp @@ -109,6 +109,7 @@ Variant[String,Array,Undef] $dns_search = [], Variant[String,Array,Undef] $lxc_conf = [], Optional[String] $service_prefix = 'docker-', + Optional[String] $service_provider = undef, Optional[Boolean] $restart_service = true, Optional[Boolean] $restart_service_on_docker_refresh = true, Optional[Boolean] $manage_service = true, @@ -176,8 +177,16 @@ assert_type(Pattern[/^(no|always|on-success|on-failure|on-abnormal|on-abort|on-watchdog)$/], $systemd_restart) } + $service_provider_real = $service_provider ? { + undef => $docker::params::service_provider, + default => $service_provider, + } + if $detach == undef { - $valid_detach = $docker::params::detach_service_in_init + $valid_detach = $service_provider_real ? { + 'systemd' => false, + default => $docker::params::detach_service_in_init, + } } else { $valid_detach = $detach } @@ -335,7 +344,7 @@ $docker_run_inline_start = template('docker/docker-run-start.erb') $docker_run_inline_stop = template('docker/docker-run-stop.erb') - case $docker::params::service_provider { + case $service_provider_real { 'systemd': { $initscript = "/etc/systemd/system/${service_prefix}${sanitised_title}.service" $startscript = "/usr/local/bin/docker-run-${sanitised_title}-start.sh" @@ -343,6 +352,7 @@ $startstop_template = 'docker/usr/local/bin/docker-run.sh.epp' $init_template = 'docker/etc/systemd/system/docker-run.erb' $mode = '0644' + $hasstatus = true } 'upstart': { $initscript = "/etc/init.d/${service_prefix}${sanitised_title}" @@ -350,7 +360,8 @@ $mode = '0750' $startscript = undef $stopscript = undef - $starstop_template = undef + $startstop_template = undef + $hasstatus = true } default: { if $::osfamily != 'windows' { @@ -359,6 +370,8 @@ elsif $ensure == 'present' { fail(translate('Restart parameter is required for Windows')) } + + $hasstatus = $::docker::params::service_hasstatus } } @@ -385,7 +398,8 @@ service { "${service_prefix}${sanitised_title}": ensure => false, enable => false, - hasstatus => $docker::params::service_hasstatus, + hasstatus => $hasstatus, + provider => $service_provider_real, } } exec { @@ -453,7 +467,7 @@ service { "${service_prefix}${sanitised_title}": ensure => $running, enable => false, - hasstatus => $docker::params::service_hasstatus, + hasstatus => $hasstatus, require => File[$initscript], } } @@ -482,8 +496,8 @@ service { "${service_prefix}${sanitised_title}": ensure => $running, enable => true, - provider => $docker::params::service_provider, - hasstatus => $docker::params::service_hasstatus, + provider => $service_provider_real, + hasstatus => $hasstatus, require => File[$initscript], } } @@ -502,7 +516,7 @@ } } } - if $docker::params::service_provider == 'systemd' { + if $service_provider_real == 'systemd' { exec { "docker-${sanitised_title}-systemd-reload": path => ['/bin/', '/sbin/', '/usr/bin/', '/usr/sbin/'], command => 'systemctl daemon-reload', diff --git a/manifests/service.pp b/manifests/service.pp index f2d75020..0e2f4467 100755 --- a/manifests/service.pp +++ b/manifests/service.pp @@ -121,7 +121,7 @@ $root_dir_flag = $docker::root_dir_flag, ) { - unless $::osfamily =~ /(Debian|RedHat|windows)/ { + unless $::osfamily =~ /(Debian|RedHat|windows)/ or $::docker::acknowledge_unsupported_os { fail(translate('The docker::service class needs a Debian, Redhat or Windows based system.')) } @@ -137,6 +137,8 @@ } else { if $::osfamily == 'Debian' { $_service_config = "/etc/default/${service_name}" + } else { + $_service_config = undef } } diff --git a/spec/defines/run_spec.rb b/spec/defines/run_spec.rb index 06119a3a..e2398bfb 100755 --- a/spec/defines/run_spec.rb +++ b/spec/defines/run_spec.rb @@ -1,9 +1,11 @@ require 'spec_helper' -['Debian', 'RedHat'].each do |osfamily| +['Debian', 'RedHat', 'generic systemd'].each do |osfamily| describe 'docker::run', :type => :define do let(:title) { 'sample' } - let(:pre_condition) { "class { 'docker': docker_group => 'docker', service_name => 'docker' }" } + + params = {'command' => 'command', 'image' => 'base'} + context "on #{osfamily}" do initscript = '/etc/systemd/system/docker-sample.service' @@ -11,6 +13,8 @@ stopscript = "/usr/local/bin/docker-run-sample-stop.sh" if osfamily == 'Debian' + pre_condition = "class { 'docker': docker_group => 'docker', service_name => 'docker' }" + let(:pre_condition) { pre_condition } let(:facts) { { :architecture => 'amd64', :osfamily => 'Debian', @@ -20,10 +24,12 @@ :kernelrelease => '4.4.0-21-generic', :operatingsystemrelease => '16.04', :operatingsystemmajrelease => '16.04', - :os => { :distro => { :codename => 'wheezy' }, :family => 'Debian', :name => 'Debian', :release => { :major => '7', :full => '7.0' } } + :os => { :distro => { :codename => 'wheezy' }, :family => 'Debian', :name => 'Debian', :release => { :major => '7', :full => '7.0' } } } } systemd = true elsif osfamily == 'RedHat' + pre_condition = "class { 'docker': docker_group => 'docker', service_name => 'docker' }" + let(:pre_condition) { pre_condition } let(:facts) { { :architecture => 'x86_64', :osfamily => osfamily, @@ -35,13 +41,22 @@ :os => { :distro => { :codename => 'wheezy' }, :family => osfamily, :name => osfamily, :release => { :major => '7', :full => '7.0' } } } } systemd = true + elsif osfamily == 'generic systemd' + pre_condition = "class { 'docker': docker_group => 'docker', service_name => 'docker', service_provider => systemd, acknowledge_unsupported_os => true }" + let(:pre_condition) { pre_condition } + let(:facts) { { + :osfamily => 'Gentoo', + :operatingsystem => 'Generic', + } } + params.merge!({'service_provider' => 'systemd'}) + systemd = true end - + startscript_or_init = systemd ? startscript : initscript stopscript_or_init = systemd ? stopscript : initscript context 'passing the required params' do - let(:params) { {'command' => 'command', 'image' => 'base'} } + let(:params) { params } it { should compile.with_all_deps } it { should contain_service('docker-sample') } it { should contain_file(initscript).with_content(/#{Regexp.escape(startscript)}/).with_mode('0644') } @@ -55,7 +70,7 @@ end context 'when passing `after` containers' do - let(:params) { {'command' => 'command', 'image' => 'base', 'after' => ['foo', 'bar', 'foo_bar/baz']} } + let(:params) { params.merge({'after' => ['foo', 'bar', 'foo_bar/baz']}) } if (systemd) it { should contain_file(initscript).with_content(/After=(.*\s+)?docker-foo.service/) } it { should contain_file(initscript).with_content(/After=(.*\s+)?docker-bar.service/) } @@ -71,7 +86,7 @@ end context 'when passing `depends` containers' do - let(:params) { {'command' => 'command', 'image' => 'base', 'depends' => ['foo', 'bar', 'foo_bar/baz']} } + let(:params) { params.merge({'depends' => ['foo', 'bar', 'foo_bar/baz']}) } if (systemd) it { should contain_file(initscript).with_content(/After=(.*\s+)?docker-foo.service/) } it { should contain_file(initscript).with_content(/After=(.*\s+)?docker-bar.service/) } @@ -90,7 +105,7 @@ end context 'when passing `depend_services`' do - let(:params) { {'command' => 'command', 'image' => 'base', 'depend_services' => ['foo', 'bar']} } + let(:params) { params.merge({'depend_services' => ['foo', 'bar']}) } if (systemd) it { should contain_file(initscript).with_content(/After=(.*\s+)?foo.service/) } it { should contain_file(initscript).with_content(/After=(.*\s+)?bar.service/) } @@ -98,7 +113,7 @@ it { should contain_file(initscript).with_content(/Requires=(.*\s+)?bar.service/) } context 'with full systemd unit names' do - let(:params) { {'command' => 'command', 'image' => 'base', 'depend_services' => ['foo', 'bar.service', 'baz.target']} } + let(:params) { params.merge({'depend_services' => ['foo', 'bar.service', 'baz.target']}) } it { should contain_file(initscript).with_content(/After=(.*\s+)?foo.service(\s+|$)/) } it { should contain_file(initscript).with_content(/After=(.*\s+)?bar.service(\s+|$)/) } it { should contain_file(initscript).with_content(/After=(.*\s+)?baz.target(\s+|$)/) } @@ -116,12 +131,10 @@ context 'removing containers and volumes' do context 'when trying to remove the volume and not the container on stop' do - let(:params) {{ - 'command' => 'command', - 'image' => 'base', + let(:params) { params.merge({ 'remove_container_on_stop' => false, 'remove_volume_on_stop' => true, - }} + }) } it do expect { should contain_service('docker-sample') @@ -130,12 +143,10 @@ end context 'when trying to remove the volume and not the container on start' do - let(:params) {{ - 'command' => 'command', - 'image' => 'base', + let(:params) { params.merge({ 'remove_container_on_start' => false, 'remove_volume_on_start' => true, - }} + }) } it do expect { should contain_service('docker-sample') @@ -144,235 +155,231 @@ end context 'When restarting an unhealthy container' do - let(:params) {{ - 'command' => 'command', - 'image' => 'base', + let(:params) { params.merge({ 'health_check_cmd' => 'pwd', 'restart_on_unhealthy' => true, 'health_check_interval' => 60, - }} + }) } if (systemd) - it { should contain_file(stopscript).with_content(/\/usr\/bin\/docker stop --time=0 /).with_content(/\/usr\/bin\/docker rm/) } + it { should contain_file(stopscript).with_content(/\/usr\/bin\/docker stop --time=0 /).with_content(/\/usr\/bin\/docker rm/) } it { should contain_file(startscript).with_content(/--health-cmd/) } end end context 'when not removing containers on container start and stop' do - let(:params) {{ - 'command' => 'command', - 'image' => 'base', + let(:params) { params.merge({ 'remove_container_on_start' => false, 'remove_container_on_stop' => false, - }} + }) } it { should_not contain_file(startscript_or_init).with_content(/\/usr\/bin\/docker rm sample/) } end context 'when removing containers on container start' do - let(:params) { {'command' => 'command', 'image' => 'base', 'remove_container_on_start' => true} } + let(:params) { params.merge({'remove_container_on_start' => true}) } it { should contain_file(startscript_or_init).with_content(/\/usr\/bin\/docker rm sample/) } end context 'when removing containers on container stop' do - let(:params) { {'command' => 'command', 'image' => 'base', 'remove_container_on_stop' => true} } + let(:params) { params.merge({'remove_container_on_stop' => true}) } it { should contain_file(stopscript_or_init).with_content(/\/usr\/bin\/docker rm sample/) } end context 'when not removing volumes on container start' do - let(:params) { {'command' => 'command', 'image' => 'base', 'remove_volume_on_start' => false} } + let(:params) { params.merge({'remove_volume_on_start' => false}) } it { should_not contain_file(startscript_or_init).with_content(/\/usr\/bin\/docker rm -v sample/) } end context 'when removing volumes on container start' do - let(:params) { {'command' => 'command', 'image' => 'base', 'remove_volume_on_start' => true} } + let(:params) { params.merge({'remove_volume_on_start' => true}) } it { should contain_file(startscript_or_init).with_content(/\/usr\/bin\/docker rm -v/) } end context 'when not removing volumes on container stop' do - let(:params) { {'command' => 'command', 'image' => 'base', 'remove_volume_on_stop' => false} } + let(:params) { params.merge({'remove_volume_on_stop' => false}) } it { should_not contain_file(stopscript_or_init).with_content(/\/usr\/bin\/docker rm -v sample/) } end context 'when removing volumes on container stop' do - let(:params) { {'command' => 'command', 'image' => 'base', 'remove_volume_on_stop' => true} } + let(:params) { params.merge({'remove_volume_on_stop' => true}) } it { should contain_file(stopscript_or_init).with_content(/\/usr\/bin\/docker rm -v/) } end end context 'with autorestart functionality' do - let(:params) { {'command' => 'command', 'image' => 'base'} } + let(:params) { params } if (systemd) it { should contain_file(initscript).with_content(/Restart=on-failure/) } end end context 'when lxc_conf disables swap' do - let(:params) { {'command' => 'command', 'image' => 'base', 'lxc_conf' => 'lxc.cgroup.memory.memsw.limit_in_bytes=536870912'} } + let(:params) { params.merge({'lxc_conf' => 'lxc.cgroup.memory.memsw.limit_in_bytes=536870912'}) } it { should contain_file(startscript_or_init).with_content(/-lxc-conf=\"lxc.cgroup.memory.memsw.limit_in_bytes=536870912\"/) } end context 'when `use_name` is true' do - let(:params) { {'command' => 'command', 'image' => 'base', 'use_name' => true } } + let(:params) { params.merge({'use_name' => true }) } it { should contain_file(startscript_or_init).with_content(/--name sample /) } end context 'when stopping the service' do - let(:params) { {'command' => 'command', 'image' => 'base', 'running' => false} } + let(:params) { params.merge({'running' => false}) } it { should contain_service('docker-sample').with_ensure(false) } end context 'when passing a memory limit in bytes' do - let(:params) { {'command' => 'command', 'image' => 'base', 'memory_limit' => '1000b'} } + let(:params) { params.merge({'memory_limit' => '1000b'}) } it { should contain_file(startscript_or_init).with_content(/-m 1000b/) } end context 'when passing a cpuset' do - let(:params) { {'command' => 'command', 'image' => 'base', 'cpuset' => '3'} } + let(:params) { params.merge({'cpuset' => '3'}) } it { should contain_file(startscript_or_init).with_content(/--cpuset-cpus=3/) } end context 'when passing a multiple cpu cpuset' do - let(:params) { {'command' => 'command', 'image' => 'base', 'cpuset' => ['0', '3']} } + let(:params) { params.merge({'cpuset' => ['0', '3']}) } it { should contain_file(startscript_or_init).with_content(/--cpuset-cpus=0,3/) } end context 'when not passing a cpuset' do - let(:params) { {'command' => 'command', 'image' => 'base'} } + let(:params) { params } it { should contain_file(startscript_or_init).without_content(/--cpuset-cpus=/) } end context 'when passing a links option' do - let(:params) { {'command' => 'command', 'image' => 'base', 'links' => ['example:one', 'example:two']} } + let(:params) { params.merge({'links' => ['example:one', 'example:two']}) } it { should contain_file(startscript_or_init).with_content(/--link example:one/).with_content(/--link example:two/) } end context 'when passing a hostname' do - let(:params) { {'command' => 'command', 'image' => 'base', 'hostname' => 'example.com'} } + let(:params) { params.merge({'hostname' => 'example.com'}) } it { should contain_file(startscript_or_init).with_content(/-h 'example.com'/) } end context 'when not passing a hostname' do - let(:params) { {'command' => 'command', 'image' => 'base'} } + let(:params) { params } it { should contain_file(startscript_or_init).without_content(/-h ''/) } end context 'when passing a username' do - let(:params) { {'command' => 'command', 'image' => 'base', 'username' => 'bob'} } + let(:params) { params.merge({'username' => 'bob'}) } it { should contain_file(startscript_or_init).with_content(/-u 'bob'/) } end context 'when not passing a username' do - let(:params) { {'command' => 'command', 'image' => 'base'} } + let(:params) { params } it { should contain_file(startscript_or_init).without_content(/-u ''/) } end context 'when passing a port number' do - let(:params) { {'command' => 'command', 'image' => 'base', 'ports' => '4444'} } + let(:params) { params.merge({'ports' => '4444'}) } it { should contain_file(startscript_or_init).with_content(/-p 4444/) } end context 'when passing a port to expose' do - let(:params) { {'command' => 'command', 'image' => 'base', 'expose' => '4666'} } + let(:params) { params.merge({'expose' => '4666'}) } it { should contain_file(startscript_or_init).with_content(/--expose=4666/) } end context 'when passing a label' do - let(:params) { {'command' => 'command', 'image' => 'base', 'labels' => 'key=value'} } + let(:params) { params.merge({'labels' => 'key=value'}) } it { should contain_file(startscript_or_init).with_content(/-l key=value/) } end context 'when passing a hostentry' do - let(:params) { {'command' => 'command', 'image' => 'base', 'hostentries' => 'dummyhost:127.0.0.2'} } + let(:params) { params.merge({'hostentries' => 'dummyhost:127.0.0.2'}) } it { should contain_file(startscript_or_init).with_content(/--add-host dummyhost:127.0.0.2/) } end context 'when connecting to shared data volumes' do - let(:params) { {'command' => 'command', 'image' => 'base', 'volumes_from' => '6446ea52fbc9'} } + let(:params) { params.merge({'volumes_from' => '6446ea52fbc9'}) } it { should contain_file(startscript_or_init).with_content(/--volumes-from 6446ea52fbc9/) } end context 'when connecting to several shared data volumes' do - let(:params) { {'command' => 'command', 'image' => 'base', 'volumes_from' => ['sample-linked-container-1', 'sample-linked-container-2']} } + let(:params) { params.merge({'volumes_from' => ['sample-linked-container-1', 'sample-linked-container-2']}) } it { should contain_file(startscript_or_init).with_content(/--volumes-from sample-linked-container-1/) } it { should contain_file(startscript_or_init).with_content(/--volumes-from sample-linked-container-2/) } end context 'when passing several port numbers' do - let(:params) { {'command' => 'command', 'image' => 'base', 'ports' => ['4444', '4555']} } + let(:params) { params.merge({'ports' => ['4444', '4555']}) } it { should contain_file(startscript_or_init).with_content(/-p 4444/).with_content(/-p 4555/) } end context 'when passing several labels' do - let(:params) { {'command' => 'command', 'image' => 'base', 'labels' => ['key1=value1', 'key2=value2']} } + let(:params) { params.merge({'labels' => ['key1=value1', 'key2=value2']}) } it { should contain_file(startscript_or_init).with_content(/-l key1=value1/).with_content(/-l key2=value2/) } end context 'when passing several ports to expose' do - let(:params) { {'command' => 'command', 'image' => 'base', 'expose' => ['4666', '4777']} } + let(:params) { params.merge({'expose' => ['4666', '4777']}) } it { should contain_file(startscript_or_init).with_content(/--expose=4666/).with_content(/--expose=4777/) } end context 'when passing serveral environment variables' do - let(:params) { {'command' => 'command', 'image' => 'base', 'env' => ['FOO=BAR', 'FOO2=BAR2']} } + let(:params) { params.merge({'env' => ['FOO=BAR', 'FOO2=BAR2']}) } it { should contain_file(startscript_or_init).with_content(/-e "FOO=BAR"/).with_content(/-e "FOO2=BAR2"/) } end context 'when passing an environment variable' do - let(:params) { {'command' => 'command', 'image' => 'base', 'env' => 'FOO=BAR'} } + let(:params) { params.merge({'env' => 'FOO=BAR'}) } it { should contain_file(startscript_or_init).with_content(/-e "FOO=BAR"/) } end context 'when passing serveral environment files' do - let(:params) { {'command' => 'command', 'image' => 'base', 'env_file' => ['/etc/foo.env', '/etc/bar.env']} } + let(:params) { params.merge({'env_file' => ['/etc/foo.env', '/etc/bar.env']}) } it { should contain_file(startscript_or_init).with_content(/--env-file \/etc\/foo.env/).with_content(/--env-file \/etc\/bar.env/) } end context 'when passing an environment file' do - let(:params) { {'command' => 'command', 'image' => 'base', 'env_file' => '/etc/foo.env'} } + let(:params) { params.merge({'env_file' => '/etc/foo.env'}) } it { should contain_file(startscript_or_init).with_content(/--env-file \/etc\/foo.env/) } end context 'when passing serveral dns addresses' do - let(:params) { {'command' => 'command', 'image' => 'base', 'dns' => ['8.8.8.8', '8.8.4.4']} } + let(:params) { params.merge({'dns' => ['8.8.8.8', '8.8.4.4']}) } it { should contain_file(startscript_or_init).with_content(/--dns 8.8.8.8/).with_content(/--dns 8.8.4.4/) } end context 'when passing a dns address' do - let(:params) { {'command' => 'command', 'image' => 'base', 'dns' => '8.8.8.8'} } + let(:params) { params.merge({'dns' => '8.8.8.8'}) } it { should contain_file(startscript_or_init).with_content(/--dns 8.8.8.8/) } end context 'when passing serveral sockets to connect to' do - let(:params) { {'command' => 'command', 'image' => 'base', 'socket_connect' => ['tcp://127.0.0.1:4567', 'tcp://127.0.0.2:4567']} } + let(:params) { params.merge({'socket_connect' => ['tcp://127.0.0.1:4567', 'tcp://127.0.0.2:4567']}) } it { should contain_file(startscript_or_init).with_content(/-H tcp:\/\/127.0.0.1:4567/) } end context 'when passing a socket to connect to' do - let(:params) { {'command' => 'command', 'image' => 'base', 'socket_connect' => 'tcp://127.0.0.1:4567'} } + let(:params) { params.merge({'socket_connect' => 'tcp://127.0.0.1:4567'}) } it { should contain_file(startscript_or_init).with_content(/-H tcp:\/\/127.0.0.1:4567/) } end context 'when passing serveral dns search domains' do - let(:params) { {'command' => 'command', 'image' => 'base', 'dns_search' => ['my.domain.local', 'other-domain.de']} } + let(:params) { params.merge({'dns_search' => ['my.domain.local', 'other-domain.de']}) } it { should contain_file(startscript_or_init).with_content(/--dns-search my.domain.local/).with_content(/--dns-search other-domain.de/) } end context 'when passing a dns search domain' do - let(:params) { {'command' => 'command', 'image' => 'base', 'dns_search' => 'my.domain.local'} } + let(:params) { params.merge({'dns_search' => 'my.domain.local'}) } it { should contain_file(startscript_or_init).with_content(/--dns-search my.domain.local/) } end context 'when disabling network' do - let(:params) { {'command' => 'command', 'image' => 'base', 'disable_network' => true} } + let(:params) { params.merge({'disable_network' => true}) } it { should contain_file(startscript_or_init).with_content(/-n false/) } end context 'when running privileged' do - let(:params) { {'command' => 'command', 'image' => 'base', 'privileged' => true} } + let(:params) { params.merge({'privileged' => true}) } it { should contain_file(startscript_or_init).with_content(/--privileged/) } end context 'should run with correct detached value' do - let(:params) { {'command' => 'command', 'image' => 'base'} } + let(:params) { params } if (systemd) it { should_not contain_file(startscript).with_content(/--detach=true/) } else @@ -381,90 +388,90 @@ end context 'should be able to override detached' do - let(:params) { {'command' => 'command', 'image' => 'base', 'detach' => false} } + let(:params) { params.merge({'detach' => false}) } it { should contain_file(startscript_or_init).without_content(/--detach=true/) } end context 'when running with a tty' do - let(:params) { {'command' => 'command', 'image' => 'base', 'tty' => true} } + let(:params) { params.merge({'tty' => true}) } it { should contain_file(startscript_or_init).with_content(/-t/) } end context 'when running with read-only image' do - let(:params) { {'command' => 'command', 'image' => 'base', 'read_only' => true} } + let(:params) { params.merge({'read_only' => true}) } it { should contain_file(startscript_or_init).with_content(/--read-only=true/) } end context 'when passing serveral extra parameters' do - let(:params) { {'command' => 'command', 'image' => 'base', 'extra_parameters' => ['--rm', '-w /tmp']} } + let(:params) { params.merge({'extra_parameters' => ['--rm', '-w /tmp']}) } it { should contain_file(startscript_or_init).with_content(/--rm/).with_content(/-w \/tmp/) } end context 'when passing an extra parameter' do - let(:params) { {'command' => 'command', 'image' => 'base', 'extra_parameters' => '-c 4'} } + let(:params) { params.merge({'extra_parameters' => '-c 4'}) } it { should contain_file(startscript_or_init).with_content(/-c 4/) } end context 'when passing a data volume' do - let(:params) { {'command' => 'command', 'image' => 'base', 'volumes' => '/var/log'} } + let(:params) { params.merge({'volumes' => '/var/log'}) } it { should contain_file(startscript_or_init).with_content(/-v \/var\/log/) } end context 'when passing serveral data volume' do - let(:params) { {'command' => 'command', 'image' => 'base', 'volumes' => ['/var/lib/couchdb', '/var/log']} } + let(:params) { params.merge({'volumes' => ['/var/lib/couchdb', '/var/log']}) } it { should contain_file(startscript_or_init).with_content(/-v \/var\/lib\/couchdb/) } it { should contain_file(startscript_or_init).with_content(/-v \/var\/log/) } end context 'when using network mode with a single network' do - let(:params) { {'command' => 'command', 'image' => 'nginx', 'net' => 'host'} } + let(:params) { params.merge({'net' => 'host'}) } it { should contain_file(startscript_or_init).with_content(/--net host/) } end context 'when using network mode with multiple networks' do - let(:params) { {'command' => 'command', 'image' => 'nginx', 'net' => ['host','foo']} } + let(:params) { params.merge({'net' => ['host','foo']}) } it { should contain_file(startscript_or_init).with_content(/docker network connect host sample/) } it { should contain_file(startscript_or_init).with_content(/docker network connect foo sample/) } end context 'when `pull_on_start` is true' do - let(:params) { {'command' => 'command', 'image' => 'base', 'pull_on_start' => true } } + let(:params) { params.merge({'pull_on_start' => true}) } it { should contain_file(startscript_or_init).with_content(/docker pull base/) } end context 'when `pull_on_start` is false' do - let(:params) { {'command' => 'command', 'image' => 'base', 'pull_on_start' => false } } + let(:params) { params.merge({'pull_on_start' => false}) } it { should_not contain_file(startscript_or_init).with_content(/docker pull base/) } end context 'when `before_start` is set' do - let(:params) { {'command' => 'command', 'image' => 'base', 'before_start' => "echo before_start" } } + let(:params) { params.merge({'before_start' => "echo before_start"}) } it { should contain_file(startscript_or_init).with_content(/before_start/) } end context 'when `before_start` is not set' do - let(:params) { {'command' => 'command', 'image' => 'base', 'before_start' => false } } + let(:params) { params.merge({'before_start' => false}) } it { should_not contain_file(startscript_or_init).with_content(/before_start/) } end context 'when `before_stop` is set' do - let(:params) { {'command' => 'command', 'image' => 'base', 'before_stop' => "echo before_stop" } } + let(:params) { params.merge({'before_stop' => "echo before_stop"}) } it { should contain_file(stopscript_or_init).with_content(/before_stop/) } end context 'when `before_stop` is not set' do - let(:params) { {'command' => 'command', 'image' => 'base', 'before_stop' => false } } + let(:params) { params.merge({'before_stop' => false}) } it { should_not contain_file(stopscript_or_init).with_content(/before_stop/) } end context 'when `after_create` is set' do - let(:params) { {'command' => 'command', 'image' => 'base', 'after_create' => "echo after_create" } } + let(:params) { params.merge({'after_create' => "echo after_create"}) } it { should contain_file(startscript_or_init).with_content(/after_create/) } end context 'with an title that will not format into a path' do let(:title) { 'this/that' } - let(:params) { {'image' => 'base'} } + let(:params) { params } new_initscript = '/etc/systemd/system/docker-this-that.service' new_startscript = '/usr/local/bin/docker-run-this-that-start.sh' @@ -478,7 +485,7 @@ context 'with manage_service turned off' do let(:title) { 'this/that' } - let(:params) { {'image' => 'base', 'manage_service' => false} } + let(:params) { params.merge({'manage_service' => false}) } new_initscript = '/etc/systemd/system/docker-this-that.service' new_startscript = '/usr/local/bin/docker-run-this-that-start.sh' @@ -492,7 +499,7 @@ context 'with service_prefix set to empty string' do let(:title) { 'this/that' } - let(:params) { {'image' => 'base', 'service_prefix' => ''} } + let(:params) { params.merge({'service_prefix' => ''}) } new_initscript = '/etc/systemd/system/this-that.service' new_startscript = '/usr/local/bin/docker-run-this-that-start.sh' @@ -515,7 +522,7 @@ context 'with title that need sanitisation' do let(:title) { 'this/that_other' } - let(:params) { {'image' => 'base' } } + let(:params) { params } new_initscript = '/etc/systemd/system/docker-this-that_other.service' new_startscript = '/usr/local/bin/docker-run-this-that_other-start.sh' @@ -528,7 +535,7 @@ end context 'with an invalid image name' do - let(:params) { {'command' => 'command', 'image' => 'with spaces', 'running' => 'not a boolean'} } + let(:params) { params.merge({'image' => 'with spaces', 'running' => 'not a boolean'}) } it do expect { should contain_service('docker-sample') @@ -538,7 +545,7 @@ context 'with an invalid running value' do let(:title) { 'with spaces' } - let(:params) { {'command' => 'command', 'image' => 'base', 'running' => 'not a boolean'} } + let(:params) { params.merge({'running' => 'not a boolean'}) } it do expect { should contain_service('docker-sample') @@ -548,7 +555,7 @@ context 'with an invalid memory value' do let(:title) { 'with spaces' } - let(:params) { {'command' => 'command', 'image' => 'base', 'memory' => 'not a number'} } + let(:params) { params.merge({'memory' => 'not a number'}) } it do expect { should contain_service('docker-sample') @@ -558,7 +565,7 @@ context 'with a missing memory unit' do let(:title) { 'with spaces' } - let(:params) { {'command' => 'command', 'image' => 'base', 'memory' => '10240'} } + let(:params) { params.merge({'memory' => '10240'}) } it do expect { should contain_service('docker-sample') @@ -567,7 +574,7 @@ end context 'with restart policy set to no' do - let(:params) { {'restart' => 'no', 'command' => 'command', 'image' => 'base', 'extra_parameters' => '-c 4'} } + let(:params) { params.merge({'restart' => 'no', 'extra_parameters' => '-c 4'}) } it { should contain_exec('run sample with docker') } it { should contain_exec('run sample with docker').with_unless(/sample/) } it { should contain_exec('run sample with docker').with_unless(/inspect/) } @@ -579,7 +586,7 @@ end context 'with restart policy set to always' do - let(:params) { {'restart' => 'always', 'command' => 'command', 'image' => 'base', 'extra_parameters' => '-c 4'} } + let(:params) { params.merge({'restart' => 'always', 'extra_parameters' => '-c 4'}) } it { should contain_exec('run sample with docker') } it { should contain_exec('run sample with docker').with_unless(/sample/) } it { should contain_exec('run sample with docker').with_unless(/inspect/) } @@ -591,7 +598,7 @@ end context 'with restart policy set to on-failure' do - let(:params) { {'restart' => 'on-failure', 'command' => 'command', 'image' => 'base', 'extra_parameters' => '-c 4'} } + let(:params) { params.merge({'restart' => 'on-failure', 'extra_parameters' => '-c 4'}) } it { should contain_exec('run sample with docker') } it { should contain_exec('run sample with docker').with_unless(/sample/) } it { should contain_exec('run sample with docker').with_unless(/inspect/) } @@ -603,7 +610,7 @@ end context 'with restart policy set to on-failure:3' do - let(:params) { {'restart' => 'on-failure:3', 'command' => 'command', 'image' => 'base', 'extra_parameters' => '-c 4'} } + let(:params) { params.merge({'restart' => 'on-failure:3', 'extra_parameters' => '-c 4'}) } it { should contain_exec('run sample with docker') } it { should contain_exec('run sample with docker').with_unless(/sample/) } it { should contain_exec('run sample with docker').with_unless(/inspect/) } @@ -615,65 +622,57 @@ end context 'when `docker_service` is false' do - let(:params) { {'command' => 'command', 'image' => 'base', 'docker_service' => false} } + let(:params) { params.merge({'docker_service' => false}) } it { should compile.with_all_deps } it { should contain_service('docker-sample') } end context 'when `docker_service` is true' do - let(:params) { {'command' => 'command', 'image' => 'base', 'docker_service' => true} } - let(:pre_condition) { - [ "service { 'docker': }", - "class { 'docker': docker_group => 'docker', service_name => 'docker' }" ] } + let(:params) { params.merge({'docker_service' => true}) } + let(:pre_condition) { ["service { 'docker': provider => systemd }", pre_condition] } it { should compile.with_all_deps } it { should contain_service('docker').that_comes_before('Service[docker-sample]') } it { should contain_service('docker').that_notifies('Service[docker-sample]') } end context 'when `docker_service` is true and `restart_service_on_docker_refresh` is false' do - let(:params) { {'command' => 'command', 'image' => 'base', 'docker_service' => true, 'restart_service_on_docker_refresh' => false} } - let(:pre_condition) { - [ "service { 'docker': }", - "class { 'docker': docker_group => 'docker', service_name => 'docker' }" ] } + let(:params) { params.merge({'docker_service' => true, 'restart_service_on_docker_refresh' => false}) } + let(:pre_condition) { ["service { 'docker': provider => systemd }", pre_condition] } it { should compile.with_all_deps } it { should contain_service('docker').that_comes_before('Service[docker-sample]') } end context 'when `docker_service` is `my-docker`' do - let(:params) { {'command' => 'command', 'image' => 'base', 'docker_service' => 'my-docker'} } - let(:pre_condition) { - [ "service { 'my-docker': }", - "class { 'docker': docker_group => 'docker', service_name => 'docker' }" ] } + let(:params) { params.merge({'docker_service' => 'my-docker'}) } + let(:pre_condition) { ["service { 'my-docker': provider => systemd }", pre_condition] } it { should compile.with_all_deps } it { should contain_service('my-docker').that_comes_before('Service[docker-sample]') } it { should contain_service('my-docker').that_notifies('Service[docker-sample]') } end context 'when `docker_service` is `my-docker` and `restart_service_on_docker_refresh` is false' do - let(:params) { {'command' => 'command', 'image' => 'base', 'docker_service' => 'my-docker', 'restart_service_on_docker_refresh' => false} } - let(:pre_condition) { - [ "service { 'my-docker': }", - "class { 'docker': docker_group => 'docker', service_name => 'docker' }" ] } + let(:params) { params.merge({'docker_service' => 'my-docker', 'restart_service_on_docker_refresh' => false}) } + let(:pre_condition) { ["service { 'my-docker': provider => systemd }", pre_condition] } it { should compile.with_all_deps } it { should contain_service('my-docker').that_comes_before('Service[docker-sample]') } end context 'with syslog_identifier' do - let(:params) { {'command' => 'command', 'image' => 'base', 'syslog_identifier' => 'docker-universe' } } + let(:params) { params.merge({'syslog_identifier' => 'docker-universe' }) } if systemd it { should contain_file(initscript).with_content(/^SyslogIdentifier=docker-universe$/) } end end context 'with extra_systemd_parameters' do - let(:params) { {'command' => 'command', 'image' => 'base', 'extra_systemd_parameters' => {'RestartSec' => 5}} } + let(:params) { params.merge({'extra_systemd_parameters' => {'RestartSec' => 5}}) } if (systemd) it { should contain_file(initscript).with_content(/^RestartSec=5$/) } end end context 'with ensure absent' do - let(:params) { {'ensure' => 'absent', 'command' => 'command', 'image' => 'base'} } + let(:params) { params.merge({'ensure' => 'absent'}) } it { should compile.with_all_deps } it { should contain_service('docker-sample').with_ensure(false) } it { should contain_exec("remove container docker-sample").with_command('docker rm -v sample') }