-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_universal_hooks.py
233 lines (171 loc) · 6.88 KB
/
test_universal_hooks.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
import io
import os
import unittest
import universal_hooks
from universal_hooks import _regex_from_dir, _make_dir_matchers, FileSystem, _run_dir, _run_pkg_dirs, \
TransactionInfo, UniversalHooksPlugin
HOOK_NAMES = 'pre_config config resolved sack pre_transaction transaction'.split()
class TestHooks(unittest.TestCase):
def test_suffix(self):
m = _regex_from_dir('ea-__WILDCARD__')
self.assertTrue(m.search('ea-'))
self.assertTrue(m.search('ea-foo'))
def test_prefix(self):
m = _regex_from_dir('__WILDCARD__-devel')
self.assertTrue(m.search('foo-devel'))
def test_middle(self):
m = _regex_from_dir('__WILDCARD__perl__WILDCARD__')
self.assertTrue(m.search('perl'))
self.assertTrue(m.search('preperlpost'))
def test_make_dir_matchers(self):
universal_hooks.fs = WildcardStub()
dir_matchers = _make_dir_matchers('/unit/wildcard_root')
self.assertEqual(2, len(dir_matchers))
self.assertListEqual(['dir1', 'dir2__WILDCARD__'], sorted(dir_matchers.keys()))
self.assertTrue(dir_matchers['dir1'].search('dir1'))
self.assertTrue(dir_matchers['dir2__WILDCARD__'].search('dir2'))
self.assertTrue(dir_matchers['dir2__WILDCARD__'].search('dir2-foo'))
def test_run_dir(self):
log_spy = LogSpy()
universal_hooks.fs = ScriptStub()
subproc_spy = SubprocSpy()
universal_hooks.subprocess = subproc_spy
_run_dir('/etc/dnf/universal-hooks/transaction', log_spy)
self.assertListEqual([], log_spy.history)
self.assertListEqual(
['/etc/dnf/universal-hooks/transaction/script1 ', '/etc/dnf/universal-hooks/transaction/script2 '],
subproc_spy.history)
def test_run_pkg_dirs(self):
fs_stub = PkgDirsStub()
temp_file_spy = TempFileSpy()
fs_stub.temp_file = temp_file_spy
universal_hooks.fs = fs_stub
subproc_spy = SubprocSpy()
universal_hooks.subprocess = subproc_spy
log_spy = LogSpy()
_run_pkg_dirs('/etc/dnf/universal-hooks', log_spy, 'transaction', TransactionStub())
self.assertListEqual([], log_spy.history)
self.assertListEqual(['/etc/dnf/universal-hooks/pkgs/unit-pkg1/transaction/unit-pkg1-script ',
'/etc/dnf/universal-hooks/multi_pkgs/transaction/unit-__WILDCARD__/unit-wildcard-script --pkg_list=/unit/tmp/unit-temp-file'],
subproc_spy.history)
expected_file_content = '''unit-pkg1
unit-pkg2
'''
self.assertEqual(expected_file_content, temp_file_spy.content.getvalue())
def test_plugin(self):
fs_stub = PkgDirsStub()
temp_file_spy = TempFileSpy()
fs_stub.temp_file = temp_file_spy
universal_hooks.fs = fs_stub
subproc_spy = SubprocSpy()
universal_hooks.subprocess = subproc_spy
p = UniversalHooksPlugin(DnfBaseStub(), None)
for hook in HOOK_NAMES:
method = getattr(p, hook)
method()
self.assertListEqual([
'/etc/dnf/universal-hooks/pre_config/script ',
'/etc/dnf/universal-hooks/config/script ',
'/etc/dnf/universal-hooks/resolved/script ',
'/etc/dnf/universal-hooks/sack/script ',
'/etc/dnf/universal-hooks/pre_transaction/script ',
'/etc/dnf/universal-hooks/pkgs/unit-pkg1/transaction/unit-pkg1-script ',
'/etc/dnf/universal-hooks/multi_pkgs/transaction/unit-__WILDCARD__/unit-wildcard-script --pkg_list=/unit/tmp/unit-temp-file',
'/etc/dnf/universal-hooks/transaction/script ',
], subproc_spy.history)
class WildcardStub(FileSystem):
def glob(self, pathname):
return [
'/unit/wildcard_root/dir1',
'/unit/wildcard_root/dir2__WILDCARD__',
]
def isdir(self, pathname):
return 'dir' in pathname
def access(self, path, mode):
pass
def NamedTemporaryFile(self, mode, encoding):
pass
class ScriptStub(FileSystem):
def glob(self, pathname):
return [
'/etc/dnf/universal-hooks/transaction/script1',
'/etc/dnf/universal-hooks/transaction/script2',
]
def isdir(self, pathname):
return '/etc/dnf/universal-hooks/transaction' == pathname
def access(self, path, mode):
return 'script' in path and os.X_OK & mode
def NamedTemporaryFile(self, mode, encoding):
pass
class LogSpy:
def __init__(self) -> None:
self.history = []
def error(self, msg):
self.history.append((self.error.__name__, msg))
class SubprocSpy(object):
def __init__(self) -> None:
self.history = []
def run(self, args, shell=False):
self.history.append(args)
return GoodCompletionStub()
class GoodCompletionStub:
@property
def returncode(self):
return 0
class TransactionStub(TransactionInfo):
def getMembers(self):
return [
MemberStub('unit-pkg1'),
MemberStub('unit-pkg2'),
]
class MemberStub:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
class PkgDirsStub(FileSystem):
def __init__(self) -> None:
self.temp_file = None
def glob(self, pathname):
paths = {
'/etc/dnf/universal-hooks/multi_pkgs/transaction/*': [
'/etc/dnf/universal-hooks/multi_pkgs/transaction/unit-__WILDCARD__'
],
'/etc/dnf/universal-hooks/multi_pkgs/transaction/unit-__WILDCARD__/*': [
'/etc/dnf/universal-hooks/multi_pkgs/transaction/unit-__WILDCARD__/unit-wildcard-script'
],
'/etc/dnf/universal-hooks/pkgs/unit-pkg1/transaction/*': [
'/etc/dnf/universal-hooks/pkgs/unit-pkg1/transaction/unit-pkg1-script'
],
'/etc/dnf/universal-hooks/pkgs/unit-pkg2/transaction/*': [],
'/etc/dnf/universal-hooks/multi_pkgs/pre_transaction/*': [],
'/etc/dnf/universal-hooks/pkgs/unit-pkg1/pre_transaction/*': [],
}
for hook in HOOK_NAMES:
paths[f'/etc/dnf/universal-hooks/{hook}/*'] = [f'/etc/dnf/universal-hooks/{hook}/script']
return paths[pathname]
def isdir(self, pathname):
return 'script' not in pathname
def access(self, path, mode):
return 'script' in path
def NamedTemporaryFile(self, mode, encoding):
return self.temp_file
class TempFileSpy:
def __init__(self) -> None:
self.content = io.StringIO()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
pass
def write(self, data):
self.content.write(data)
def flush(self):
pass
@property
def name(self):
return '/unit/tmp/unit-temp-file'
class DnfBaseStub:
@property
def transaction(self):
return [MemberStub('unit-pkg1')]