blob: 2d0f8805e3288807a736ddcb5f1131638213f279 [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
9from osmo_gsm_tester import config, log, resource, util
10
11workdir = util.get_tempdir()
12
13# override config locations to make sure we use only the test conf
14config.ENV_CONF = './conf'
15
16log.get_process_id = lambda: '123-1490837279'
17
18print('- expect solutions:')
19pprint.pprint(
20 resource.solve([ [0, 1, 2],
21 [0, 1, 2],
22 [0, 1, 2] ]) )
23pprint.pprint(
24 resource.solve([ [0, 1, 2],
25 [0, 1],
26 [0, 2] ]) ) # == [0, 1, 2]
27pprint.pprint(
28 resource.solve([ [0, 1, 2],
29 [0],
30 [0, 2] ]) ) # == [1, 0, 2]
31pprint.pprint(
32 resource.solve([ [0, 1, 2],
33 [2],
34 [0, 2] ]) ) # == [1, 2, 0]
35
36print('- expect failure to solve:')
37try:
38 resource.solve([ [0, 2],
39 [2],
40 [0, 2] ])
41 assert False
42except resource.NoResourceExn as e:
43 print(e)
44
45print('- test removing a Resources list from itself')
46try:
47 r = resource.Resources({ 'k': [ {'a': 1, 'b': 2}, {'a': 3, 'b': 4}, ],
48 'i': [ {'c': 1, 'd': 2}, {'c': 3, 'd': 4}, ] })
49 r.drop(r)
50 assert False
51except RuntimeError as e:
52 print('ok, caused exception: %r' % e)
53
54print('- test removing a Resources list from one with the same list in it')
55r = resource.Resources({ 'k': [ {'a': 1, 'b': 2}, {'a': 3, 'b': 4}, ],
56 'i': [ {'c': 1, 'd': 2}, {'c': 3, 'd': 4}, ] })
57r.drop({ 'k': r.get('k'), 'i': r.get('i') })
58assert not r
59
60print('- test resources config and state dir:')
61resources_conf = os.path.join(_prep.script_dir, 'resource_test', 'etc',
62 'resources.conf')
63
64state_dir = config.get_state_dir()
65rrfile = state_dir.child(resource.RESERVED_RESOURCES_FILE)
66
67pool = resource.ResourcesPool()
68
69print('*** all resources:')
70pprint.pprint(pool.all_resources)
71print('*** end: all resources\n')
72
73print('- request some resources')
74want = {
75 'nitb_iface': [ { 'times': 1 } ],
76 'bts': [ { 'type': 'sysmo', 'times': 1 }, { 'type': 'oct', 'times': 1 } ],
77 'arfcn': [ { 'band': 'GSM-1800', 'times': 2 } ],
78 'modem': [ { 'times': 2 } ],
79 }
80
81origin = log.Origin('testowner')
82
83resources = pool.reserve(origin, want)
84
85print('~~~ currently reserved:')
86with open(rrfile, 'r') as f:
87 print(f.read())
88print('~~~ end: currently reserved\n')
89
90resources.free()
91
92print('~~~ currently reserved:')
93with open(rrfile, 'r') as f:
94 print(f.read())
95print('~~~ end: currently reserved\n')
96
97# vim: expandtab tabstop=4 shiftwidth=4