diff options
author | Lars Wirzenius <liw@liw.fi> | 2018-03-30 10:03:56 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2018-03-30 10:32:54 +0300 |
commit | 974631edc803b66e4487431c596664371bf0400a (patch) | |
tree | 18d003e2e607a1619b1204f6d4529f5240649518 | |
parent | 3e98768fc55aedadc7362a58f50134db9eb4fc63 (diff) | |
download | ick2-974631edc803b66e4487431c596664371bf0400a.tar.gz |
Add: BlobClient class to use artifact store API
-rw-r--r-- | ick2/__init__.py | 2 | ||||
-rw-r--r-- | ick2/client.py | 34 | ||||
-rw-r--r-- | ick2/client_tests.py | 67 |
3 files changed, 99 insertions, 4 deletions
diff --git a/ick2/__init__.py b/ick2/__init__.py index 0fd0013..5b312e7 100644 --- a/ick2/__init__.py +++ b/ick2/__init__.py @@ -52,4 +52,4 @@ from .controllerapi import ( from .blobapi import BlobAPI from .blob_store import BlobStore -from .client import HttpAPI, HttpError, ControllerClient +from .client import HttpAPI, HttpError, ControllerClient, BlobClient diff --git a/ick2/client.py b/ick2/client.py index 28d4c1e..c26b298 100644 --- a/ick2/client.py +++ b/ick2/client.py @@ -133,6 +133,13 @@ class ControllerClient: version = self._api.get_dict(url) return version.get('blob_service') + def get_blob_client(self): + url = self.get_blob_service_url() + blobs = BlobClient() + blobs.set_url(blobs) + blobs.set_http_api(self._api) + return blobs + def register(self): assert self._url is not None url = self.url('/workers') @@ -159,3 +166,30 @@ class ControllerClient: } body = json.dumps(work) self._api.post(url, headers=headers, body=body) + + +class BlobClient: + + def __init__(self): + self._url = None + self._api = None + + def set_url(self, url): + self._url = url + + def set_http_api(self, api): + self._api = api + + def url(self, blob_name): + assert self._url is not None + return '{}/blobs/{}'.format(self._url, blob_name) + + def download(self, blob_name): + logging.info('Download blob %s', blob_name) + url = self.url(blob_name) + return self._api.get_blob(url) + + def upload(self, blob_name, blob): + logging.info('Upload blob %s', blob_name) + url = self.url(blob_name) + return self._api.put(url, body=blob) diff --git a/ick2/client_tests.py b/ick2/client_tests.py index 856f4c3..57486bd 100644 --- a/ick2/client_tests.py +++ b/ick2/client_tests.py @@ -61,12 +61,12 @@ class HttpAPITests(unittest.TestCase): def test_get_blob_raises_exception_on_error(self): self.session.response = FakeResponse(404) with self.assertRaises(ick2.HttpError): - self.client.get_blob('http://blobs/blob/foo') + self.client.get_blob('http://blobs/foo') def test_get_blob_returns_response(self): blob = b'hello, world\n' self.session.response = FakeResponse(200, body=blob) - obj = self.client.get_blob('http://blobs/blob/foo') + obj = self.client.get_blob('http://blobs/foo') self.assertEqual(obj, blob) def test_post_raises_exception_on_error(self): @@ -89,7 +89,7 @@ class HttpAPITests(unittest.TestCase): blob = b'fooblob' self.session.response = FakeResponse(400) with self.assertRaises(ick2.HttpError): - self.client.put('http://blobs/blob/foo', body=blob) + self.client.put('http://blobs/foo', body=blob) def test_put_succeeds(self): blob = b'fooblob' @@ -165,6 +165,67 @@ class ControllerClientTests(unittest.TestCase): self.assertEqual(self.controller.get_blob_service_url(), url) +class BlobServiceClientTests(unittest.TestCase): + + def setUp(self): + self.session = FakeHttpSession() + self.session.token = 'SECRET-TOKEN' + + self.client = ick2.HttpAPI() + self.client.set_session(self.session) + self.client.set_token(self.session.token) + + self.controller = ick2.ControllerClient() + self.controller.set_http_api(self.client) + self.controller.set_controller_url('https://controller') + self.controller.set_token('SECRET-TOKEN') + self.controller.set_client_name('asterix') + + def get_blob_client(self): + url = 'https://blobs' + version = { + 'blob_service': url, + } + self.session.response = FakeResponse( + 200, body=json.dumps(version), content_type=json_type) + + return self.controller.get_blob_client() + + def test_get_blob_client_raises_exception_on_error(self): + self.session.response = FakeResponse(400) + with self.assertRaises(ick2.HttpError): + self.controller.get_blob_client() + + def test_get_blob_client_succeeds(self): + blobs = self.get_blob_client() + self.assertTrue(isinstance(blobs, ick2.BlobClient)) + + def test_download_raises_exception_on_error(self): + blobs = self.get_blob_client() + self.session.response = FakeResponse(400) + with self.assertRaises(ick2.HttpError): + blobs.download('foo') + + def test_download_succeeds(self): + blobs = self.get_blob_client() + blob = b'hello, world' + self.session.response = FakeResponse(200, body=blob) + self.assertEqual(blobs.download('foo'), blob) + + def test_upload_raises_exception_on_error(self): + blobs = self.get_blob_client() + blob = b'hello, world' + self.session.response = FakeResponse(400) + with self.assertRaises(ick2.HttpError): + blobs.upload('foo', blob) + + def test_upload_succeeds(self): + blobs = self.get_blob_client() + blob = b'hello, world' + self.session.response = FakeResponse(200) + self.assertEqual(blobs.upload('foo', blob), None) + + class FakeHttpSession: def __init__(self): |