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