/
test_AdvancedRoutingHelper.py
117 lines (89 loc) · 3.62 KB
/
test_AdvancedRoutingHelper.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
"""Main unit testing module."""
import pathlib
import shutil
import unittest.mock as mock
import routing_validator
class TestAdvancedRoutingHelper():
"""Main test class."""
test_dir = pathlib.Path('/tmp/test/charm-advanced-routing')
test_ifup_path = test_dir / 'symlink_test' / 'ifup'
test_netplanup_path = test_dir / 'symlink_test' / 'netplanup'
test_cleanup_path = test_dir / 'symlink_test' / 'netplandown'
test_script = 'test-script'
@classmethod
def setUp(cls):
"""Setup."""
cls.test_dir.mkdir(parents=True)
cls.test_ifup_path.mkdir(parents=True)
cls.test_netplanup_path.mkdir(parents=True)
@classmethod
def tearDown(cls):
"""Teardown method."""
try:
shutil.rmtree(cls.test_dir)
except OSError:
pass
def test_pre_setup(self, advanced_routing_helper):
"""Test pre_setup."""
test_obj = advanced_routing_helper
test_obj.common_location = self.test_dir
test_obj.routing_script_name = self.test_script
test_obj.policy_routing_service_dir_path = self.test_dir
try:
(test_obj.policy_routing_service_dir_path / 'charm-pre-install-policy-routing.service').unlink()
except FileNotFoundError:
pass
test_obj.pre_setup()
uppath = test_obj.common_location / 'if-up'
assert uppath.exists()
def test_setup(self, advanced_routing_helper):
"""Test setup."""
def noop():
pass
test_obj = advanced_routing_helper
test_obj.common_location = self.test_dir
test_obj.routing_script_name = self.test_script
test_obj.common_ifup_path = self.test_dir / 'if-up' / self.test_script
test_obj.common_cleanup_path = self.test_dir / 'cleanup' / self.test_script
test_obj.post_setup = noop
routing_validator.RoutingConfigValidator.__init__ = mock.Mock(return_value=None)
test_obj.setup()
assert test_obj.common_ifup_path.exists()
assert test_obj.common_cleanup_path.exists()
def test_remove_routes(self, advanced_routing_helper, mock_check_call):
"""Test post_setup."""
test_obj = advanced_routing_helper
with mock.patch('charmhelpers.core.host.lsb_release') as lsbrelbionic:
test_obj.netplan_up_dir_path = self.test_netplanup_path
test_obj.common_cleanup_path = self.test_cleanup_path
lsbrelbionic.return_value = "bionic"
test_obj.remove_routes()
with mock.patch('charmhelpers.core.host.lsb_release') as lsbrelxenial:
test_obj.net_tools_up_dir_path = self.test_ifup_path
test_obj.common_cleanup_path = self.test_cleanup_path
lsbrelxenial.return_value = "xenial"
test_obj.remove_routes()
assert not test_obj.common_ifup_path.exists()
assert not test_obj.common_cleanup_path.exists()
def test_symlink_force(self, advanced_routing_helper):
"""Test symlink_force."""
test_obj = advanced_routing_helper
target = self.test_dir / 'testfile'
link = self.test_dir / 'testlink'
try:
target.unlink()
except FileNotFoundError:
pass # dont care
# touch target file to link to
try:
with open(target, "w+") as f:
f.write('dont care\n')
except IOError:
pass # dont care
assert target.exists()
# link it
test_obj.symlink_force(target, link)
assert link.exists()
# link it again
test_obj.symlink_force(target, link)
assert link.exists()