Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import tempfile |
| 4 | import os |
Pau Espin Pedrol | ab1904a | 2020-05-06 18:35:26 +0200 | [diff] [blame] | 5 | import sys |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 6 | import pprint |
| 7 | import shutil |
| 8 | import atexit |
Pau Espin Pedrol | 600c799 | 2020-11-09 21:17:51 +0100 | [diff] [blame] | 9 | import time |
| 10 | import threading |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 11 | import _prep |
Pau Espin Pedrol | 06cb536 | 2020-05-04 18:58:53 +0200 | [diff] [blame] | 12 | from osmo_gsm_tester.core import config, log, util, resource |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 13 | from osmo_gsm_tester.core.schema import generate_schemas |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 14 | |
| 15 | workdir = util.get_tempdir() |
| 16 | |
| 17 | # override config locations to make sure we use only the test conf |
Pau Espin Pedrol | 6c6c0e8 | 2020-05-11 18:30:58 +0200 | [diff] [blame] | 18 | config.override_conf = os.path.join(os.path.dirname(sys.argv[0]), 'conf', 'paths.conf') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 19 | |
| 20 | log.get_process_id = lambda: '123-1490837279' |
| 21 | |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 22 | # Generate supported schemas dynamically from objects: |
| 23 | generate_schemas() |
| 24 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 25 | print('- expect solutions:') |
| 26 | pprint.pprint( |
| 27 | resource.solve([ [0, 1, 2], |
| 28 | [0, 1, 2], |
| 29 | [0, 1, 2] ]) ) |
| 30 | pprint.pprint( |
| 31 | resource.solve([ [0, 1, 2], |
| 32 | [0, 1], |
| 33 | [0, 2] ]) ) # == [0, 1, 2] |
| 34 | pprint.pprint( |
| 35 | resource.solve([ [0, 1, 2], |
| 36 | [0], |
| 37 | [0, 2] ]) ) # == [1, 0, 2] |
| 38 | pprint.pprint( |
| 39 | resource.solve([ [0, 1, 2], |
| 40 | [2], |
| 41 | [0, 2] ]) ) # == [1, 2, 0] |
| 42 | |
| 43 | print('- expect failure to solve:') |
| 44 | try: |
| 45 | resource.solve([ [0, 2], |
| 46 | [2], |
Pau Espin Pedrol | 32e3372 | 2017-09-04 16:35:02 +0200 | [diff] [blame] | 47 | [0, 2] ]) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 48 | assert False |
Neels Hofmeyr | a8a05a2 | 2017-06-06 19:47:40 +0200 | [diff] [blame] | 49 | except resource.NotSolvable as e: |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 50 | print(e) |
| 51 | |
| 52 | print('- test removing a Resources list from itself') |
| 53 | try: |
| 54 | r = resource.Resources({ 'k': [ {'a': 1, 'b': 2}, {'a': 3, 'b': 4}, ], |
| 55 | 'i': [ {'c': 1, 'd': 2}, {'c': 3, 'd': 4}, ] }) |
| 56 | r.drop(r) |
| 57 | assert False |
| 58 | except RuntimeError as e: |
Pau Espin Pedrol | afa2fc3 | 2020-05-06 17:29:50 +0200 | [diff] [blame] | 59 | print('ok, caused exception RuntimeError: %s' % str(e)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 60 | |
| 61 | print('- test removing a Resources list from one with the same list in it') |
| 62 | r = resource.Resources({ 'k': [ {'a': 1, 'b': 2}, {'a': 3, 'b': 4}, ], |
| 63 | 'i': [ {'c': 1, 'd': 2}, {'c': 3, 'd': 4}, ] }) |
| 64 | r.drop({ 'k': r.get('k'), 'i': r.get('i') }) |
| 65 | assert not r |
| 66 | |
| 67 | print('- test resources config and state dir:') |
| 68 | resources_conf = os.path.join(_prep.script_dir, 'resource_test', 'etc', |
| 69 | 'resources.conf') |
| 70 | |
| 71 | state_dir = config.get_state_dir() |
| 72 | rrfile = state_dir.child(resource.RESERVED_RESOURCES_FILE) |
| 73 | |
| 74 | pool = resource.ResourcesPool() |
| 75 | |
| 76 | print('*** all resources:') |
| 77 | pprint.pprint(pool.all_resources) |
| 78 | print('*** end: all resources\n') |
| 79 | |
| 80 | print('- request some resources') |
| 81 | want = { |
Neels Hofmeyr | 76d8103 | 2017-05-18 18:35:32 +0200 | [diff] [blame] | 82 | 'ip_address': [ { 'times': 1 } ], |
Pau Espin Pedrol | 438a308 | 2017-08-28 14:31:28 +0200 | [diff] [blame] | 83 | 'bts': [ { 'type': 'osmo-bts-sysmo', 'times': 1 , 'ciphers': ['a5_1']}, { 'type': 'osmo-bts-trx', 'times': 1 } ], |
Pau Espin Pedrol | 438a308 | 2017-08-28 14:31:28 +0200 | [diff] [blame] | 84 | 'modem': [ { 'times': 2 , 'ciphers': ['a5_0', 'a5_1']} ], |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 85 | } |
Pau Espin Pedrol | aab5692 | 2018-08-21 14:58:29 +0200 | [diff] [blame] | 86 | modifiers = { |
| 87 | 'bts': [ {}, {'num_trx': 2 }], |
| 88 | } |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 89 | origin = log.Origin(None, 'testowner') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 90 | |
Pau Espin Pedrol | aab5692 | 2018-08-21 14:58:29 +0200 | [diff] [blame] | 91 | resources = pool.reserve(origin, config.replicate_times(want), config.replicate_times(modifiers)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 92 | |
| 93 | print('~~~ currently reserved:') |
| 94 | with open(rrfile, 'r') as f: |
| 95 | print(f.read()) |
| 96 | print('~~~ end: currently reserved\n') |
| 97 | |
Pau Espin Pedrol | aab5692 | 2018-08-21 14:58:29 +0200 | [diff] [blame] | 98 | print('~~~ with modifiers:') |
| 99 | print(repr(resources)) |
| 100 | print('~~~ end: with modifiers:') |
| 101 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 102 | resources.free() |
| 103 | |
| 104 | print('~~~ currently reserved:') |
| 105 | with open(rrfile, 'r') as f: |
| 106 | print(f.read()) |
| 107 | print('~~~ end: currently reserved\n') |
| 108 | |
Pau Espin Pedrol | 5847551 | 2017-09-14 15:33:15 +0200 | [diff] [blame] | 109 | print('- item_matches:') |
| 110 | superset = { 'hello': 'world', 'foo': 'bar', 'ordered_list': [{'xkey': 'xvalue'},{'ykey': 'yvalue'}], 'unordered_list_set': [1, 2, 3]} |
| 111 | |
| 112 | subset = { 'foo': 'bar', 'ordered_list': [{'xkey': 'xvalue'},{'ykey': 'yvalue'}], 'unordered_list_set': [2, 1] } |
| 113 | if resource.item_matches(superset, subset): |
| 114 | print('1st subset matches correctly, pass') |
| 115 | |
| 116 | subset = { 'ordered_list': [{},{'ykey': 'yvalue'}], 'unordered_list_set': [] } |
| 117 | if resource.item_matches(superset, subset): |
| 118 | print('2nd subset matches correctly, pass') |
| 119 | |
| 120 | subset = { 'ordered_list': [{'ykey': 'yvalue'}, {'xkey': 'xvalue'}] } |
| 121 | if not resource.item_matches(superset, subset): |
| 122 | print('3rd subset should not match, pass') |
| 123 | |
| 124 | subset = { 'ordered_list': [{'xkey': 'xvalue'}, {'ykey': 'yvalue'}, {'zkey': 'zvalue'}] } |
| 125 | if not resource.item_matches(superset, subset): |
| 126 | print('3rd subset should not match, pass') |
| 127 | |
| 128 | subset = { 'unordered_list_set': [4] } |
| 129 | if not resource.item_matches(superset, subset): |
| 130 | print('4th subset should not match, pass') |
| 131 | |
Pau Espin Pedrol | 600c799 | 2020-11-09 21:17:51 +0100 | [diff] [blame] | 132 | print('*** concurrent allocation:') |
| 133 | origin1 = log.Origin(None, 'testowner1') |
| 134 | origin2 = log.Origin(None, 'testowner2') |
| 135 | # We disable dbg() for second thread since FileWatch output result is |
| 136 | # non-deterministic, since sometimes 1 Modiffied event is triggered, sometimes 2. |
| 137 | origin1.dbg = origin2.dbg = lambda obj, *messages, _src=3, **named_items: None |
| 138 | resources2 = None |
| 139 | def second_ogt_instance(): |
| 140 | # should block here until "resources" are freed. |
| 141 | print('- 2nd instance reserve() start') |
| 142 | resources2 = pool.reserve(origin2, config.replicate_times(want), config.replicate_times(modifiers)) |
| 143 | print('- 2nd instance reserve() done') |
| 144 | resources2.free() |
| 145 | resources = pool.reserve(origin1, config.replicate_times(want), config.replicate_times(modifiers)) |
| 146 | th = threading.Thread(target=second_ogt_instance) |
| 147 | th.start() |
| 148 | time.sleep(1.0) |
| 149 | print('- 1st instance free()') |
| 150 | resources.free() |
| 151 | th.join() |
| 152 | print('*** end: concurrent allocation') |
| 153 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 154 | # vim: expandtab tabstop=4 shiftwidth=4 |