convert test/unit based tests to rspec based tests
This commit is contained in:
parent
3e488b56be
commit
07ae2713e6
8
Rakefile
8
Rakefile
|
@ -1,7 +1,7 @@
|
||||||
require 'rake/extensiontask'
|
require 'rake/extensiontask'
|
||||||
require 'rake/testtask'
|
|
||||||
|
|
||||||
require 'rubygems/package_task'
|
require 'rubygems/package_task'
|
||||||
|
require 'rspec/core/rake_task'
|
||||||
|
|
||||||
spec = Gem::Specification.load('ruby-lxc.gemspec')
|
spec = Gem::Specification.load('ruby-lxc.gemspec')
|
||||||
Gem::PackageTask.new(spec) do |pkg|
|
Gem::PackageTask.new(spec) do |pkg|
|
||||||
|
@ -11,8 +11,6 @@ Rake::ExtensionTask.new('lxc', spec) do |ext|
|
||||||
ext.lib_dir = 'lib/lxc'
|
ext.lib_dir = 'lib/lxc'
|
||||||
end
|
end
|
||||||
|
|
||||||
Rake::TestTask.new do |t|
|
RSpec::Core::RakeTask.new(:spec) do |t|
|
||||||
t.libs << 'test'
|
t.pattern = %w{spec/**/*_spec.rb}
|
||||||
t.test_files = FileList['test/test_*.rb']
|
|
||||||
t.verbose = true
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,6 +14,7 @@ Gem::Specification.new do |s|
|
||||||
s.has_rdoc = false
|
s.has_rdoc = false
|
||||||
|
|
||||||
s.add_development_dependency "rake-compiler"
|
s.add_development_dependency "rake-compiler"
|
||||||
|
s.add_development_dependency "rspec"
|
||||||
|
|
||||||
s.description = <<-EOF
|
s.description = <<-EOF
|
||||||
Ruby-LXC is a Ruby binding for the liblxc library, allowing
|
Ruby-LXC is a Ruby binding for the liblxc library, allowing
|
||||||
|
|
38
spec/lxc/container/create_spec.rb
Normal file
38
spec/lxc/container/create_spec.rb
Normal file
|
@ -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
|
54
spec/lxc/container/freeze_spec.rb
Normal file
54
spec/lxc/container/freeze_spec.rb
Normal file
|
@ -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
|
93
spec/lxc/container/running_spec.rb
Normal file
93
spec/lxc/container/running_spec.rb
Normal file
|
@ -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
|
32
spec/lxc/container/undefined_spec.rb
Normal file
32
spec/lxc/container/undefined_spec.rb
Normal file
|
@ -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
|
16
spec/lxc_spec.rb
Normal file
16
spec/lxc_spec.rb
Normal file
|
@ -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
|
44
spec/spec_helper.rb
Normal file
44
spec/spec_helper.rb
Normal file
|
@ -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
|
|
@ -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
|
|
|
@ -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
|
|
|
@ -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
|
|
|
@ -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
|
|
Loading…
Reference in a new issue