From 1c049cfd33e8b8f705d2f13d4ce27d5d5fc9c0f3 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Thu, 24 Dec 2009 07:00:45 -0800 Subject: [PATCH] start ruby port of java unit tests --- Rakefile | 18 +++++++++-- bindings/ruby/test/cpu_test.rb | 24 +++++++++++++++ bindings/ruby/test/file_system_test.rb | 27 +++++++++++++++++ bindings/ruby/test/helper.rb | 41 ++++++++++++++++++++++++++ bindings/ruby/test/loadavg_test.rb | 14 +++++++++ bindings/ruby/test/mem_test.rb | 29 ++++++++++++++++++ bindings/ruby/test/swap_test.rb | 20 +++++++++++++ bindings/ruby/test/uptime_test.rb | 10 +++++++ 8 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 bindings/ruby/test/cpu_test.rb create mode 100644 bindings/ruby/test/file_system_test.rb create mode 100644 bindings/ruby/test/helper.rb create mode 100644 bindings/ruby/test/loadavg_test.rb create mode 100644 bindings/ruby/test/mem_test.rb create mode 100644 bindings/ruby/test/swap_test.rb create mode 100644 bindings/ruby/test/uptime_test.rb diff --git a/Rakefile b/Rakefile index 89f0c3c0..160e894e 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,6 @@ require 'rubygems' require 'rake/gempackagetask' +require 'rake/testtask' #so we can: ssh host rake -f $hudson_workspace/sigar/Rakefile Dir.chdir(File.dirname(__FILE__)) @@ -40,6 +41,8 @@ Rake::GemPackageTask.new(spec) do |pkg| pkg.gem_spec = spec end +task :default => :test + task :make_spec do File.open("#{GEM}.gemspec", "w") do |file| file.puts spec.to_ruby @@ -54,9 +57,11 @@ end desc 'Build sigar extension' task :build do in_ext(); - unless system("ruby extconf.rb") - STDERR.puts "Failed to configure" - break + unless File.exists? "Makefile" + unless system("ruby extconf.rb") + STDERR.puts "Failed to configure" + break + end end unless system(MAKE) STDERR.puts 'Failed to ' + MAKE @@ -64,6 +69,13 @@ task :build do end end +Rake::TestTask.new do |t| + in_ext() + t.pattern = 'test/*_test.rb' +end + +task :test => [:build] + desc 'Clean sigar extension' task :clean do in_ext() diff --git a/bindings/ruby/test/cpu_test.rb b/bindings/ruby/test/cpu_test.rb new file mode 100644 index 00000000..481da4ae --- /dev/null +++ b/bindings/ruby/test/cpu_test.rb @@ -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 diff --git a/bindings/ruby/test/file_system_test.rb b/bindings/ruby/test/file_system_test.rb new file mode 100644 index 00000000..c70b1d76 --- /dev/null +++ b/bindings/ruby/test/file_system_test.rb @@ -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 diff --git a/bindings/ruby/test/helper.rb b/bindings/ruby/test/helper.rb new file mode 100644 index 00000000..32ff247f --- /dev/null +++ b/bindings/ruby/test/helper.rb @@ -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 + diff --git a/bindings/ruby/test/loadavg_test.rb b/bindings/ruby/test/loadavg_test.rb new file mode 100644 index 00000000..b1aca4be --- /dev/null +++ b/bindings/ruby/test/loadavg_test.rb @@ -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 diff --git a/bindings/ruby/test/mem_test.rb b/bindings/ruby/test/mem_test.rb new file mode 100644 index 00000000..3f4e2f57 --- /dev/null +++ b/bindings/ruby/test/mem_test.rb @@ -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 diff --git a/bindings/ruby/test/swap_test.rb b/bindings/ruby/test/swap_test.rb new file mode 100644 index 00000000..565161c1 --- /dev/null +++ b/bindings/ruby/test/swap_test.rb @@ -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 diff --git a/bindings/ruby/test/uptime_test.rb b/bindings/ruby/test/uptime_test.rb new file mode 100644 index 00000000..501a0937 --- /dev/null +++ b/bindings/ruby/test/uptime_test.rb @@ -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