-
Notifications
You must be signed in to change notification settings - Fork 149
/
push_spec.rb
150 lines (115 loc) · 4.85 KB
/
push_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
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
# encoding: UTF-8
require 'prometheus/client/push'
describe Prometheus::Client::Push do
let(:gateway) { 'http://localhost:9091' }
let(:registry) { Prometheus::Client.registry }
let(:push) { Prometheus::Client::Push.new('test-job', nil, gateway) }
describe '.new' do
it 'returns a new push instance' do
expect(push).to be_a(Prometheus::Client::Push)
end
it 'uses localhost as default Pushgateway' do
push = Prometheus::Client::Push.new('test-job')
expect(push.gateway).to eql('http://localhost:9091')
end
it 'allows to specify a custom Pushgateway' do
push = Prometheus::Client::Push.new('test-job', nil, 'http://pu.sh:1234')
expect(push.gateway).to eql('http://pu.sh:1234')
end
it 'raises an ArgumentError if the given gateway URL is invalid' do
['inva.lid:1233', 'http://[invalid]'].each do |url|
expect do
Prometheus::Client::Push.new('test-job', nil, url)
end.to raise_error ArgumentError
end
end
end
describe '#add' do
it 'sends a given registry to via HTTP POST' do
expect(push).to receive(:request).with(Net::HTTP::Post, registry)
push.add(registry)
end
end
describe '#replace' do
it 'sends a given registry to via HTTP PUT' do
expect(push).to receive(:request).with(Net::HTTP::Put, registry)
push.replace(registry)
end
end
describe '#delete' do
it 'deletes existing metrics with HTTP DELETE' do
expect(push).to receive(:request).with(Net::HTTP::Delete)
push.delete
end
end
describe '#path' do
it 'uses the default metrics path if no instance value given' do
push = Prometheus::Client::Push.new('test-job')
expect(push.path).to eql('/metrics/job/test-job')
end
it 'uses the full metrics path if an instance value is given' do
push = Prometheus::Client::Push.new('bar-job', 'foo')
expect(push.path).to eql('/metrics/job/bar-job/instance/foo')
end
it 'escapes non-URL characters' do
push = Prometheus::Client::Push.new('bar job', 'foo <my instance>')
expected = '/metrics/job/bar+job/instance/foo+%3Cmy+instance%3E'
expect(push.path).to eql(expected)
end
end
describe '#request' do
let(:content_type) { Prometheus::Client::Formats::Text::CONTENT_TYPE }
let(:data) { Prometheus::Client::Formats::Text.marshal(registry) }
let(:uri) { URI.parse("#{gateway}/metrics/job/test-job") }
it 'sends marshalled registry to the specified gateway' do
request = double(:request)
expect(request).to receive(:content_type=).with(content_type)
expect(request).to receive(:body=).with(data)
expect(Net::HTTP::Post).to receive(:new).with(uri).and_return(request)
http = double(:http)
expect(http).to receive(:use_ssl=).with(false)
expect(http).to receive(:request).with(request)
expect(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)
push.send(:request, Net::HTTP::Post, registry)
end
it 'deletes data from the registry' do
request = double(:request)
expect(request).to receive(:content_type=).with(content_type)
expect(Net::HTTP::Delete).to receive(:new).with(uri).and_return(request)
http = double(:http)
expect(http).to receive(:use_ssl=).with(false)
expect(http).to receive(:request).with(request)
expect(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)
push.send(:request, Net::HTTP::Delete)
end
context 'HTTPS support' do
let(:gateway) { 'https://localhost:9091' }
it 'uses HTTPS when requested' do
request = double(:request)
expect(request).to receive(:content_type=).with(content_type)
expect(request).to receive(:body=).with(data)
expect(Net::HTTP::Post).to receive(:new).with(uri).and_return(request)
http = double(:http)
expect(http).to receive(:use_ssl=).with(true)
expect(http).to receive(:request).with(request)
expect(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)
push.send(:request, Net::HTTP::Post, registry)
end
end
context 'Basic Auth support' do
let(:gateway) { 'https://super:secret@localhost:9091' }
it 'sets Basic Auth header when requested' do
request = double(:request)
expect(request).to receive(:content_type=).with(content_type)
expect(request).to receive(:basic_auth).with('super', 'secret')
expect(request).to receive(:body=).with(data)
expect(Net::HTTP::Put).to receive(:new).with(uri).and_return(request)
http = double(:http)
expect(http).to receive(:use_ssl=).with(true)
expect(http).to receive(:request).with(request)
expect(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)
push.send(:request, Net::HTTP::Put, registry)
end
end
end
end