This repository has been archived by the owner on Feb 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtests.py
197 lines (180 loc) · 8.55 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
import unittest
import yaml
import builder
import util.input_validator as iv
from util.data import aws_namespaces as ns_list
class TestBuilder(unittest.TestCase):
def test_init_configuration(self):
# Fail FileNotFoundError
self.assertRaises(FileNotFoundError,
builder._init_configuration, '../wrong/path', builder.CW_RAW_CONFIG, builder.OTEL_CONFIG,
builder.OTEL_RAW_CONFIG)
self.assertRaises(FileNotFoundError,
builder._init_configuration, builder.CW_CONFIG, '../wrong/path', builder.OTEL_CONFIG,
'../wrong/path')
self.assertRaises(TypeError,
builder._init_configuration, None, builder.CW_RAW_CONFIG, 6,
builder.OTEL_RAW_CONFIG)
self.assertRaises(TypeError,
builder._init_configuration, 54.5, '../wrong/path', builder.OTEL_CONFIG,
builder.OTEL_RAW_CONFIG)
# Success
try:
builder._init_configuration(builder.CW_CONFIG, builder.CW_RAW_CONFIG, builder.OTEL_CONFIG,
builder.OTEL_RAW_CONFIG)
except (TypeError, FileNotFoundError) as e:
self.fail(f'Unexpected error {e}')
def test_update_otel_config(self):
# Fail FileNotFoundError
self.assertRaises(FileNotFoundError,
builder._update_otel_config, builder.LOGZIO_TOKEN, builder.REGION, builder.P8S_LOGZIO_NAME,
'./wrong/path')
# Success
try:
builder._update_otel_config(builder.LOGZIO_TOKEN, builder.REGION, builder.P8S_LOGZIO_NAME,
builder.OTEL_CONFIG)
except Exception as e:
self.fail(f'Unexpected error {e}')
def test_load_aws_custom_config(self):
# Fail FileNotFoundError
self.assertRaises(FileNotFoundError,
builder._load_aws_custom_config, builder.CW_CONFIG, '/wrong/path')
# Fail ParserError
self.assertRaises(yaml.parser.ParserError,
builder._load_aws_custom_config, builder.CW_CONFIG, './tests_resources/invalid.yaml')
# Success
try:
builder._load_aws_custom_config(builder.CW_CONFIG, './tests_resources/valid.yaml')
except (TypeError, ValueError) as e:
self.fail(f'Unexpected error {e}')
def test_dump_and_close_file(self):
test_file = open('./tests_resources/empty.yaml', 'r+')
valid_yaml = yaml.safe_load(open('./tests_resources/valid.yaml', 'r+'))
yaml.dump({}, test_file)
# Success
try:
builder._dump_and_close_file(valid_yaml, test_file)
except (TypeError, ValueError) as e:
self.fail(f'Unexpected error {e}')
test_file.close()
def test_add_cloudwatch_namespace(self):
# Fail FileNotFoundError
self.assertRaises(FileNotFoundError,
builder._add_cloudwatch_namespace, 'AWS/EC2', './wrong/path')
# Success
try:
for ns in ns_list:
builder._add_cloudwatch_namespace(ns, builder.CW_CONFIG)
except Exception as e:
self.fail(f'Unexpected error {e}')
def test_get_listener_url(self):
if not builder.CUSTOM_LISTENER:
# Equal
valid_ns = ["au", "ca", "eu", "nl", "uk", "wa"]
for ns in valid_ns:
self.assertEqual(builder._get_listener_url(ns), f'https://listener-{ns}.logz.io:8053')
self.assertEqual(builder._get_listener_url('us'), 'https://listener.logz.io:8053')
else:
# Equal
self.assertEqual(builder._get_listener_url('us'), builder.CUSTOM_LISTENER)
self.assertEqual(builder._get_listener_url('ca'), builder.CUSTOM_LISTENER)
self.assertEqual(builder._get_listener_url('usa'), builder.CUSTOM_LISTENER)
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_scrape_interval, t)
# Fail Value
non_valid_vals = [-60, 55, 10, 306]
for v in non_valid_vals:
self.assertRaises(ValueError, iv.is_valid_scrape_interval, v)
# Success
try:
iv.is_valid_scrape_interval(360000)
except (TypeError, ValueError) as e:
self.fail(f'Unexpected error {e}')
try:
iv.is_valid_scrape_interval(60)
except (TypeError, ValueError) as e:
self.fail(f'Unexpected error {e}')
def test_is_valid_aws_namespaces(self):
# Fail Type
non_valid_types = [-2, None, 4j, ['string', 'string']]
for t in non_valid_types:
self.assertRaises(TypeError, iv.is_valid_aws_namespaces, t)
# 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()