forked from itsderek23/highgroove-scout-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.rb
150 lines (131 loc) · 6.31 KB
/
test.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require File.expand_path('../../test_helper.rb', __FILE__)
require File.expand_path('../haproxy_monitoring.rb', __FILE__)
require 'open-uri'
class HaProxyTest < Test::Unit::TestCase
def teardown
FakeWeb.clean_registry
end
def test_should_error_with_non_unique_proxy_names
uri='http://fake'
proxy = 'metrics-api'
FakeWeb.register_uri(:get, uri, :body => File.read(File.dirname(__FILE__)+'/fixtures/non_unique_proxies.csv'))
@plugin=HaproxyMonitoring.new(nil,{},{:uri=>uri, :proxy => proxy})
res = @plugin.run()
assert res[:reports].empty?
assert res[:memory].empty?
assert res[:errors].any?
assert res[:errors].first[:subject] =~ /Multiple proxies/
end
def test_should_error_with_invalid_proxy_type
uri='http://fake'
proxy = 'metrics-api'
FakeWeb.register_uri(:get, uri, :body => File.read(File.dirname(__FILE__)+'/fixtures/non_unique_proxies.csv'))
@plugin=HaproxyMonitoring.new(nil,{},{:uri=>uri, :proxy => proxy, :proxy_type => 'invalid'})
res = @plugin.run()
assert res[:reports].empty?
assert res[:memory].empty?
assert res[:errors].any?
assert res[:errors].first[:subject] =~ /Invalid Proxy Type/
end
def test_should_run_with_proxy_type
uri='http://fake'
proxy = 'metrics-api'
time = Time.now
Timecop.travel(time-60*10) do
FakeWeb.register_uri(:get, uri, :body => File.read(File.dirname(__FILE__)+'/fixtures/non_unique_proxies.csv'))
@plugin=HaproxyMonitoring.new(nil,{},{:uri=>uri, :proxy => proxy, :proxy_type => 'backend'})
res = @plugin.run()
assert_equal 61, res[:memory]["_counter_requests"][:value]
assert_equal 1, res[:reports].find { |e| e[:proxy_up]}[:proxy_up]
first_run_memory = res[:memory]
# now - 10 minutes later
Timecop.travel(time) do
FakeWeb.register_uri(:get, uri, :body => File.read(File.dirname(__FILE__)+'/fixtures/non_unique_proxies.csv'))
@plugin=HaproxyMonitoring.new(time-60*10,first_run_memory,{:uri=>uri, :proxy => proxy, :proxy_type => 'backend'})
res = @plugin.run()
assert_in_delta 0, res[:reports].first[:requests], 0
assert_equal 1, res[:reports].find { |e| e[:proxy_up]}[:proxy_up]
end # 2nd run
end # travel
end
def test_should_run
# Stub the plugin instance where necessary and run
# @plugin=PluginName.new(last_run, memory, options)
# date hash hash
uri='http://fake'
proxy = 'rails'
time = Time.now
Timecop.travel(time-60*10) do
FakeWeb.register_uri(:get, uri, :body => File.read(File.dirname(__FILE__)+'/fixtures/sample.csv'))
@plugin=HaproxyMonitoring.new(nil,{},{:uri=>uri, :proxy => proxy})
res = @plugin.run()
assert_equal 120350789, res[:memory]["_counter_requests"][:value]
assert_equal 10860, res[:memory]["_counter_errors_req"][:value]
assert_equal 10, res[:memory]["_counter_errors_conn"][:value]
assert_equal 20, res[:memory]["_counter_errors_resp"][:value]
assert_equal 1, res[:reports].find { |hash| hash[:proxy_up] }.values.last
first_run_memory = res[:memory]
# now - 10 minutes later
Timecop.travel(time) do
FakeWeb.register_uri(:get, uri, :body => File.read(File.dirname(__FILE__)+'/fixtures/sample_second_run.csv'))
@plugin=HaproxyMonitoring.new(time-60*10,first_run_memory,{:uri=>uri, :proxy => 'rails'})
res = @plugin.run()
assert_in_delta 10, res[:reports].first[:requests], 0.001
assert_in_delta 1, res[:reports].find { |e| e[:errors_req]}[:errors_req], 0.001
assert_in_delta 0, res[:reports].first[:errors_conn], 0.001
assert_in_delta 0.1, res[:reports].find { |e| e[:errors_resp]}[:errors_resp], 0.001
assert_equal 0, res[:reports].find { |hash| hash[:proxy_up] }.values.last
end # 2nd run
end # travel
end
def test_should_error_with_no_proxy_provided
uri='http://fake' # output comes from http://demo.1wt.eu/;csv
FakeWeb.register_uri(:get, uri, :body => File.read(File.dirname(__FILE__)+'/fixtures/sample.csv'))
@plugin=HaproxyMonitoring.new(nil,{},{:uri=>uri})
res = @plugin.run()
error = res[:errors].first
assert_equal "Proxy name required", error[:subject]
end
def test_should_error_with_proxy_not_found
uri='http://fake' # output comes from http://demo.1wt.eu/;csv
FakeWeb.register_uri(:get, uri, :body => File.read(File.dirname(__FILE__)+'/fixtures/sample.csv'))
@plugin=HaproxyMonitoring.new(nil,{},{:uri=>uri, :proxy => 'invalid'})
res = @plugin.run()
error = res[:errors].first
assert_equal "Proxy not found", error[:subject]
end
def test_should_error_with_invalid_csv
uri='http://fake'
FakeWeb.register_uri(:get, uri, :body => File.read(File.dirname(__FILE__)+'/fixtures/invalid.csv'))
@plugin=HaproxyMonitoring.new(nil,{},{:uri=>uri,:proxy => 'rails'})
res = @plugin.run()
assert_equal 0, res[:reports].size
assert_equal 1, res[:errors].size
assert_equal "Unable to access stats page", res[:errors].first[:subject]
end
def test_should_error_with_blank_uri
@plugin=HaproxyMonitoring.new(nil,{},{:uri=>nil})
res = @plugin.run()
assert_equal 0, res[:reports].size
assert_equal 1, res[:errors].size
assert_equal "URI to HAProxy Stats Required", res[:errors].first[:subject]
end
def test_should_error_with_invalid_basic_auth
uri_invalid_auth = "http://user:invalid@example.com/secret"
FakeWeb.register_uri(:get, uri_invalid_auth, :body => "Unauthorized", :status => ["401", "Unauthorized"])
@plugin=HaproxyMonitoring.new(nil,{},{:uri=>uri_invalid_auth, :user => 'user', :password => 'invalid'})
res = @plugin.run()
assert_equal 1, res[:errors].size
assert_equal "Authentication Failed", res[:errors].first[:subject]
end
def test_should_run_with_valid_basic_auth
uri_no_auth = "http://example.com/secret"
uri = "http://user:pass@example.com/secret"
FakeWeb.register_uri(:get, uri_no_auth, :body => "Unauthorized", :status => ["401", "Unauthorized"])
FakeWeb.register_uri(:get, uri, :body => File.read(File.dirname(__FILE__)+'/fixtures/sample.csv'))
@plugin=HaproxyMonitoring.new(nil,{},{:uri=>uri_no_auth, :user => 'user', :password => 'pass', :proxy => 'rails'})
res = @plugin.run()
assert res[:errors].empty?
assert res[:memory].any?
end
end