Skip to content

Commit 01dd782

Browse files
committed
refactor a bit get_config and adding simple test
1 parent 480d7a7 commit 01dd782

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

python/chaos_lib.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ def request(self, method, url, **kwargs):
2222
time.sleep(self.delay / 1000.0)
2323
return super(SessionWithDelay, self).request(method, url, **kwargs)
2424

25-
def close(self):
26-
self.session.close()
27-
2825

2926
def get_config(config_key):
3027
param = SSMParameter(os.environ['FAILURE_INJECTION_PARAM'])
@@ -36,11 +33,11 @@ def get_config(config_key):
3633
key_ = value[config_key]
3734
return key_
3835
except InvalidParameterError as e:
39-
print("{} does not exist in SSM".format(e))
40-
raise InvalidParameterError
36+
# key does not exist in SSM
37+
raise InvalidParameterError("{} does not exist in SSM".format(e))
4138
except KeyError as e:
42-
print("{} is not a valid Key in the SSM configuration".format(e))
43-
raise KeyError
39+
# not a valid Key in the SSM configuration
40+
raise KeyError("{} is not a valid Key in the SSM configuration".format(e))
4441

4542

4643
def corrupt_delay(func):
@@ -76,7 +73,6 @@ def wrapper(*args, **kwargs):
7673
print("exception_msg from config {}".format(exception_msg))
7774
print("corrupting now")
7875
raise Exception(exception_msg)
79-
return result
8076
return wrapper
8177

8278

python/test.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from chaos_lib import get_config
2+
from ssm_cache import InvalidParameterError
3+
import unittest
4+
import os
5+
import warnings
6+
import boto3
7+
8+
client = boto3.client('ssm', region_name='eu-north-1')
9+
10+
11+
def ignore_warnings(test_func):
12+
def do_test(self, *args, **kwargs):
13+
with warnings.catch_warnings():
14+
warnings.simplefilter("ignore", ResourceWarning)
15+
warnings.simplefilter("ignore", DeprecationWarning)
16+
test_func(self, *args, **kwargs)
17+
return do_test
18+
19+
20+
class TestStringMethods(unittest.TestCase):
21+
22+
@ignore_warnings
23+
def setUp(self):
24+
os.environ['FAILURE_INJECTION_PARAM'] = 'test.config'
25+
client.put_parameter(
26+
Value="{ \"delay\": 200, \"isEnabled\": true, \"error_code\": 404, \"exception_msg\": \"I FAILED\"}",
27+
Name='test.config',
28+
Type='String',
29+
Overwrite=True
30+
)
31+
32+
@ignore_warnings
33+
def tearDown(self):
34+
client.delete_parameters(Names=['test.config'])
35+
36+
@ignore_warnings
37+
def test_get_config(self):
38+
isEnabled = get_config('isEnabled')
39+
self.assertEqual(isEnabled, True or False)
40+
41+
@ignore_warnings
42+
def test_get_config_delay(self):
43+
delay = get_config('delay')
44+
self.assertEqual(delay, 200)
45+
46+
@ignore_warnings
47+
def test_get_config_error_code(self):
48+
delay = get_config('error_code')
49+
self.assertEqual(delay, 404)
50+
51+
@ignore_warnings
52+
def test_get_config_bad_key(self):
53+
with self.assertRaises(KeyError):
54+
get_config('dela')
55+
56+
@ignore_warnings
57+
def test_get_config_bad_config(self):
58+
os.environ['FAILURE_INJECTION_PARAM'] = 'test.conf'
59+
with self.assertRaises(InvalidParameterError):
60+
get_config('delay')
61+
62+
63+
64+
65+
if __name__ == '__main__':
66+
unittest.main()

0 commit comments

Comments
 (0)