Adding unit tests

This commit is contained in:
Andre Nathan 2013-12-05 17:29:25 -02:00
parent 8f86513c23
commit 772b8b80d7
3 changed files with 118 additions and 0 deletions

40
test/tc_lxc_created.rb Normal file
View file

@ -0,0 +1,40 @@
require 'test/unit'
require './lxc'
LXC_TEMPLATE = 'ubuntu'
CONTAINER_NAME = 'test'
CLONE_NAME = 'test_clone'
class TestLXCCreated < Test::Unit::TestCase
def setup
if Process::Sys::geteuid != 0
raise 'This test must be ran as root'
end
@name = CONTAINER_NAME
@container = LXC::Container.new(@name)
@container.create(LXC_TEMPLATE) unless @container.defined?
end
def test_container_defined
assert(@container.defined?)
end
def test_container_name
assert_equal(@name, @container.name)
assert_equal(@name, @container.config_item('lxc.utsname'))
end
def test_container_configuration
capdrop = @container.config_item('lxc.cap.drop')
@container.clear_config_item('lxc.cap.drop')
@container.set_config_item('lxc.cap.drop', capdrop[0...-1])
@container.set_config_item('lxc.cap.drop', capdrop[-1])
@container.save_config
assert_equal(capdrop, @container.config_item('lxc.cap.drop'))
end
def test_container_networking
assert(@container.keys('lxc.network.0').include?('name'))
assert_match(/^00:16:3e:/, @container.config_item('lxc.network.0.hwaddr'))
end
end

47
test/tc_lxc_running.rb Normal file
View file

@ -0,0 +1,47 @@
require 'test/unit'
require './lxc'
LXC_TEMPLATE = 'ubuntu'
CONTAINER_NAME = 'test'
CLONE_NAME = 'test_clone'
class TestLXCRunning < Test::Unit::TestCase
def setup
if Process::Sys::geteuid != 0
raise 'This test must be ran as root'
end
@name = CONTAINER_NAME
@container = LXC::Container.new(@name)
@container.create(LXC_TEMPLATE) unless @container.defined?
@container.start
end
def teardown
@container.stop
end
def test_container_running
@container.wait('RUNNING', 3)
assert(@container.init_pid > 1)
assert(@container.running?)
assert_equal('RUNNING', @container.state)
end
def test_container_interfaces
assert_equal(['eth0', 'lo'], @container.interfaces.sort)
end
def test_container_ip_addresses
ips = nil
10.times do
ips = @container.ip_addresses
break unless ips.empty?
sleep 1
end
assert(ips.length > 0)
@container.attach(:wait => true,
:namespaces => LXC::CLONE_NEWNET | LXC::CLONE_NEWUTS) do
LXC.run_command(['ifconfig', 'eth0'])
end
end
end

31
test/tc_lxc_undefined.rb Normal file
View file

@ -0,0 +1,31 @@
require 'test/unit'
require './lxc'
class TestLXCUndefined < Test::Unit::TestCase
def setup
@name = 'test'
@container = LXC::Container.new(@name)
end
def test_container_config_file_name
config_path = File.join(LXC.default_config_path, @name, 'config')
assert_equal(config_path, @container.config_file_name)
end
def test_container_not_defined
assert_equal(false, @container.defined?)
end
def test_container_init_pid
assert_equal(nil, @container.init_pid)
end
def test_container_not_running
assert_equal(false, @container.running?)
end
def test_container_stopped
assert_equal(false, @container.running?)
assert_equal('STOPPED', @container.state)
end
end