File: command_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 (171 lines) | stat: -rw-r--r-- 5,589 bytes parent folder | download | duplicates (4)
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
require_relative "../../../base"
require_relative "../../../../../plugins/commands/validate/command"

describe VagrantPlugins::CommandValidate::Command do
  include_context "unit"
  include_context "command plugin helpers"

  let(:vagrantfile_content){ "" }
  let(:iso_env) do
    env = isolated_environment
    env.vagrantfile(vagrantfile_content)
    env.create_vagrant_env
  end

  let(:action_runner) { double("action_runner") }
  let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) }

  let(:argv)   { [] }

  before(:all) do
    I18n.load_path << Vagrant.source_root.join("plugins/commands/port/locales/en.yml")
    I18n.reload!
  end

  subject { described_class.new(argv, iso_env) }

  describe "#execute" do
    context "validating configs" do
      let(:vagrantfile_content) do
          <<-VF
          Vagrant.configure("2") do |config|
            config.vm.box = "hashicorp/precise64"
            config.vm.synced_folder ".", "/vagrant", disabled: true
          end
          VF
      end
      it "validates correct Vagrantfile" do
        expect(machine).to receive(:action_raw) do |name, action, env|
          expect(name).to eq(:config_validate)
          expect(action).to eq(Vagrant::Action::Builtin::ConfigValidate)
          expect(env).to eq({})
        end
        expect(iso_env.ui).to receive(:info).with(any_args) { |message, _|
          expect(message).to include("Vagrantfile validated successfully.")
        }

        expect(subject.execute).to eq(0)
      end
    end

    context "invalid configs" do
      let(:vagrantfile_content) do
        <<-VF
        Vagrant.configure("2") do |config|
          config.vm.bix = "hashicorp/precise64"
          config.vm.synced_folder ".", "/vagrant", disabled: true
        end
        VF
      end
      it "validates the configuration" do
        expect { subject.execute }.to raise_error(Vagrant::Errors::ConfigInvalid) { |err|
          expect(err.message).to include("The following settings shouldn't exist: bix")
        }
      end
    end

    context "valid configs for multiple vms" do
      let(:vagrantfile_content) do
        <<-VF
        Vagrant.configure("2") do |config|
          config.vm.box = "hashicorp/precise64"
          config.vm.synced_folder ".", "/vagrant", disabled: true

          config.vm.define "test" do |vm|
            vm.vm.provider :virtualbox
          end

          config.vm.define "machine" do |vm|
            vm.vm.provider :virtualbox
          end
        end
        VF
      end
      it "validates correct Vagrantfile of all vms" do
        expect(machine).to receive(:action_raw) do |name, action, env|
          expect(name).to eq(:config_validate)
          expect(action).to eq(Vagrant::Action::Builtin::ConfigValidate)
          expect(env).to eq({})
        end
        expect(iso_env.ui).to receive(:info).with(any_args) { |message, _|
          expect(message).to include("Vagrantfile validated successfully.")
        }

        expect(subject.execute).to eq(0)
      end
    end

    context "an invalid config for some vms" do
      let(:vagrantfile_content) do
        <<-VF
        Vagrant.configure("2") do |config|
          config.vm.box = "hashicorp/precise64"
          config.vm.synced_folder ".", "/vagrant", disabled: true

          config.vm.define "test" do |vm|
            vm.vm.provider :virtualbox
          end

          config.vm.define "machine" do |vm|
            vm.vm.not_provider :virtualbox
          end
        end
        VF
      end
      it "validates the configuration of all vms" do
        expect(machine).to receive(:action_raw) do |name, action, env|
          expect(name).to eq(:config_validate)
          expect(action).to eq(Vagrant::Action::Builtin::ConfigValidate)
          expect(env).to eq({})
        end

        expect { subject.execute }.to raise_error(Vagrant::Errors::ConfigInvalid) { |err|
          expect(err.message).to include("The following settings shouldn't exist: not_provider")
        }
      end
    end

    context "with the ignore provider flag" do
      let(:argv) { ["--ignore-provider"]}
      let(:vagrantfile_content) do
        <<-VF
        Vagrant.configure("2") do |config|
          config.vm.box = "hashicorp/precise64"
          config.vm.synced_folder ".", "/vagrant", disabled: true

          config.vm.define "test" do |vm|
            vm.vm.hostname = "test"
            vm.vm.provider :virtualbox do |v|
              v.not_a_real_option = true
            end
          end
        end
        VF
      end
      it "ignores provider specific configurations with the flag" do
        allow(subject).to receive(:mockup_providers!).and_return("")
        allow(FileUtils).to receive(:remove_entry).and_return(true)
        expect(iso_env.ui).to receive(:info).with(any_args) { |message, _|
          expect(message).to include("Vagrantfile validated successfully.")
        }

        expect(machine).to receive(:action_raw) do |name, action, env|
          expect(name).to eq(:config_validate)
          expect(action).to eq(Vagrant::Action::Builtin::ConfigValidate)
          expect(env).to eq({:ignore_provider=>true})
        end

        expect(subject.execute).to eq(0)
      end
    end

    context "no vagrantfile" do
      let(:vagrantfile_content){ "" }
      let(:env) { isolated_environment.create_vagrant_env }
      subject { described_class.new(argv, env) }
      it "throws an exception if there's no Vagrantfile" do
        expect { subject.execute }.to raise_error(Vagrant::Errors::NoEnvironmentError)
      end
    end
  end
end