summaryrefslogtreecommitdiff
path: root/spec/unit/provider/yumrepo/inifile_spec.rb
blob: b0843276d928aaaf8f24db49a638261ca26d8f60 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
require 'spec_helper'

describe Puppet::Type.type(:yumrepo).provider(:inifile) do

  after(:each) do
    described_class.clear
  end

  describe "enumerating all yum repo files" do
    it "reads all files in the directories specified by reposdir" do
      described_class.expects(:reposdir).returns ['/etc/yum.repos.d']

      Dir.expects(:glob).with("/etc/yum.repos.d/*.repo").returns(['/etc/yum.repos.d/first.repo', '/etc/yum.repos.d/second.repo'])

      actual = described_class.repofiles
      expect(actual).to include("/etc/yum.repos.d/first.repo")
      expect(actual).to include("/etc/yum.repos.d/second.repo")
    end

    it "includes '/etc/yum.conf' as the first element" do
      described_class.expects(:reposdir).returns []

      actual = described_class.repofiles
      expect(actual[0]).to eq "/etc/yum.conf"
    end
  end

  describe "generating the virtual inifile" do
    let(:files) { ['/etc/yum.repos.d/first.repo', '/etc/yum.repos.d/second.repo', '/etc/yum.conf'] }
    let(:collection) { mock('virtual inifile') }

    before do
      described_class.clear
      Puppet::Util::IniConfig::FileCollection.expects(:new).returns collection
    end

    it "reads all files in the directories specified by self.repofiles" do
      described_class.expects(:repofiles).returns(files)

      files.each do |file|
        Puppet::FileSystem.stubs(:file?).with(file).returns true
        collection.expects(:read).with(file)
      end
      described_class.virtual_inifile
    end

    it "ignores repofile entries that are not files" do
      described_class.expects(:repofiles).returns(files)

      Puppet::FileSystem.stubs(:file?).with('/etc/yum.repos.d/first.repo').returns true
      Puppet::FileSystem.stubs(:file?).with('/etc/yum.repos.d/second.repo').returns false
      Puppet::FileSystem.stubs(:file?).with('/etc/yum.conf').returns true

      collection.expects(:read).with('/etc/yum.repos.d/first.repo').once
      collection.expects(:read).with('/etc/yum.repos.d/second.repo').never
      collection.expects(:read).with('/etc/yum.conf').once
      described_class.virtual_inifile
    end
  end

  describe 'creating provider instances' do
    let(:virtual_inifile) { stub('virtual inifile') }

    before :each do
      described_class.stubs(:virtual_inifile).returns(virtual_inifile)
    end

    let(:main_section) do
      sect = Puppet::Util::IniConfig::Section.new('main', '/some/imaginary/file')
      sect.entries << ['distroverpkg', 'centos-release']
      sect.entries << ['plugins', '1']

      sect
    end

    let(:updates_section) do
      sect = Puppet::Util::IniConfig::Section.new('updates', '/some/imaginary/file')
      sect.entries << ['name', 'Some long description of the repo']
      sect.entries << ['enabled', '1']

      sect
    end

    it "ignores the main section" do
      virtual_inifile.expects(:each_section).multiple_yields(main_section, updates_section)

      instances = described_class.instances
      expect(instances).to have(1).items
      expect(instances[0].name).to eq 'updates'
    end

    it "creates provider instances for every non-main section that was found" do
      virtual_inifile.expects(:each_section).multiple_yields(main_section, updates_section)

      sect = described_class.instances.first
      expect(sect.name).to eq 'updates'
      expect(sect.descr).to eq 'Some long description of the repo'
      expect(sect.enabled).to eq '1'
    end
  end

  describe "retrieving a section from the inifile" do

    let(:collection) { stub('ini file collection') }

    let(:ini_section) { stub('ini file section') }

    before do
      described_class.stubs(:virtual_inifile).returns(collection)
    end

    describe "and the requested section exists" do
      before do
        collection.stubs(:[]).with('updates').returns ini_section
      end

      it "returns the existing section" do
        expect(described_class.section('updates')).to eq ini_section
      end

      it "doesn't create a new section" do
        collection.expects(:add_section).never
        described_class.section('updates')
      end
    end

    describe "and the requested section doesn't exist" do
      it "creates a section in the preferred repodir" do
        described_class.stubs(:reposdir).returns ['/etc/yum.repos.d', '/etc/alternate.repos.d']
        collection.expects(:[]).with('updates')
        collection.expects(:add_section).with('updates', '/etc/alternate.repos.d/updates.repo')

        described_class.section('updates')
      end

      it "creates a section in yum.conf if no repodirs exist" do
        described_class.stubs(:reposdir).returns []
        collection.expects(:[]).with('updates')
        collection.expects(:add_section).with('updates', '/etc/yum.conf')

        described_class.section('updates')
      end
    end
  end

  describe "setting and getting properties" do

    let(:type_instance) do
      Puppet::Type.type(:yumrepo).new(
        :name     => 'puppetlabs-products',
        :ensure   => :present,
        :baseurl  => 'http://yum.puppetlabs.com/el/6/products/$basearch',
        :descr    => 'Puppet Labs Products El 6 - $basearch',
        :enabled  => '1',
        :gpgcheck => '1',
        :gpgkey   => 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs'
      )
    end

    let(:provider) do
      described_class.new(type_instance)
    end

    let(:section) do
      stub('inifile puppetlabs section', :name => 'puppetlabs-products')
    end

    before do
      type_instance.provider = provider
      described_class.stubs(:section).with('puppetlabs-products').returns(section)
    end

    describe "methods used by ensurable" do
      it "#create sets the yumrepo properties on the according section" do
        section.expects(:[]=).with('baseurl', 'http://yum.puppetlabs.com/el/6/products/$basearch')
        section.expects(:[]=).with('name', 'Puppet Labs Products El 6 - $basearch')
        section.expects(:[]=).with('enabled', '1')
        section.expects(:[]=).with('gpgcheck', '1')
        section.expects(:[]=).with('gpgkey', 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs')

        provider.create
      end

      it "#exists? checks if the repo has been marked as present" do
        described_class.stubs(:section).returns(stub(:[]= => nil))
        provider.create
        expect(provider).to be_exist
      end

      it "#destroy deletes the associated ini file section" do
        described_class.expects(:section).returns(section)
        section.expects(:destroy=).with(true)
        provider.destroy
      end
    end

    describe "getting properties" do
      it "maps the 'descr' property to the 'name' INI property" do
        section.expects(:[]).with('name').returns 'Some rather long description of the repository'
        expect(provider.descr).to eq 'Some rather long description of the repository'
      end

      it "gets the property from the INI section" do
        section.expects(:[]).with('enabled').returns '1'
        expect(provider.enabled).to eq '1'
      end

      it "sets the property as :absent if the INI property is nil" do
        section.expects(:[]).with('exclude').returns nil
        expect(provider.exclude).to eq :absent
      end
    end

    describe "setting properties" do
      it "maps the 'descr' property to the 'name' INI property" do
        section.expects(:[]=).with('name', 'Some rather long description of the repository')
        provider.descr = 'Some rather long description of the repository'
      end

      it "sets the property on the INI section" do
        section.expects(:[]=).with('enabled', '0')
        provider.enabled = '0'
      end

      it "sets the section field to nil when the specified value is absent" do
        section.expects(:[]=).with('exclude', nil)
        provider.exclude = :absent
      end
    end
  end

  describe 'reposdir' do
    let(:defaults) { ['/etc/yum.repos.d', '/etc/yum/repos.d'] }

    before do
      Puppet::FileSystem.stubs(:exist?).with('/etc/yum.repos.d').returns(true)
      Puppet::FileSystem.stubs(:exist?).with('/etc/yum/repos.d').returns(true)
    end

    it "returns the default directories if yum.conf doesn't contain a `reposdir` entry" do
      described_class.stubs(:find_conf_value).with('reposdir', '/etc/yum.conf')
      described_class.reposdir('/etc/yum.conf').should == defaults
    end

    it "includes the directory specified by the yum.conf 'reposdir' entry when the directory is present" do
      Puppet::FileSystem.expects(:exist?).with("/etc/yum/extra.repos.d").returns(true)

      described_class.expects(:find_conf_value).with('reposdir', '/etc/yum.conf').returns "/etc/yum/extra.repos.d"
      described_class.reposdir('/etc/yum.conf').should include("/etc/yum/extra.repos.d")
    end

    it "doesn't include the directory specified by the yum.conf 'reposdir' entry when the directory is absent" do
      Puppet::FileSystem.expects(:exist?).with("/etc/yum/extra.repos.d").returns(false)

      described_class.expects(:find_conf_value).with('reposdir', '/etc/yum.conf').returns "/etc/yum/extra.repos.d"
      described_class.reposdir('/etc/yum.conf').should_not include("/etc/yum/extra.repos.d")
    end

    it "logs a warning and returns an empty array if none of the specified repo directories exist" do
      Puppet::FileSystem.unstub(:exist?)
      Puppet::FileSystem.stubs(:exist?).returns false

      described_class.stubs(:find_conf_value).with('reposdir', '/etc/yum.conf')
      Puppet.expects(:debug).with('No yum directories were found on the local filesystem')
      expect(described_class.reposdir('/etc/yum.conf')).to be_empty
    end
  end

  describe "looking up a conf value" do
    describe "and the file doesn't exist" do
      it "returns nil" do
        Puppet::FileSystem.stubs(:exist?).returns false
        expect(described_class.find_conf_value('reposdir')).to be_nil
      end
    end

    describe "and the file exists" do
      let(:pfile) { stub('yum.conf physical file') }
      let(:sect) { stub('ini section') }

      before do
        Puppet::FileSystem.stubs(:exist?).with('/etc/yum.conf').returns true
        Puppet::Util::IniConfig::PhysicalFile.stubs(:new).with('/etc/yum.conf').returns pfile
      end

      it "creates a PhysicalFile to parse the given file" do
        pfile.expects(:get_section)
        described_class.find_conf_value('reposdir')
      end

      it "returns nil if the file exists but the 'main' section doesn't exist" do
        pfile.expects(:get_section).with('main')
        expect(described_class.find_conf_value('reposdir')).to be_nil
      end

      it "returns nil if the file exists but the INI property doesn't exist" do
        pfile.expects(:get_section).with('main').returns sect
        sect.expects(:[]).with('reposdir')
        expect(described_class.find_conf_value('reposdir')).to be_nil
      end

      it "returns the value if the value is defined in the PhysicalFile" do
        pfile.expects(:get_section).with('main').returns sect
        sect.expects(:[]).with('reposdir').returns '/etc/alternate.repos.d'
        expect(described_class.find_conf_value('reposdir')).to eq '/etc/alternate.repos.d'
      end
    end
  end
end