/
shell_command_agent_spec.rb
99 lines (81 loc) · 2.62 KB
/
shell_command_agent_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'spec_helper'
describe Agents::ShellCommandAgent do
before do
@valid_path = Dir.pwd
@valid_params = {
:path => @valid_path,
:command => "pwd",
:expected_update_period_in_days => "1",
}
@checker = Agents::ShellCommandAgent.new(:name => "somename", :options => @valid_params)
@checker.user = users(:jane)
@checker.save!
@event = Event.new
@event.agent = agents(:jane_weather_agent)
@event.payload = {
:command => "ls"
}
@event.save!
stub(Agents::ShellCommandAgent).should_run? { true }
end
describe "validation" do
before do
@checker.should be_valid
end
it "should validate presence of necessary fields" do
@checker.options[:command] = nil
@checker.should_not be_valid
end
it "should validate path" do
@checker.options[:path] = 'notarealpath/itreallyisnt'
@checker.should_not be_valid
end
it "should validate path" do
@checker.options[:path] = '/'
@checker.should be_valid
end
end
describe "#working?" do
it "generating events as scheduled" do
stub(@checker).run_command(@valid_path, 'pwd') { ["fake pwd output", "", 0] }
@checker.should_not be_working
@checker.check
@checker.reload.should be_working
three_days_from_now = 3.days.from_now
stub(Time).now { three_days_from_now }
@checker.should_not be_working
end
end
describe "#check" do
before do
stub(@checker).run_command(@valid_path, 'pwd') { ["fake pwd output", "", 0] }
end
it "should create an event when checking" do
expect { @checker.check }.to change { Event.count }.by(1)
Event.last.payload[:path].should == @valid_path
Event.last.payload[:command].should == 'pwd'
Event.last.payload[:output].should == "fake pwd output"
end
it "does not run when should_run? is false" do
stub(Agents::ShellCommandAgent).should_run? { false }
expect { @checker.check }.not_to change { Event.count }
end
end
describe "#receive" do
before do
stub(@checker).run_command(@valid_path, @event.payload[:command]) { ["fake ls output", "", 0] }
end
it "creates events" do
@checker.receive([@event])
Event.last.payload[:path].should == @valid_path
Event.last.payload[:command].should == @event.payload[:command]
Event.last.payload[:output].should == "fake ls output"
end
it "does not run when should_run? is false" do
stub(Agents::ShellCommandAgent).should_run? { false }
expect {
@checker.receive([@event])
}.not_to change { Event.count }
end
end
end