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