[PATCH] osmo-gsm-tester[master]: resource: Refactor next_msisdn to be more generic
Hello Neels Hofmeyr, Jenkins Builder, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/4703 to look at the new patch set (#2). resource: Refactor next_msisdn to be more generic The logic in next_msisdn can be later re-used for other purposes, such as getting different location area codes for BTS. Change-Id: Ib04a34892aacd7e71f8f5961e7339add583f73f2 --- M src/osmo_gsm_tester/resource.py 1 file changed, 17 insertions(+), 15 deletions(-) git pull ssh://gerrit.osmocom.org:29418/osmo-gsm-tester refs/changes/03/4703/2 diff --git a/src/osmo_gsm_tester/resource.py b/src/osmo_gsm_tester/resource.py index 25bb00f..ecafa2f 100644 --- a/src/osmo_gsm_tester/resource.py +++ b/src/osmo_gsm_tester/resource.py @@ -38,7 +38,6 @@ USED_KEY = '_used' RESOURCES_CONF = 'resources.conf' -LAST_USED_MSISDN_FILE = 'last_used_msisdn.state' RESERVED_RESOURCES_FILE = 'reserved_resources.state' R_IP_ADDRESS = 'ip_address' @@ -189,24 +188,27 @@ if not self._remember_to_free: self.unregister_exit_handler() -def next_msisdn(self, origin): +def next_persistent_value(self, token, first_val, validate_func, inc_func, origin): origin_id = origin.origin_id() with self.state_dir.lock(origin_id): -msisdn_path = self.state_dir.child(LAST_USED_MSISDN_FILE) -log.ctx(msisdn_path) -last_msisdn = '1000' -if os.path.exists(msisdn_path): -if not os.path.isfile(msisdn_path): -raise RuntimeError('path should be a file but is not: %r' % msisdn_path) -with open(msisdn_path, 'r') as f: -last_msisdn = f.read().strip() -schema.msisdn(last_msisdn) +token_path = self.state_dir.child('last_used_%s.state' % token) +log.ctx(token_path) +last_value = first_val +if os.path.exists(token_path): +if not os.path.isfile(token_path): +raise RuntimeError('path should be a file but is not: %r' % token_path) +with open(token_path, 'r') as f: +last_value = f.read().strip() +validate_func(last_value) -next_msisdn = util.msisdn_inc(last_msisdn) -with open(msisdn_path, 'w') as f: -f.write(next_msisdn) -return next_msisdn +next_value = inc_func(last_value) +with open(token_path, 'w') as f: +f.write(next_value) +return next_value + +def next_msisdn(self, origin): +return self.next_persistent_value('msisdn', '1000', schema.msisdn, util.msisdn_inc, origin) class NoResourceExn(log.Error): -- To view, visit https://gerrit.osmocom.org/4703 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: newpatchset Gerrit-Change-Id: Ib04a34892aacd7e71f8f5961e7339add583f73f2 Gerrit-PatchSet: 2 Gerrit-Project: osmo-gsm-tester Gerrit-Branch: master Gerrit-Owner: Pau Espin PedrolGerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: Neels Hofmeyr
[PATCH] osmo-gsm-tester[master]: resource: Refactor next_msisdn to be more generic
Review at https://gerrit.osmocom.org/4703 resource: Refactor next_msisdn to be more generic The logic in next_msisdn can be later re-used for other purposes, such as getting different location areas for BTS. Change-Id: Ib04a34892aacd7e71f8f5961e7339add583f73f2 --- M src/osmo_gsm_tester/resource.py 1 file changed, 18 insertions(+), 15 deletions(-) git pull ssh://gerrit.osmocom.org:29418/osmo-gsm-tester refs/changes/03/4703/1 diff --git a/src/osmo_gsm_tester/resource.py b/src/osmo_gsm_tester/resource.py index 25bb00f..278fe4f 100644 --- a/src/osmo_gsm_tester/resource.py +++ b/src/osmo_gsm_tester/resource.py @@ -38,7 +38,6 @@ USED_KEY = '_used' RESOURCES_CONF = 'resources.conf' -LAST_USED_MSISDN_FILE = 'last_used_msisdn.state' RESERVED_RESOURCES_FILE = 'reserved_resources.state' R_IP_ADDRESS = 'ip_address' @@ -189,24 +188,28 @@ if not self._remember_to_free: self.unregister_exit_handler() -def next_msisdn(self, origin): +def next_persistent_value(self, token, first_val, validate_func, inc_func, origin): origin_id = origin.origin_id() with self.state_dir.lock(origin_id): -msisdn_path = self.state_dir.child(LAST_USED_MSISDN_FILE) -log.ctx(msisdn_path) -last_msisdn = '1000' -if os.path.exists(msisdn_path): -if not os.path.isfile(msisdn_path): -raise RuntimeError('path should be a file but is not: %r' % msisdn_path) -with open(msisdn_path, 'r') as f: -last_msisdn = f.read().strip() -schema.msisdn(last_msisdn) +token_path = self.state_dir.child(token + '_last_used.state') +log.ctx(token_path) +last_value = first_val +if os.path.exists(token_path): +if not os.path.isfile(token_path): +raise RuntimeError('path should be a file but is not: %r' % token_path) +with open(token_path, 'r') as f: +last_value = f.read().strip() +validate_func(last_value) -next_msisdn = util.msisdn_inc(last_msisdn) -with open(msisdn_path, 'w') as f: -f.write(next_msisdn) -return next_msisdn +next_value = inc_func(last_value) +validate_func(next_value) +with open(token_path, 'w') as f: +f.write(next_value) +return next_value + +def next_msisdn(self, origin): +return self.next_persistent_value('msisdn', '1000', schema.msisdn, util.msisdn_inc, origin) class NoResourceExn(log.Error): -- To view, visit https://gerrit.osmocom.org/4703 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ib04a34892aacd7e71f8f5961e7339add583f73f2 Gerrit-PatchSet: 1 Gerrit-Project: osmo-gsm-tester Gerrit-Branch: master Gerrit-Owner: Pau Espin Pedrol