diff options
author | Guillaume Tucker <guillaume.tucker@collabora.com> | 2019-10-31 18:52:23 +0000 |
---|---|---|
committer | Guillaume Tucker <guillaume.tucker@collabora.com> | 2019-11-06 07:45:00 +0000 |
commit | 98025dbc3022b69657ae289e4984d43f551a996c (patch) | |
tree | 5543b617f8e9e5a736cdc85f00bcfbacb161bbae /kernelci | |
parent | e1eeddde5df9e6440139ec283f8f4afc9b1e34fb (diff) |
kernelci.lab: split LabAPI.connect() as a separate method
In order to not require providing a user and token when not necessary,
make the LabAPI.connect() a public method to only connect then a user
and a token are provided. Update kernelci.lab.get_api() function
accordingly, and accept an extra lab_json argument to also load any
cached lab information.
Update kci_test accordingly to use this new approach.
Signed-off-by: Guillaume Tucker <guillaume.tucker@collabora.com>
Diffstat (limited to 'kernelci')
-rw-r--r-- | kernelci/lab/__init__.py | 32 | ||||
-rw-r--r-- | kernelci/lab/lava.py | 4 |
2 files changed, 24 insertions, 12 deletions
diff --git a/kernelci/lab/__init__.py b/kernelci/lab/__init__.py index 1acf417..d70e44d 100644 --- a/kernelci/lab/__init__.py +++ b/kernelci/lab/__init__.py @@ -16,6 +16,7 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA import importlib +import json import urlparse import xmlrpclib @@ -23,15 +24,13 @@ import xmlrpclib class LabAPI(object): """Remote API to a test lab""" - def __init__(self, config, user, token): + def __init__(self, config): """A test lab API object can be used to remotely interact with a lab *config* is a kernelci.config.lab.Lab object - *user* is the name of the user to connect to the lab - *token* is the token associated with the user to connect to the lab """ self._config = config - self._connect(user, token) + self._server = None self._devices = None @property @@ -44,7 +43,15 @@ class LabAPI(object): self._devices = self._get_devices() return self._devices - def _connect(self, user=None, token=None): + def _get_devices(self): + return list() + + def connect(self, user=None, token=None): + """Connect to the remote server API + + *user* is the name of the user to connect to the lab + *token* is the token associated with the user to connect to the lab + """ if user and token: url = urlparse.urlparse(self.config.url) api_url = "{scheme}://{user}:{token}@{loc}{path}".format( @@ -54,9 +61,6 @@ class LabAPI(object): api_url = self.config.url self._server = xmlrpclib.ServerProxy(api_url) - def _get_devices(self): - return list() - def import_devices(self, data): self._devices = data @@ -76,12 +80,20 @@ class LabAPI(object): raise NotImplementedError("Lab.submit() is required") -def get_api(lab, user, token): +def get_api(lab, user=None, token=None, lab_json=None): """Get the LabAPI object for a given lab config. *lab* is a kernelci.config.lab.Lab object *user* is the name of the user to connect to the remote lab *token* is the associated token to connect to the remote lab + *lab_json* is the path to a JSON file with cached lab information """ m = importlib.import_module('.'.join(['kernelci', 'lab', lab.lab_type])) - return m.get_api(lab, user, token) + api = m.get_api(lab) + if lab_json: + with open(lab_json) as json_file: + devices = json.load(json_file)['devices'] + api.import_devices(devices) + if user and token: + api.connect(user, token) + return api diff --git a/kernelci/lab/lava.py b/kernelci/lab/lava.py index 64bbead..290ef0e 100644 --- a/kernelci/lab/lava.py +++ b/kernelci/lab/lava.py @@ -141,6 +141,6 @@ class LAVA(LabAPI): return self._server.scheduler.submit_job(job) -def get_api(lab, user, token): +def get_api(lab): """Get a LAVA lab API object""" - return LAVA(lab, user, token) + return LAVA(lab) |