start ruby port of java unit tests

This commit is contained in:
Doug MacEachern 2009-12-24 07:00:45 -08:00
parent 1e2f84f631
commit 1c049cfd33
8 changed files with 180 additions and 3 deletions

View File

@ -1,5 +1,6 @@
require 'rubygems' require 'rubygems'
require 'rake/gempackagetask' require 'rake/gempackagetask'
require 'rake/testtask'
#so we can: ssh host rake -f $hudson_workspace/sigar/Rakefile #so we can: ssh host rake -f $hudson_workspace/sigar/Rakefile
Dir.chdir(File.dirname(__FILE__)) Dir.chdir(File.dirname(__FILE__))
@ -40,6 +41,8 @@ Rake::GemPackageTask.new(spec) do |pkg|
pkg.gem_spec = spec pkg.gem_spec = spec
end end
task :default => :test
task :make_spec do task :make_spec do
File.open("#{GEM}.gemspec", "w") do |file| File.open("#{GEM}.gemspec", "w") do |file|
file.puts spec.to_ruby file.puts spec.to_ruby
@ -54,9 +57,11 @@ end
desc 'Build sigar extension' desc 'Build sigar extension'
task :build do task :build do
in_ext(); in_ext();
unless system("ruby extconf.rb") unless File.exists? "Makefile"
STDERR.puts "Failed to configure" unless system("ruby extconf.rb")
break STDERR.puts "Failed to configure"
break
end
end end
unless system(MAKE) unless system(MAKE)
STDERR.puts 'Failed to ' + MAKE STDERR.puts 'Failed to ' + MAKE
@ -64,6 +69,13 @@ task :build do
end end
end end
Rake::TestTask.new do |t|
in_ext()
t.pattern = 'test/*_test.rb'
end
task :test => [:build]
desc 'Clean sigar extension' desc 'Clean sigar extension'
task :clean do task :clean do
in_ext() in_ext()

View File

@ -0,0 +1,24 @@
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'helper'
class CpuTest < Test::Unit::TestCase
def check_cpu(cpu)
assert_gt_eq_zero cpu.user, "user"
assert_gt_eq_zero cpu.sys, "sys"
assert_gt_eq_zero cpu.idle, "idle"
assert_gt_eq_zero cpu.wait, "wait"
assert_gt_eq_zero cpu.irq, "irq"
assert_gt_eq_zero cpu.soft_irq, "soft_irq"
assert_gt_eq_zero cpu.stolen, "stolen"
assert_gt_zero cpu.total, "total"
end
def test_cpu
sigar = Sigar.new
check_cpu sigar.cpu
sigar.cpu_list.each do |cpu|
check_cpu cpu
end
end
end

View File

@ -0,0 +1,27 @@
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'helper'
class FileSystemTest < Test::Unit::TestCase
def test_file_system
sigar = Sigar.new
sigar.file_system_list.each do |fs|
assert_length fs.dev_name, "dev_name"
assert_length fs.dir_name, "dir_name"
assert_length fs.type_name, "type_name"
assert_length fs.sys_type_name, "sys_type_name"
assert fs.options.length >= 0, "options"
begin
usage = sigar.file_system_usage fs.dir_name
rescue err
if fs.type == Sigar::FSTYPE_LOCAL_DISK
raise err
end
# else ok, e.g. floppy drive on windows
next
end
end
end
end

View File

@ -0,0 +1,41 @@
require 'test/unit'
require 'rbsigar'
module Test::Unit::Assertions
def assert_gt_eq_zero(value, message)
message = build_message message, '<?> is not >= 0.', value
assert_block message do
value >= 0
end
end
def assert_gt_zero(value, message)
message = build_message message, '<?> is not > 0.', value
assert_block message do
value > 0
end
end
def assert_eq(expected, actual, message)
message = build_message message, '<?> != <?>.', expected, actual
assert_block message do
expected == actual
end
end
def assert_length(value, message)
message = build_message message, '<?>.length > 0.', value
assert_block message do
value.length > 0
end
end
def assert_any(value, message)
message = build_message message, '<?> is anything.', value
assert_block message do
true
end
end
end

View File

@ -0,0 +1,14 @@
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'helper'
class LoadAvgTest < Test::Unit::TestCase
def test_loadavg
begin
loadavg = Sigar.new.loadavg
rescue #XXX SigarNotImplemented (win32)
return
end
assert loadavg.length == 3
end
end

View File

@ -0,0 +1,29 @@
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'helper'
class MemTest < Test::Unit::TestCase
def test_mem
sigar = Sigar.new
mem = sigar.mem
assert_gt_zero mem.total, "total"
assert_gt_zero mem.used, "used"
assert_gt_zero mem.used_percent, "used_percent"
assert mem.used_percent <= 100, "used_percent <= 100"
assert_gt_eq_zero mem.free_percent, "free_percent"
assert mem.free_percent < 100, "free_percent < 100"
assert_gt_zero mem.free, "free"
assert_gt_zero mem.actual_used, "actual_used"
assert_gt_zero mem.actual_free, "actual_free"
assert_gt_zero mem.ram, "ram"
assert (mem.ram % 8) == 0
end
end

View File

@ -0,0 +1,20 @@
require 'test/unit'
require 'rbsigar'
class SwapTest < Test::Unit::TestCase
def test_swap
sigar = Sigar.new
swap = sigar.swap
assert_gt_eq_zero swap.total, "total"
assert_gt_eq_zero swap.used, "used"
assert_gt_eq_zero swap.free, "free"
assert_eq swap.total - swap.used, swap.free, "total-used==free"
assert_any swap.page_in, "page_in"
assert_any swap.page_out, "page_out"
end
end

View File

@ -0,0 +1,10 @@
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'helper'
class UptimeTest < Test::Unit::TestCase
def test_uptime
uptime = Sigar.new.uptime
assert_gt_zero uptime.uptime, "uptime"
end
end