-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
246 lines (226 loc) · 11.8 KB
/
tests.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import os
import unittest
import yaml
from builder import Builder
from config import Config
import input_validator as iv
from testdata.data import aws_namespaces as ns_list
class TestBuilder(unittest.TestCase):
def test_getRegionCode(self):
test_config = Config('./testdata/test-config.yml')
valid_ns = ["au", "ca", "eu", "nl", "uk", "wa"]
for ns in valid_ns:
self.assertEqual(test_config.getRegionCode(ns), f"-{ns}")
def test_get_listener_url(self):
test_config = Config('./testdata/test-config.yml')
self.assertEqual(test_config.getListenerUrl(), 'https://listener.logz.io:8053')
valid_ns = ["au", "ca", "eu", "nl", "uk", "wa"]
for ns in valid_ns:
test_config.otel['logzio_region'] = ns
self.assertEqual(test_config.getListenerUrl(), f'https://listener-{ns}.logz.io:8053')
test_config.otel['custom_listener'] = 'test'
self.assertEqual(test_config.getListenerUrl(), test_config.otel['custom_listener'])
def test_load_config(self):
# otel
test_config = Config('./testdata/test-config.yml')
self.assertEqual(test_config.otel['logzio_region'], 'us')
self.assertEqual(test_config.otel['custom_listener'], '')
self.assertEqual(test_config.otel['p8s_logzio_name'], 'cloudwatch-metrics')
self.assertEqual(test_config.otel['token'], 'fakeXamgZErKKkMhmzdVZDhuZcpGKXeo')
self.assertEqual(test_config.otel['scrape_interval'], 300)
self.assertEqual(test_config.otel['remote_timeout'], 120)
self.assertEqual(test_config.otel['log_level'], 'debug')
self.assertEqual(test_config.otel['logzio_log_level'], 'info')
self.assertEqual(test_config.otel['AWS_ACCESS_KEY_ID'], 'fakeXamgZErKKkMhmzdVZDhuZcpGKXeo')
self.assertEqual(test_config.otel['AWS_SECRET_ACCESS_KEY'], 'fakeXamgZErKKkMhmzdVZDhuZcpGKXeo')
# cloudwatch
self.assertEqual(test_config.cloudwatch['custom_config'], 'false')
self.assertEqual(test_config.cloudwatch['region'], 'us-east-1')
self.assertEqual(test_config.cloudwatch['role_arn'], '')
self.assertEqual(test_config.cloudwatch['aws_namespaces'], ["AWS/Lambda", "AWS/EC2"])
self.assertEqual(test_config.cloudwatch['set_timestamp'], 'false')
self.assertEqual(test_config.cloudwatch['delay_seconds'], 600)
self.assertEqual(test_config.cloudwatch['range_seconds'], 600)
self.assertEqual(test_config.cloudwatch['period_seconds'], 300)
# Success
try:
Config('./testdata/test-config.yml')
except Exception as e:
self.fail(f'Unexpected error {e}')
def test_load_config_env_overwrite(self):
# otel
os.environ['LOGZIO_REGION'] = 'eu'
os.environ['SCRAPE_INTERVAL'] = '600'
os.environ['P8S_LOGZIO_NAME'] = 'test'
os.environ['TOKEN'] = 'test'
os.environ['CUSTOM_LISTENER'] = 'test'
os.environ['REMOTE_TIMEOUT'] = '600'
os.environ['LOG_LEVEL'] = 'info'
os.environ['LOGZIO_LOG_LEVEL'] = 'debug'
os.environ['AWS_ACCESS_KEY_ID'] = 'test'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'test'
# cloudwatch
os.environ['DELAY_SECONDS'] = '60'
os.environ['RANGE_SECONDS'] = '60'
os.environ['PERIOD_SECONDS'] = '60'
os.environ['SET_TIMESTAMP'] = 'true'
os.environ['AWS_REGION'] = 'us-east-2'
os.environ['AWS_NAMESPACES'] = 'AWS/RDS,AWS/ELB'
os.environ['CUSTOM_CONFIG'] = 'true'
os.environ['AWS_ROLE_ARN'] = 'test'
test_config = Config('./testdata/test-config.yml')
os.environ.clear()
# otel
self.assertEqual(test_config.otel['logzio_region'], 'eu')
self.assertEqual(test_config.otel['scrape_interval'], 600)
self.assertEqual(test_config.otel['token'], 'test')
self.assertEqual(test_config.otel['custom_listener'], 'test')
self.assertEqual(test_config.otel['remote_timeout'], 600)
self.assertEqual(test_config.otel['log_level'], 'info')
self.assertEqual(test_config.otel['logzio_log_level'], 'debug')
self.assertEqual(test_config.otel['AWS_ACCESS_KEY_ID'], 'test')
self.assertEqual(test_config.otel['AWS_SECRET_ACCESS_KEY'], 'test')
# cloudwatch
self.assertEqual(test_config.cloudwatch['custom_config'], 'true')
self.assertEqual(test_config.cloudwatch['region'], 'us-east-2')
self.assertEqual(test_config.cloudwatch['role_arn'], 'test')
self.assertEqual(test_config.cloudwatch['aws_namespaces'], 'AWS/RDS,AWS/ELB')
self.assertEqual(test_config.cloudwatch['delay_seconds'], 60)
self.assertEqual(test_config.cloudwatch['range_seconds'], 60)
self.assertEqual(test_config.cloudwatch['period_seconds'], 60)
self.assertEqual(test_config.cloudwatch['set_timestamp'], 'true')
def test_add_all_cloudwatch_namespaces(self):
try:
builder = Builder('./testdata/test-config.yml', cloudwatchConfigPath='./testdata/cloudwatch-test.yml')
builder.config.cloudwatch['aws_namespaces'] = ns_list
builder.updateCloudwatchConfiguration('./cw_namespaces/')
with open(builder.cloudwatchConfigPath, 'r+') as cw:
builder.dumpAndCloseFile({'metrics': []}, cw)
except Exception as e:
self.fail(f'Unexpected error {e}')
def test_cloudwatch_config(self):
try:
builder = Builder('./testdata/test-config.yml', cloudwatchConfigPath='./testdata/cloudwatch-test.yml')
builder.config.cloudwatch['aws_namespaces'], remove = iv.is_valid_aws_namespaces('AWS/EC2,AWS/RDS')
builder.updateCloudwatchConfiguration('./cw_namespaces/')
with open(builder.cloudwatchConfigPath, 'r+') as cw:
builder.dumpAndCloseFile({'metrics': []}, cw)
except Exception as e:
self.fail(f'Unexpected error {e}')
def test_update_otel_collector(self):
try:
builder = Builder('./testdata/test-config.yml', otelConfigPath='./testdata/otel-test.yml')
with open(builder.otelConfigPath, 'r+') as otel:
builder.updateOtelConfiguration()
values = yaml.safe_load(otel)
self.assertEqual(values['receivers']['prometheus_exec']['scrape_interval'], '300s')
self.assertEqual(values['receivers']['prometheus_exec']['scrape_timeout'], '300s')
self.assertEqual(values['exporters']['prometheusremotewrite']['endpoint'],
'https://listener.logz.io:8053')
self.assertEqual(values['exporters']['prometheusremotewrite']['timeout'], '120s')
self.assertEqual(values['exporters']['prometheusremotewrite']['external_labels']['p8s_logzio_name'],
'cloudwatch-metrics')
self.assertEqual(values['exporters']['prometheusremotewrite']['headers']['Authorization'],
'Bearer fakeXamgZErKKkMhmzdVZDhuZcpGKXeo')
self.assertEqual(values['service']['telemetry']['logs']['level'], 'debug')
# reset config
with open('./testdata/default-otel.yml', 'r+') as otel_def:
testing_otel_yaml = yaml.safe_load(otel_def)
builder.dumpAndCloseFile(testing_otel_yaml, otel)
except Exception as e:
self.fail(f'Unexpected error {e}')
class TestInput(unittest.TestCase):
def test_is_valid_logzio_token(self):
# Fail Type
non_valid_types = [-2, None, 4j, ['string', 'string']]
for t in non_valid_types:
self.assertRaises(TypeError, iv.is_valid_logzio_token, t)
# Fail Value
non_valid_vals = ['12', 'quwyekclshyrflclhf', 'rDRJEidvpIbecUwshyCn4kuUjbymiHev']
for v in non_valid_vals:
self.assertRaises(ValueError, iv.is_valid_logzio_token, v)
# Success
try:
iv.is_valid_logzio_token('rDRJEidvpIbecUwshyCnGkuUjbymiHev')
except (TypeError, ValueError) as e:
self.fail(f'Unexpected error {e}')
def test_is_valid_logzio_region_code(self):
# Fail Type
non_valid_types = [-2, None, 4j, ['string', 'string']]
for t in non_valid_types:
self.assertRaises(TypeError, iv.is_valid_logzio_region_code, t)
# Fail Value
non_valid_vals = ['12', 'usa', 'au,ca']
for v in non_valid_vals:
self.assertRaises(ValueError, iv.is_valid_logzio_region_code, v)
# Success
try:
iv.is_valid_logzio_region_code('ca')
except (TypeError, ValueError) as e:
self.fail(f'Unexpected error {e}')
try:
iv.is_valid_logzio_region_code('us')
except (TypeError, ValueError) as e:
self.fail(f'Unexpected error {e}')
def test_is_valid_scrape_interval(self):
# Fail type
non_valid_types = ['12', None, False, ['string', 'string'], 4j]
for t in non_valid_types:
self.assertRaises(TypeError, iv.is_valid_interval, t)
# Fail Value
non_valid_vals = [-60, 55, 10, 306]
for v in non_valid_vals:
self.assertRaises(ValueError, iv.is_valid_interval, v)
# Success
try:
iv.is_valid_interval(360000)
except (TypeError, ValueError) as e:
self.fail(f'Unexpected error {e}')
try:
iv.is_valid_interval(60)
except (TypeError, ValueError) as e:
self.fail(f'Unexpected error {e}')
def test_is_valid_aws_namespaces(self):
# Fail Value
self.assertRaises(ValueError, iv.is_valid_aws_namespaces, '')
self.assertRaises(ValueError, iv.is_valid_aws_namespaces, 'AWS/ec2, aws/RDS, AWS/lambda, AWS/fdfdf')
# Success
self.assertTupleEqual(iv.is_valid_aws_namespaces('AWS/RDS,AWS/Lambda,AWS/CloudFront'),
(['AWS/CloudFront', 'AWS/Lambda', 'AWS/RDS'], []))
self.assertTupleEqual(iv.is_valid_aws_namespaces('AWS/RDS,AWS/nosuch,AWS/Lambda,AWS/CloudFront'),
(['AWS/CloudFront', 'AWS/Lambda', 'AWS/RDS'], ['AWS/nosuch']))
self.assertTupleEqual(iv.is_valid_aws_namespaces('AWS/RDS,AWS/Lambda,AWS/Cloudfront'),
(['AWS/Lambda', 'AWS/RDS'], ['AWS/Cloudfront']))
self.assertTupleEqual(iv.is_valid_aws_namespaces('AWS/RDS, AWS/RDS, AWS/Lambda,AWS/Lambda,AWS/Cloudfront'),
(['AWS/Lambda', 'AWS/RDS'], ['AWS/Cloudfront']))
def test_is_valid_p8s_logzio_name(self):
# Fail Type
non_valid_types = [-2, None, 4j, ['string', 'string']]
for t in non_valid_types:
self.assertRaises(TypeError, iv.is_valid_p8s_logzio_name, t)
# Success
try:
iv.is_valid_p8s_logzio_name('dev5')
except (TypeError, ValueError) as e:
self.fail(f'Unexpected error {e}')
def test_is_valid_custom_listener(self):
# Fail Type
non_valid_types = [-2, None, 4j, ['string', 'string']]
for t in non_valid_types:
self.assertRaises(TypeError, iv.is_valid_custom_listener, t)
# Fail Value
non_valid_vals = ['12', 'www.custom.listener:3000', 'custom.listener:3000', 'htt://custom.listener:3000',
'https://custom.listener:', 'https://custom.']
for v in non_valid_vals:
self.assertRaises(ValueError, iv.is_valid_custom_listener, v)
# Success
try:
iv.is_valid_custom_listener('http://custom.listener:3000')
except (TypeError, ValueError) as e:
self.fail(f'Unexpected error {e}')
try:
iv.is_valid_custom_listener('https://localhost:9200')
except (TypeError, ValueError) as e:
self.fail(f'Unexpected error {e}')
if __name__ == '__main__':
unittest.main()