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], |
Pau Espin Pedrol | 32e3372 | 2017-09-04 16:35:02 +0200 | [diff] [blame] | 40 | [0, 2] ]) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 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 } ], |
Pau Espin Pedrol | 438a308 | 2017-08-28 14:31:28 +0200 | [diff] [blame] | 76 | 'bts': [ { 'type': 'osmo-bts-sysmo', 'times': 1 , 'ciphers': ['a5_1']}, { 'type': 'osmo-bts-trx', 'times': 1 } ], |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 77 | 'arfcn': [ { 'band': 'GSM-1800', 'times': 2 } ], |
Pau Espin Pedrol | 438a308 | 2017-08-28 14:31:28 +0200 | [diff] [blame] | 78 | 'modem': [ { 'times': 2 , 'ciphers': ['a5_0', 'a5_1']} ], |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 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 | |
Pau Espin Pedrol | 0b30279 | 2017-09-10 16:33:10 +0200 | [diff] [blame] | 83 | resources = pool.reserve(origin, config.replicate_times(want)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 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 | |
Pau Espin Pedrol | 5847551 | 2017-09-14 15:33:15 +0200 | [diff] [blame] | 97 | print('- item_matches:') |
| 98 | superset = { 'hello': 'world', 'foo': 'bar', 'ordered_list': [{'xkey': 'xvalue'},{'ykey': 'yvalue'}], 'unordered_list_set': [1, 2, 3]} |
| 99 | |
| 100 | subset = { 'foo': 'bar', 'ordered_list': [{'xkey': 'xvalue'},{'ykey': 'yvalue'}], 'unordered_list_set': [2, 1] } |
| 101 | if resource.item_matches(superset, subset): |
| 102 | print('1st subset matches correctly, pass') |
| 103 | |
| 104 | subset = { 'ordered_list': [{},{'ykey': 'yvalue'}], 'unordered_list_set': [] } |
| 105 | if resource.item_matches(superset, subset): |
| 106 | print('2nd subset matches correctly, pass') |
| 107 | |
| 108 | subset = { 'ordered_list': [{'ykey': 'yvalue'}, {'xkey': 'xvalue'}] } |
| 109 | if not resource.item_matches(superset, subset): |
| 110 | print('3rd subset should not match, pass') |
| 111 | |
| 112 | subset = { 'ordered_list': [{'xkey': 'xvalue'}, {'ykey': 'yvalue'}, {'zkey': 'zvalue'}] } |
| 113 | if not resource.item_matches(superset, subset): |
| 114 | print('3rd subset should not match, pass') |
| 115 | |
| 116 | subset = { 'unordered_list_set': [4] } |
| 117 | if not resource.item_matches(superset, subset): |
| 118 | print('4th subset should not match, pass') |
| 119 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 120 | # vim: expandtab tabstop=4 shiftwidth=4 |