From c59737a63bce70186c32c14b4a5c5980279e88f5 Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Fri, 21 Sep 2007 22:47:30 -0700 Subject: [PATCH] more tests for task, real valid? method for task --- Manifest.txt | 1 + lib/god/process.rb | 6 --- lib/god/task.rb | 22 +++++++- lib/god/watch.rb | 2 +- test/test_conditions_http_response_code.rb | 65 +++++++++++----------- test/test_process.rb | 9 ---- test/test_task.rb | 86 ++++++++++++++++++++++++++++++ 7 files changed, 143 insertions(+), 48 deletions(-) create mode 100644 test/test_task.rb diff --git a/Manifest.txt b/Manifest.txt index 5932320..ea50fc0 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -87,6 +87,7 @@ test/test_reporter.rb test/test_server.rb test/test_sugar.rb test/test_system_process.rb +test/test_task.rb test/test_timeline.rb test/test_timer.rb test/test_trigger.rb diff --git a/lib/god/process.rb b/lib/god/process.rb index 224d66b..3dc08ef 100644 --- a/lib/god/process.rb +++ b/lib/god/process.rb @@ -26,12 +26,6 @@ module God valid = true - # a name must be specified - if self.name.nil? - valid = false - LOG.log(self, :error, "No name was specified") - end - # a start command must be specified if self.start.nil? valid = false diff --git a/lib/god/task.rb b/lib/god/task.rb index 251e52a..4f14882 100644 --- a/lib/god/task.rb +++ b/lib/god/task.rb @@ -35,7 +35,27 @@ module God end def valid? - true + valid = true + + # a name must be specified + if self.name.nil? + valid = false + LOG.log(self, :error, "No name was specified") + end + + # valid_states must be specified + if self.valid_states.nil? + valid = false + LOG.log(self, :error, "No valid_states array was specified") + end + + # valid_states must be specified + if self.initial_state.nil? + valid = false + LOG.log(self, :error, "No initial_state was specified") + end + + valid end ########################################################################### diff --git a/lib/god/watch.rb b/lib/god/watch.rb index 347fb9e..d4b2506 100644 --- a/lib/god/watch.rb +++ b/lib/god/watch.rb @@ -29,7 +29,7 @@ module God end def valid? - @process.valid? + super && @process.valid? end ########################################################################### diff --git a/test/test_conditions_http_response_code.rb b/test/test_conditions_http_response_code.rb index 78dd8b6..e0c9b44 100644 --- a/test/test_conditions_http_response_code.rb +++ b/test/test_conditions_http_response_code.rb @@ -9,6 +9,8 @@ class TestHttpResponseCode < Test::Unit::TestCase c.path = '/' c.timeout = 10 c.code_is = 200 + c.times = 1 + yield(c) if block_given? c.prepare c end @@ -21,44 +23,42 @@ class TestHttpResponseCode < Test::Unit::TestCase end def test_valid_should_return_false_if_both_code_is_and_code_is_not_are_set - c = valid_condition - c.code_is_not = 500 + c = valid_condition do |cc| + cc.code_is_not = 500 + end no_stdout { assert !c.valid? } end def test_valid_should_return_false_if_no_host_set - c = valid_condition - c.host = nil + c = valid_condition do |cc| + cc.host = nil + end no_stdout { assert !c.valid? } end def test_valid_should_return_false_if_no_port_set - c = valid_condition - c.port = nil + c = valid_condition do |cc| + cc.port = nil + end no_stdout { assert !c.valid? } end def test_valid_should_return_false_if_no_path_set - c = valid_condition - c.path = nil + c = valid_condition do |cc| + cc.path = nil + end no_stdout { assert !c.valid? } end def test_valid_should_return_false_if_no_timeout_set - c = valid_condition - c.timeout = nil + c = valid_condition do |cc| + cc.timeout = nil + end no_stdout { assert !c.valid? } end # test - def test_real - c = valid_condition - c.code_is = [302] - c.port = 3000 - assert_equal true, c.test - end - def test_test_should_return_false_if_code_is_is_set_to_200_but_response_is_500 c = valid_condition Net::HTTP.expects(:start).yields(stub(:read_timeout= => nil, :head => stub(:code => 500))) @@ -66,9 +66,10 @@ class TestHttpResponseCode < Test::Unit::TestCase end def test_test_should_return_false_if_code_is_not_is_set_to_200_and_response_is_200 - c = valid_condition - c.code_is = nil - c.code_is_not = [200] + c = valid_condition do |cc| + cc.code_is = nil + cc.code_is_not = [200] + end Net::HTTP.expects(:start).yields(stub(:read_timeout= => nil, :head => stub(:code => 200))) assert_equal false, c.test end @@ -80,31 +81,33 @@ class TestHttpResponseCode < Test::Unit::TestCase end def test_test_should_return_false_if_code_is_not_is_set_to_200_but_response_is_500 - c = valid_condition - c.code_is = nil - c.code_is_not = [200] + c = valid_condition do |cc| + cc.code_is = nil + cc.code_is_not = [200] + end Net::HTTP.expects(:start).yields(stub(:read_timeout= => nil, :head => stub(:code => 500))) assert_equal true, c.test end def test_test_should_return_false_if_code_is_is_set_to_200_but_response_times_out c = valid_condition - Net::HTTP.expects(:start).raises(Timeout::Error) + Net::HTTP.expects(:start).raises(Timeout::Error, '') assert_equal false, c.test end def test_test_should_return_true_if_code_is_not_is_set_to_200_and_response_times_out - c = valid_condition - c.code_is = nil - c.code_is_not = [200] - Net::HTTP.expects(:start).raises(Timeout::Error) + c = valid_condition do |cc| + cc.code_is = nil + cc.code_is_not = [200] + end + Net::HTTP.expects(:start).raises(Timeout::Error, '') assert_equal true, c.test end def test_test_should_return_true_if_code_is_is_set_to_200_and_response_is_200_twice_for_times_two_of_two - c = valid_condition - c.times = [2, 2] - c.prepare + c = valid_condition do |cc| + cc.times = [2, 2] + end Net::HTTP.expects(:start).yields(stub(:read_timeout= => nil, :head => stub(:code => 200))).times(2) assert_equal false, c.test assert_equal true, c.test diff --git a/test/test_process.rb b/test/test_process.rb index 274a252..1966a5b 100644 --- a/test/test_process.rb +++ b/test/test_process.rb @@ -99,15 +99,6 @@ class TestProcessDaemon < Test::Unit::TestCase # valid? - def test_valid_should_return_false_if_no_name - @p.name = nil - @p.start = 'bar' - @p.stop = 'baz' - no_stdout do - assert !@p.valid? - end - end - def test_valid_should_return_false_if_no_start @p.name = 'foo' @p.stop = 'baz' diff --git a/test/test_task.rb b/test/test_task.rb new file mode 100644 index 0000000..f0cd8d3 --- /dev/null +++ b/test/test_task.rb @@ -0,0 +1,86 @@ +require File.dirname(__FILE__) + '/helper' + +class TestTask < Test::Unit::TestCase + def setup + God.internal_init + @task = Task.new + @task.name = 'foo' + @task.valid_states = [:foo, :bar] + @task.initial_state = :foo + @task.interval = 5 + @task.prepare + end + + # valid? + + def test_valid_should_return_false_if_no_name + @task.name = nil + no_stdout do + assert !@task.valid? + end + end + + def test_valid_should_return_false_if_no_valid_states + @task.valid_states = nil + no_stdout do + assert !@task.valid? + end + end + + def test_valid_should_return_false_if_no_initial_state + @task.initial_state = nil + no_stdout do + assert !@task.valid? + end + end + + # transition + + def test_transition_should_be_always_if_no_block_was_given + @task.transition(:foo, :bar) + + assert 1, @task.metrics.size + assert Conditions::Always, @task.metrics.keys.first.class + end + + # method_missing + + def test_method_missing_should_create_accessor_for_states + assert_nothing_raised do + @task.foo = 'testing' + end + end + + def test_method_missing_should_raise_for_non_states + assert_raise NoMethodError do + @task.baz = 5 + end + end + + def test_method_missing_should_raise_for_non_setters + assert_raise NoMethodError do + @task.baz + end + end + + # action + + def test_action_should_send_string_commands_to_system + @task.foo = 'foo' + @task.expects(:system).with('foo') + no_stdout { @task.action(:foo, nil) } + end + + def test_action_should_cal_lambda_commands + @task.foo = lambda { } + @task.foo.expects(:call) + no_stdout { @task.action(:foo, nil) } + end + + def test_action_should_raise_not_implemented_on_non_string_or_lambda_action + assert_raise NotImplementedError do + @task.foo = 7 + @task.action(:foo, nil) + end + end +end \ No newline at end of file -- 2.11.4.GIT