ruby-lxc/test/test_lxc_running.rb

115 lines
2.8 KiB
Ruby
Raw Normal View History

2013-12-06 17:43:19 +01:00
$:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
2013-12-05 20:29:25 +01:00
require 'test/unit'
require 'tempfile'
2013-12-06 17:43:19 +01:00
require 'lxc'
2013-12-05 20:29:25 +01:00
class TestLXCRunning < Test::Unit::TestCase
def setup
if Process::Sys::geteuid != 0
raise 'This test must be ran as root'
end
2013-12-06 17:43:19 +01:00
@name = 'test'
2013-12-05 20:29:25 +01:00
@container = LXC::Container.new(@name)
2013-12-06 17:43:19 +01:00
@container.create('ubuntu') unless @container.defined?
2013-12-05 20:29:25 +01:00
@container.start
end
def teardown
@container.shutdown(3) rescue nil
if @container.running?
@container.stop
2014-01-14 12:24:00 +01:00
@container.wait(:stopped, 3)
end
2013-12-05 20:29:25 +01:00
end
def test_container_running
2014-01-14 12:24:00 +01:00
@container.wait(:running, 3)
2013-12-05 20:29:25 +01:00
assert(@container.init_pid > 1)
assert(@container.running?)
2014-01-14 12:24:00 +01:00
assert_equal(:running, @container.state)
2013-12-05 20:29:25 +01:00
end
2014-01-28 18:08:10 +01:00
def test_container_config_item
key = 'lxc.network.0.type'
assert_equal('veth', @container.running_config_item(key))
end
2013-12-05 20:29:25 +01:00
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)
path = "/tmp/tc_lxc_running_ifconfig_eth0.#{Process.pid}"
file = File.open(path, 'w+')
begin
opts = {
:wait => true,
:stdout => file,
:namespaces => LXC::CLONE_NEWNET | LXC::CLONE_NEWUTS,
}
@container.attach(opts) do
LXC.run_command('ifconfig eth0')
end
file.rewind
assert_match(/^eth0\s+Link\sencap:Ethernet\s+HWaddr\s/, file.readline)
ensure
file.close
File.unlink(path)
2013-12-05 20:29:25 +01:00
end
end
def test_container_cgroups
max_mem = @container.cgroup_item('memory.max_usage_in_bytes')
cur_lim = @container.cgroup_item('memory.limit_in_bytes')
2014-01-14 11:54:43 +01:00
assert_nothing_raised(LXC::Error) do
@container.set_cgroup_item('memory.limit_in_bytes', max_mem)
end
assert_not_equal(cur_lim, @container.cgroup_item('memory.limit_in_bytes'))
end
def test_container_freeze
@container.freeze
2014-01-14 12:24:00 +01:00
@container.wait(:frozen, 3)
assert(@container.init_pid > 1)
assert(@container.running?)
2014-01-14 12:24:00 +01:00
assert_equal(:frozen, @container.state)
@container.unfreeze
2014-01-14 12:24:00 +01:00
@container.wait(:running, 3)
assert(@container.init_pid > 1)
assert(@container.running?)
2014-01-14 12:24:00 +01:00
assert_equal(:running, @container.state)
end
def test_container_clone
teardown
assert_nil(@container.init_pid)
assert(!@container.running?)
2014-01-14 12:24:00 +01:00
assert_equal(:stopped, @container.state)
assert_nothing_raised do
begin
2013-12-06 17:43:19 +01:00
clone = @container.clone('test_clone')
clone.start
clone.stop
ensure
clone.destroy
end
end
end
2013-12-23 15:03:54 +01:00
def test_container_listed
containers = LXC.list_containers
assert(containers.length > 0)
assert(containers.include?(@name))
2013-12-23 15:03:54 +01:00
end
2013-12-05 20:29:25 +01:00
end