From 07ae2713e69abb58b58312cc6e98f65d47bd6248 Mon Sep 17 00:00:00 2001 From: Ranjib Dey Date: Fri, 27 Dec 2013 02:44:53 -0800 Subject: [PATCH] convert test/unit based tests to rspec based tests --- Rakefile | 8 +- ruby-lxc.gemspec | 1 + spec/lxc/container/create_spec.rb | 38 ++++++++++ spec/lxc/container/freeze_spec.rb | 54 ++++++++++++++ spec/lxc/container/running_spec.rb | 93 +++++++++++++++++++++++ spec/lxc/container/undefined_spec.rb | 32 ++++++++ spec/lxc_spec.rb | 16 ++++ spec/spec_helper.rb | 44 +++++++++++ test/test_lxc_class_methods.rb | 25 ------- test/test_lxc_created.rb | 46 ------------ test/test_lxc_running.rb | 107 --------------------------- test/test_lxc_undefined.rb | 32 -------- 12 files changed, 281 insertions(+), 215 deletions(-) create mode 100644 spec/lxc/container/create_spec.rb create mode 100644 spec/lxc/container/freeze_spec.rb create mode 100644 spec/lxc/container/running_spec.rb create mode 100644 spec/lxc/container/undefined_spec.rb create mode 100644 spec/lxc_spec.rb create mode 100644 spec/spec_helper.rb delete mode 100644 test/test_lxc_class_methods.rb delete mode 100644 test/test_lxc_created.rb delete mode 100644 test/test_lxc_running.rb delete mode 100644 test/test_lxc_undefined.rb diff --git a/Rakefile b/Rakefile index 8512208..f52f58e 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,7 @@ require 'rake/extensiontask' -require 'rake/testtask' require 'rubygems/package_task' +require 'rspec/core/rake_task' spec = Gem::Specification.load('ruby-lxc.gemspec') Gem::PackageTask.new(spec) do |pkg| @@ -11,8 +11,6 @@ Rake::ExtensionTask.new('lxc', spec) do |ext| ext.lib_dir = 'lib/lxc' end -Rake::TestTask.new do |t| - t.libs << 'test' - t.test_files = FileList['test/test_*.rb'] - t.verbose = true +RSpec::Core::RakeTask.new(:spec) do |t| + t.pattern = %w{spec/**/*_spec.rb} end diff --git a/ruby-lxc.gemspec b/ruby-lxc.gemspec index 1eaa95a..51896a8 100644 --- a/ruby-lxc.gemspec +++ b/ruby-lxc.gemspec @@ -14,6 +14,7 @@ Gem::Specification.new do |s| s.has_rdoc = false s.add_development_dependency "rake-compiler" + s.add_development_dependency "rspec" s.description = <<-EOF Ruby-LXC is a Ruby binding for the liblxc library, allowing diff --git a/spec/lxc/container/create_spec.rb b/spec/lxc/container/create_spec.rb new file mode 100644 index 0000000..71eecb4 --- /dev/null +++ b/spec/lxc/container/create_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +describe LXC::Container do + + it '#defined should be true for an existing container' do + expect(container.defined?).to be_true + end + + it '#name should return container name' do + expect(container.name).to eq(container_name) + end + + it '#name should be same as utsname config parameter' do + expect(container.name).to eq(container.config_item('lxc.utsname')) + end + + it '#config_item should allow setting and retrival of container specific config' do + 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 + expect(container.config_item('lxc.cap.drop')).to eq(capdrop) + end + + it '#keys should allow retrival of network information' do + expect(container.keys('lxc.network.0')).to include('name') + expect(container.config_item('lxc.network.0.hwaddr')).to match(/^00:16:3e:/) + end + + it '#rename should allow renaming existing container' do + new_name = "renamed_#{container_name}" + renamed = container.rename(new_name) + expect(renamed.name).to eq(new_name) + rerenamed = renamed.rename(container_name) + expect(rerenamed.name).to eq(container_name) + end +end diff --git a/spec/lxc/container/freeze_spec.rb b/spec/lxc/container/freeze_spec.rb new file mode 100644 index 0000000..9d52b96 --- /dev/null +++ b/spec/lxc/container/freeze_spec.rb @@ -0,0 +1,54 @@ +require 'spec_helper' + +describe LXC::Container do + + before(:all) do + container.start + container.wait('RUNNING',3) + end + + after(:all) do + container.shutdown + container.wait('STOPPED',3) + end + + context '#freeze' do + + before(:all) do + container.freeze + container.wait('FROZEN',3) + end + + it 'should have init pid > 1' do + expect(container.init_pid).to be > 1 + end + + it '#running? should be true' do + expect(container).to be_running + end + + it 'should be in frozen state' do + expect(container.state).to eq('FROZEN') + end + end + + context '#unfreeze' do + + before(:all) do + container.unfreeze + container.wait('RUNNING', 3) + end + + it 'should have init pid > 0' do + expect(container.init_pid).to be > 1 + end + + it 'running? should return true' do + expect(container).to be_running + end + + it 'state should be running' do + expect(container.state).to eq('RUNNING') + end + end +end diff --git a/spec/lxc/container/running_spec.rb b/spec/lxc/container/running_spec.rb new file mode 100644 index 0000000..2146960 --- /dev/null +++ b/spec/lxc/container/running_spec.rb @@ -0,0 +1,93 @@ +require 'spec_helper' +require 'timeout' +require 'tempfile' + +describe LXC::Container do + + before(:all) do + container.start + container.wait('RUNNING', 3) + end + + after(:all) do + container.stop + container.wait('STOPPED', 3) + end + + context 'when container is running' do + + it 'should be have init pid greater that 0' do + expect(container.init_pid).to be > 1 + end + + it '#running? should return true' do + expect(container).to be_running + end + + it 'its state should be "RUNNING"' do + expect(container.state).to eq('RUNNING') + end + + it 'should have loop back interfacce' do + expect(container.interfaces).to include('lo') + end + + it 'should have eth0 interfacce' do + expect(container.interfaces).to include('eth0') + end + end + + context '#ip_addresses' do + + it 'should have a valid ip address' do + Timeout::timeout(10) do + while container.ip_addresses.empty? + sleep 1 + end + end + expect(container.ip_addresses).to_not be_empty + path = "/tmp/tc_lxc_running_ifconfig_eth0.#{Process.pid}" + file = File.open(path, 'w+') + begin + nses = LXC::CLONE_NEWNET | LXC::CLONE_NEWUTS + container.attach(wait: true, stdout:file, namespaces: nses) do + LXC.run_command('ifconfig eth0') + end + file.rewind + expect(file.readline).to match(/^eth0\s+Link\sencap:Ethernet\s+HWaddr\s/) + ensure + file.close + File.unlink(path) + end + end + end + + it 'should allow setting cgroup values' do + max_mem = container.cgroup_item('memory.max_usage_in_bytes') + cur_lim = container.cgroup_item('memory.limit_in_bytes') + expect(container.set_cgroup_item('memory.limit_in_bytes', max_mem)).to_not be_nil + expect(container.cgroup_item('memory.limit_in_bytes')).to_not eq(cur_lim) + end + + context 'cloning a container' do + + it 'should allow cloning container' do + if container.running? + container.stop + container.wait('STOPPED', 3) + end + expect(container.init_pid).to be_nil + expect(container).to_not be_running + expect(container.state).to eq('STOPPED') + expect do + begin + clone = container.clone('test_clone') + clone.start + clone.stop + ensure + clone.destroy + end + end.to_not raise_error + end + end +end diff --git a/spec/lxc/container/undefined_spec.rb b/spec/lxc/container/undefined_spec.rb new file mode 100644 index 0000000..aa12a16 --- /dev/null +++ b/spec/lxc/container/undefined_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +describe LXC::Container do + + context 'container that is not defined' do + + let(:undefined_container) do + LXC::Container.new('test_undefined') + end + + it '#config_file_name' do + config_path = File.join(LXC.default_config_path, 'test_undefined', 'config') + expect(undefined_container.config_file_name).to eq(config_path) + end + + it '#defined? should be false for undefined container' do + expect(undefined_container.defined?).to be_false + end + + it 'should not have an init pid' do + expect(undefined_container.init_pid).to be_nil + end + + it 'should not be in running state' do + expect(undefined_container).to_not be_running + end + + it 'should be in stopped state' do + expect(undefined_container.state).to eq('STOPPED') + end + end +end diff --git a/spec/lxc_spec.rb b/spec/lxc_spec.rb new file mode 100644 index 0000000..ada569f --- /dev/null +++ b/spec/lxc_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe LXC do + + it '#list_containers should return non-empty list' do + expect(LXC.list_containers).to_not be_empty + end + + it '#arch_to_personality should convert 32 bit arch to linux32' do + expect(LXC.arch_to_personality('x86')).to eq(:linux32) + end + + it '#arch_to_personality should convert 32 bit arch to linux' do + expect(LXC.arch_to_personality('x86_64')).to eq(:linux) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..414f613 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,44 @@ +require 'rspec' +require 'lxc' + +module LXCSpecHelper + + extend self + + def spawn_test_container + validate_root! + container.create('ubuntu') unless container.defined? + end + + def container_name + 'test' + end + + def container + LXC::Container.new(container_name) + end + + def destroy_test_container + validate_root! + container.destroy + end + + def validate_root! + if Process::Sys::geteuid != 0 + raise 'This test must be ran as root' + end + end +end + +RSpec.configure do |config| + + config.include LXCSpecHelper + + config.before(:suite) do + LXCSpecHelper.spawn_test_container + end + + config.after(:suite) do + LXCSpecHelper.destroy_test_container + end +end diff --git a/test/test_lxc_class_methods.rb b/test/test_lxc_class_methods.rb deleted file mode 100644 index 736ce4f..0000000 --- a/test/test_lxc_class_methods.rb +++ /dev/null @@ -1,25 +0,0 @@ -$:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib')) - -require 'test/unit' -require 'lxc' -require 'timeout' - -class TestLXCClassMethods < Test::Unit::TestCase - def setup - if Process::Sys::geteuid != 0 - raise 'This test must be ran as root' - end - @name = 'test' - @container = LXC::Container.new(@name) - @container.create('ubuntu') unless @container.defined? - end - - def test_list_containers - assert_equal([@name], LXC.list_containers) - end - - def test_arch_to_personality - assert_equal(:linux32, LXC.arch_to_personality('x86')) - assert_equal(:linux, LXC.arch_to_personality('x86_64')) - end -end diff --git a/test/test_lxc_created.rb b/test/test_lxc_created.rb deleted file mode 100644 index 904fe55..0000000 --- a/test/test_lxc_created.rb +++ /dev/null @@ -1,46 +0,0 @@ -$:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib')) - -require 'test/unit' -require 'lxc' - -class TestLXCCreated < Test::Unit::TestCase - def setup - if Process::Sys::geteuid != 0 - raise 'This test must be ran as root' - end - @name = 'test' - @container = LXC::Container.new(@name) - @container.create('ubuntu') 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 - - def test_container_rename - new_name = "renamed_#{@name}" - renamed = @container.rename(new_name) - assert_equal(new_name, renamed.name) - rerenamed = renamed.rename(@name) - assert_equal(@name, rerenamed.name) - end -end diff --git a/test/test_lxc_running.rb b/test/test_lxc_running.rb deleted file mode 100644 index d85579b..0000000 --- a/test/test_lxc_running.rb +++ /dev/null @@ -1,107 +0,0 @@ -$:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib')) - -require 'test/unit' -require 'tempfile' -require 'lxc' - -class TestLXCRunning < Test::Unit::TestCase - def setup - if Process::Sys::geteuid != 0 - raise 'This test must be ran as root' - end - @name = 'test' - @container = LXC::Container.new(@name) - @container.create('ubuntu') unless @container.defined? - @container.start - end - - def teardown - @container.shutdown(3) - if @container.running? - @container.stop - @container.wait('STOPPED', 3) - end - 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) - 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) - 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') - assert_not_nil(@container.set_cgroup_item('memory.limit_in_bytes', max_mem)) - assert_not_equal(cur_lim, @container.cgroup_item('memory.limit_in_bytes')) - end - - def test_container_freeze - @container.freeze - @container.wait('FROZEN', 3) - assert(@container.init_pid > 1) - assert(@container.running?) - assert_equal('FROZEN', @container.state) - - @container.unfreeze - @container.wait('RUNNING', 3) - assert(@container.init_pid > 1) - assert(@container.running?) - assert_equal('RUNNING', @container.state) - end - - def test_container_clone - teardown - assert_nil(@container.init_pid) - assert(!@container.running?) - assert_equal('STOPPED', @container.state) - - assert_nothing_raised do - begin - clone = @container.clone('test_clone') - clone.start - clone.stop - ensure - clone.destroy - end - end - end - - def test_container_listed - containers = LXC.list_containers - assert_equal(1, containers.length) - assert_equal(@name, containers.first) - end -end diff --git a/test/test_lxc_undefined.rb b/test/test_lxc_undefined.rb deleted file mode 100644 index 2c49f19..0000000 --- a/test/test_lxc_undefined.rb +++ /dev/null @@ -1,32 +0,0 @@ -$:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib')) - -require 'test/unit' -require 'lxc' - -class TestLXCUndefined < Test::Unit::TestCase - def setup - @name = 'test_undefined' - @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(!@container.defined?) - end - - def test_container_init_pid - assert_nil(@container.init_pid) - end - - def test_container_not_running - assert(!@container.running?) - end - - def test_container_stopped - assert_equal('STOPPED', @container.state) - end -end