spec/arborist/monitor_spec.rb in arborist-0.2.0.pre20170519125456 vs spec/arborist/monitor_spec.rb in arborist-0.2.0

- old
+ new

@@ -18,10 +18,11 @@ properties['pork'] = 'yes' end end let( :leaf_node ) do testing_node( 'leaf', 'branch' ) do + tags :one, :two properties['pork'] = 'twice' end end @@ -35,11 +36,11 @@ it "can be created with just a description and key" do mon = described_class.new( "the description", :key ) expect( mon ).to be_a( described_class ) expect( mon.description ).to eq( "the description" ) expect( mon.key ).to eq( :key ) - expect( mon.include_down? ).to be_falsey + expect( mon.exclude_down? ).to be_falsey expect( mon.interval ).to eq( Arborist::Monitor::DEFAULT_INTERVAL ) expect( mon.splay ).to eq( 0 ) expect( mon.positive_criteria ).to be_empty expect( mon.negative_criteria ).to be_empty expect( mon.node_properties ).to be_empty @@ -53,11 +54,11 @@ end expect( mon ).to be_a( described_class ) expect( mon.description ).to eq( "the description" ) expect( mon.key ).to eq( :key ) - expect( mon.include_down? ).to be_falsey + expect( mon.exclude_down? ).to be_falsey expect( mon.interval ).to eq( Arborist::Monitor::DEFAULT_INTERVAL ) expect( mon.splay ).to eq( 0 ) expect( mon.positive_criteria ).to be_empty expect( mon.negative_criteria ).to be_empty expect( mon.node_properties ).to be_empty @@ -70,11 +71,11 @@ end expect( mon ).to be_a( described_class ) expect( mon.description ).to eq( "the description" ) expect( mon.key ).to eq( :key ) - expect( mon.include_down? ).to be_falsey + expect( mon.exclude_down? ).to be_falsey expect( mon.interval ).to eq( Arborist::Monitor::DEFAULT_INTERVAL ) expect( mon.splay ).to eq( 0 ) expect( mon.positive_criteria ).to be_empty expect( mon.negative_criteria ).to be_empty expect( mon.node_properties ).to be_empty @@ -85,25 +86,25 @@ mon = Arborist::Monitor( "the description", :the_key ) expect( mon ).to be_a( described_class ) expect( mon.description ).to eq( "the description" ) expect( mon.key ).to eq( :the_key ) - expect( mon.include_down? ).to be_falsey + expect( mon.exclude_down? ).to be_falsey expect( mon.interval ).to eq( Arborist::Monitor::DEFAULT_INTERVAL ) expect( mon.splay ).to eq( 0 ) expect( mon.positive_criteria ).to be_empty expect( mon.negative_criteria ).to be_empty expect( mon.node_properties ).to be_empty end - it "raises a ConfigError if constructed without a description" do - expect { - described_class.new do - key :key - end - }.to raise_error( Arborist::ConfigError, /no description/i ) + it "uses a default description if constructed without one" do + mon = described_class.new do + key :key + end + + expect( mon.description ).to_not be_empty end it "raises a ConfigError if constructed without a key" do expect { @@ -161,20 +162,20 @@ it "automatically includes 'down' nodes if the matcher specifies an unreachable state" do mon = described_class.new( "testing monitor", :testing ) do match status: 'down' end - expect( mon.include_down? ).to be_truthy + expect( mon.exclude_down? ).to be_falsey end - it "can specify that it will include hosts marked as 'down'" do + it "can specify that it will exclude hosts marked as 'down'" do mon = described_class.new( "testing monitor", :testing ) do - include_down true + exclude_down true end - expect( mon.include_down? ).to be_truthy + expect( mon.exclude_down? ).to be_truthy end it "can specify one or more properties to include in the input to the monitor" do mon = described_class.new( "testing monitor", :testing ) do @@ -228,10 +229,31 @@ expect( mod.was_run ).to be_truthy end + it "uses node properties specified by the runnable object if it provides them" do + mod = Module.new do + class << self; attr_accessor :was_run ; end + @was_run = false + + def self::run( nodes ) + self.was_run = true + end + + def self::node_properties + %i[ uri http_method body mimetype ] + end + end + + mon = described_class.new( "the description", :testing ) + mon.exec( mod ) + + expect( mon.node_properties ).to include( :uri, :http_method, :body, :mimetype ) + end + + it "can provide a function for building arguments for its command" do mon = described_class.new( "the description", :testing ) do exec 'the_command' @@ -255,10 +277,34 @@ mon.run( testing_nodes ) end + it "stringifies any Array properties with the default exec_input context" do + mon = described_class.new( "the description", :testing ) do + exec 'the_command' + handle_results {|*| } + end + + child_stdin, parent_writer = IO.pipe + parent_reader, child_stdout = IO.pipe + parent_err_reader, child_stderr = IO.pipe + + expect( IO ).to receive( :pipe ).and_return( + [ child_stdin, parent_writer ], + [ parent_reader, child_stdout ], + [ parent_err_reader, child_stderr ] + ) + + expect( parent_writer ).to receive( :puts ).with match( 'tags=one,two' ) + expect( Process ).to receive( :spawn ). + with( 'the_command', out: child_stdout, in: child_stdin, err: child_stderr ) + + mon.run({ leaf: leaf_node.to_h }) + end + + it "handles system call errors while running the monitor command" do mon = described_class.new( "the description", :testing ) do exec 'the_command' @@ -346,9 +392,38 @@ expect( results.size ).to eq( 3 ) expect( results ).to include( *testing_nodes.keys ) expect( results['trunk'] ).to eq({ echoed: 'yep' }) expect( results['branch'] ).to eq({ echoed: 'yep' }) expect( results['leaf'] ).to eq({ echoed: 'yep' }) + end + + + it "uses node properties specified by the exec_callbacks module if it provides them" do + the_module = Module.new do + + def self::node_properties + %i[ uri http_method body mimetype ] + end + + def exec_input( nodes, writer ) + writer.puts( nodes.keys ) + end + + def handle_results( pid, out, err ) + err.flush + return out.each_line.with_object({}) do |line, accum| + accum[ line.chomp ] = { echoed: 'yep' } + end + end + + end + + mon = described_class.new( "the description", :testing ) do + exec 'cat' + exec_callbacks( the_module ) + end + + expect( mon.node_properties ).to include( :uri, :http_method, :body, :mimetype ) end end