diff --git a/manifests/run.pp b/manifests/run.pp index 1d3dcb9..82406f0 100755 --- a/manifests/run.pp +++ b/manifests/run.pp @@ -1,526 +1,526 @@ # == Define: docker:run # # A define which manages a running docker container. # # == Parameters # # [*restart*] # Sets a restart policy on the docker run. # Note: If set, puppet will NOT setup an init script to manage, instead # it will do a raw docker run command using a CID file to track the container # ID. # # If you want a normal named container with an init script and a restart policy # you must use the extra_parameters feature and pass it in like this: # # extra_parameters => ['--restart=always'] # # However, if your system is using sytemd this restart policy will be # ineffective because the ExecStop commands will run which will cause # docker to stop restarting it. In this case you should use the # systemd_restart option to specify the policy you want. # # This will allow the docker container to be restarted if it dies, without # puppet help. # # [*service_prefix*] # (optional) The name to prefix the startup script with and the Puppet # service resource title with. Default: 'docker-' # # [*restart_service*] # (optional) Whether or not to restart the service if the the generated init # script changes. Default: true # # [*restart_service_on_docker_refresh*] # Whether or not to restart the service if the docker service is restarted. # Only has effect if the docker_service parameter is set. # Default: true # # [*manage_service*] # (optional) Whether or not to create a puppet Service resource for the init # script. Disabling this may be useful if integrating with existing modules. # Default: true # # [*docker_service*] # (optional) If (and how) the Docker service itself is managed by Puppet # true -> Service['docker'] # false -> no Service dependency # anything else -> Service[docker_service] # Default: false # # [*health_check_cmd*] # (optional) Specifies the command to execute to check that the container is healthy using the docker health check functionality. # Default: undef # # [*health_check_interval*] # (optional) Specifies the interval that the health check command will execute in seconds. # Default: undef # # [*restart_on_unhealthy*] # (optional) Checks the health status of Docker container and if it is unhealthy the service will be restarted. # The health_check_cmd parameter must be set to true to use this functionality. # Default: undef # # [*extra_parameters*] # An array of additional command line arguments to pass to the `docker run` # command. Useful for adding additional new or experimental options that the # module does not yet support. # # [*systemd_restart*] # (optional) If the container is to be managed by a systemd unit file set the # Restart option on the unit file. Can be any valid value for this systemd # configuration. Most commonly used are on-failure or always. # Default: on-failure # # [*custom_unless*] # (optional) Specify an additional unless for the Docker run command when using restart. # Default: undef # # [*after_create*] # (optional) Specifies the command to execute after container is created but before it is started. # Default: undef # define docker::run( Optional[Pattern[/^[\S]*$/]] $image, Optional[Pattern[/^present$|^absent$/]] $ensure = 'present', Optional[String] $command = undef, Optional[Pattern[/^[\d]*(b|k|m|g)$/]] $memory_limit = '0b', Variant[String,Array,Undef] $cpuset = [], Variant[String,Array,Undef] $ports = [], Variant[String,Array,Undef] $labels = [], Variant[String,Array,Undef] $expose = [], Variant[String,Array,Undef] $volumes = [], Variant[String,Array,Undef] $links = [], Optional[Boolean] $use_name = false, Optional[Boolean] $running = true, Variant[String,Array,Undef] $volumes_from = [], Optional[String] $net = 'bridge', Variant[String,Boolean] $username = false, Variant[String,Boolean] $hostname = false, Variant[String,Array,Undef] $env = [], Variant[String,Array,Undef] $env_file = [], Variant[String,Array,Undef] $dns = [], Variant[String,Array,Undef] $dns_search = [], Variant[String,Array,Undef] $lxc_conf = [], Optional[String] $service_prefix = 'docker-', Optional[Boolean] $restart_service = true, Optional[Boolean] $restart_service_on_docker_refresh = true, Optional[Boolean] $manage_service = true, Variant[String,Boolean] $docker_service = false, Optional[Boolean] $disable_network = false, Optional[Boolean] $privileged = false, Optional[Boolean] $detach = undef, Variant[String,Array[String],Undef] $extra_parameters = undef, Optional[String] $systemd_restart = 'on-failure', Variant[String,Hash,Undef] $extra_systemd_parameters = {}, Optional[Boolean] $pull_on_start = false, Variant[String,Array,Undef] $after = [], Variant[String,Array,Undef] $after_service = [], Variant[String,Array,Undef] $depends = [], Variant[String,Array,Undef] $depend_services = [], Optional[Boolean] $tty = false, Variant[String,Array,Undef] $socket_connect = [], Variant[String,Array,Undef] $hostentries = [], Optional[String] $restart = undef, Variant[String,Boolean] $before_start = false, Variant[String,Boolean] $before_stop = false, Optional[String] $after_create = undef, Optional[Boolean] $remove_container_on_start = true, Optional[Boolean] $remove_container_on_stop = true, Optional[Boolean] $remove_volume_on_start = false, Optional[Boolean] $remove_volume_on_stop = false, Optional[Integer] $stop_wait_time = 0, Optional[String] $syslog_identifier = undef, Optional[Boolean] $read_only = false, Optional[String] $health_check_cmd = undef, Optional[Boolean] $restart_on_unhealthy = false, Optional[Integer] $health_check_interval = undef, Variant[String,Array,Undef] $custom_unless = [], ) { include docker::params if ($socket_connect != []) { $sockopts = join(any2array($socket_connect), ',') $docker_command = "${docker::params::docker_command} -H ${sockopts}" }else { $docker_command = $docker::params::docker_command } $service_name = $docker::service_name $docker_group = $docker::docker_group if $restart { assert_type(Pattern[/^(no|always|unless-stopped|on-failure)|^on-failure:[\d]+$/], $restart) } if ($remove_volume_on_start and !$remove_container_on_start) { fail translate(("In order to remove the volume on start for ${title} you need to also remove the container")) } if ($remove_volume_on_stop and !$remove_container_on_stop) { fail translate(("In order to remove the volume on stop for ${title} you need to also remove the container")) } if $use_name { notify { "docker use_name warning: ${title}": message => 'The use_name parameter is no-longer required and will be removed in a future release', withpath => true, } } if $systemd_restart { assert_type(Pattern[/^(no|always|on-success|on-failure|on-abnormal|on-abort|on-watchdog)$/], $systemd_restart) } if $detach == undef { $valid_detach = $docker::params::detach_service_in_init } else { $valid_detach = $detach } $extra_parameters_array = any2array($extra_parameters) $after_array = any2array($after) $depends_array = any2array($depends) $depend_services_array = any2array($depend_services) $docker_run_flags = docker_run_flags({ cpuset => any2array($cpuset), disable_network => $disable_network, dns => any2array($dns), dns_search => any2array($dns_search), env => any2array($env), env_file => any2array($env_file), expose => any2array($expose), extra_params => any2array($extra_parameters), hostentries => any2array($hostentries), hostname => $hostname, links => any2array($links), lxc_conf => any2array($lxc_conf), memory_limit => $memory_limit, net => $net, ports => any2array($ports), labels => any2array($labels), privileged => $privileged, socket_connect => any2array($socket_connect), tty => $tty, username => $username, volumes => any2array($volumes), volumes_from => any2array($volumes_from), read_only => $read_only, health_check_cmd => $health_check_cmd, restart_on_unhealthy => $restart_on_unhealthy, health_check_interval => $health_check_interval, osfamily => $::osfamily, }) $sanitised_title = docker::sanitised_name($title) if empty($depends_array) { $sanitised_depends_array = [] } else { $sanitised_depends_array = docker::sanitised_name($depends_array) } if empty($after_array) { $sanitised_after_array = [] } else { $sanitised_after_array = docker::sanitised_name($after_array) } if $::osfamily == 'windows' { $exec_environment = "PATH=${::docker_program_files_path}/Docker/;${::docker_systemroot}/System32/" $exec_timeout = 3000 $exec_path = ["${::docker_program_files_path}/Docker/"] $exec_provider = 'powershell' $cidfile = "${::docker_user_temp_path}/${service_prefix}${sanitised_title}.cid" # lint:ignore:140chars $restart_check = "${docker_command} inspect ${sanitised_title} -f '{{ if eq \\\"unhealthy\\\" .State.Health.Status }} {{ .Name }}{{ end }}' | findstr ${sanitised_title}" # lint:endignore } else { $exec_environment = 'HOME=/root' $exec_path = ['/bin', '/usr/bin'] $exec_timeout = 0 $exec_provider = undef $cidfile = "/var/run/${service_prefix}${sanitised_title}.cid" # lint:ignore:140chars $restart_check = "${docker_command} inspect ${sanitised_title} -f '{{ if eq \"unhealthy\" .State.Health.Status }} {{ .Name }}{{ end }}' | grep ${sanitised_title}" # lint:endignore } if $restart_on_unhealthy { exec { "Restart unhealthy container ${title} with docker": command => "${docker_command} restart ${sanitised_title}", onlyif => $restart_check, environment => $exec_environment, path => $exec_path, provider => $exec_provider, timeout => $exec_timeout } } if $restart { if $ensure == 'absent' { exec { "stop ${title} with docker": command => "${docker_command} stop --time=${stop_wait_time} ${sanitised_title}", onlyif => "${docker_command} inspect ${sanitised_title}", environment => $exec_environment, path => $exec_path, provider => $exec_provider, timeout => $exec_timeout } exec { "remove ${title} with docker": command => "${docker_command} rm -v ${sanitised_title}", onlyif => "${docker_command} inspect ${sanitised_title}", environment => $exec_environment, path => $exec_path, provider => $exec_provider, timeout => $exec_timeout } file { $cidfile: ensure => absent, } } else { $run_with_docker_command = [ "${docker_command} run -d ${docker_run_flags}", "--name ${sanitised_title} --cidfile=${cidfile}", "--restart=\"${restart}\" ${image} ${command}", ] $inspect = ["${docker_command} inspect ${sanitised_title}"] if $custom_unless { $exec_unless = concat($custom_unless, $inspect) } else { $exec_unless = $inspect } exec { "run ${title} with docker": command => join($run_with_docker_command, ' '), unless => $exec_unless, environment => $exec_environment, path => $exec_path, provider => $exec_provider, timeout => $exec_timeout } if $running == false { exec { "stop ${title} with docker": command => "${docker_command} stop --time=${stop_wait_time} ${sanitised_title}", unless => "${docker_command} inspect ${sanitised_title} -f \"{{ if (.State.Running) }} {{ nil }}{{ end }}\"", environment => $exec_environment, path => $exec_path, provider => $exec_provider, timeout => $exec_timeout } } else { exec { "start ${title} with docker": command => "${docker_command} start ${sanitised_title}", onlyif => "${docker_command} inspect ${sanitised_title} -f \"{{ if (.State.Running) }} {{ nil }}{{ end }}\"", environment => $exec_environment, path => $exec_path, provider => $exec_provider, timeout => $exec_timeout } } } } else { $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 { 'systemd': { $initscript = "/etc/systemd/system/${service_prefix}${sanitised_title}.service" $startscript = "/usr/local/bin/docker-run-${sanitised_title}-start.sh" $stopscript = "/usr/local/bin/docker-run-${sanitised_title}-stop.sh" $startstop_template = 'docker/usr/local/bin/docker-run.sh.epp' $init_template = 'docker/etc/systemd/system/docker-run.erb' - $mode = '0640' + $mode = '0644' } 'upstart': { $initscript = "/etc/init.d/${service_prefix}${sanitised_title}" $init_template = 'docker/etc/init.d/docker-run.erb' $mode = '0750' $startscript = undef $stopscript = undef $starstop_template = undef } default: { if $::osfamily != 'windows' { fail translate(('Docker needs a Debian or RedHat based system.')) } elsif $ensure == 'present' { fail translate(('Restart parameter is required for Windows')) } } } if $syslog_identifier { $_syslog_identifier = $syslog_identifier } else { $_syslog_identifier = "${service_prefix}${sanitised_title}" } if $ensure == 'absent' { if $::osfamily == 'windows'{ exec { "stop container ${service_prefix}${sanitised_title}": command => "${docker_command} stop --time=${stop_wait_time} ${sanitised_title}", onlyif => "${docker_command} inspect ${sanitised_title}", environment => $exec_environment, path => $exec_path, provider => $exec_provider, timeout => $exec_timeout, notify => Exec["remove container ${service_prefix}${sanitised_title}"] } } else { service { "${service_prefix}${sanitised_title}": ensure => false, enable => false, hasstatus => $docker::params::service_hasstatus, } } exec { "remove container ${service_prefix}${sanitised_title}": command => "${docker_command} rm -v ${sanitised_title}", onlyif => "${docker_command} inspect ${sanitised_title}", environment => $exec_environment, path => $exec_path, refreshonly => true, provider => $exec_provider, timeout => $exec_timeout } if $::osfamily != 'windows' { file { "/etc/systemd/system/${service_prefix}${sanitised_title}.service": ensure => absent, path => "/etc/systemd/system/${service_prefix}${sanitised_title}.service", } if ($startscript) { file { $startscript: ensure => absent } } if ($stopscript) { file { $stopscript: ensure => absent } } } else { file { $cidfile: ensure => absent, } } } else { if ($startscript) { file { $startscript: ensure => present, content => epp($startstop_template, {'script' => $docker_run_inline_start}), owner => 'root', group => $docker_group, mode => '0770' } } if ($stopscript) { file { $stopscript: ensure => present, content => epp($startstop_template, {'script' => $docker_run_inline_stop}), owner => 'root', group => $docker_group, mode => '0770' } } file { $initscript: ensure => present, content => template($init_template), owner => 'root', group => $docker_group, mode => $mode, } if $manage_service { if $running == false { service { "${service_prefix}${sanitised_title}": ensure => $running, enable => false, hasstatus => $docker::params::service_hasstatus, require => File[$initscript], } } else { # Transition help from moving from CID based container detection to # Name-based container detection. See #222 for context. # This code should be considered temporary until most people have # transitioned. - 2015-04-15 if $initscript == "/etc/init.d/${service_prefix}${sanitised_title}" { # This exec sequence will ensure the old-style CID container is stopped # before we replace the init script with the new-style. $transition_onlyif = [ "/usr/bin/test -f /var/run/docker-${sanitised_title}.cid &&", "/usr/bin/test -f /etc/init.d/${service_prefix}${sanitised_title}", ] exec { "/bin/sh /etc/init.d/${service_prefix}${sanitised_title} stop": onlyif => join($transition_onlyif, ' '), require => [], } -> file { "/var/run/${service_prefix}${sanitised_title}.cid": ensure => absent, } -> File[$initscript] } service { "${service_prefix}${sanitised_title}": ensure => $running, enable => true, provider => $docker::params::service_provider, hasstatus => $docker::params::service_hasstatus, require => File[$initscript], } } if $docker_service { if $docker_service == true { Service['docker'] -> Service["${service_prefix}${sanitised_title}"] if $restart_service_on_docker_refresh == true { Service['docker'] ~> Service["${service_prefix}${sanitised_title}"] } } else { Service[$docker_service] -> Service["${service_prefix}${sanitised_title}"] if $restart_service_on_docker_refresh == true { Service[$docker_service] ~> Service["${service_prefix}${sanitised_title}"] } } } } if $docker::params::service_provider == 'systemd' { exec { "docker-${sanitised_title}-systemd-reload": path => ['/bin/', '/sbin/', '/usr/bin/', '/usr/sbin/'], command => 'systemctl daemon-reload', refreshonly => true, require => [File[$initscript],File[$startscript],File[$stopscript]], subscribe => [File[$initscript],File[$startscript],File[$stopscript]] } Exec["docker-${sanitised_title}-systemd-reload"] -> Service<| title == "${service_prefix}${sanitised_title}" |> } if $restart_service { if $startscript or $stopscript { [File[$initscript],File[$startscript],File[$stopscript]] ~> Service<| title == "${service_prefix}${sanitised_title}" |> } else { [File[$initscript]] ~> Service<| title == "${service_prefix}${sanitised_title}" |> } } else { if $startscript or $stopscript { [File[$initscript],File[$startscript],File[$stopscript]] -> Service<| title == "${service_prefix}${sanitised_title}" |> } else { [File[$initscript]] -> Service<| title == "${service_prefix}${sanitised_title}" |> } } } } } diff --git a/spec/defines/run_spec.rb b/spec/defines/run_spec.rb index d3aa466..1d86c3a 100755 --- a/spec/defines/run_spec.rb +++ b/spec/defines/run_spec.rb @@ -1,680 +1,680 @@ require 'spec_helper' ['Debian', 'RedHat'].each do |osfamily| describe 'docker::run', :type => :define do let(:title) { 'sample' } let(:pre_condition) { "class { 'docker': docker_group => 'docker', service_name => 'docker' }" } context "on #{osfamily}" do initscript = '/etc/systemd/system/docker-sample.service' startscript = "/usr/local/bin/docker-run-sample-start.sh" stopscript = "/usr/local/bin/docker-run-sample-stop.sh" if osfamily == 'Debian' let(:facts) { { :architecture => 'amd64', :osfamily => 'Debian', :operatingsystem => 'Ubuntu', :lsbdistid => 'Ubuntu', :lsbdistcodename => 'xenial', :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' } } } } systemd = true elsif osfamily == 'RedHat' let(:facts) { { :architecture => 'x86_64', :osfamily => osfamily, :operatingsystem => 'RedHat', :lsbdistcodename => 'xenial', :operatingsystemrelease => '7.2', :operatingsystemmajrelease => '7', :kernelversion => '3.10.0', :os => { :distro => { :codename => 'wheezy' }, :family => osfamily, :name => osfamily, :release => { :major => '7', :full => '7.0' } } } } 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'} } it { should compile.with_all_deps } it { should contain_service('docker-sample') } - it { should contain_file(initscript).with_content(/#{Regexp.escape(startscript)}/) } + it { should contain_file(initscript).with_content(/#{Regexp.escape(startscript)}/).with_mode('0644') } it { should contain_file(initscript).with_content(/#{Regexp.escape(stopscript)}/) } it { should contain_file(startscript_or_init).with_content(/docker start/).with_content(/command/).with_content(/base/)} if systemd it { should contain_file(initscript).with_content(/^SyslogIdentifier=docker-sample$/) } end end context 'when passing `after` containers' do let(:params) { {'command' => 'command', 'image' => 'base', '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/) } it { should contain_file(initscript).with_content(/After=(.*\s+)?docker-foo_bar-baz.service/) } it { should contain_file(initscript).with_content(/Wants=(.*\s+)?docker-foo.service/) } it { should contain_file(initscript).with_content(/Wants=(.*\s+)?docker-bar.service/) } it { should contain_file(initscript).with_content(/Wants=(.*\s+)?docker-foo_bar-baz.service/) } else it { should contain_file(initscript).with_content(/Required-Start:.*\s+docker-foo/) } it { should contain_file(initscript).with_content(/Required-Start:.*\s+docker-bar/) } it { should contain_file(initscript).with_content(/Required-Start:.*\s+docker-foo_bar-baz/) } end end context 'when passing `depends` containers' do let(:params) { {'command' => 'command', 'image' => 'base', '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/) } it { should contain_file(initscript).with_content(/After=(.*\s+)?docker-foo_bar-baz.service/) } it { should contain_file(initscript).with_content(/Requires=(.*\s+)?docker-foo.service/) } it { should contain_file(initscript).with_content(/Requires=(.*\s+)?docker-bar.service/) } it { should contain_file(initscript).with_content(/Requires=(.*\s+)?docker-foo_bar-baz.service/) } else it { should contain_file(initscript).with_content(/Required-Start:.*\s+docker-foo/) } it { should contain_file(initscript).with_content(/Required-Start:.*\s+docker-bar/) } it { should contain_file(initscript).with_content(/Required-Start:.*\s+docker-foo_bar-baz/) } it { should contain_file(initscript).with_content(/Required-Stop:.*\s+docker-foo/) } it { should contain_file(initscript).with_content(/Required-Stop:.*\s+docker-bar/) } it { should contain_file(initscript).with_content(/Required-Stop:.*\s+docker-foo_bar-baz/) } end end context 'when passing `depend_services`' do let(:params) { {'command' => 'command', 'image' => 'base', '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/) } it { should contain_file(initscript).with_content(/Requires=(.*\s+)?foo.service/) } 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']} } 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+|$)/) } it { should contain_file(initscript).with_content(/Requires=(.*\s+)?foo.service(\s+|$)/) } it { should contain_file(initscript).with_content(/Requires=(.*\s+)?bar.service(\s+|$)/) } it { should contain_file(initscript).with_content(/Requires=(.*\s+)?baz.target(\s+|$)/) } end else it { should contain_file(initscript).with_content(/Required-Start:.*\s+foo/) } it { should contain_file(initscript).with_content(/Required-Start:.*\s+bar/) } it { should contain_file(initscript).with_content(/Required-Stop:.*\s+foo/) } it { should contain_file(initscript).with_content(/Required-Stop:.*\s+bar/) } end end 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', 'remove_container_on_stop' => false, 'remove_volume_on_stop' => true, }} it do expect { should contain_service('docker-sample') }.to raise_error(Puppet::Error) end end context 'when trying to remove the volume and not the container on start' do let(:params) {{ 'command' => 'command', 'image' => 'base', 'remove_container_on_start' => false, 'remove_volume_on_start' => true, }} it do expect { should contain_service('docker-sample') }.to raise_error(Puppet::Error) end end context 'When restarting an unhealthy container' do let(:params) {{ 'command' => 'command', 'image' => 'base', '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(startscript).with_content(/--health-cmd/) } end end context 'when not removing containers on container start and stop' do let(:params) {{ 'command' => 'command', 'image' => 'base', '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} } 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} } 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} } 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} } 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} } 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} } 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'} } 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'} } 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 } } 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} } 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'} } 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'} } 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']} } 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'} } 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']} } 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'} } 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'} } 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'} } 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'} } 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'} } 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'} } 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'} } 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'} } 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'} } 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']} } 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']} } 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']} } 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']} } 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']} } 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'} } 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']} } 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'} } 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']} } 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'} } 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']} } 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'} } 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']} } 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'} } 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} } 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} } 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'} } if (systemd) it { should_not contain_file(startscript).with_content(/--detach=true/) } else it { should contain_file(initscript).with_content(/--detach=true/) } end end context 'should be able to override detached' do let(:params) { {'command' => 'command', 'image' => 'base', '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} } 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} } 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']} } 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'} } 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'} } 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']} } 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' do let(:params) { {'command' => 'command', 'image' => 'nginx', 'net' => 'host'} } it { should contain_file(startscript_or_init).with_content(/--net host/) } end context 'when `pull_on_start` is true' do let(:params) { {'command' => 'command', 'image' => 'base', '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 } } 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" } } 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 } } 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" } } 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 } } 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" } } 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'} } new_initscript = '/etc/systemd/system/docker-this-that.service' new_startscript = '/usr/local/bin/docker-run-this-that-start.sh' new_stopscript = '/usr/local/bin/docker-run-this-that-stop.sh' it { should contain_service('docker-this-that') } it { should contain_file(new_initscript) } it { should contain_file(new_startscript) } it { should contain_file(new_stopscript) } end context 'with manage_service turned off' do let(:title) { 'this/that' } let(:params) { {'image' => 'base', 'manage_service' => false} } new_initscript = '/etc/systemd/system/docker-this-that.service' new_startscript = '/usr/local/bin/docker-run-this-that-start.sh' new_stopscript = '/usr/local/bin/docker-run-this-that-stop.sh' it { should_not contain_service('docker-this-that') } it { should contain_file(new_initscript) } it { should contain_file(new_startscript) } it { should contain_file(new_stopscript) } end context 'with service_prefix set to empty string' do let(:title) { 'this/that' } let(:params) { {'image' => 'base', 'service_prefix' => ''} } new_initscript = '/etc/systemd/system/this-that.service' new_startscript = '/usr/local/bin/docker-run-this-that-start.sh' new_stopscript = '/usr/local/bin/docker-run-this-that-stop.sh' it { should contain_service('this-that') } it { should contain_file(new_initscript) } it { should contain_file(new_startscript) } it { should contain_file(new_stopscript) } end context 'with an invalid title' do let(:title) { 'with spaces' } it do expect { should contain_service('docker-sample') }.to raise_error(Puppet::Error) end end context 'with title that need sanitisation' do let(:title) { 'this/that_other' } let(:params) { {'image' => 'base' } } new_initscript = '/etc/systemd/system/docker-this-that_other.service' new_startscript = '/usr/local/bin/docker-run-this-that_other-start.sh' new_stopscript = '/usr/local/bin/docker-run-this-that_other-stop.sh' it { should contain_service('docker-this-that_other') } it { should contain_file(new_initscript) } it { should contain_file(new_startscript) } it { should contain_file(new_stopscript) } end context 'with an invalid image name' do let(:params) { {'command' => 'command', 'image' => 'with spaces', 'running' => 'not a boolean'} } it do expect { should contain_service('docker-sample') }.to raise_error(Puppet::Error) end end context 'with an invalid running value' do let(:title) { 'with spaces' } let(:params) { {'command' => 'command', 'image' => 'base', 'running' => 'not a boolean'} } it do expect { should contain_service('docker-sample') }.to raise_error(Puppet::Error) end end context 'with an invalid memory value' do let(:title) { 'with spaces' } let(:params) { {'command' => 'command', 'image' => 'base', 'memory' => 'not a number'} } it do expect { should contain_service('docker-sample') }.to raise_error(Puppet::Error) end end context 'with a missing memory unit' do let(:title) { 'with spaces' } let(:params) { {'command' => 'command', 'image' => 'base', 'memory' => '10240'} } it do expect { should contain_service('docker-sample') }.to raise_error(Puppet::Error) end end context 'with restart policy set to no' do let(:params) { {'restart' => 'no', 'command' => 'command', 'image' => 'base', '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/) } it { should contain_exec('run sample with docker').with_command(/--cidfile=\/var\/run\/docker-sample.cid/) } it { should contain_exec('run sample with docker').with_command(/-c 4/) } it { should contain_exec('run sample with docker').with_command(/--restart="no"/) } it { should contain_exec('run sample with docker').with_command(/base command/) } it { should contain_exec('run sample with docker').with_timeout(0) } end context 'with restart policy set to always' do let(:params) { {'restart' => 'always', 'command' => 'command', 'image' => 'base', '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/) } it { should contain_exec('run sample with docker').with_command(/--cidfile=\/var\/run\/docker-sample.cid/) } it { should contain_exec('run sample with docker').with_command(/-c 4/) } it { should contain_exec('run sample with docker').with_command(/--restart="always"/) } it { should contain_exec('run sample with docker').with_command(/base command/) } it { should contain_exec('run sample with docker').with_timeout(0) } end context 'with restart policy set to on-failure' do let(:params) { {'restart' => 'on-failure', 'command' => 'command', 'image' => 'base', '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/) } it { should contain_exec('run sample with docker').with_command(/--cidfile=\/var\/run\/docker-sample.cid/) } it { should contain_exec('run sample with docker').with_command(/-c 4/) } it { should contain_exec('run sample with docker').with_command(/--restart="on-failure"/) } it { should contain_exec('run sample with docker').with_command(/base command/) } it { should contain_exec('run sample with docker').with_timeout(0) } 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'} } 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/) } it { should contain_exec('run sample with docker').with_command(/--cidfile=\/var\/run\/docker-sample.cid/) } it { should contain_exec('run sample with docker').with_command(/-c 4/) } it { should contain_exec('run sample with docker').with_command(/--restart="on-failure:3"/) } it { should contain_exec('run sample with docker').with_command(/base command/) } it { should contain_exec('run sample with docker').with_timeout(0) } end context 'when `docker_service` is false' do let(:params) { {'command' => 'command', 'image' => 'base', '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' }" ] } 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' }" ] } 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' }" ] } 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' }" ] } 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' } } 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}} } 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'} } 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') } it { should_not contain_file('docker-sample.service')} end end end end