blob: 3f7cd5198653e95eac906598f480c57ec238e4d5 [file] [log] [blame]
Neels Hofmeyr3531a192017-03-28 14:30:28 +02001#!/usr/bin/env python3
2
3import tempfile
4import os
5import pprint
6import shutil
7import atexit
8import _prep
Pau Espin Pedrol06cb5362020-05-04 18:58:53 +02009from osmo_gsm_tester.core import config, log, util, resource
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020010from osmo_gsm_tester.core.schema import generate_schemas
Neels Hofmeyr3531a192017-03-28 14:30:28 +020011
12workdir = util.get_tempdir()
13
14# override config locations to make sure we use only the test conf
15config.ENV_CONF = './conf'
16
17log.get_process_id = lambda: '123-1490837279'
18
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020019# Generate supported schemas dynamically from objects:
20generate_schemas()
21
Neels Hofmeyr3531a192017-03-28 14:30:28 +020022print('- expect solutions:')
23pprint.pprint(
24 resource.solve([ [0, 1, 2],
25 [0, 1, 2],
26 [0, 1, 2] ]) )
27pprint.pprint(
28 resource.solve([ [0, 1, 2],
29 [0, 1],
30 [0, 2] ]) ) # == [0, 1, 2]
31pprint.pprint(
32 resource.solve([ [0, 1, 2],
33 [0],
34 [0, 2] ]) ) # == [1, 0, 2]
35pprint.pprint(
36 resource.solve([ [0, 1, 2],
37 [2],
38 [0, 2] ]) ) # == [1, 2, 0]
39
40print('- expect failure to solve:')
41try:
42 resource.solve([ [0, 2],
43 [2],
Pau Espin Pedrol32e33722017-09-04 16:35:02 +020044 [0, 2] ])
Neels Hofmeyr3531a192017-03-28 14:30:28 +020045 assert False
Neels Hofmeyra8a05a22017-06-06 19:47:40 +020046except resource.NotSolvable as e:
Neels Hofmeyr3531a192017-03-28 14:30:28 +020047 print(e)
48
49print('- test removing a Resources list from itself')
50try:
51 r = resource.Resources({ 'k': [ {'a': 1, 'b': 2}, {'a': 3, 'b': 4}, ],
52 'i': [ {'c': 1, 'd': 2}, {'c': 3, 'd': 4}, ] })
53 r.drop(r)
54 assert False
55except RuntimeError as e:
Pau Espin Pedrolafa2fc32020-05-06 17:29:50 +020056 print('ok, caused exception RuntimeError: %s' % str(e))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020057
58print('- test removing a Resources list from one with the same list in it')
59r = resource.Resources({ 'k': [ {'a': 1, 'b': 2}, {'a': 3, 'b': 4}, ],
60 'i': [ {'c': 1, 'd': 2}, {'c': 3, 'd': 4}, ] })
61r.drop({ 'k': r.get('k'), 'i': r.get('i') })
62assert not r
63
64print('- test resources config and state dir:')
65resources_conf = os.path.join(_prep.script_dir, 'resource_test', 'etc',
66 'resources.conf')
67
68state_dir = config.get_state_dir()
69rrfile = state_dir.child(resource.RESERVED_RESOURCES_FILE)
70
71pool = resource.ResourcesPool()
72
73print('*** all resources:')
74pprint.pprint(pool.all_resources)
75print('*** end: all resources\n')
76
77print('- request some resources')
78want = {
Neels Hofmeyr76d81032017-05-18 18:35:32 +020079 'ip_address': [ { 'times': 1 } ],
Pau Espin Pedrol438a3082017-08-28 14:31:28 +020080 'bts': [ { 'type': 'osmo-bts-sysmo', 'times': 1 , 'ciphers': ['a5_1']}, { 'type': 'osmo-bts-trx', 'times': 1 } ],
Neels Hofmeyr3531a192017-03-28 14:30:28 +020081 'arfcn': [ { 'band': 'GSM-1800', 'times': 2 } ],
Pau Espin Pedrol438a3082017-08-28 14:31:28 +020082 'modem': [ { 'times': 2 , 'ciphers': ['a5_0', 'a5_1']} ],
Neels Hofmeyr3531a192017-03-28 14:30:28 +020083 }
Pau Espin Pedrolaab56922018-08-21 14:58:29 +020084modifiers = {
85 'bts': [ {}, {'num_trx': 2 }],
86}
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020087origin = log.Origin(None, 'testowner')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020088
Pau Espin Pedrolaab56922018-08-21 14:58:29 +020089resources = pool.reserve(origin, config.replicate_times(want), config.replicate_times(modifiers))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020090
91print('~~~ currently reserved:')
92with open(rrfile, 'r') as f:
93 print(f.read())
94print('~~~ end: currently reserved\n')
95
Pau Espin Pedrolaab56922018-08-21 14:58:29 +020096print('~~~ with modifiers:')
97print(repr(resources))
98print('~~~ end: with modifiers:')
99
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200100resources.free()
101
102print('~~~ currently reserved:')
103with open(rrfile, 'r') as f:
104 print(f.read())
105print('~~~ end: currently reserved\n')
106
Pau Espin Pedrol58475512017-09-14 15:33:15 +0200107print('- item_matches:')
108superset = { 'hello': 'world', 'foo': 'bar', 'ordered_list': [{'xkey': 'xvalue'},{'ykey': 'yvalue'}], 'unordered_list_set': [1, 2, 3]}
109
110subset = { 'foo': 'bar', 'ordered_list': [{'xkey': 'xvalue'},{'ykey': 'yvalue'}], 'unordered_list_set': [2, 1] }
111if resource.item_matches(superset, subset):
112 print('1st subset matches correctly, pass')
113
114subset = { 'ordered_list': [{},{'ykey': 'yvalue'}], 'unordered_list_set': [] }
115if resource.item_matches(superset, subset):
116 print('2nd subset matches correctly, pass')
117
118subset = { 'ordered_list': [{'ykey': 'yvalue'}, {'xkey': 'xvalue'}] }
119if not resource.item_matches(superset, subset):
120 print('3rd subset should not match, pass')
121
122subset = { 'ordered_list': [{'xkey': 'xvalue'}, {'ykey': 'yvalue'}, {'zkey': 'zvalue'}] }
123if not resource.item_matches(superset, subset):
124 print('3rd subset should not match, pass')
125
126subset = { 'unordered_list_set': [4] }
127if not resource.item_matches(superset, subset):
128 print('4th subset should not match, pass')
129
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200130# vim: expandtab tabstop=4 shiftwidth=4