Files
vagrant-libvirt/spec/support/libvirt_acceptance_context.rb
Darragh Bailey 41bd20269e Migrate acceptance tests to rspec (#1513)
Move existing tests executed with the help of bats to use rspec directly
combined with tags to filter them out from being executed by default.

This allows for more complex assertions as well as easier debug as the
code supports use of setting 'VAGRANT_SPEC_SKIP_CLEANUP' to prevent the
tests from removing the temporary directory created for home and work
directories.

Extend a number of classes from vagrant-spec to override default
behaviour to allow passing of additional environment variables for
packaging tests, as well as supporting the skip cleanup. Given the use
of after to perform the cleanup, need to vendor the vagrant-spec
acceptance context in order to modify it easily.
2022-06-13 23:43:06 +01:00

65 lines
1.8 KiB
Ruby

# frozen_string_literal: true
require_relative 'acceptance/context'
FALSEY_VALUES = %w[f false no n 0].freeze
shared_context 'libvirt_acceptance' do
include_context 'acceptance'
# The skeleton paths that will be used to configure environments.
let(:skeleton_paths) do
root = File.expand_path('../acceptance/support-skeletons', __dir__)
config.skeleton_paths.dup.unshift(root)
end
let(:config) do
c = VagrantPlugins::VagrantLibvirt::Spec::Acceptance::Configuration.new
c.clean_on_fail = FALSEY_VALUES.include?(ENV.fetch('VAGRANT_SPEC_SKIP_CLEANUP', 'false').to_s.downcase)
c
end
before(:each) do
# allow execution environment to cache boxes used
symlink_boxes(ENV.fetch('VAGRANT_HOME', nil), environment)
end
after(:each) do
# ensure we remove the symlink
boxes_symlink = File.join(environment.homedir, 'boxes')
File.delete(boxes_symlink) if File.symlink?(boxes_symlink)
end
around do |example|
vagrant_cwd = ENV.delete('VAGRANT_CWD')
env_provider_before = ENV.fetch('VAGRANT_DEFAULT_PROVIDER', nil)
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'
begin
example.run
ensure
ENV['VAGRANT_CWD'] = vagrant_cwd if vagrant_cwd
if env_provider_before.nil?
ENV.delete('VAGRANT_DEFAULT_PROVIDER')
else
ENV['VAGRANT_DEFAULT_PROVIDER'] = env_provider_before
end
end
end
def duplicate_environment(env, *args)
dup_env = new_environment(*args)
symlink_boxes(env.homedir, dup_env)
dup_env
end
def symlink_boxes(vagrant_home, target_env)
return if vagrant_home.nil?
# allow use the same boxes location as source environment
File.symlink File.realpath(File.join(vagrant_home, 'boxes')), File.join(target_env.homedir, 'boxes')
end
end