File: disk_test.rb

package info (click to toggle)
vagrant 2.2.14%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,800 kB
  • sloc: ruby: 97,301; sh: 375; makefile: 16; lisp: 1
file content (126 lines) | stat: -rw-r--r-- 3,318 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
require File.expand_path("../../../../base", __FILE__)

require Vagrant.source_root.join("plugins/kernel_v2/config/disk")

describe VagrantPlugins::Kernel_V2::VagrantConfigDisk do
  include_context "unit"

  let(:type) { :disk }

  subject { described_class.new(type) }

  let(:ui) { double("ui") }
  let(:env) { double("env", ui: ui) }
  let(:provider) { double("provider") }
  let(:machine) { double("machine", name: "name", provider: provider, env: env,
                         provider_name: :virtualbox) }

  def assert_invalid
    errors = subject.validate(machine)
    if errors.empty?
      raise "No errors: #{errors.inspect}"
    end
  end

  def assert_valid
    errors = subject.validate(machine)
    if !errors.empty?
      raise "Errors: #{errors.inspect}"
    end
  end

  before do
    env = double("env")

    allow(provider).to receive(:capability?).with(:validate_disk_ext).and_return(true)
    allow(provider).to receive(:capability).with(:validate_disk_ext, "vdi").and_return(true)
    allow(provider).to receive(:capability?).with(:set_default_disk_ext).and_return(true)
    allow(provider).to receive(:capability).with(:set_default_disk_ext).and_return("vdi")
  end

  describe "with defaults" do
    before do
      subject.name = "foo"
      subject.size = 100
    end

    it "is valid with test defaults" do
      subject.finalize!
      assert_valid
    end

    it "sets a disk type" do
      subject.finalize!
      expect(subject.type).to eq(type)
    end

    it "defaults to non-primary disk" do
      subject.finalize!
      expect(subject.primary).to eq(false)
    end
  end

  describe "with an invalid config" do
    before do
      subject.name = "bar"
    end

    it "raises an error if size not set" do
      subject.finalize!
      assert_invalid
    end

    context "with an invalid disk extension" do
      before do
        subject.size = 100
        subject.disk_ext = "fake"

        allow(provider).to receive(:capability?).with(:validate_disk_ext).and_return(true)
        allow(provider).to receive(:capability).with(:validate_disk_ext, "fake").and_return(false)
        allow(provider).to receive(:capability?).with(:default_disk_exts).and_return(true)
        allow(provider).to receive(:capability).with(:default_disk_exts).and_return(["vdi", "vmdk"])
      end

      it "raises an error" do
        subject.finalize!
        assert_invalid
      end
    end
  end

  describe "config for dvd type" do
    let(:iso_path) { "/tmp/untitled.iso" }

    before do
      subject.type = :dvd
      subject.name = "untitled"
      allow(File).to receive(:file?).with(iso_path).and_return(true)
      subject.file = iso_path
    end

    it "is valid with test defaults" do
      subject.finalize!
      assert_valid
    end

    it "is invalid if file path is unset" do
      subject.file = nil
      subject.finalize!
      assert_invalid
    end

    it "is invalid if primary" do
      subject.primary = true
      subject.finalize!
      assert_invalid
    end
  end

  describe "#add_provider_config" do
    it "normalizes provider config" do
      test_provider_config = {provider__something: "special" }
      subject.add_provider_config(test_provider_config)
      expect(subject.provider_config).to eq( { provider: {something: "special" }} )
    end
  end
end