This repository has been archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
inspector_test.py
471 lines (378 loc) · 17.4 KB
/
inspector_test.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import unittest
from deployer.query import Q, QueryResult
from deployer.node import Node, SimpleNode, Env, Action
from deployer.groups import Production, Staging, production, staging
from deployer.node import map_roles, suppress_action_result, EnvAction
from deployer.inspection import Inspector, PathType
from deployer.inspection.inspector import NodeIterator
from deployer.inspection import filters
from our_hosts import LocalHost1, LocalHost2, LocalHost3, LocalHost4, LocalHost5
class InspectorTest(unittest.TestCase):
def setUp(self):
class Root(Node):
@production
class A(Node):
pass
@staging
class B(Node):
class C(Node):
def __call__(self):
# __call__ is the default action
pass
def a(self): pass
def b(self): pass
def c(self): pass
c.__name__ = 'another-name' # Even if we override this name, Action.name should remain 'c'
@property
def p1(self):
return 'p1'
@property
def p2(self):
return 'p1'
query1 = Q.something
query2 = Q.something_else
self.s = Root()
self.insp = Inspector(self.s)
def test_get_childnodes(self):
insp = self.insp
self.assertEqual(repr(insp.get_childnodes()), '[<Node Root.A>, <Node Root.B>]')
self.assertEqual(repr(insp.get_actions()), '[<Action Root.a>, <Action Root.b>, <Action Root.c>]')
for a in insp.get_actions():
self.assertIn(a.name, ['a', 'b', 'c'])
def test_get_childnode(self):
insp = self.insp
self.assertEqual(repr(insp.get_childnode('A')), '<Node Root.A>')
self.assertIsInstance(insp.get_childnode('A'), Node)
self.assertRaises(AttributeError, insp.get_childnode, 'unknown_childnode')
def test_has_childnode(self):
insp = self.insp
self.assertEqual(insp.has_childnode('A'), True)
self.assertEqual(insp.has_childnode('C'), False)
def test_get_actions(self):
insp = self.insp
self.assertEqual(len(insp.get_actions()), 3)
self.assertEqual(repr(insp.get_actions()), '[<Action Root.a>, <Action Root.b>, <Action Root.c>]')
def test_get_action(self):
insp = self.insp
self.assertIsInstance(insp.get_action('a'), Action)
self.assertEqual(repr(insp.get_action('a')), '<Action Root.a>')
self.assertRaises(AttributeError, insp.get_action, 'unknown_action')
def test_has_action(self):
insp = self.insp
self.assertEqual(insp.has_action('a'), True)
self.assertEqual(insp.has_action('d'), False)
def test_get_properties(self):
insp = self.insp
self.assertEqual(repr(insp.get_properties()), '[<Action Root.p1>, <Action Root.p2>]')
def test_get_property(self):
insp = self.insp
self.assertIsInstance(insp.get_property('p1'), Action)
self.assertEqual(repr(insp.get_property('p1')), '<Action Root.p1>')
self.assertRaises(AttributeError, insp.get_property, 'unknown_property')
def has_property(self):
insp = self.insp
self.assertEqual(insp.has_property('p1'), True)
self.assertEqual(insp.has_property('unknown_property'), False)
def test_get_queries(self):
insp = self.insp
self.assertEqual(repr(insp.get_queries()), '[<Action Root.query1>, <Action Root.query2>]')
def test_get_query(self):
insp = self.insp
self.assertIsInstance(insp.get_query('query1'), Action)
self.assertEqual(repr(insp.get_query('query1')), '<Action Root.query1>')
self.assertRaises(AttributeError, insp.get_query, 'unknown_query')
def test_has_query(self):
insp = self.insp
self.assertEqual(insp.has_query('query1'), True)
self.assertEqual(insp.has_query('unknown_query'), False)
def test_get_path(self):
s = self.s
self.assertEqual(repr(Inspector(s.A).get_path()), "('Root', 'A')")
self.assertEqual(repr(Inspector(s.B.C).get_path()), "('Root', 'B', 'C')")
self.assertEqual(repr(Inspector(s.B.C).get_path(path_type=PathType.NODE_AND_NAME)),
"((<Node Root>, 'Root'), (<Node Root.B>, 'B'), (<Node Root.B.C>, 'C'))")
self.assertEqual(repr(Inspector(s.B.C).get_path(path_type=PathType.NODE_ONLY)),
"(<Node Root>, <Node Root.B>, <Node Root.B.C>)")
def test_get_name(self):
s = self.s
self.assertEqual(Inspector(s.A).get_name(), 'A')
self.assertEqual(Inspector(s.B.C).get_name(), 'C')
self.assertEqual(Inspector(s.A).get_full_name(), 'Root.A')
self.assertEqual(Inspector(s.B.C).get_full_name(), 'Root.B.C')
def test_is_callable(self):
s = self.s
self.assertEqual(Inspector(s.A).is_callable(), False)
self.assertEqual(Inspector(s.B.C).is_callable(), True)
def test_repr(self):
s = self.s
self.assertEqual(repr(Inspector(s)), 'Inspector(node=Root)')
self.assertEqual(repr(Inspector(s.B.C)), 'Inspector(node=Root.B.C)')
def test_get_group(self):
s = self.s
self.assertEqual(Inspector(s.A).get_group(), Production)
self.assertEqual(Inspector(s.B).get_group(), Staging)
self.assertEqual(Inspector(s.B.C).get_group(), Staging)
def test_walk(self):
insp = self.insp
self.assertEqual(len(list(insp.walk())), 4)
self.assertEqual({ Inspector(i).get_name() for i in insp.walk() }, { 'Root', 'A', 'B', 'C' })
for i in insp.walk():
self.assertIsInstance(i, Node)
def test_walk_from_childnode(self):
s = self.s
insp = Inspector(s.B)
self.assertEqual(len(list(insp.walk())), 2)
self.assertEqual({ Inspector(i).get_name() for i in insp.walk() }, { 'B', 'C' })
class InspectorOnEnvTest(unittest.TestCase):
def setUp(self):
class Root(Node):
class A(Node):
pass
class B(Node):
def action(self):
return 'action-b'
def action(self):
return 'action-root'
def action2(self):
return 'action-root2'
@property
def p1(self):
return 'p1'
@property
def p2(self):
return 'p2'
self.env = Env(Root())
self.insp = Inspector(self.env)
def test_get_childnodes(self):
insp = self.insp
self.assertEqual(repr(insp.get_childnodes()), '[Env(Root.A), Env(Root.B)]')
self.assertEqual(insp.get_childnode('B').action(), 'action-b')
def test_get_childnode(self):
insp = self.insp
self.assertEqual(repr(insp.get_childnode('A')), 'Env(Root.A)')
self.assertIsInstance(insp.get_childnode('A'), Env)
self.assertRaises(AttributeError, insp.get_childnode, 'unknown_childnode')
def test_get_actions(self):
insp = self.insp
self.assertIsInstance(insp.get_actions()[0], EnvAction)
def test_get_action(self):
insp = self.insp
self.assertEqual(repr(insp.get_action('action')), '<Env.Action Root.action>')
self.assertIsInstance(insp.get_action('action'), EnvAction)
self.assertEqual(insp.get_action('action')(), 'action-root')
self.assertEqual(insp.get_action('action').name, 'action')
def test_get_properties(self):
insp = self.insp
self.assertEqual(repr(insp.get_properties()), '[<Env.Action Root.p1>, <Env.Action Root.p2>]')
self.assertIsInstance(insp.get_properties()[0], EnvAction)
def test_get_property(self):
insp = self.insp
self.assertEqual(repr(insp.get_property('p1')), '<Env.Action Root.p1>')
self.assertIsInstance(insp.get_property('p1'), EnvAction)
def test_walk(self):
insp = self.insp
self.assertEqual(len(list(insp.walk())), 3)
self.assertEqual({ Inspector(i).get_name() for i in insp.walk() }, { 'Root', 'A', 'B' })
for i in insp.walk():
self.assertIsInstance(i, Env)
class InspectorChildnodesOrder(unittest.TestCase):
def test_get_childnodes_order(self):
"""
The inspector should return the childnodes in the order, as they were
nested inside the node definition.
"""
# Automatic
class Root(Node):
class A(Node): pass
class B(Node): pass
self.assertEqual(repr(Inspector(Root()).get_childnodes()), '[<Node Root.A>, <Node Root.B>]')
self.assertEqual(repr(Inspector(Env(Root())).get_childnodes()), '[Env(Root.A), Env(Root.B)]')
# Manually set creation order
class Root(Node):
class A(Node): pass
class B(Node): pass
A._node_creation_counter = 44
B._node_creation_counter = 45
self.assertEqual(repr(Inspector(Root()).get_childnodes()), '[<Node Root.A>, <Node Root.B>]')
self.assertEqual(repr(Inspector(Env(Root())).get_childnodes()), '[Env(Root.A), Env(Root.B)]')
# Manually revert the creation order
class Root(Node):
class A(Node): pass
class B(Node): pass
A._node_creation_counter = 45
B._node_creation_counter = 44
self.assertEqual(repr(Inspector(Root()).get_childnodes()), '[<Node Root.B>, <Node Root.A>]')
self.assertEqual(repr(Inspector(Env(Root())).get_childnodes()), '[Env(Root.B), Env(Root.A)]')
class InspectorTestIterIsolations(unittest.TestCase):
def setUp(self):
class A(Node):
class Hosts:
role1 = { LocalHost1, LocalHost2, LocalHost3 }
role2 = { LocalHost2, LocalHost4, LocalHost5 }
@map_roles('role1', extra='role2')
class B(SimpleNode.Array):
class C(Node):
@map_roles('extra')
class D(SimpleNode.Array):
pass
self.A = A
def test_inspection_env_node(self):
A = self.A
def test(insp, type):
self.assertEqual(len(list(insp.iter_isolations())), 9) # 3x3
# Inspection on env should yield Env objects, Node should yield
# node objects.
for i, node in insp.iter_isolations():
self.assertIsInstance(node, type)
# Test get_isolation
node = insp.get_isolation((0, 0))
self.assertIsInstance(node, type)
self.assertEqual(repr(node),
'<Node A.B[0].C.D[0]>' if type == Node else 'Env(A.B[0].C.D[0])')
node = insp.get_isolation((2, 2))
self.assertIsInstance(node, type)
self.assertEqual(repr(node),
'<Node A.B[2].C.D[2]>' if type == Node else 'Env(A.B[2].C.D[2])')
test(Inspector(A().B.C.D), Node)
test(Inspector(Env(A()).B.C.D), Env)
class InspectorIteratorTest(unittest.TestCase):
def setUp(self):
class Base(Node):
pass
self.Base = Base
# Ensure that we have a correct __repr__ for this class
self.Base.__module__ = 'inspector_test'
assert repr(Base) == "<class 'inspector_test.Base'>"
class A(Node):
def my_action(self): return 'a'
@production
class B(Base):
def my_action(self): return 'b'
def my_other_action(self): return 'b2'
class C(SimpleNode.Array):
class Hosts:
host = { LocalHost1, LocalHost2, LocalHost3, LocalHost4 }
def my_action(self): return 'c'
@staging
class E(Base):
def my_action(self): return 'e'
@production
class D(Base):
def my_action(self): return 'd'
def my_other_action(self): return 'd2'
@staging
class _P(Base):
# A private node
def my_action(self): return '_p'
self.env = Env(A())
self.insp = Inspector(self.env)
def test_walk(self):
insp = self.insp
Base = self.Base
self.assertEqual(len(insp.walk()), 6)
self.assertIsInstance(insp.walk(), NodeIterator)
self.assertEqual(len(insp.walk().filter(filters.IsInstance(Base))), 4)
def test_walk_public_only(self):
insp = self.insp
self.assertEqual(len(insp.walk(filters.PublicOnly)), 5)
self.assertEqual(len(insp.walk().filter(filters.PublicOnly)), 5)
def test_walk_private_only(self):
insp = self.insp
self.assertEqual(len(insp.walk(filters.PrivateOnly)), 1)
def test_or_operation(self):
insp = self.insp
self.assertEqual(len(insp.walk(filters.PrivateOnly | filters.HasAction('my_other_action'))), 3)
self.assertEqual({ repr(n) for n in insp.walk(filters.PrivateOnly | filters.HasAction('my_other_action')) },
{ 'Env(A.B)', 'Env(A.D)', 'Env(A._P)' })
# Check repr
self.assertEqual(repr(filters.PrivateOnly | filters.HasAction('my_other_action')),
"PrivateOnly | HasAction('my_other_action')")
def test_and_operation(self):
insp = self.insp
self.assertEqual(len(insp.walk(filters.PrivateOnly & filters.IsInstance(self.Base))), 1)
# Check repr
self.assertEqual(repr(filters.PrivateOnly & filters.IsInstance(self.Base)),
"PrivateOnly & IsInstance(<class 'inspector_test.Base'>)")
def test_not_operation(self):
insp = self.insp
self.assertEqual(len(insp.walk(~ filters.PrivateOnly)), 5)
self.assertEqual(repr(~ filters.PrivateOnly), '~ PrivateOnly')
def test_call_action(self):
insp = self.insp
Base = self.Base
result = list(insp.walk().filter(filters.IsInstance(Base)).call_action('my_action'))
self.assertEqual(len(result), 7)
self.assertEqual(set(result), { 'b', 'd', '_p', 'e', 'e', 'e', 'e' })
def test_filter_on_action(self):
insp = self.insp
result = insp.walk().filter(filters.HasAction('my_action'))
self.assertEqual(len(result), 6)
result = insp.walk().filter(filters.HasAction('my_other_action'))
self.assertEqual(len(result), 2)
result = insp.walk().filter(filters.HasAction('my_other_action')).call_action('my_other_action')
self.assertEqual(set(result), { 'b2', 'd2' })
def test_filter_on_group(self):
insp = self.insp
# Following are production nodes B, B.C, B.D
self.assertEqual(len(list(insp.walk().filter(filters.InGroup(Production)))), 3)
for node in insp.walk().filter(filters.InGroup(Production)):
self.assertEqual(Inspector(node).get_group(), Production)
def test_prefer_isolation(self):
insp = self.insp
env = self.env
result = insp.walk().prefer_isolation(LocalHost2)
self.assertEqual( set(repr(e) for e in result),
{ repr(e) for e in { env, env.B, env.D, env._P, env.B.C[LocalHost2], env.B.C[LocalHost2].E }})
# Maybe we should also implement a better Node.__eq__ and Env.__eq__, then we can do this:
# >> self.assertEqual(set(result), { env, env.B, env.D, env.B.C[LocalHost2], env.B.C[LocalHost2].E })
def test_multiple_iterator(self):
insp = self.insp
node_iterator = insp.walk()
self.assertEqual(len(list(node_iterator)), 6)
self.assertEqual(len(list(node_iterator)), 6)
self.assertEqual(len(node_iterator), 6)
self.assertEqual(len(node_iterator), 6)
def test_unknown_action(self):
insp = self.insp
self.assertRaises(AttributeError, lambda: list(insp.walk().call_action('my_action2')))
class SuppressResultTest(unittest.TestCase):
def setUp(self):
class Root(Node):
def a(self):
pass
@suppress_action_result
def b(self):
pass
self.env = Env(Root())
self.env_insp = Inspector(self.env)
self.node_insp = Inspector(Root())
def test_suppress_decorator(self):
# On an node object
self.assertEqual(self.node_insp.suppress_result_for_action('a'), False)
self.assertEqual(self.node_insp.suppress_result_for_action('b'), True)
# On an env object
self.assertEqual(self.env_insp.suppress_result_for_action('a'), False)
self.assertEqual(self.env_insp.suppress_result_for_action('b'), True)
class QueryInspectionTest(unittest.TestCase):
def setUp(self):
class Root(Node):
attr = 'value'
query = Q.attr + Q.attr
def my_action(self): pass
self.env = Env(Root())
self.env_insp = Inspector(self.env)
self.node_insp = Inspector(Root())
def test_has_query(self):
self.assertEqual(self.env_insp.has_query('query'), True)
self.assertEqual(self.env_insp.has_query('not_a_query'), False)
self.assertEqual(self.node_insp.has_query('query'), True)
self.assertEqual(self.node_insp.has_query('not_a_query'), False)
def test_get_query(self):
self.assertIsInstance(self.node_insp.get_query('query'), Action)
self.assertRaises(AttributeError, self.node_insp.get_query, 'not_a_query')
def test_trace_query(self):
# trace_query is private because it's for internal use. (In the
# shell.)
result = self.env_insp.trace_query('query')
self.assertIsInstance(result, QueryResult)
self.assertEqual(result.result, 'valuevalue')