From a8a05a2e23770615a5ad96ff1cb33c373f306928 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Tue, 6 Jun 2017 19:47:40 +0200 Subject: [PATCH] error log: clarify for reserving more resources than available When trying to reserve more resources than available in the resources.conf, actually print an intelligible error message: catch the nameless error from solve() and fill in what was requested for solution. Change-Id: Iba3707f1aaeb40a58c616c33af52a60c9a2e7e1f --- selftest/resource_test.py | 2 +- src/osmo_gsm_tester/resource.py | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/selftest/resource_test.py b/selftest/resource_test.py index 08f1fbfa..c78485ea 100755 --- a/selftest/resource_test.py +++ b/selftest/resource_test.py @@ -39,7 +39,7 @@ try: [2], [0, 2] ]) assert False -except resource.NoResourceExn as e: +except resource.NotSolvable as e: print(e) print('- test removing a Resources list from itself') diff --git a/src/osmo_gsm_tester/resource.py b/src/osmo_gsm_tester/resource.py index 3e8924a2..52b23c76 100644 --- a/src/osmo_gsm_tester/resource.py +++ b/src/osmo_gsm_tester/resource.py @@ -337,7 +337,13 @@ class Resources(dict): continue # figure out who gets what - solution = solve(all_matches) + try: + solution = solve(all_matches) + except NotSolvable: + # instead of a cryptic error message, raise an exception that + # conveys meaning to the user. + raise NoResourceExn('Could not resolve request to reserve resources: ' + '%d x %s with requirements: %r' % (len(want_list), key, want_list)) picked = [ my_list[i] for i in solution if i is not None ] for_origin.dbg('Picked', config.tostr(picked)) matches[key] = picked @@ -365,6 +371,9 @@ class Resources(dict): item[RESERVED_KEY] = origin_id +class NotSolvable(Exception): + pass + def solve(all_matches): ''' all_matches shall be a list of index-lists. @@ -403,8 +412,8 @@ def solve(all_matches): solution = search_in_permutations() if not solution: - raise NoResourceExn('The requested resource requirements are not solvable %r' - % all_matches) + raise NotSolvable('The requested resource requirements are not solvable %r' + % all_matches) return solution