File: forge_spec.rb

package info (click to toggle)
r10k 5.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,228 kB
  • sloc: ruby: 18,180; makefile: 10; sh: 1
file content (271 lines) | stat: -rw-r--r-- 10,189 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
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
require 'r10k/module/forge'
require 'spec_helper'

describe R10K::Module::Forge do
  # TODO: make these *unit* tests not depend on a real module on the real Forge :(

  include_context 'fail on execution'

  let(:fixture_modulepath) { File.expand_path('spec/fixtures/module/forge', PROJECT_ROOT) }
  let(:empty_modulepath) { File.expand_path('spec/fixtures/empty', PROJECT_ROOT) }

  describe "statically determined version support" do
    it 'returns explicitly released forge versions' do
      static_version = described_class.statically_defined_version('branan/eight_hundred', { version: '8.0.0' })
      expect(static_version).to eq('8.0.0')
    end

    it 'returns explicit pre-released forge versions' do
      static_version = described_class.statically_defined_version('branan/eight_hundred', { version: '8.0.0-pre1' })
      expect(static_version).to eq('8.0.0-pre1')
    end

    it 'retuns nil for latest versions' do
      static_version = described_class.statically_defined_version('branan/eight_hundred', { version: :latest })
      expect(static_version).to eq(nil)
    end

    it 'retuns nil for undefined versions' do
      static_version = described_class.statically_defined_version('branan/eight_hundred', { version: nil })
      expect(static_version).to eq(nil)
    end
  end

  describe "implementing the Puppetfile spec" do
    it "should implement 'branan/eight_hundred', '8.0.0'" do
      expect(described_class).to be_implement('branan/eight_hundred', { type: 'forge', version: '8.0.0' })
    end

    it "should implement 'branan-eight_hundred', '8.0.0'" do
      expect(described_class).to be_implement('branan-eight_hundred', { type: 'forge', version: '8.0.0' })
    end
  end

  describe "implementing the standard options interface" do
    it "should implement {type: forge}" do
      expect(described_class).to be_implement('branan-eight_hundred', { type: 'forge', version: '8.0.0', source: 'not implemented' })
    end
  end

  describe "setting attributes" do
    subject { described_class.new('branan/eight_hundred', '/moduledir', { version: '8.0.0' }) }

    it "sets the name" do
      expect(subject.name).to eq 'eight_hundred'
    end

    it "sets the author" do
      expect(subject.author).to eq 'branan'
    end

    it "sets the dirname" do
      expect(subject.dirname).to eq '/moduledir'
    end

    it "sets the title" do
      expect(subject.title).to eq 'branan-eight_hundred'
    end
  end

  describe "invalid attributes" do
    it "errors on invalid versions" do
      expect { described_class.new('branan/eight_hundred', '/moduledir', { version: '_8.0.0_' }) }.to raise_error ArgumentError, /version/
    end
  end

  describe "properties" do
    subject { described_class.new('branan/eight_hundred', fixture_modulepath, { version: '8.0.0' }) }

    it "sets the module type to :forge" do
      expect(subject.properties).to include(:type => :forge)
    end

    it "sets the expected version" do
      expect(subject.properties).to include(:expected => '8.0.0')
    end

    it "sets the actual version" do
      expect(subject).to receive(:current_version).and_return('0.8.0')
      expect(subject.properties).to include(:actual => '0.8.0')
    end
  end

  context "when a module is deprecated" do
    subject { described_class.new('puppetlabs/corosync', fixture_modulepath, { version: :latest }) }

    xit "warns on sync if module is not already insync" do
      allow(subject).to receive(:status).and_return(:absent)

      allow(R10K::Forge::ModuleRelease).to receive(:new).and_return(double('mod_release', install: true))

      logger_dbl = double(Log4r::Logger)
      allow_any_instance_of(described_class).to receive(:logger).and_return(logger_dbl)

      allow(logger_dbl).to receive(:info).with(/Deploying module to.*/)
      allow(logger_dbl).to receive(:debug2).with(/No spec dir detected/)
      expect(logger_dbl).to receive(:warn).with(/puppet forge module.*puppetlabs-corosync.*has been deprecated/i)

      subject.sync
    end

    it "does not warn on sync if module is already insync" do
      allow(subject).to receive(:status).and_return(:insync)

      logger_dbl = double(Log4r::Logger)
      allow_any_instance_of(described_class).to receive(:logger).and_return(logger_dbl)

      allow(logger_dbl).to receive(:info).with(/Deploying module to.*/)
      allow(logger_dbl).to receive(:debug2).with(/No spec dir detected/)
      expect(logger_dbl).to_not receive(:warn).with(/puppet forge module.*puppetlabs-corosync.*has been deprecated/i)

      subject.sync
    end
  end

  describe '#expected_version' do
    it "returns an explicitly given expected version" do
      subject = described_class.new('branan/eight_hundred', fixture_modulepath, { version: '8.0.0' })
      expect(subject.expected_version).to eq '8.0.0'
    end

    it "uses the latest version from the forge when the version is :latest" do
      subject = described_class.new('branan/eight_hundred', fixture_modulepath, { version: :latest })
      release = double("Module Release", version: '8.8.8')
      expect(subject.v3_module).to receive(:current_release).and_return(release).twice
      expect(subject.expected_version).to eq '8.8.8'
    end

    it "throws when there are no available versions" do
      subject = described_class.new('branan/eight_hundred', fixture_modulepath, { version: :latest })
      expect(subject.v3_module).to receive(:current_release).and_return(nil)
      expect { subject.expected_version }.to raise_error(PuppetForge::ReleaseNotFound)
    end
  end

  describe "determining the status" do

    subject { described_class.new('branan/eight_hundred', fixture_modulepath, { version: '8.0.0' }) }

    it "is :absent if the module directory is absent" do
      allow(subject).to receive(:exist?).and_return false
      expect(subject.status).to eq :absent
    end

    it "is :mismatched if there is no module metadata" do
      allow(subject).to receive(:exist?).and_return true
      allow(File).to receive(:exist?).and_return false

      expect(subject.status).to eq :mismatched
    end

    it "is :mismatched if module was previously a git checkout" do
      allow(File).to receive(:directory?).and_return true

      expect(subject.status).to eq :mismatched
    end

    it "is :mismatched if the metadata author doesn't match the expected author" do
      allow(subject).to receive(:exist?).and_return true

      allow(subject.instance_variable_get(:@metadata_file)).to receive(:read).and_return subject.metadata
      allow(subject.metadata).to receive(:full_module_name).and_return 'blargh-blargh'

      expect(subject.status).to eq :mismatched
    end

    it "is :outdated if the metadata version doesn't match the expected version" do
      allow(subject).to receive(:exist?).and_return true

      allow(subject.instance_variable_get(:@metadata_file)).to receive(:read).and_return subject.metadata
      allow(subject.metadata).to receive(:version).and_return '7.0.0'
      expect(subject.status).to eq :outdated
    end

    it "is :insync if the version and the author are in sync" do
      allow(subject).to receive(:exist?).and_return true

      expect(subject.status).to eq :insync
    end
  end

  describe "#sync" do
    subject { described_class.new('branan/eight_hundred', fixture_modulepath, { version: '8.0.0' }) }

    context "syncing the repo" do
      let(:module_org) { "coolorg" }
      let(:module_name) { "coolmod" }
      let(:title) { "#{module_org}-#{module_name}" }
      let(:dirname) { Pathname.new(Dir.mktmpdir) }
      let(:spec_path) { dirname + module_name + 'spec' }
      subject { described_class.new(title, dirname, {}) }

      it 'defaults to deleting the spec dir' do
        FileUtils.mkdir_p(spec_path)
        expect(subject).to receive(:status).and_return(:absent)
        expect(subject).to receive(:install)
        subject.sync
        expect(Dir.exist?(spec_path)).to eq false
      end
    end

    it 'does nothing when the module is in sync' do
      allow(subject).to receive(:status).and_return :insync

      expect(subject).to receive(:install).never
      expect(subject).to receive(:upgrade).never
      expect(subject).to receive(:reinstall).never
      expect(subject.sync).to be false
    end

    it 'reinstalls the module when it is mismatched' do
      allow(subject).to receive(:status).and_return :mismatched
      expect(subject).to receive(:reinstall)
      expect(subject.sync).to be true
    end

    it 'upgrades the module when it is outdated' do
      allow(subject).to receive(:status).and_return :outdated
      expect(subject).to receive(:upgrade)
      expect(subject.sync).to be true
    end

    it 'installs the module when it is absent' do
      allow(subject).to receive(:status).and_return :absent
      expect(subject).to receive(:install)
      expect(subject.sync).to be true
    end

    it 'returns false if `should_sync?` is false' do
      # modules do not sync if they are not requested
      mod = described_class.new('my_org/my_mod', '/path/to/mod', { overrides: { modules: { requested_modules: ['other_mod'] } } })
      expect(mod.sync).to be false
    end
  end

  describe '#install' do
    xit 'installs the module from the forge' do
      subject = described_class.new('branan/eight_hundred', fixture_modulepath, { version: '8.0.0' })
      release = instance_double('R10K::Forge::ModuleRelease')
      expect(R10K::Forge::ModuleRelease).to receive(:new).with('branan-eight_hundred', '8.0.0').and_return(release)
      expect(release).to receive(:install).with(subject.path)
      subject.install
    end
  end

  describe '#uninstall' do
    it 'removes the module path' do
      subject = described_class.new('branan/eight_hundred', fixture_modulepath, { version: '8.0.0' })
      expect(FileUtils).to receive(:rm_rf).with(subject.path.to_s)
      subject.uninstall
    end
  end

  describe '#reinstall' do
    it 'uninstalls and then installs the module' do
      subject = described_class.new('branan/eight_hundred', fixture_modulepath, { version: '8.0.0' })
      expect(subject).to receive(:uninstall)
      expect(subject).to receive(:install)
      subject.reinstall
    end
  end
end