<commit_before>require 'spec_helper'

['Debian', 'RedHat', 'generic systemd'].each do |osfamily|
  describe 'docker::run', type: :define do
    let(:title) { 'sample' }

    params = { 'command' => 'command', 'image' => 'base' }

    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'
        pre_condition = "class { 'docker': docker_group => 'docker', service_name => 'docker' }"
        let(:pre_condition) { pre_condition }
        let(:facts) do
          {
            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' } },
          }
        end

        systemd = true
      elsif osfamily == 'RedHat'
        pre_condition = "class { 'docker': docker_group => 'docker', service_name => 'docker' }"
        let(:pre_condition) { pre_condition }
        let(:facts) do
          {
            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' } },
          }
        end

        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) do
          {
            osfamily: 'Gentoo',
            operatingsystem: 'Generic',
          }
        end

        params['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) { params }

        it { is_expected.to compile.with_all_deps }
        it { is_expected.to contain_service('docker-sample') }
        it { is_expected.to contain_file(initscript).with_content(%r{#{Regexp.escape(startscript)}}).with_mode('0644') }
        it { is_expected.to contain_file(initscript).with_content(%r{#{Regexp.escape(stopscript)}}) }
        it { is_expected.to contain_file(startscript_or_init).with_content(%r{docker start}).with_content(%r{command}).with_content(%r{base}) }

        if systemd
          it { is_expected.to contain_file(initscript).with_content(%r{^SyslogIdentifier=docker-sample$}) }
        end
      end

      context 'when passing `after` containers' do
        let(:params) { params.merge('after' => ['foo', 'bar', 'foo_bar/baz']) }

        if systemd
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?docker-foo.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?docker-bar.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?docker-foo_bar-baz.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Wants=(.*\s+)?docker-foo.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Wants=(.*\s+)?docker-bar.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Wants=(.*\s+)?docker-foo_bar-baz.service}) }
        else
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+docker-foo}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+docker-bar}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+docker-foo_bar-baz}) }
        end
      end

      context 'when passing `depends` containers' do
        let(:params) { params.merge('depends' => ['foo', 'bar', 'foo_bar/baz']) }

        if systemd
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?docker-foo.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?docker-bar.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?docker-foo_bar-baz.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?docker-foo.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?docker-bar.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?docker-foo_bar-baz.service}) }
        else
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+docker-foo}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+docker-bar}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+docker-foo_bar-baz}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Stop:.*\s+docker-foo}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Stop:.*\s+docker-bar}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Stop:.*\s+docker-foo_bar-baz}) }
        end
      end

      context 'when passing `depend_services`' do
        let(:params) { params.merge('depend_services' => ['foo', 'bar']) }

        if systemd
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?foo.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?bar.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?foo.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?bar.service}) }

          context 'with full systemd unit names' do
            let(:params) { params.merge('depend_services' => ['foo', 'bar.service', 'baz.target']) }

            it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?foo.service(\s+|$)}) }
            it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?bar.service(\s+|$)}) }
            it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?baz.target(\s+|$)}) }
            it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?foo.service(\s+|$)}) }
            it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?bar.service(\s+|$)}) }
            it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?baz.target(\s+|$)}) }
          end
        else
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+foo}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+bar}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Stop:.*\s+foo}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Stop:.*\s+bar}) }
        end
      end<commit_msg>Modify RSpecs to test WantedBy field in systemd units<commit_after>require 'spec_helper'

['Debian', 'RedHat', 'generic systemd'].each do |osfamily|
  describe 'docker::run', type: :define do
    let(:title) { 'sample' }

    params = { 'command' => 'command', 'image' => 'base' }

    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'
        pre_condition = "class { 'docker': docker_group => 'docker', service_name => 'docker' }"
        let(:pre_condition) { pre_condition }
        let(:facts) do
          {
            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' } },
          }
        end

        systemd = true
      elsif osfamily == 'RedHat'
        pre_condition = "class { 'docker': docker_group => 'docker', service_name => 'docker' }"
        let(:pre_condition) { pre_condition }
        let(:facts) do
          {
            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' } },
          }
        end

        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) do
          {
            osfamily: 'Gentoo',
            operatingsystem: 'Generic',
          }
        end

        params['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) { params }

        it { is_expected.to compile.with_all_deps }
        it { is_expected.to contain_service('docker-sample') }
        it { is_expected.to contain_file(initscript).with_content(%r{#{Regexp.escape(startscript)}}).with_mode('0644') }
        it { is_expected.to contain_file(initscript).with_content(%r{#{Regexp.escape(stopscript)}}) }
        it { is_expected.to contain_file(startscript_or_init).with_content(%r{docker start}).with_content(%r{command}).with_content(%r{base}) }

        if systemd
          it { is_expected.to contain_file(initscript).with_content(%r{^SyslogIdentifier=docker-sample$}) }
          it { is_expected.to contain_file(initscript).with_content(%r{^WantedBy=docker.service$}) }
        end
      end

      context 'when passing `after` containers' do
        let(:params) { params.merge('after' => ['foo', 'bar', 'foo_bar/baz']) }

        if systemd
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?docker-foo.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?docker-bar.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?docker-foo_bar-baz.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Wants=(.*\s+)?docker-foo.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Wants=(.*\s+)?docker-bar.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Wants=(.*\s+)?docker-foo_bar-baz.service}) }
        else
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+docker-foo}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+docker-bar}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+docker-foo_bar-baz}) }
        end
      end

      context 'when passing `depends` containers' do
        let(:params) { params.merge('depends' => ['foo', 'bar', 'foo_bar/baz']) }

        if systemd
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?docker-foo.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?docker-bar.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?docker-foo_bar-baz.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?docker-foo.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?docker-bar.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?docker-foo_bar-baz.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{WantedBy=docker.service}) }
        else
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+docker-foo}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+docker-bar}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+docker-foo_bar-baz}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Stop:.*\s+docker-foo}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Stop:.*\s+docker-bar}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Stop:.*\s+docker-foo_bar-baz}) }
        end
      end

      context 'when passing `depend_services`' do
        let(:params) { params.merge('depend_services' => ['foo', 'bar']) }

        if systemd
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?foo.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?bar.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?foo.service}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?bar.service}) }

          context 'with full systemd unit names' do
            let(:params) { params.merge('depend_services' => ['foo', 'bar.service', 'baz.target']) }

            it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?foo.service(\s+|$)}) }
            it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?bar.service(\s+|$)}) }
            it { is_expected.to contain_file(initscript).with_content(%r{After=(.*\s+)?baz.target(\s+|$)}) }
            it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?foo.service(\s+|$)}) }
            it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?bar.service(\s+|$)}) }
            it { is_expected.to contain_file(initscript).with_content(%r{Requires=(.*\s+)?baz.target(\s+|$)}) }
            it { is_expected.to contain_file(initscript).with_content(%r{WantedBy=docker.service}) }
          end
        else
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+foo}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Start:.*\s+bar}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Stop:.*\s+foo}) }
          it { is_expected.to contain_file(initscript).with_content(%r{Required-Stop:.*\s+bar}) }
        end
      end

      context 'with different docker service name' do
        # let(:pre_condition) { ["class { 'docker': docker_group => 'docker', service_name => 'dockerd', service_provider => systemd, acknowledge_unsupported_os => true }"] }
        let(:pre_condition) { pre_condition.gsub("service_name => 'docker'", "service_name => 'dockerd'") }

        let(:params) { params }

        if systemd
          it { is_expected.to contain_file(initscript).with_content(%r{^WantedBy=dockerd.service$}) }
        end
      end
{'fraction_non_alphanumeric': 0.16279647336396375, 'fraction_numerical': 0.004470383707934931, 'mean_word_length': 3.0541152781273597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6167310', 'n_tokens_mistral': 5342, 'n_tokens_neox': 5232, 'n_words': 752}