File: virtualbox_context.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 (39 lines) | stat: -rw-r--r-- 1,520 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
shared_context "virtualbox" do
  include_context "unit"

  let(:vbox_context) { true                                }
  let(:uuid)         { "1234-abcd-5678-efgh"               }
  let(:vbox_version) { "4.3.4"                             }
  let(:subprocess)   { double("Vagrant::Util::Subprocess") }

  # this is a helper that returns a duck type suitable from a system command
  # execution; allows setting exit_code, stdout, and stderr in stubs.
  def subprocess_result(options={})
    defaults = {exit_code: 0, stdout: "", stderr: ""}
    double("subprocess_result", defaults.merge(options))
  end

  before do
    # we don't want unit tests to ever run commands on the system; so we wire
    # in a double to ensure any unexpected messages raise exceptions
    stub_const("Vagrant::Util::Subprocess", subprocess)

    # drivers will blow up on instantiation if they cannot determine the
    # virtualbox version, so wire this stub in automatically
    allow(subprocess).to receive(:execute).
      with("VBoxManage", "--version", an_instance_of(Hash)).
      and_return(subprocess_result(stdout: vbox_version))

    # drivers also call vm_exists? during init;
    allow(subprocess).to receive(:execute).
      with("VBoxManage", "showvminfo", kind_of(String), kind_of(Hash)).
      and_return(subprocess_result(exit_code: 0))
  end

  around do |example|
    # On Windows, we don't want to accidentally call the actual VirtualBox
    with_temp_env("VBOX_INSTALL_PATH" => nil) do
      example.run
    end
  end
end