blob: f399e2020442fb35b22ac29ae9c20b21851bbdb0 [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 Pedrole8bbcbf2020-04-10 19:51:31 +02009from osmo_gsm_tester.core import config, log, util
10from osmo_gsm_tester import resource
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
19print('- expect solutions:')
20pprint.pprint(
21 resource.solve([ [0, 1, 2],
22 [0, 1, 2],
23 [0, 1, 2] ]) )
24pprint.pprint(
25 resource.solve([ [0, 1, 2],
26 [0, 1],
27 [0, 2] ]) ) # == [0, 1, 2]
28pprint.pprint(
29 resource.solve([ [0, 1, 2],
30 [0],
31 [0, 2] ]) ) # == [1, 0, 2]
32pprint.pprint(
33 resource.solve([ [0, 1, 2],
34 [2],
35 [0, 2] ]) ) # == [1, 2, 0]
36
37print('- expect failure to solve:')
38try:
39 resource.solve([ [0, 2],
40 [2],
Pau Espin Pedrol32e33722017-09-04 16:35:02 +020041 [0, 2] ])
Neels Hofmeyr3531a192017-03-28 14:30:28 +020042 assert False
Neels Hofmeyra8a05a22017-06-06 19:47:40 +020043except resource.NotSolvable as e:
Neels Hofmeyr3531a192017-03-28 14:30:28 +020044 print(e)
45
46print('- test removing a Resources list from itself')
47try:
48 r = resource.Resources({ 'k': [ {'a': 1, 'b': 2}, {'a': 3, 'b': 4}, ],
49 'i': [ {'c': 1, 'd': 2}, {'c': 3, 'd': 4}, ] })
50 r.drop(r)
51 assert False
52except RuntimeError as e:
53 print('ok, caused exception: %r' % e)
54
55print('- test removing a Resources list from one with the same list in it')
56r = resource.Resources({ 'k': [ {'a': 1, 'b': 2}, {'a': 3, 'b': 4}, ],
57 'i': [ {'c': 1, 'd': 2}, {'c': 3, 'd': 4}, ] })
58r.drop({ 'k': r.get('k'), 'i': r.get('i') })
59assert not r
60
61print('- test resources config and state dir:')
62resources_conf = os.path.join(_prep.script_dir, 'resource_test', 'etc',
63 'resources.conf')
64
65state_dir = config.get_state_dir()
66rrfile = state_dir.child(resource.RESERVED_RESOURCES_FILE)
67
68pool = resource.ResourcesPool()
69
70print('*** all resources:')
71pprint.pprint(pool.all_resources)
72print('*** end: all resources\n')
73
74print('- request some resources')
75want = {
Neels Hofmeyr76d81032017-05-18 18:35:32 +020076 'ip_address': [ { 'times': 1 } ],
Pau Espin Pedrol438a3082017-08-28 14:31:28 +020077 'bts': [ { 'type': 'osmo-bts-sysmo', 'times': 1 , 'ciphers': ['a5_1']}, { 'type': 'osmo-bts-trx', 'times': 1 } ],
Neels Hofmeyr3531a192017-03-28 14:30:28 +020078 'arfcn': [ { 'band': 'GSM-1800', 'times': 2 } ],
Pau Espin Pedrol438a3082017-08-28 14:31:28 +020079 'modem': [ { 'times': 2 , 'ciphers': ['a5_0', 'a5_1']} ],
Neels Hofmeyr3531a192017-03-28 14:30:28 +020080 }
Pau Espin Pedrolaab56922018-08-21 14:58:29 +020081modifiers = {
82 'bts': [ {}, {'num_trx': 2 }],
83}
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020084origin = log.Origin(None, 'testowner')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020085
Pau Espin Pedrolaab56922018-08-21 14:58:29 +020086resources = pool.reserve(origin, config.replicate_times(want), config.replicate_times(modifiers))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020087
88print('~~~ currently reserved:')
89with open(rrfile, 'r') as f:
90 print(f.read())
91print('~~~ end: currently reserved\n')
92
Pau Espin Pedrolaab56922018-08-21 14:58:29 +020093print('~~~ with modifiers:')
94print(repr(resources))
95print('~~~ end: with modifiers:')
96
Neels Hofmeyr3531a192017-03-28 14:30:28 +020097resources.free()
98
99print('~~~ currently reserved:')
100with open(rrfile, 'r') as f:
101 print(f.read())
102print('~~~ end: currently reserved\n')
103
Pau Espin Pedrol58475512017-09-14 15:33:15 +0200104print('- item_matches:')
105superset = { 'hello': 'world', 'foo': 'bar', 'ordered_list': [{'xkey': 'xvalue'},{'ykey': 'yvalue'}], 'unordered_list_set': [1, 2, 3]}
106
107subset = { 'foo': 'bar', 'ordered_list': [{'xkey': 'xvalue'},{'ykey': 'yvalue'}], 'unordered_list_set': [2, 1] }
108if resource.item_matches(superset, subset):
109 print('1st subset matches correctly, pass')
110
111subset = { 'ordered_list': [{},{'ykey': 'yvalue'}], 'unordered_list_set': [] }
112if resource.item_matches(superset, subset):
113 print('2nd subset matches correctly, pass')
114
115subset = { 'ordered_list': [{'ykey': 'yvalue'}, {'xkey': 'xvalue'}] }
116if not resource.item_matches(superset, subset):
117 print('3rd subset should not match, pass')
118
119subset = { 'ordered_list': [{'xkey': 'xvalue'}, {'ykey': 'yvalue'}, {'zkey': 'zvalue'}] }
120if not resource.item_matches(superset, subset):
121 print('3rd subset should not match, pass')
122
123subset = { 'unordered_list_set': [4] }
124if not resource.item_matches(superset, subset):
125 print('4th subset should not match, pass')
126
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200127# vim: expandtab tabstop=4 shiftwidth=4