From 772b8b80d793880e54c4a7abe2ac74a3d6f72d5d Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Thu, 5 Dec 2013 17:29:25 -0200 Subject: [PATCH] Adding unit tests --- test/tc_lxc_created.rb | 40 ++++++++++++++++++++++++++++++++++ test/tc_lxc_running.rb | 47 ++++++++++++++++++++++++++++++++++++++++ test/tc_lxc_undefined.rb | 31 ++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 test/tc_lxc_created.rb create mode 100644 test/tc_lxc_running.rb create mode 100644 test/tc_lxc_undefined.rb diff --git a/test/tc_lxc_created.rb b/test/tc_lxc_created.rb new file mode 100644 index 0000000..6ee38f0 --- /dev/null +++ b/test/tc_lxc_created.rb @@ -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 diff --git a/test/tc_lxc_running.rb b/test/tc_lxc_running.rb new file mode 100644 index 0000000..4761052 --- /dev/null +++ b/test/tc_lxc_running.rb @@ -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 diff --git a/test/tc_lxc_undefined.rb b/test/tc_lxc_undefined.rb new file mode 100644 index 0000000..2a582b3 --- /dev/null +++ b/test/tc_lxc_undefined.rb @@ -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