Add rudimentary spec test to WaitTillUp using vagrant-spec

Add some basic spec tests for the WaitTillUp action class to lay some
foundations. Utilize vagrant-spec pinned to a known working commit for
tests to pass consistently until they provide releases.

Requires updating some of the rspec libraries and includes
sharedcontext.rb from the jantman/vagrant-r10k project on github.
This commit is contained in:
Darragh Bailey 2016-01-25 19:07:14 +00:00
parent 3a6acf4288
commit afb53addb1
6 changed files with 97 additions and 3 deletions

View File

@ -13,6 +13,10 @@ group :development do
else
gem 'vagrant', :git => 'https://github.com/mitchellh/vagrant.git'
end
gem 'vagrant-spec', :github => 'mitchellh/vagrant-spec',
tag: ENV['VAGRANT_SPEC_VERSION'] || "9bba7e1228379c0a249a06ce76ba8ea7d276afbe"
gem 'pry'
end

View File

@ -1,5 +1,6 @@
require 'vagrant-libvirt'
require 'support/environment_helper'
require 'vagrant-spec/unit'
RSpec.configure do |spec|
end

View File

@ -0,0 +1,24 @@
require 'fog/libvirt'
shared_context "libvirt" do
include_context "unit"
let(:libvirt_context) { true }
let(:id) { "dummy-vagrant_dummy" }
let(:connection) { double("::Fog::Compute") }
def connection_result(options={})
result = options.fetch(:result, nil)
double("connection_result" => result)
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("::Fog::Compute", connection)
# drivers also call vm_exists? during init;
allow(connection).to receive(:servers).with(kind_of(String)).
and_return(connection_result(result: nil))
end
end

View File

@ -0,0 +1,34 @@
require 'spec_helper'
shared_context "unit" do
include_context 'vagrant-unit'
let(:test_env) do
vagrantfile ||= <<-EOF
Vagrant.configure('2') do |config|
config.vm.define :test
end
EOF
test_env = isolated_environment
test_env.vagrantfile vagrantfile
test_env
end
let(:env) { { env: iso_env, machine: machine, ui: ui, root_path: '/rootpath' } }
let(:conf) { Vagrant::Config::V2::DummyConfig.new() }
let(:ui) { Vagrant::UI::Basic.new() }
let(:iso_env) { test_env.create_vagrant_env ui_class: Vagrant::UI::Basic }
let(:machine) { iso_env.machine(:test, :libvirt) }
# Mock the communicator to prevent SSH commands for being executed.
let(:communicator) { double('communicator') }
# Mock the guest operating system.
let(:guest) { double('guest') }
let(:app) { lambda { |env| } }
let(:plugin) { register_plugin() }
before (:each) do
machine.stub(:guest => guest)
machine.stub(:communicator => communicator)
machine.stub(:id => id)
end
end

View File

@ -0,0 +1,31 @@
require "vagrant-libvirt/action/wait_till_up"
require "vagrant-libvirt/errors"
require "spec_helper"
require "support/sharedcontext"
require "support/libvirt_context"
describe VagrantPlugins::ProviderLibvirt::Action::WaitTillUp do
subject { described_class.new(app, env) }
include_context "vagrant-unit"
include_context "libvirt"
include_context "unit"
describe "#call" do
context "when machine does not exist" do
before do
allow_any_instance_of(VagrantPlugins::ProviderLibvirt::Driver).to receive(:get_domain).and_return(nil)
allow_any_instance_of(VagrantPlugins::ProviderLibvirt::Driver).to receive(:state).
and_return(:not_created)
end
it "raises exception" do
expect(app).to_not receive(:call)
expect{subject.call(env)}.to raise_error
end
end
end
end

View File

@ -16,9 +16,9 @@ Gem::Specification.new do |gem|
gem.require_paths = ['lib']
gem.version = VagrantPlugins::ProviderLibvirt::VERSION
gem.add_development_dependency "rspec-core", "~> 2.12.2"
gem.add_development_dependency "rspec-expectations", "~> 2.12.1"
gem.add_development_dependency "rspec-mocks", "~> 2.12.1"
gem.add_development_dependency "rspec-core", "~> 2.14.0"
gem.add_development_dependency "rspec-expectations", "~> 2.14.0"
gem.add_development_dependency "rspec-mocks", "~> 2.14.0"
gem.add_runtime_dependency 'fog-libvirt', '~> 0.0.1'
gem.add_runtime_dependency 'nokogiri', '~> 1.6.0'