vagrant-lxc-ng/spec/acceptance/sanity_check_spec.rb

142 lines
3.7 KiB
Ruby
Raw Normal View History

2013-04-20 20:02:25 +00:00
require 'acceptance_helper'
2013-04-20 22:19:11 +00:00
class TestUI < Vagrant::UI::Interface
attr_reader :messages
METHODS = [:clear_line, :report_progress, :warn, :error, :info, :success]
def initialize(resource = nil)
super
@messages = METHODS.each_with_object({}) { |m, h| h[m] = [] }
end
def ask(*args)
super
# Automated tests should not depend on user input, obviously.
raise Errors::UIExpectsTTY
end
METHODS.each do |method|
define_method(method) do |message, *opts|
@messages[method].push message
end
end
end
# Monkey patch vagrant in order to reuse the UI test object that is set on
# our Vagrant::Environments
#
# TODO: Find out if this makes sense to be on vagrant core itself
require 'vagrant/machine'
Vagrant::Machine.class_eval do
alias :old_action :action
define_method :action do |name, extra_env = nil|
extra_env = { ui: @env.ui }.merge(extra_env || {})
old_action name, extra_env
end
end
2013-04-20 20:02:25 +00:00
describe 'Sanity check' do
2013-04-20 22:24:24 +00:00
after(:all) { destroy_container }
context 'running `vagrant up` from scratch' do
2013-04-20 20:02:25 +00:00
before(:all) do
destroy_container
vagrant_up
end
2013-04-20 22:24:24 +00:00
it 'creates a container' do
containers = `sudo lxc-ls`.chomp.split(/\s+/).uniq
expect(containers).to include File.read('/vagrant/spec/.vagrant/machines/default/lxc/id').strip.chomp
end
2013-04-20 20:02:25 +00:00
2013-04-20 22:24:24 +00:00
it 'starts the newly created container' do
status = `sudo lxc-info -n #{File.read('/vagrant/spec/.vagrant/machines/default/lxc/id').strip.chomp}`
expect(status).to include 'RUNNING'
end
2013-04-20 20:02:25 +00:00
2013-04-20 22:24:24 +00:00
it "is able to be SSH'ed" do
expect(vagrant_ssh('hostname')).to eq 'lxc-test-box'
end
it 'mounts shared folders with the right permissions' do
vagrant_ssh 'mkdir -p /vagrant/tmp && echo -n "Shared" > /vagrant/tmp/shared'
shared_file_contents = File.read('/vagrant/spec/tmp/shared')
2013-04-20 22:24:24 +00:00
expect(shared_file_contents).to eq 'Shared'
end
2013-04-20 20:02:25 +00:00
it 'provisions the container based on Vagrantfile configs' do
provisioned_file_contents = File.read('/vagrant/spec/tmp/provisioning')
expect(provisioned_file_contents).to eq 'Provisioned'
end
2013-04-20 20:02:25 +00:00
it 'forwards configured ports' do
output = `curl -s localhost:8080`.strip.chomp
expect(output).to include 'It works!'
end
2013-04-20 20:02:25 +00:00
end
pending '`vagrant halt` on a running container' do
2013-04-20 20:02:25 +00:00
before(:all) do
destroy_container
vagrant_up
vagrant_halt
end
it 'shuts down container'
it 'clears forwarded ports'
end
pending '`vagrant destroy`' do
2013-04-20 20:02:25 +00:00
before(:all) do
destroy_container
vagrant_up
vagrant_destroy
end
it 'destroys the underlying container'
end
def destroy_container
`sudo lxc-shutdown -n \`cat /vagrant/spec/.vagrant/machines/default/lxc/id\``
`sudo lxc-wait -n \`cat /vagrant/spec/.vagrant/machines/default/lxc/id\` --state STOPPED`
`sudo lxc-destroy -n \`cat /vagrant/spec/.vagrant/machines/default/lxc/id\``
2013-04-20 23:03:36 +00:00
`sudo killall -9 redir 2>/dev/null`
2013-04-20 20:02:25 +00:00
end
def vagrant_up
opts = { cwd: 'spec' }
env = Vagrant::Environment.new(opts)
env.cli('up', '--provider', 'lxc')
2013-04-20 22:19:11 +00:00
env.unload
2013-04-20 20:02:25 +00:00
end
def vagrant_halt
opts = { cwd: 'spec' }
env = Vagrant::Environment.new(opts)
env.cli('halt')
2013-04-20 22:19:11 +00:00
env.unload
2013-04-20 20:02:25 +00:00
end
def vagrant_destroy
opts = { cwd: 'spec' }
env = Vagrant::Environment.new(opts)
env.cli('destroy', '-f')
2013-04-20 22:19:11 +00:00
env.unload
end
def vagrant_ssh(cmd)
opts = { cwd: 'spec', ui_class: TestUI }
env = Vagrant::Environment.new(opts)
result = env.cli('ssh', '-c', cmd)
if result.to_i != 0
raise "SSH command failed: '#{cmd}'\n#{env.ui.messages.inspect}"
end
output = env.ui.messages[:info].join("\n").chomp
env.unload
output
2013-04-20 20:02:25 +00:00
end
end