-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathwrite_points_spec.rb
182 lines (161 loc) · 5.64 KB
/
write_points_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
require "spec_helper"
require "json"
describe InfluxDB::Client do
let(:subject) do
described_class.new \
database: "database",
host: "influxdb.test",
port: 9999,
username: "username",
password: "password",
time_precision: "s"
end
let(:database) { subject.config.database }
describe "#write_point" do
let(:series) { "cpu" }
let(:data) do
{ tags: { region: 'us', host: 'server_1' },
values: { temp: 88, value: 54 } }
end
let(:body) do
InfluxDB::PointValue.new(data.merge(series: series)).dump
end
before do
stub_request(:post, "http://influxdb.test:9999/write").with(
query: { u: "username", p: "password", precision: 's', db: database },
headers: { "Content-Type" => "application/octet-stream", "Content-Encoding" => "gzip" },
body: Zlib.gzip(body, level: 1)
).to_return(status: 204)
end
it "should POST to add single point" do
expect(subject.write_point(series, data)).to be_a(Net::HTTPNoContent)
end
it "should not mutate data object" do
original_data = data
subject.write_point(series, data)
expect(data[:series]).to be_nil
expect(original_data).to eql(data)
end
end
describe "#write_points" do
context "with multiple series" do
let(:data) do
[{ series: 'cpu',
tags: { region: 'us', host: 'server_1' },
values: { temp: 88, value: 54 } },
{ series: 'gpu',
tags: { region: 'uk', host: 'server_5' },
values: { value: 0.5435345 } }]
end
let(:body) do
data.map do |point|
InfluxDB::PointValue.new(point).dump
end.join("\n")
end
before do
stub_request(:post, "http://influxdb.test:9999/write").with(
query: { u: "username", p: "password", precision: 's', db: database },
headers: { "Content-Type" => "application/octet-stream", "Content-Encoding" => "gzip" },
body: Zlib.gzip(body, level: 1)
).to_return(status: 204)
end
it "should POST multiple points" do
expect(subject.write_points(data)).to be_a(Net::HTTPNoContent)
end
end
context "with no tags" do
let(:data) do
[{ series: 'cpu',
values: { temp: 88, value: 54 } },
{ series: 'gpu',
values: { value: 0.5435345 } }]
end
let(:body) do
data.map do |point|
InfluxDB::PointValue.new(point).dump
end.join("\n")
end
before do
stub_request(:post, "http://influxdb.test:9999/write").with(
query: { u: "username", p: "password", precision: 's', db: database },
headers: { "Content-Type" => "application/octet-stream", "Content-Encoding" => "gzip" },
body: Zlib.gzip(body, level: 1)
).to_return(status: 204)
end
it "should POST multiple points" do
expect(subject.write_points(data)).to be_a(Net::HTTPNoContent)
end
end
context "with time precision set to milisceconds" do
let(:data) do
[{ series: 'cpu',
values: { temp: 88, value: 54 },
timestamp: (Time.now.to_f * 1000).to_i },
{ series: 'gpu',
values: { value: 0.5435345 },
timestamp: (Time.now.to_f * 1000).to_i }]
end
let(:body) do
data.map do |point|
InfluxDB::PointValue.new(point).dump
end.join("\n")
end
before do
stub_request(:post, "http://influxdb.test:9999/write").with(
query: { u: "username", p: "password", precision: 'ms', db: database },
headers: { "Content-Type" => "application/octet-stream", "Content-Encoding" => "gzip" },
body: Zlib.gzip(body, level: 1)
).to_return(status: 204)
end
it "should POST multiple points" do
expect(subject.write_points(data, 'ms')).to be_a(Net::HTTPNoContent)
end
end
context "with retention policy" do
let(:data) do
[{ series: 'cpu',
values: { temp: 88, value: 54 } },
{ series: 'gpu',
values: { value: 0.5435345 } }]
end
let(:body) do
data.map do |point|
InfluxDB::PointValue.new(point).dump
end.join("\n")
end
before do
stub_request(:post, "http://influxdb.test:9999/write").with(
query: { u: "username", p: "password", precision: 's', db: database, rp: 'rp_1_hour' },
headers: { "Content-Type" => "application/octet-stream", "Content-Encoding" => "gzip" },
body: Zlib.gzip(body, level: 1)
).to_return(status: 204)
end
it "should POST multiple points" do
expect(subject.write_points(data, nil, 'rp_1_hour')).to be_a(Net::HTTPNoContent)
end
end
context "with database" do
let(:data) do
[{ series: 'cpu',
values: { temp: 88, value: 54 } },
{ series: 'gpu',
values: { value: 0.5435345 } }]
end
let(:body) do
data.map do |point|
InfluxDB::PointValue.new(point).dump
end.join("\n")
end
before do
stub_request(:post, "http://influxdb.test:9999/write").with(
query: { u: "username", p: "password", precision: 's', db: 'overriden_db' },
headers: { "Content-Type" => "application/octet-stream", "Content-Encoding" => "gzip" },
body: Zlib.gzip(body, level: 1)
).to_return(status: 204)
end
it "should POST multiple points" do
expect(subject.write_points(data, nil, nil, 'overriden_db')).to be_a(Net::HTTPNoContent)
end
end
end
end