Module: Vagrant::TestHelpers

Defined in:
lib/vagrant/test_helpers.rb

Overview

Test helpers provided by Vagrant to allow for plugin developers to write automated tests for their code. This module simply provides methods which can be included into any test framework (test/unit, RSpec, Shoulda, etc.)

Instance Method Summary (collapse)

Instance Method Details

- (Object) action_env(v_env = nil)

Returns a blank app (callable) and action environment with the given vagrant environment. This allows for testing of middlewares.



79
80
81
82
83
84
85
86
87
88
# File 'lib/vagrant/test_helpers.rb', line 79

def action_env(v_env = nil)
  v_env ||= vagrant_env
  # duplicate the Vagrant::Environment ui and get the default vm object
  # for the new action environment from the first pair in the vms list
  opts = {:ui => v_env.ui.dup, :vm => v_env.vms.first.last}
  app = lambda { |env| }
  env = Vagrant::Action::Environment.new(opts)
  env["vagrant.test"] = true
  [app, env]
end

- (Pathname) boxes_path

Path to the boxes directory in the home directory

Returns:

  • (Pathname)


137
138
139
140
141
# File 'lib/vagrant/test_helpers.rb', line 137

def boxes_path
  result = home_path.join("boxes")
  FileUtils.mkdir_p(result)
  result
end

- (Object) capture(stream) Also known as: silence

Utility method for capturing output streams.

Examples:

Evaluate the output

output = capture(:stdout){ env.cli("foo") }
assert_equal "bar", output

Silence the output

silence(:stdout){ env.cli("init") }

Parameters:

  • stream (:stdout, :stderr)

    The stream to capture

Yield Returns:

  • String

See Also:



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vagrant/test_helpers.rb', line 99

def capture(stream)
  begin
    stream = stream.to_s
    eval "$#{stream} = StringIO.new"
    yield
    result = eval("$#{stream}").string
  ensure
    eval("$#{stream} = #{stream.upcase}")
  end

  result
end

- (Object) clean_paths

Cleans all the test temp paths, which includes the boxes path, home path, etc. This allows for cleaning between tests.



145
146
147
148
149
150
151
152
# File 'lib/vagrant/test_helpers.rb', line 145

def clean_paths
  FileUtils.rm_rf(tmp_path)

  # Call these methods only to rebuild the directories
  tmp_path
  home_path
  boxes_path
end

- (Pathname) home_path

Path to the "home" directory for the tests

Returns:

  • (Pathname)


128
129
130
131
132
# File 'lib/vagrant/test_helpers.rb', line 128

def home_path
  result = tmp_path.join("home")
  FileUtils.mkdir_p(result)
  result
end

- (Pathname) tmp_path


Path helpers

Path to the tmp directory for the tests.

Returns:

  • (Pathname)


119
120
121
122
123
# File 'lib/vagrant/test_helpers.rb', line 119

def tmp_path
  result = Vagrant.source_root.join("test", "tmp")
  FileUtils.mkdir_p(result)
  result
end

- (Object) vagrant_app(*path)


Environment creation helpers

Creates a "vagrant_app" directory in the test tmp folder which can be used for creating test Vagrant environments. Returns the root directory of the app. This typically doesn't need to be called directly unless you're setting up a custom application. See the examples for common use cases.



15
16
17
18
19
20
# File 'lib/vagrant/test_helpers.rb', line 15

def vagrant_app(*path)
  root = tmp_path.join("vagrant_app")
  FileUtils.rm_rf(root)
  FileUtils.mkdir_p(root)
  root.join(*path)
end

- (Pathname) vagrant_box(name)

Creates the folder to contain a vagrant box. This allows for "fake" boxes to be made with the specified name.

Parameters:

  • name (String)

Returns:

  • (Pathname)


58
59
60
61
62
# File 'lib/vagrant/test_helpers.rb', line 58

def vagrant_box(name)
  result = boxes_path.join(name)
  FileUtils.mkdir_p(result)
  result
end

- (Object) vagrant_env(*args)

Creates and loads a Vagrant environment at the given path. If no path is given, then a default #vagrantfile is used.



47
48
49
50
51
# File 'lib/vagrant/test_helpers.rb', line 47

def vagrant_env(*args)
  path = args.shift if Pathname === args.first
  path ||= vagrantfile
  Vagrant::Environment.new(:cwd => path).load!
end

- (Array) vagrant_mock_downloader(klass)

Returns an instantiated downloader with a mocked tempfile which can be passed into it.

Parameters:

  • klass (Class)

    The downloader class

Returns:

  • (Array)

    Returns an array of downloader tempfile



69
70
71
72
73
74
75
# File 'lib/vagrant/test_helpers.rb', line 69

def vagrant_mock_downloader(klass)
  tempfile = mock("tempfile")
  tempfile.stubs(:write)

  _, env = action_env
  [klass.new(env), tempfile]
end

- (Object) vagrantfile(*args)

Creates a Vagrantfile with the given contents in the given app directory. If no app directory is specified, then a default Vagrant app is used.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vagrant/test_helpers.rb', line 25

def vagrantfile(*args)
  path = args.shift.join("Vagrantfile") if Pathname === args.first
  path ||= vagrant_app("Vagrantfile")

  # Create this box so that it exists
  vagrant_box("base")

  str  = args.shift || ""
  File.open(path.to_s, "w") do |f|
    f.puts "ENV['VAGRANT_HOME'] = '#{home_path}'"
    f.puts "Vagrant::Config.run do |config|"
    f.puts "config.vm.base_mac = 'foo' if !config.vm.base_mac"
    f.puts "config.vm.box = 'base'"
    f.puts str
    f.puts "end"
  end

  path.parent
end