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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::UsageDataCounters::KubernetesAgentCounter do
described_class::KNOWN_EVENTS.each do |event|
it_behaves_like 'a redis usage counter', 'Kubernetes Agent', event
it_behaves_like 'a redis usage counter with totals', :kubernetes_agent, event => 1
end
describe '.increment_event_counts' do
let(:events) do
{
'gitops_sync' => 1,
'k8s_api_proxy_request' => 2,
'flux_git_push_notifications_total' => 3,
'k8s_api_proxy_requests_via_ci_access' => 4,
'k8s_api_proxy_requests_via_user_access' => 5
}
end
subject { described_class.increment_event_counts(events) }
it 'increments the specified counters by the new increment amount' do
described_class.increment_event_counts(events)
described_class.increment_event_counts(events)
described_class.increment_event_counts(events)
expect(described_class.totals).to eq(
kubernetes_agent_gitops_sync: 3,
kubernetes_agent_k8s_api_proxy_request: 6,
kubernetes_agent_flux_git_push_notifications_total: 9,
kubernetes_agent_k8s_api_proxy_requests_via_ci_access: 12,
kubernetes_agent_k8s_api_proxy_requests_via_user_access: 15
)
end
context 'with empty events' do
let(:events) { nil }
it { expect { subject }.not_to change(described_class, :totals) }
end
context 'event is unknown' do
let(:events) do
{
'gitops_sync' => 1,
'other_event' => 2
}
end
it 'raises an ArgumentError' do
expect(described_class).not_to receive(:increment_by)
expect { subject }.to raise_error(ArgumentError, 'unknown event other_event')
end
end
context 'increment is negative' do
let(:events) do
{
'gitops_sync' => -1,
'k8s_api_proxy_request' => 2
}
end
it 'raises an ArgumentError' do
expect(described_class).not_to receive(:increment_by)
expect { subject }.to raise_error(ArgumentError, 'gitops_sync count must be greater than or equal to zero')
end
end
end
end
|