chain-reactor 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'chainfile_parser'
4
+ require 'helpers'
5
+ require 'conf'
6
+
7
+ # Test case for the <tt>ChainReactor::ChainfileParser</tt> class.
8
+ class TestChainfileParser < Test::Unit::TestCase
9
+ include ChainReactor::TestHelpers
10
+
11
+ def test_single_reaction_is_added_to_reactor
12
+ chain = <<-chain
13
+ react_to('192.168.0.1') { |data| puts data.inspect }
14
+ chain
15
+
16
+ parser = ChainReactor::ChainfileParser.new(File.new(chain),
17
+ ChainReactor::Conf.new({}),
18
+ get_logger)
19
+ reactor = parser.parse
20
+ assert_equal 1, reactor.reactions_for('192.168.0.1').length
21
+ end
22
+
23
+ def test_multiple_reactions_are_added_to_reactor
24
+ chain = <<-chain
25
+ react_to('192.168.0.1') { |data| puts data.inspect }
26
+ react_to('192.168.0.2') { |data| puts data.inspect }
27
+ react_to('192.168.0.2') { |data| puts data.inspect }
28
+ chain
29
+
30
+ parser = ChainReactor::ChainfileParser.new(File.new(chain),
31
+ ChainReactor::Conf.new({}),
32
+ get_logger)
33
+ reactor = parser.parse
34
+ assert_equal 1, reactor.reactions_for('192.168.0.1').length
35
+ assert_equal 2, reactor.reactions_for('192.168.0.2').length
36
+ end
37
+
38
+ def test_multiple_reactions_are_added_to_reactor_with_alt_syntax
39
+ chain = <<-chain
40
+ react_to(['192.168.0.1','192.168.0.2']) { |data| puts data.inspect }
41
+ chain
42
+
43
+ parser = ChainReactor::ChainfileParser.new(File.new(chain),
44
+ ChainReactor::Conf.new({}),
45
+ get_logger)
46
+ reactor = parser.parse
47
+ assert_equal 1, reactor.reactions_for('192.168.0.1').length
48
+ assert_equal 1, reactor.reactions_for('192.168.0.2').length
49
+ end
50
+
51
+ def test_single_reaction_with_options_is_added_to_reactor
52
+ chain = <<-chain
53
+ react_to('192.168.0.1', parser: :dummy, required_keys: [:hello,:world]) { |data| puts data.inspect }
54
+ chain
55
+
56
+ parser = ChainReactor::ChainfileParser.new(File.new(chain),
57
+ ChainReactor::Conf.new({}),
58
+ get_logger)
59
+ reactor = parser.parse
60
+ reactions = reactor.reactions_for('192.168.0.1')
61
+ reaction = reactions.first
62
+
63
+ assert_equal 1, reactions.length
64
+ assert_equal :dummy, reaction.options[:parser]
65
+ assert_equal [:hello,:world], reaction.options[:required_keys]
66
+ end
67
+ end
@@ -0,0 +1,57 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'mocha/setup'
4
+ require 'client_connection'
5
+ require 'helpers'
6
+
7
+ # Test case for the <tt>ChainReactor::ClientConnection</tt> class.
8
+ class TestClientConnection < Test::Unit::TestCase
9
+ include ChainReactor::TestHelpers
10
+
11
+ # Create a mock TCPSocket object loaded with the data specified.
12
+ def mock_socket(ip,port)
13
+ addr = ["",port,ip.split('.')]
14
+ sock = stub("client socket")
15
+ peername = stub("peer name")
16
+ sock.stubs(:getpeername).returns(peername)
17
+ peername.stubs(:unpack).returns(addr)
18
+ sock
19
+ end
20
+
21
+ # Test that the client IP address is readable.
22
+ def test_ip_exists
23
+ ip = "127.0.0.1"
24
+ client = ChainReactor::ClientConnection.new(mock_socket(ip,""),get_logger)
25
+ assert_equal ip, client.ip
26
+ end
27
+
28
+ # Test that the port number is readable.
29
+ def test_port_exists
30
+ port = 20000
31
+ client = ChainReactor::ClientConnection.new(mock_socket("",port),get_logger)
32
+ assert_equal port, client.port
33
+ end
34
+ #
35
+ # Test that the say method uses the puts method on the socket.
36
+ def test_say_uses_socket_puts
37
+ my_string = "This is a string"
38
+
39
+ # Create mock socket that checks for method calls
40
+ socket = mock_socket("","")
41
+ socket.expects(:puts).once.with(my_string)
42
+
43
+ client = ChainReactor::ClientConnection.new(socket,get_logger)
44
+ client.say(my_string)
45
+ end
46
+
47
+ # Test that the close method uses the close method on the socket.
48
+ def test_close_uses_socket_close
49
+ # Create mock socket that checks for method calls
50
+ socket = mock_socket("","")
51
+ socket.expects(:close).once
52
+
53
+ client = ChainReactor::ClientConnection.new(socket,get_logger)
54
+ client.close
55
+ end
56
+
57
+ end
data/test/test_conf.rb ADDED
@@ -0,0 +1,90 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'conf'
4
+ require 'helpers'
5
+
6
+ # Test case for the <tt>ChainReaction::Conf</tt> class.
7
+ class TestConf < Test::Unit::TestCase
8
+ include ChainReactor::TestHelpers
9
+ def get_conf(cli_params)
10
+ ChainReactor::Conf.new(cli_params)
11
+ end
12
+
13
+ def test_address_from_cli_params
14
+ params = ChainReactor::TestHelpers::Params.new({:address => CliParam.new('192.168.0.1')})
15
+ conf = get_conf params
16
+ assert_equal '192.168.0.1', conf.address
17
+ end
18
+
19
+ def test_address_default
20
+ param = CliParam.new('192.168.0.1')
21
+ param.given = false
22
+ params = Params.new({:address => param})
23
+ conf = get_conf params
24
+ conf.address = '127.0.0.1'
25
+ assert_equal '127.0.0.1', conf.address
26
+ end
27
+
28
+ def test_address_from_default_cli_params
29
+ param = CliParam.new('192.168.0.1')
30
+ param.given = false
31
+ params = Params.new({:address => param})
32
+ conf = get_conf params
33
+ assert_equal '192.168.0.1', conf.address
34
+ end
35
+
36
+ def test_address_raises_error
37
+ conf = ChainReactor::Conf.new(Params.new({}))
38
+ assert_raises(ChainReactor::ConfError) { conf.address }
39
+ end
40
+
41
+ def test_port
42
+ params = Params.new({:port => CliParam.new(20000)})
43
+ conf = get_conf params
44
+ assert_equal 20000, conf.port
45
+ end
46
+
47
+ def test_pid_file
48
+ pid_file = '/path/to/pid.file'
49
+ params = Params.new({:pidfile => CliParam.new(pid_file)})
50
+ conf = get_conf params
51
+ assert_equal pid_file, conf.pid_file
52
+ end
53
+
54
+ def test_log_file
55
+ log_file = '/path/to/log.file'
56
+ params = Params.new({:logfile => CliParam.new(log_file)})
57
+ conf = get_conf params
58
+ assert_equal log_file, conf.log_file
59
+ end
60
+
61
+ def test_multithreaded
62
+ params = Params.new({:multithreaded => CliParam.new(true)})
63
+ conf = get_conf params
64
+ assert_equal true, conf.multithreaded
65
+ end
66
+
67
+ def test_multithreaded_alias
68
+ params = Params.new({:multithreaded => CliParam.new(true)})
69
+ conf = get_conf params
70
+ assert_equal true, conf.multithreaded?
71
+ end
72
+
73
+ def test_verbosity
74
+ params = Params.new({:verbosity => CliParam.new('debug')})
75
+ conf = get_conf params
76
+ assert_equal 'debug', conf.verbosity
77
+ end
78
+
79
+ def test_on_top
80
+ params = Params.new({:ontop => CliParam.new(true)})
81
+ conf = get_conf params
82
+ assert_equal true, conf.on_top
83
+ end
84
+
85
+ def test_on_top_alias
86
+ params = Params.new({:ontop => CliParam.new(true)})
87
+ conf = get_conf params
88
+ assert_equal true, conf.on_top?
89
+ end
90
+ end
@@ -0,0 +1,55 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'mocha/setup'
4
+ require 'parsers/parser'
5
+ require 'parsers/json_parser'
6
+ require 'helpers'
7
+
8
+ # Test case for the <tt>ChainReactor::Parsers::JsonParser</tt> class.
9
+ class TestJsonParser < Test::Unit::TestCase
10
+ include ChainReactor::TestHelpers
11
+
12
+ # Test that a ParseError is raised when an invalid JSON string is
13
+ # passed.
14
+ def test_parse_invalid_json_raises_error
15
+ parser = ChainReactor::Parsers::JsonParser.new get_logger
16
+ ex = assert_raise ChainReactor::Parsers::ParseError do
17
+ parser.parse("not a json",[],false)
18
+ end
19
+ assert_match( /not a valid JSON/, ex.message)
20
+ end
21
+
22
+ # Test that a ParseError is raised when a JSON with invalid keys is
23
+ # passed.
24
+ def test_parse_wrong_json_raises_error
25
+ parser = ChainReactor::Parsers::JsonParser.new get_logger
26
+ ex = assert_raise ChainReactor::Parsers::RequiredKeyError do
27
+ parser.parse('{"key1":"value","key2":"value"}',['monkey'],false)
28
+ end
29
+ assert_match(/Required key 'monkey'/, ex.message)
30
+ end
31
+
32
+ # Test that a valid JSON string returns a cause object with correct
33
+ # data.
34
+ def test_parse_valid_json_returns_hash
35
+ name = "A name"
36
+ type = "A type"
37
+ json = "{\"name\":\"#{name}\",\"type\":\"#{type}\"}"
38
+ parser = ChainReactor::Parsers::JsonParser.new get_logger
39
+ cause = parser.parse(json,[],false)
40
+ assert_equal name, cause['name']
41
+ assert_equal type, cause['type']
42
+ end
43
+
44
+ # Test that a valid JSON string returns a cause object with correct
45
+ # data.
46
+ def test_parse_valid_json_returns_hash_with_symbol_keys
47
+ name = "A name"
48
+ type = "A type"
49
+ json = "{\"name\":\"#{name}\",\"type\":\"#{type}\"}"
50
+ parser = ChainReactor::Parsers::JsonParser.new get_logger
51
+ cause = parser.parse(json,[],true)
52
+ assert_equal name, cause[:name]
53
+ assert_equal type, cause[:type]
54
+ end
55
+ end
@@ -0,0 +1,30 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'parser_factory'
4
+ require 'helpers'
5
+ require 'dummy_parser'
6
+
7
+ # Test case for the <tt>ChainReactor::ParserFactory</tt> class.
8
+ class TestParserFactory < Test::Unit::TestCase
9
+ include ChainReactor::TestHelpers
10
+
11
+ def test_get_parser_gives_json_parser_with_symbol
12
+ parser = ChainReactor::ParserFactory.get_parser(:json,get_logger)
13
+ assert_kind_of ChainReactor::Parsers::JsonParser, parser
14
+ end
15
+
16
+ def test_get_parser_gives_dummy_parser_with_symbol
17
+ parser = ChainReactor::ParserFactory.get_parser(:dummy,get_logger)
18
+ assert_kind_of ChainReactor::Parsers::DummyParser, parser
19
+ end
20
+
21
+ def test_get_parser_gives_dummy_parser_with_string
22
+ parser = ChainReactor::ParserFactory.get_parser('dummy',get_logger)
23
+ assert_kind_of ChainReactor::Parsers::DummyParser, parser
24
+ end
25
+
26
+ def test_get_parser_gives_dummy_parser_with_capitalized_string
27
+ parser = ChainReactor::ParserFactory.get_parser('Dummy',get_logger)
28
+ assert_kind_of ChainReactor::Parsers::DummyParser, parser
29
+ end
30
+ end
@@ -0,0 +1,39 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'reactor'
4
+ require 'helpers'
5
+ require 'dummy_parser'
6
+
7
+
8
+ # Test case for the <tt>ChainReactor::Reaction</tt> class.
9
+ class TestReaction < Test::Unit::TestCase
10
+ include ChainReactor::TestHelpers
11
+
12
+ def test_block_is_executed
13
+ block = Proc.new { |d| raise 'Block has been called' }
14
+ reaction = ChainReactor::Reaction.new({:parser => :dummy}, block, get_logger)
15
+
16
+ assert_raise ChainReactor::ReactionError, 'Block has been called' do
17
+ reaction.execute('')
18
+ end
19
+ end
20
+
21
+ def test_execute_sets_previous_result
22
+ block = Proc.new { |d| 'Block has been called' }
23
+ reaction = ChainReactor::Reaction.new({:parser => :dummy}, block, get_logger)
24
+ reaction.execute('')
25
+
26
+ assert_equal 'Block has been called', reaction.previous_result
27
+ end
28
+
29
+ def test_execute_sets_previous_data
30
+
31
+ block = Proc.new { |d| 'Block has been called' }
32
+ reaction = ChainReactor::Reaction.new({:parser => :json}, block, get_logger)
33
+ data_string = '{"hello" : "world"}'
34
+ data = {:hello => 'world'}
35
+ reaction.execute(data_string)
36
+
37
+ assert_equal data, reaction.previous_data
38
+ end
39
+ end
@@ -0,0 +1,63 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'reactor'
4
+ require 'helpers'
5
+ require 'dummy_parser'
6
+
7
+ # Test case for the <tt>ChainReaction::Reaction</tt> class.
8
+ class TestReactor < Test::Unit::TestCase
9
+ include ChainReactor::TestHelpers
10
+
11
+ def test_address_not_allowed_by_default
12
+ reactor = ChainReactor::Reactor.new get_logger
13
+ assert_equal false, reactor.address_allowed?('192.168.0.1')
14
+ end
15
+
16
+ def test_adding_reaction_makes_address_allowable
17
+ reactor = ChainReactor::Reactor.new get_logger
18
+ reactor.add(['127.0.0.1'],{parser: :dummy},Proc.new {})
19
+ assert reactor.address_allowed? '127.0.0.1'
20
+ end
21
+
22
+ def test_react_raises_error
23
+ reactor = ChainReactor::Reactor.new get_logger
24
+ assert_raises RuntimeError, 'Address is not allowed' do
25
+ reactor.react('127.0.0.1',{parser: :dummy})
26
+ end
27
+ end
28
+
29
+ def test_react_calls_block
30
+ reactor = ChainReactor::Reactor.new get_logger
31
+ block = Proc.new { |d| 'block has been called' }
32
+ reactor.add(['127.0.0.1'],{parser: :dummy},block)
33
+ reactions = reactor.reactions_for('127.0.0.1')
34
+ reactor.react('127.0.0.1','This is a string')
35
+ assert_equal 'block has been called', reactions[0].previous_result
36
+ end
37
+
38
+ def test_react_calls_multiple_blocks
39
+ reactor = ChainReactor::Reactor.new get_logger
40
+
41
+ block1 = Proc.new { |d| 'block1' }
42
+ block2 = Proc.new { |d| 'block2' }
43
+
44
+ reactor.add(['127.0.0.1'],{parser: :dummy},block1)
45
+ reactor.add(['127.0.0.1'],{parser: :dummy},block2)
46
+
47
+ reactor.react('127.0.0.1','This is a string')
48
+
49
+ reactions = reactor.reactions_for('127.0.0.1')
50
+ assert_equal 'block1', reactions[0].previous_result
51
+ assert_equal 'block2', reactions[1].previous_result
52
+ end
53
+
54
+ def test_react_catches_exceptions
55
+ reactor = ChainReactor::Reactor.new get_logger
56
+ block = Proc.new { |d| raise 'Block has been called' }
57
+ reactor.add(['127.0.0.1'],{parser: :dummy},block)
58
+ reactor.reactions_for('127.0.0.1')
59
+ assert_nothing_raised do
60
+ reactor.react('127.0.0.1','This is a string')
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,65 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'mocha/setup'
4
+ require 'parsers/parser'
5
+ require 'parsers/xml_simple_parser'
6
+ require 'helpers'
7
+
8
+ # Test case for the <tt>ChainReactor::Parsers::XmlSimpleParser</tt> class.
9
+ class TestXmlSimpleParser < Test::Unit::TestCase
10
+ include ChainReactor::TestHelpers
11
+
12
+ # Test that a ParseError is raised when an invalid xml string is
13
+ # passed.
14
+ def test_parse_non_xml_raises_error
15
+ parser = ChainReactor::Parsers::XmlSimpleParser.new get_logger
16
+ ex = assert_raise ChainReactor::Parsers::ParseError do
17
+ parser.parse("not an xml string",[],false)
18
+ end
19
+ assert_match( /not a valid XML string/, ex.message)
20
+ end
21
+
22
+ # Test that a ParseError is raised when an invalid xml string is
23
+ # passed.
24
+ def test_parse_invalid_xml_raises_error
25
+ parser = ChainReactor::Parsers::XmlSimpleParser.new get_logger
26
+ ex = assert_raise ChainReactor::Parsers::ParseError do
27
+ parser.parse("<data>",[],false)
28
+ end
29
+ assert_match( /not a valid XML string/, ex.message)
30
+ end
31
+
32
+ # Test that a ParseError is raised when a xml with invalid keys is
33
+ # passed.
34
+ def test_parse_wrong_xml_raises_error
35
+ parser = ChainReactor::Parsers::XmlSimpleParser.new get_logger
36
+ ex = assert_raise ChainReactor::Parsers::RequiredKeyError do
37
+ parser.parse('<data><key1>value</key1><key2>value</key2></data>',['monkey'],false)
38
+ end
39
+ assert_match(/Required key 'monkey'/, ex.message)
40
+ end
41
+
42
+ # Test that a valid xml string returns a cause object with correct
43
+ # data.
44
+ def test_parse_valid_xml_returns_hash
45
+ name = "A name"
46
+ type = "A type"
47
+ xml = "<data><name>#{name}</name><type>#{type}</type></data>"
48
+ parser = ChainReactor::Parsers::XmlSimpleParser.new get_logger
49
+ cause = parser.parse(xml,[],false)
50
+ assert_equal name, cause['name'].first
51
+ assert_equal type, cause['type'].first
52
+ end
53
+
54
+ # Test that a valid xml string returns a cause object with correct
55
+ # data.
56
+ def test_parse_valid_xml_returns_hash_with_symbol_keys
57
+ name = "A name"
58
+ type = "A type"
59
+ xml = "<data><name>#{name}</name><type>#{type}</type></data>"
60
+ parser = ChainReactor::Parsers::XmlSimpleParser.new get_logger
61
+ cause = parser.parse(xml,[],true)
62
+ assert_equal name, cause[:name].first
63
+ assert_equal type, cause[:type].first
64
+ end
65
+ end