Commit 9ccda901 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Add Prometheus client tests

parent aaeda829
......@@ -119,6 +119,36 @@ describe Gitlab::PrometheusClient, lib: true do
end
end
describe '#series' do
let(:query_url) { prometheus_series_url('series_name', 'other_service') }
around do |example|
Timecop.freeze { example.run }
end
it 'calls endpoint and returns list of series' do
req_stub = stub_prometheus_request(query_url, body: prometheus_series('series_name'))
expected = prometheus_series('series_name').deep_stringify_keys['data']
expect(subject.series('series_name', 'other_service')).to eq(expected)
expect(req_stub).to have_been_requested
end
end
describe '#label_values' do
let(:query_url) { prometheus_label_values_url('__name__') }
it 'calls endpoint and returns label values' do
req_stub = stub_prometheus_request(query_url, body: prometheus_label_values)
expected = prometheus_label_values.deep_stringify_keys['data']
expect(subject.label_values('__name__')).to eq(expected)
expect(req_stub).to have_been_requested
end
end
describe '#query_range' do
let(:prometheus_query) { prometheus_memory_query('env-slug') }
let(:query_url) { prometheus_query_range_url(prometheus_query) }
......
......@@ -36,6 +36,19 @@ module PrometheusHelpers
"https://prometheus.example.com/api/v1/query_range?#{query}"
end
def prometheus_label_values_url(name)
"https://prometheus.example.com/api/v1/label/#{name}/values"
end
def prometheus_series_url(*matches, start: 8.hours.ago, stop: Time.now)
query = {
match: matches,
start: start.to_f,
end: stop.to_f
}.to_query
"https://prometheus.example.com/api/v1/series?#{query}"
end
def stub_prometheus_request(url, body: {}, status: 200)
WebMock.stub_request(:get, url)
.to_return({
......@@ -140,4 +153,37 @@ module PrometheusHelpers
}
}
end
def prometheus_label_values
{
'status': 'success',
'data': %w(job_adds job_controller_rate_limiter_use job_depth job_queue_latency job_work_duration_sum up)
}
end
def prometheus_series(name)
{
'status': 'success',
'data': [
{
'__name__': name,
'container_name': 'gitlab',
'environment': 'mattermost',
'id': '/docker/9953982f95cf5010dfc59d7864564d5f188aaecddeda343699783009f89db667',
'image': 'gitlab/gitlab-ce:8.15.4-ce.1',
'instance': 'minikube',
'job': 'kubernetes-nodes',
'name': 'k8s_gitlab.e6611886_mattermost-4210310111-77z8r_gitlab_2298ae6b-da24-11e6-baee-8e7f67d0eb3a_43536cb6',
'namespace': 'gitlab',
'pod_name': 'mattermost-4210310111-77z8r'
},
{
'__name__': name,
'id': '/docker',
'instance': 'minikube',
'job': 'kubernetes-nodes'
}
]
}
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment