File: publish_test.rb

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (343 lines) | stat: -rw-r--r-- 10,743 bytes parent folder | download | duplicates (3)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
require File.expand_path("../../../../base", __FILE__)

require Vagrant.source_root.join("plugins/commands/cloud/publish")

describe VagrantPlugins::CloudCommand::Command::Publish do
  include_context "unit"

  let(:argv) { [] }
  let(:iso_env) { double("iso_env") }
  let(:account) { double("account") }
  let(:organization) { double("organization") }
  let(:box) { double("box") }
  let(:box_size) { 1 }
  let(:version) { double("version") }
  let(:provider) { double("provider") }
  let(:uploader) { double("uploader") }
  let(:ui) { Vagrant::UI::Silent.new }
  let(:upload_url) { double("upload_url") }
  let(:access_token) { double("access_token") }

  subject { described_class.new(argv, iso_env) }

  before do
    allow(iso_env).to receive(:ui).and_return(ui)
    allow(File).to receive(:stat).with(box).
      and_return(double("box_stat", size: box_size))
    allow(VagrantCloud::Account).to receive(:new).
      with(custom_server: anything, access_token: anything).
      and_return(account)
  end

  describe "#upload_box_file" do
    before do
      allow(provider).to receive(:upload).and_yield(upload_url)
      allow(uploader).to receive(:upload!)
      allow(File).to receive(:absolute_path).and_return(box)
      allow(Vagrant::Util::Uploader).to receive(:new).and_return(uploader)
    end

    it "should get absolute path for box file" do
      expect(File).to receive(:absolute_path).and_return(box)
      subject.upload_box_file(provider, box)
    end

    it "should upload through provider" do
      expect(provider).to receive(:upload).and_return(upload_url)
      subject.upload_box_file(provider, box)
    end

    it "should create uploader with given url" do
      expect(Vagrant::Util::Uploader).to receive(:new).
        with(upload_url, any_args).and_return(uploader)
      subject.upload_box_file(provider, box)
    end

    it "should upload with PUT method by default" do
      expect(Vagrant::Util::Uploader).to receive(:new).
        with(upload_url, anything, hash_including(method: :put)).and_return(uploader)
      subject.upload_box_file(provider, box)
    end

    context "with direct upload option enabled" do
      it "should upload with PUT method when direct upload option set" do
        expect(Vagrant::Util::Uploader).to receive(:new).
          with(upload_url, anything, hash_including(method: :put)).and_return(uploader)
        subject.upload_box_file(provider, box, direct_upload: true)
      end

      context "with box size of 5GB" do
        let(:box_size) { 5368709120 }

        it "should upload using direct to storage option" do
          expect(provider).to receive(:upload).with(direct: true)
          subject.upload_box_file(provider, box, direct_upload: true)
        end
      end

      context "with box size greater than 5GB" do
        let(:box_size) { 5368709121 }

        it "should disable direct to storage upload" do
          expect(provider).to receive(:upload).with(direct: false)
          subject.upload_box_file(provider, box, direct_upload: true)
        end
      end
    end
  end

  describe "#release_version" do
    it "should release the version" do
      expect(version).to receive(:release)
      subject.release_version(version)
    end
  end

  describe "#set_box_info" do
    context "with no options set" do
      let(:options) { {} }

      it "should not modify the box" do
        subject.set_box_info(box, options)
      end
    end

    context "with options set" do
      let(:priv) { double("private") }
      let(:short_description) { double("short_description") }
      let(:description) { double("description") }

      let(:options) {
        {private: priv, description: description, short_description: short_description}
      }

      it "should set info on box" do
        expect(box).to receive(:private=).with(priv)
        expect(box).to receive(:short_description=).with(short_description)
        expect(box).to receive(:description=).with(description)
        subject.set_box_info(box, options)
      end
    end
  end

  describe "#set_version_info" do
    context "with no options set" do
      let(:options) { {} }

      it "should not modify the verison" do
        subject.set_version_info(version, options)
      end
    end

    context "with options set" do
      let(:options) { {version_description: version_description} }
      let(:version_description) { double("version_description") }

      it "should set info on version" do
        expect(version).to receive(:description=).with(version_description)
        subject.set_version_info(version, options)
      end
    end
  end

  describe "#set_provider_info" do
    context "with no options set" do
      let(:options) { {} }

      it "should not modify the provider" do
        subject.set_provider_info(provider, options)
      end
    end

    context "with options set" do
      let(:options) { {url: url, checksum: checksum, checksum_type: checksum_type} }
      let(:url) { double("url") }
      let(:checksum) { double("checksum") }
      let(:checksum_type) { double("checksum_type") }

      it "should set info on provider" do
        expect(provider).to receive(:url=).with(url)
        expect(provider).to receive(:checksum=).with(checksum)
        expect(provider).to receive(:checksum_type=).with(checksum_type)
        subject.set_provider_info(provider, options)
      end
    end
  end

  describe "load_box_version" do
    let(:box_version) { "1.0.0" }

    context "when version exists" do
      before do
        allow(box).to receive(:versions).and_return([version])
        allow(version).to receive(:version).and_return(box_version)
      end

      it "should return the existing version" do
        expect(subject.load_box_version(box, box_version)).to eq(version)
      end
    end

    context "when version does not exist" do
      let(:new_version) { double("new_version") }

      before do
        allow(box).to receive(:versions).and_return([version])
        allow(version).to receive(:version)
      end

      it "should add a new version" do
        expect(box).to receive(:add_version).with(box_version).
          and_return(new_version)
        expect(subject.load_box_version(box, box_version)).to eq(new_version)
      end
    end
  end

  describe "#load_box" do
    let(:org_name) { "org-name" }
    let(:box_name) { "my-box" }

    before do
      allow(account).to receive(:organization).with(name: org_name).
        and_return(organization)
    end

    context "when box exists" do
      before do
        allow(box).to receive(:name).and_return(box_name)
        allow(organization).to receive(:boxes).and_return([box])
      end

      it "should return the existing box" do
        expect(subject.load_box(org_name, box_name, access_token)).to eq(box)
      end
    end

    context "when box does not exist" do
      let(:new_box) { double("new_box") }

      before do
        allow(organization).to receive(:boxes).and_return([])
      end

      it "should add a new box to organization" do
        expect(organization).to receive(:add_box).with(box_name).
          and_return(new_box)
        expect(subject.load_box(org_name, box_name, access_token)).to eq(new_box)
      end
    end
  end

  context "#execute" do
    let(:iso_env) do
      # We have to create a Vagrantfile so there is a root path
      env = isolated_environment
      env.vagrantfile("")
      env.create_vagrant_env
    end
    let(:client) { double("client", token: "1234token1234") }
    let(:action_runner) { double("action_runner") }
    let(:box_path) { "path/to/the/virtualbox.box" }
    let(:full_box_path) { "/full/#{box_path}" }
    let(:box) { full_box_path }

    before do
      allow(iso_env).to receive(:action_runner).
        and_return(action_runner)
      allow(subject).to receive(:client_login).
        and_return(client)
      allow(subject).to receive(:format_box_results)

      allow(iso_env.ui).to receive(:ask).and_return("y")
      allow(File).to receive(:absolute_path).with(box_path)
        .and_return("/full/#{box_path}")
      allow(File).to receive(:file?).with(box_path)
        .and_return(true)
    end

    context "with no arguments" do
      it "shows help" do
        expect { subject.execute }.
          to raise_error(Vagrant::Errors::CLIInvalidUsage)
      end
    end

    context "missing required arguments" do
      let(:argv) { ["vagrant/box", "1.0.0", "virtualbox"] }

      it "shows help" do
        expect { subject.execute }.
          to raise_error(Vagrant::Errors::CLIInvalidUsage)
      end
    end

    context "missing box file" do
      let(:argv) { ["vagrant/box", "1.0.0", "virtualbox", "/notreal/file.box"] }

      it "raises an exception" do
        allow(File).to receive(:file?).and_return(false)
        expect { subject.execute }.
          to raise_error(Vagrant::Errors::BoxFileNotExist)
      end
    end

    context "with arguments" do
      let(:org_name) { "vagrant" }
      let(:box_name) { "box" }
      let(:box_version) { "1.0.0" }
      let(:box_version_provider) { "virtualbox" }

      let(:argv) { [
        "#{org_name}/#{box_name}", box_version, box_version_provider, box_path
      ] }

      before do
        allow(account).to receive(:organization).with(name: org_name).
          and_return(organization)
        allow(subject).to receive(:load_box).and_return(box)
        allow(subject).to receive(:load_box_version).and_return(version)
        allow(subject).to receive(:load_version_provider).and_return(provider)
        allow(provider).to receive(:upload)

        allow(box).to receive(:save)
      end

      it "should prompt user for confirmation" do
        expect(iso_env.ui).to receive(:ask).and_return("y")
        expect(subject.execute).to eq(0)
      end

      context "when --force option is provided" do
        before { argv << "--force" }

        it "should not prompt user for confirmation" do
          expect(iso_env.ui).not_to receive(:ask)
          expect(subject.execute).to eq(0)
        end
      end

      context "when --release option is provided" do
        before do
          argv << "--release"
        end

        it "should release box version when not released" do
          expect(version).to receive(:released?).and_return(false)
          expect(version).to receive(:release)
          expect(subject.execute).to eq(0)
        end
      end

      context "when Vagrant Cloud error is encountered" do
        before { expect(box).to receive(:save).and_raise(VagrantCloud::Error) }

        it "should return non-zero result" do
          result = subject.execute
          expect(result).not_to eq(0)
          expect(result).to be_a(Integer)
        end
      end
    end
  end
end