File: command_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 (111 lines) | stat: -rw-r--r-- 3,171 bytes parent folder | download | duplicates (5)
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
require File.expand_path("../../../../base", __FILE__)
require Vagrant.source_root.join("plugins/commands/suspend/command")

describe VagrantPlugins::CommandSuspend::Command do
  include_context "unit"

  let(:entry_klass) { Vagrant::MachineIndex::Entry }
  let(:argv)     { [] }
  let(:vagrantfile_content){ "" }
  let(:iso_env) do
    env = isolated_environment
    env.vagrantfile(vagrantfile_content)
    env.create_vagrant_env
  end

  subject { described_class.new(argv, iso_env) }

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

  def new_entry(name)
    entry_klass.new.tap do |e|
      e.name = name
      e.vagrantfile_path = "/bar"
    end
  end

  before do
    allow(iso_env).to receive(:action_runner).and_return(action_runner)
    allow(subject).to receive(:with_target_vms) { |&block| block.call machine }
  end

  context "with no argument" do
    let(:vagrantfile_content) do
        <<-VF
        Vagrant.configure("2") do |config|
          config.vm.define "app"
          config.vm.define "db"
        end
        VF
    end

    it "should suspend all vms" do
      allow(subject).to receive(:with_target_vms) { |&block|
        block.call machine
        block.call machine2
      }
      expect(machine).to receive(:action) do |name, opts|
        expect(name).to eq(:suspend)
      end
      expect(machine2).to receive(:action) do |name, opts|
        expect(name).to eq(:suspend)
      end

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

  context "with an argument" do
    let(:vagrantfile_content) do
        <<-VF
        Vagrant.configure("2") do |config|
          config.vm.define "app"
          config.vm.define "db"
        end
        VF
    end
    let(:argv) { ["app"] }

    it "should suspend a vm" do
      expect(machine).to receive(:action) do |name, opts|
        expect(name).to eq(:suspend)
      end

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

  context "with the global all flag" do
    let(:argv){ ["--all-global"] }

    it "should suspend all vms globally" do
      global_env = isolated_environment
      global_env.vagrantfile("Vagrant.configure(2){|config| config.vm.box = 'dummy'}")
      global_venv = global_env.create_vagrant_env
      global_machine = global_venv.machine(global_venv.machine_names[0], :dummy)
      global_machine.id = "1234"
      global = new_entry(global_machine.name)
      global.provider = "dummy"
      global.vagrantfile_path = global_env.workdir
      locked = iso_env.machine_index.set(global)
      iso_env.machine_index.release(locked)

      allow(subject).to receive(:with_target_vms) { |&block| block.call global_machine }
      expect(global_machine).to receive(:action) do |name, opts|
        expect(name).to eq(:suspend)
      end

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

    context "with an argument is used" do
      let(:argv){ ["machine", "--all-global"] }

      it "errors out" do
        expect{subject.execute}.to raise_error(Vagrant::Errors::CommandSuspendAllArgs)
      end
    end
  end
end