File: yumrepo_spec.rb

package info (click to toggle)
puppet-agent 8.10.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 27,392 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (154 lines) | stat: -rw-r--r-- 4,767 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require 'spec_helper_acceptance'

puppet_repo = '/etc/yum.repos.d/puppetlabs.repo'
manifest = <<MANIFEST
yumrepo { 'puppetrepo-products':
  name      => 'puppetrepo-products',
  descr     => 'Puppet Labs Products El 7 - $basearch',
  ensure    => 'present',
  baseurl   => 'http://myownmirror',
  gpgkey    => 'http://myownmirror',
  enabled   => '1',
  gpgcheck  => '1',
  target    => '/etc/yum.repos.d/puppetlabs.repo',
}
yumrepo { 'puppetrepo-deps':
  name      => 'puppetrepo-deps',
  descr     => 'Puppet Labs Dependencies El 7 - $basearch',
  ensure    => 'present',
  baseurl   => 'http://myownmirror',
  gpgkey    => 'http://myownmirror',
  enabled   => '1',
  gpgcheck  => '1',
  target    => '/etc/yum.repos.d/puppetlabs.repo',
}
MANIFEST

def resource(host, type, name)
  on(host, puppet("resource -y #{type} #{name}")) do |result|
    yaml = YAML.safe_load(result.stdout)
    yield yaml[type][name]
  end
end

RSpec.context 'Manages yumrepo' do
  agents.each do |agent|
    it 'creates a yum repo file' do
      apply_manifest_on(agent, manifest)

      resource(agent, 'file', puppet_repo) do |res|
        assert_equal(res['ensure'], 'file')
        assert_equal(res['mode'], '0644')
      end
    end

    it 'removes a yumrepo entry' do
      apply_manifest_on(agent, <<ABSENT)
yumrepo { 'puppetrepo-deps':
  name      => 'puppetrepo-deps',
  ensure    => 'absent',
  target    => '/etc/yum.repos.d/puppetlabs.repo',
}
ABSENT
      resource(agent, 'file', puppet_repo) do |res|
        assert_equal(res['ensure'], 'file')

        # Puppet 7 and up uses SHA256 as the default digest algorithm
        if %r{^6\.}.match?(on(agent, puppet('--version')).stdout)
          assert_equal(res['content'], '{md5}8df43e112c614f3062545995b32ed3c0')
        else
          assert_equal(res['content'], '{sha256}a361f9e6174b1f3baca261d254c21d8d89ca274b36ebdfee5bf3223f1820aeea')
        end
      end
    end

    it 'updates a yumrepo entry' do
      apply_manifest_on(agent, manifest)
      apply_manifest_on(agent, <<UPDATED)
yumrepo { 'puppetrepo-products':
  ensure    => 'present',
  enabled   => 'no',
  baseurl   => 'http://myothermirror',
  gpgkey    => 'http://myothermirror',
  priority  => '99',
  retries   => '11',
  timeout   => '2000',
}
UPDATED
      on(agent, "grep enabled=No #{puppet_repo}") # booleans are special
      on(agent, "grep baseurl=http://myothermirror #{puppet_repo}")
      on(agent, "grep gpgkey=http://myothermirror #{puppet_repo}")
      on(agent, "grep priority=99 #{puppet_repo}")
      on(agent, "grep retries=11 #{puppet_repo}")
      on(agent, "grep timeout=2000 #{puppet_repo}")
    end

    it 'discovers yumrepos' do
      resource(agent, 'yumrepo', 'puppetrepo-products') do |res|
        # verify some basic info
        assert_match(res['ensure'], 'present')
        assert_match(res['descr'], 'Puppet Labs Products El 7 - $basearch')
      end
    end

    describe '`proxy` property' do
      context 'when set to a URL' do
        it 'applies idempotently' do
          pp = <<-MANIFEST
          yumrepo {'proxied-repo':
            baseurl => 'http://myownmirror',
            proxy   => 'http://proxy.example.com:3128',
          }
          MANIFEST

          apply_manifest(pp, catch_failures: true)
          apply_manifest(pp, catch_changes: true)
        end

        describe file('/etc/yum.repos.d/proxied-repo.repo') do
          its(:content) { is_expected.to contain('proxy=http://proxy.example.com:3128') }
        end
      end
      context 'when set to `absent`' do
        it 'applies idempotently' do
          pp = <<-MANIFEST
          yumrepo {'proxied-repo':
            baseurl => 'http://myownmirror',
            proxy   => absent,
          }
          MANIFEST

          apply_manifest(pp, catch_failures: true)
          apply_manifest(pp, catch_changes: true)
        end

        describe file('/etc/yum.repos.d/proxied-repo.repo') do
          its(:content) { is_expected.not_to contain('proxy') }
        end
      end
      context 'when set to `_none_`' do
        it 'applies idempotently' do
          pp = <<-MANIFEST
          yumrepo {'proxied-repo':
            baseurl => 'http://myownmirror',
            proxy   => '_none_',
          }
          MANIFEST

          apply_manifest(pp, catch_failures: true)
          apply_manifest(pp, catch_changes: true)
        end

        if fact('os.release.major').to_i >= 8
          describe file('/etc/yum.repos.d/proxied-repo.repo') do
            its(:content) { is_expected.to match(%r{^proxy=$}) }
          end
        else
          describe file('/etc/yum.repos.d/proxied-repo.repo') do
            its(:content) { is_expected.to match(%r{^proxy=_none_$}) }
          end
        end
      end
    end
  end
end