-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
78 lines (62 loc) · 2.58 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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import copy
from unittest import TestCase
from unittest.mock import patch, Mock
from faker import Faker
import lister
faker = Faker()
class TestListerThreading(TestCase):
def get_instace(self, **kwargs):
config = {
"profile": faker.word(),
"region": faker.word(),
"regions": [faker.word() for _ in range(4)],
"arg_list": {faker.word(): faker.word() for _ in range(3)},
}
config.update(kwargs)
lister_threading = lister.ListerThreading(**config)
return config, lister_threading
def test_init(self):
kwargs, lister_threading = self.get_instace()
expected_config = copy.deepcopy(kwargs)
expected_config.update({"options": kwargs["arg_list"]})
del expected_config["arg_list"]
assert lister_threading.config == expected_config
def test_run_ok(self):
params = {"arg_list": {"list": faker.word()}}
kwargs, lister_threading = self.get_instace(**params)
kwargs["options"] = params["arg_list"]
del kwargs["arg_list"]
all_mocked = (faker.word() for _ in range(4))
instances_mock = Mock()
instances_mock.all = Mock(return_value=all_mocked)
get_ec2_mocked = Mock()
get_ec2_mocked.instances = instances_mock
console_mocked = Mock()
console_mocked.log = Mock()
expected_msg = (
f"Found [bold underline white on black]4[/] instances on"
f"region [bold underline white on black]{kwargs['region']}[/]"
)
with patch.object(lister, "get_ec2", return_value=get_ec2_mocked) as e1:
with patch.object(lister, "console", console_mocked) as e2:
lister_threading.run()
e1.assert_called_with(**kwargs)
e2.log.assert_called_with(expected_msg, style="bold green")
def test_run_fail(self):
kwargs, lister_threading = self.get_instace()
get_ec2_mocked = Mock()
console_mocked = Mock()
console_mocked.log = Mock()
with patch.object(lister, "get_ec2", return_value=get_ec2_mocked) as e1:
with patch.object(lister, "console", console_mocked) as e2:
lister_threading.run()
e1.assert_not_called()
e2.log.assert_not_called()
def test_start(self):
start_mocked = Mock()
with patch.object(lister.Thread, "start", start_mocked) as e1:
kwargs, lister_threading = self.get_instace()
lister_threading.start()
e1.assert_called()