fluent-plugin-k8s-metrics-agg 1.1.0 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/helper.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'simplecov'
2
- require 'fluent/plugin/in_kubernetes_metrics_aggregator.rb'
3
2
  SimpleCov.start
4
3
 
5
4
  $LOAD_PATH.unshift(File.expand_path('..', __dir__))
@@ -37,33 +36,16 @@ module PluginTestHelper
37
36
  k8s_url + '/v1/nodes/generics-aws-node-three:10255/proxy/stats/summary'
38
37
  end
39
38
 
40
- def stub_api_port_10250
41
- WebMock.stub_request(:get, 'https://node.fakedestination.com:10250/api')
42
- .with(
43
- headers: {
44
- 'Accept' => '*/*',
45
- 'Accept-Encoding' => 'gzip, deflate',
46
- 'Host' => 'node.fakedestination.com:10250'
47
- }
48
- )
49
- .to_return(status: 200,
50
- body: File.open(File.expand_path('../v1.json', __FILE__)),
51
- headers: {})
52
- end
53
-
54
39
  def stub_api_port_10255
55
40
  WebMock.stub_request(:get,
56
41
  'https://node.fakedestination.com:10255/api')
57
42
  .with(
58
43
  headers: {
59
- 'Accept' => '*/*',
60
- 'Accept-Encoding' => 'gzip, deflate',
61
44
  'Host' => 'node.fakedestination.com:10255'
62
45
  }
63
46
  )
64
47
  .to_return(status: 200,
65
- body: File.open(File.expand_path('../v1.json', __FILE__)),
66
- headers: {})
48
+ body: File.open(File.expand_path('../v1.json', __FILE__)))
67
49
  end
68
50
 
69
51
  def stub_api_v1
@@ -71,29 +53,27 @@ module PluginTestHelper
71
53
  'https://node.fakedestination.com:10255/api/v1')
72
54
  .with(
73
55
  headers: {
74
- 'Accept' => '*/*',
75
- 'Accept-Encoding' => 'gzip, deflate',
76
56
  'Host' => 'node.fakedestination.com:10255'
77
57
  }
78
58
  )
79
59
  .to_return(status: 200,
80
- body: File.open(File.expand_path('../v1.json', __FILE__)),
81
- headers: {})
82
- end
60
+ body: File.open(File.expand_path('../v1.json', __FILE__)))
61
+ end
62
+
63
+ def stub_api_pods(timeout=false)
64
+ get_pods = WebMock.stub_request(:get,
65
+ 'https://node.fakedestination.com:10255/api/v1/pods')
66
+ .with(
67
+ headers: {
68
+ 'Host' => 'node.fakedestination.com:10255'
69
+ }
70
+ )
71
+ if timeout
72
+ get_pods = get_pods.to_timeout.then
73
+ end
83
74
 
84
- def stub_api_pods
85
- WebMock.stub_request(:get,
86
- 'https://node.fakedestination.com:10255/api/v1/pods')
87
- .with(
88
- headers: {
89
- 'Accept' => '*/*',
90
- 'Accept-Encoding' => 'gzip, deflate',
91
- 'Host' => 'node.fakedestination.com:10255'
92
- }
93
- )
94
- .to_return(status: 200,
95
- body: File.open(File.expand_path('../pods.json', __FILE__)),
96
- headers: {})
75
+ get_pods.to_return(status: 200,
76
+ body: File.open(File.expand_path('../pods.json', __FILE__)))
97
77
  end
98
78
 
99
79
  def stub_api_node_1
@@ -101,29 +81,28 @@ module PluginTestHelper
101
81
  'https://node.fakedestination.com:10255/api/v1/nodes/generics-aws-node-one:10255/proxy/stats/summary')
102
82
  .with(
103
83
  headers: {
104
- 'Accept' => '*/*',
105
- 'Accept-Encoding' => 'gzip, deflate',
106
84
  'Host' => 'node.fakedestination.com:10255'
107
85
  }
108
86
  )
109
87
  .to_return(status: 200,
110
- body: File.open(File.expand_path('../node1.json', __FILE__)),
111
- headers: {})
88
+ body: File.open(File.expand_path('../node1.json', __FILE__)))
112
89
  end
113
90
 
114
- def stub_api_node_2
115
- WebMock.stub_request(:get,
116
- 'https://node.fakedestination.com:10255/api/v1/nodes/generics-aws-node-two:10255/proxy/stats/summary')
91
+ def stub_api_node_2(timeout=false)
92
+ get_node_summary = WebMock.stub_request(:get,
93
+ 'https://node.fakedestination.com:10255/api/v1/nodes/generics-aws-node-two:10255/proxy/stats/summary')
117
94
  .with(
118
95
  headers: {
119
- 'Accept' => '*/*',
120
- 'Accept-Encoding' => 'gzip, deflate',
121
96
  'Host' => 'node.fakedestination.com:10255'
122
97
  }
123
98
  )
124
- .to_return(status: 200,
125
- body: File.open(File.expand_path('../node2.json', __FILE__)),
126
- headers: {})
99
+
100
+ if timeout
101
+ get_node_summary = get_node_summary.to_timeout
102
+ end
103
+
104
+ get_node_summary.to_return(status: 200,
105
+ body: File.open(File.expand_path('../node2.json', __FILE__)))
127
106
  end
128
107
 
129
108
  def stub_api_node_3
@@ -131,40 +110,45 @@ module PluginTestHelper
131
110
  'https://node.fakedestination.com:10255/api/v1/nodes/generics-aws-node-three:10255/proxy/stats/summary')
132
111
  .with(
133
112
  headers: {
134
- 'Accept' => '*/*',
135
- 'Accept-Encoding' => 'gzip, deflate',
136
113
  'Host' => 'node.fakedestination.com:10255'
137
114
  }
138
115
  )
139
116
  .to_return(status: 200,
140
- body: File.open(File.expand_path('../node3.json', __FILE__)),
141
- headers: {})
117
+ body: File.open(File.expand_path('../node3.json', __FILE__)))
142
118
  end
143
119
 
144
- def stub_api_nodes
145
- WebMock.stub_request(:get,
146
- 'https://node.fakedestination.com:10255/api/v1/nodes')
147
- .with(
148
- headers: {
149
- 'Accept' => '*/*',
150
- 'Accept-Encoding' => 'gzip, deflate',
151
- 'Host' => 'node.fakedestination.com:10255'
152
- }
153
- )
154
- .to_return(status: 200,
155
- body: File.open(File.expand_path('../nodes.json', __FILE__)),
156
- headers: {})
120
+ def stub_api_nodes(timeout=false)
121
+ get_nodes = WebMock.stub_request(:get, 'https://node.fakedestination.com:10255/api/v1/nodes')
122
+ .with(
123
+ headers: {
124
+ 'Host' => 'node.fakedestination.com:10255'
125
+ }
126
+ )
127
+
128
+ if timeout
129
+ get_nodes = get_nodes.to_timeout.times(2) # Nodes endpoint is called from two timers so must fail in both cases
130
+ end
131
+
132
+ get_nodes.to_return(status: 200,
133
+ body: File.open(File.expand_path('../nodes.json', __FILE__)))
157
134
  end
158
135
 
159
- def stub_k8s_requests
160
- stub_api_port_10250
136
+ def stub_k8s_init_requests
137
+ WebMock.reset!
138
+
139
+ stub_api_port_10255
140
+ end
141
+
142
+ def stub_k8s_requests(nodes_timeout: false, node_summary_timeout: false, pods_timeout: false)
143
+ WebMock.reset!
144
+
161
145
  stub_api_port_10255
162
146
  stub_api_v1
163
- stub_api_pods
164
- stub_api_nodes
147
+ stub_api_pods(pods_timeout)
148
+ stub_api_nodes(nodes_timeout)
165
149
  stub_api_node_1
150
+ stub_api_node_2(node_summary_timeout)
166
151
  stub_api_node_3
167
- stub_api_node_2
168
152
  end
169
153
 
170
154
  def get_parsed_file(file_name)
@@ -5,10 +5,6 @@ class KubernetesMetricsAggInputTest < Test::Unit::TestCase
5
5
  include Fluent::Test::Helpers
6
6
  include PluginTestHelper
7
7
 
8
- @driver = nil
9
- @driver_test = nil
10
- @@hash_map_test = {}
11
-
12
8
  ZERO_CONFIG = %([
13
9
  ]).freeze
14
10
 
@@ -34,20 +30,22 @@ class KubernetesMetricsAggInputTest < Test::Unit::TestCase
34
30
  METRIC_TEST_CONFIG = %([
35
31
  kubernetes_url https://node.fakedestination.com
36
32
  kubelet_port 10255
33
+ interval 5s
34
+ tag kube.*
35
+ ]).freeze
36
+
37
+ TIMEOUT_TEST_CONFIG = %([
38
+ kubernetes_url https://node.fakedestination.com
39
+ kubelet_port 10255
40
+ interval 5s
37
41
  tag kube.*
38
42
  ]).freeze
39
43
 
40
44
  setup do
41
45
  Fluent::Test.setup
46
+
42
47
  ENV['KUBERNETES_SERVICE_HOST'] = "node.fakedestination.com"
43
48
  ENV['KUBERNETES_SERVICE_PORT'] = "10255"
44
- stub_k8s_requests
45
- @driver = create_driver(METRIC_TEST_CONFIG)
46
- @driver.run timeout: 20, expect_emits: 200, shutdown: false
47
-
48
- @driver.events.each do |tag, time, record|
49
- @@hash_map_test[tag] = tag, time, record
50
- end
51
49
  end
52
50
 
53
51
  def create_driver(conf = BASIC_CONFIG)
@@ -60,6 +58,8 @@ class KubernetesMetricsAggInputTest < Test::Unit::TestCase
60
58
 
61
59
  sub_test_case 'default parameter configuration' do
62
60
  test 'test default params' do
61
+ stub_k8s_init_requests
62
+
63
63
  d = create_driver(ZERO_CONFIG)
64
64
  assert_equal 10_250, d.instance.kubelet_port
65
65
  assert_equal 'kubernetes.metrics.*', d.instance.tag
@@ -78,141 +78,174 @@ class KubernetesMetricsAggInputTest < Test::Unit::TestCase
78
78
 
79
79
  sub_test_case 'modify parameter changes' do
80
80
  test 'test kubelet_port and supplied kubernetes URL parameters' do
81
+ stub_k8s_init_requests
82
+
81
83
  d = create_driver(ADVANCED_CONFIG_NO_CERTS)
82
84
  assert_equal 'https://node.fakedestination.com', d.instance.kubernetes_url
83
85
  assert_equal 10_255, d.instance.kubelet_port
84
86
  end
85
87
 
86
88
  test 'test tag and interval parameters' do
89
+ stub_k8s_init_requests
90
+
87
91
  d = create_driver(ADVANCED_CONFIG_NO_CERTS)
88
92
  assert_equal 'test.tag.check', d.instance.tag
89
93
  assert_equal 120, d.instance.interval
90
94
  end
91
95
 
92
96
  test 'test insecure_ssl and cluster_name parameters ' do
97
+ stub_k8s_init_requests
98
+
93
99
  d = create_driver(ADVANCED_CONFIG_NO_CERTS)
94
100
  assert_true d.instance.insecure_ssl
95
101
  assert_equal 'awesome_cluster', d.instance.cluster_name
96
102
  end
97
103
  end
98
104
 
99
- sub_test_case 'Test metrics exist, limits_request_scraper - limits' do
100
- test 'Testing kube.container.memory.limit' do
101
- assert_true @@hash_map_test.key?('kube.container.memory.limit')
102
- end
103
-
104
- test 'Testing kube.namespace.cpu.limit' do
105
- assert_true @@hash_map_test.key?('kube.namespace.cpu.limit')
106
- end
107
-
108
- test 'Testing kube.namespace.memory.limit ' do
109
- assert_true @@hash_map_test.key?('kube.namespace.memory.limit')
110
- end
111
-
112
- test 'Testing kube.container.cpu.limit' do
113
- assert_true @@hash_map_test.key?('kube.container.cpu.limit')
114
- end
115
-
116
- test 'Testing kube.pod.cpu.limit' do
117
- assert_true @@hash_map_test.key?('kube.pod.cpu.limit')
118
- end
119
-
120
- test 'Testing kube.cluster.memory.limit ' do
121
- assert_true @@hash_map_test.key?('kube.cluster.memory.limit')
122
- end
123
-
124
- test 'Testing kube.pod.memory.limit ' do
125
- assert_true @@hash_map_test.key?('kube.pod.memory.limit')
126
- end
127
-
128
- test 'Testing kube.cluster.cpu.limit' do
129
- assert_true @@hash_map_test.key?('kube.cluster.cpu.limit')
105
+ sub_test_case 'Test metrics exist' do
106
+ test 'Testing all expected metrics are emitted' do
107
+ stub_k8s_requests
108
+
109
+ hash_map_test = {}
110
+
111
+ d = create_driver(METRIC_TEST_CONFIG)
112
+ d.run timeout: 12, expect_emits: 200, shutdown: false
113
+
114
+ d.events.each do |tag, time, record|
115
+ hash_map_test[tag] = tag, time, record
116
+ end
117
+
118
+ # Test metrics exist, limits_request_scraper - limits
119
+ assert_true hash_map_test.key?('kube.namespace.cpu.limit')
120
+ assert_true hash_map_test.key?('kube.namespace.memory.limit')
121
+ assert_true hash_map_test.key?('kube.container.cpu.limit')
122
+ assert_true hash_map_test.key?('kube.pod.cpu.limit')
123
+ assert_true hash_map_test.key?('kube.cluster.memory.limit')
124
+ assert_true hash_map_test.key?('kube.pod.memory.limit')
125
+ assert_true hash_map_test.key?('kube.cluster.cpu.limit')
126
+
127
+ # Test metrics exist, limits_request_scraper - request
128
+ assert_true hash_map_test.key?('kube.cluster.memory.request')
129
+ assert_true hash_map_test.key?('kube.container.memory.request')
130
+ assert_true hash_map_test.key?('kube.pod.memory.request')
131
+ assert_true hash_map_test.key?('kube.namespace.memory.request')
132
+ assert_true hash_map_test.key?('kube.container.cpu.request')
133
+ assert_true hash_map_test.key?('kube.namespace.cpu.request')
134
+ assert_true hash_map_test.key?('kube.pod.cpu.request')
135
+ assert_true hash_map_test.key?('kube.cluster.cpu.request')
136
+
137
+ # Test metrics exist, node_scraper/resource_usage_scraper 1
138
+ assert_true hash_map_test.key?('kube.node.cpu.capacity')
139
+ assert_true hash_map_test.key?('kube.node.memory.capacity')
140
+ assert_true hash_map_test.key?('kube.node.memory.allocatable')
141
+ assert_true hash_map_test.key?('kube.node.cpu.utilization')
142
+ assert_true hash_map_test.key?('kube.node.memory.reservation')
143
+ assert_true hash_map_test.key?('kube.node.memory.utilization')
144
+
145
+ # Test metrics exist, node_scraper/resource_usage_scraper 2
146
+ assert_true hash_map_test.key?('kube.namespace.memory.usage')
147
+ assert_true hash_map_test.key?('kube.cluster.memory.usage')
148
+ assert_true hash_map_test.key?('kube.namespace.cpu.usage')
149
+ assert_true hash_map_test.key?('kube.node.cpu.allocatable')
150
+ assert_true hash_map_test.key?('kube.node.cpu.reservation')
151
+ assert_true hash_map_test.key?('kube.cluster.cpu.usage')
152
+
153
+ d.instance_shutdown
130
154
  end
131
155
  end
132
156
 
133
- sub_test_case 'Test metrics exist, limits_request_scraper - request' do
134
- test 'Testing kube.cluster.memory.request ' do
135
- assert_true @@hash_map_test.key?('kube.cluster.memory.request')
136
- end
157
+ sub_test_case 'Test handles request timeouts' do
137
158
 
138
- test 'Testing kube.container.memory.request' do
139
- assert_true @@hash_map_test.key?('kube.container.memory.request')
140
- end
159
+ test 'Testing event count with nodes call timeout' do
160
+ stub_k8s_requests(nodes_timeout: true)
141
161
 
142
- test 'Testing kube.pod.memory.request' do
143
- assert_true @@hash_map_test.key?('kube.pod.memory.request')
144
- end
162
+ namespace_event_count = 0
163
+ pod_event_count = 0
164
+ node_event_count = 0
145
165
 
146
- test 'Testing kube.namespace.memory.request ' do
147
- assert_true @@hash_map_test.key?('kube.namespace.memory.request')
148
- end
166
+ d = create_driver(TIMEOUT_TEST_CONFIG)
167
+ # Should run for two intervals, the first call to node 1 which has the only 'default' namespace pod should timeout the first time
168
+ d.run timeout: 12, expect_emits: 500, shutdown: false
149
169
 
150
- test 'Testing kube.container.cpu.request' do
151
- assert_true @@hash_map_test.key?('kube.container.cpu.request')
152
- end
170
+ d.events.each do |tag, _time, record|
171
+ # Limit to one events that should be emitted once per interval
172
+ if tag == 'kube.pod.cpu.limit' && record['name'] == 'new-metrics-test-final-splunk-kubernetes-metrics-fgszl'
173
+ pod_event_count += 1
174
+ end
175
+ if tag == 'kube.namespace.cpu.usage' && record['name'] == 'kube-system'
176
+ namespace_event_count += 1
177
+ end
178
+ if tag == 'kube.node.cpu.capacity' && record['node'] == 'generics-aws-node-one'
179
+ node_event_count += 1
180
+ end
181
+ end
153
182
 
154
- test 'Testing kube.namespace.cpu.request' do
155
- assert_true @@hash_map_test.key?('kube.namespace.cpu.request')
156
- end
183
+ # 2 intervals - first call times out but timer continues emitting successfully next interval
184
+ assert_equal 1, node_event_count, 'Number of node events emitted was wrong'
185
+ # 2 intervals - first call times out but timer continues emitting successfully next interval
186
+ assert_equal 1, namespace_event_count, 'Number of namespace events emitted was wrong'
187
+ # 2 intervals - not timeouts
188
+ assert_equal 2, pod_event_count, 'Number of pod events emitted was wrong'
157
189
 
158
- test 'Testing kube.pod.cpu.request' do
159
- assert_true @@hash_map_test.key?('kube.pod.cpu.request')
190
+ d.instance_shutdown
160
191
  end
161
192
 
162
- test 'Testing kube.cluster.cpu.request' do
163
- assert_true @@hash_map_test.key?('kube.cluster.cpu.request')
164
- end
165
- end
193
+ test 'Testing event count with pods call timeout' do
194
+ stub_k8s_requests(pods_timeout: true)
166
195
 
167
- sub_test_case 'Test metrics exist, node_scraper/resource_usage_scraper 1' do
168
- test 'Testing kube.node.cpu.capacity' do
169
- assert_true @@hash_map_test.key?('kube.node.cpu.capacity')
170
- end
196
+ pod_event_count = 0
197
+ node_event_count = 0
171
198
 
172
- test 'Testing kube.node.memory.capacity ' do
173
- assert_true @@hash_map_test.key?('kube.node.memory.capacity')
174
- end
199
+ d = create_driver(TIMEOUT_TEST_CONFIG)
200
+ # Should run for two intervals, the first call to node 1 which has the only 'default' namespace pod should timeout the first time
201
+ d.run timeout: 12, expect_emits: 500, shutdown: false
175
202
 
176
- test 'Testing kube.node.memory.allocatable' do
177
- assert_true @@hash_map_test.key?('kube.node.memory.allocatable')
178
- end
203
+ d.events.each do |tag, _time, record|
204
+ # Limit to one events that should be emitted once per interval
205
+ if tag == 'kube.pod.cpu.limit' && record['name'] == 'new-metrics-test-final-splunk-kubernetes-metrics-fgszl'
206
+ pod_event_count += 1
207
+ end
179
208
 
180
- test 'Testing kube.node.cpu.utilization ' do
181
- assert_true @@hash_map_test.key?('kube.node.cpu.utilization')
182
- end
209
+ if tag == 'kube.node.cpu.utilization' && record['node'] == 'generics-aws-node-one'
210
+ node_event_count += 1
211
+ end
212
+ end
183
213
 
184
- test 'Testing kube.node.memory.reservation' do
185
- assert_true @@hash_map_test.key?('kube.node.memory.reservation')
186
- end
214
+ # 2 intervals - first call times out but timer continues emitting successfully next interval
215
+ assert_equal 1, pod_event_count, 'Number of pod events emitted was wrong'
216
+ # 2 intervals - not timeouts
217
+ assert_equal 2, node_event_count, 'Number of namespace events emitted was wrong'
187
218
 
188
- test 'Testing kube.node.memory.utilization' do
189
- assert_true @@hash_map_test.key?('kube.node.memory.utilization')
219
+ d.instance_shutdown
190
220
  end
191
- end
192
221
 
193
- sub_test_case 'Test metrics exist, node_scraper/resource_usage_scraper 2' do
194
- test 'Testing kube.namespace.memory.usage ' do
195
- assert_true @@hash_map_test.key?('kube.namespace.memory.usage')
196
- end
222
+ test 'Testing event count with node summary call timeout' do
223
+ stub_k8s_requests(node_summary_timeout: true)
197
224
 
198
- test 'Testing kube.cluster.memory.usage' do
199
- assert_true @@hash_map_test.key?('kube.cluster.memory.usage')
200
- end
225
+ namespace_event_count = 0
226
+ pod_event_count = 0
201
227
 
202
- test 'Testing kube.namespace.cpu.usage' do
203
- assert_true @@hash_map_test.key?('kube.namespace.cpu.usage')
204
- end
228
+ d = create_driver(TIMEOUT_TEST_CONFIG)
229
+ # Should run for two intervals, the first call to node 1 which has the only 'default' namespace pod should timeout the first time
230
+ d.run timeout: 12, expect_emits: 500, shutdown: false
205
231
 
206
- test 'Testing kube.node.cpu.allocatable ' do
207
- assert_true @@hash_map_test.key?('kube.node.cpu.allocatable')
208
- end
232
+ d.events.each do |tag, _time, record|
233
+ # Limit to one events that should be emitted once per interval
234
+ if tag == 'kube.namespace.cpu.usage' && record['name'] == 'kube-system'
235
+ namespace_event_count += 1
236
+ end
237
+ if tag == 'kube.pod.cpu.limit' && record['name'] == 'new-metrics-test-final-splunk-kubernetes-metrics-fgszl'
238
+ pod_event_count += 1
239
+ end
240
+ end
209
241
 
210
- test 'Testing kube.node.cpu.reservation ' do
211
- assert_true @@hash_map_test.key?('kube.node.cpu.reservation')
212
- end
242
+ # 2 intervals - first call times out but timer continues emitting successfully next interval
243
+ assert_equal 1, namespace_event_count, 'Number of namespace events emitted was wrong'
244
+ # 2 intervals - not timeouts
245
+ assert_equal 2, pod_event_count, 'Number of pod events emitted was wrong'
213
246
 
214
- test 'Testing kube.cluster.cpu.usage' do
215
- assert_true @@hash_map_test.key?('kube.cluster.cpu.usage')
247
+ d.instance_shutdown
216
248
  end
249
+
217
250
  end
218
251
  end