Add some initial basic tests for Backblaze B2 storage driver.
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/88c22780 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/88c22780 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/88c22780 Branch: refs/heads/trunk Commit: 88c22780e1d546b89a1386133e5f81f9f488139e Parents: b035d8a Author: Tomaz Muraus <[email protected]> Authored: Sun Jan 17 19:03:33 2016 +0100 Committer: Tomaz Muraus <[email protected]> Committed: Sun Jan 17 19:21:16 2016 +0100 ---------------------------------------------------------------------- .../fixtures/backblaze_b2/b2_create_bucket.json | 6 + .../fixtures/backblaze_b2/b2_delete_bucket.json | 6 + .../backblaze_b2/b2_delete_file_version.json | 4 + .../backblaze_b2/b2_get_upload_url.json | 5 + .../fixtures/backblaze_b2/b2_list_buckets.json | 22 +++ .../backblaze_b2/b2_list_file_names.json | 34 ++++ .../fixtures/backblaze_b2/b2_upload_file.json | 10 ++ libcloud/test/storage/test_backblaze_b2.py | 172 +++++++++++++++++++ 8 files changed, 259 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/88c22780/libcloud/test/storage/fixtures/backblaze_b2/b2_create_bucket.json ---------------------------------------------------------------------- diff --git a/libcloud/test/storage/fixtures/backblaze_b2/b2_create_bucket.json b/libcloud/test/storage/fixtures/backblaze_b2/b2_create_bucket.json new file mode 100644 index 0000000..326a1d4 --- /dev/null +++ b/libcloud/test/storage/fixtures/backblaze_b2/b2_create_bucket.json @@ -0,0 +1,6 @@ +{ + "accountId": "8c7eea3fe570", + "bucketId": "681c87aebeaa530f5e250710", + "bucketName": "test0005", + "bucketType": "allPrivate" +} http://git-wip-us.apache.org/repos/asf/libcloud/blob/88c22780/libcloud/test/storage/fixtures/backblaze_b2/b2_delete_bucket.json ---------------------------------------------------------------------- diff --git a/libcloud/test/storage/fixtures/backblaze_b2/b2_delete_bucket.json b/libcloud/test/storage/fixtures/backblaze_b2/b2_delete_bucket.json new file mode 100644 index 0000000..326a1d4 --- /dev/null +++ b/libcloud/test/storage/fixtures/backblaze_b2/b2_delete_bucket.json @@ -0,0 +1,6 @@ +{ + "accountId": "8c7eea3fe570", + "bucketId": "681c87aebeaa530f5e250710", + "bucketName": "test0005", + "bucketType": "allPrivate" +} http://git-wip-us.apache.org/repos/asf/libcloud/blob/88c22780/libcloud/test/storage/fixtures/backblaze_b2/b2_delete_file_version.json ---------------------------------------------------------------------- diff --git a/libcloud/test/storage/fixtures/backblaze_b2/b2_delete_file_version.json b/libcloud/test/storage/fixtures/backblaze_b2/b2_delete_file_version.json new file mode 100644 index 0000000..c0ac2f8 --- /dev/null +++ b/libcloud/test/storage/fixtures/backblaze_b2/b2_delete_file_version.json @@ -0,0 +1,4 @@ +{ + "fileId": "4_z481c37de2e1ab3bf5e150710_f1060bc60382f42a2_d20160111_m063042_c001_v0001012_t0008", + "fileName": "test8.txt" +} http://git-wip-us.apache.org/repos/asf/libcloud/blob/88c22780/libcloud/test/storage/fixtures/backblaze_b2/b2_get_upload_url.json ---------------------------------------------------------------------- diff --git a/libcloud/test/storage/fixtures/backblaze_b2/b2_get_upload_url.json b/libcloud/test/storage/fixtures/backblaze_b2/b2_get_upload_url.json new file mode 100644 index 0000000..2ee28de --- /dev/null +++ b/libcloud/test/storage/fixtures/backblaze_b2/b2_get_upload_url.json @@ -0,0 +1,5 @@ +{ + "authorizationToken": "nope", + "bucketId": "481c37de2e1ab3bf5e150710", + "uploadUrl": "https://podxxx.backblaze.com/b2api/v1/b2_upload_file/abcd/defg" +} http://git-wip-us.apache.org/repos/asf/libcloud/blob/88c22780/libcloud/test/storage/fixtures/backblaze_b2/b2_list_buckets.json ---------------------------------------------------------------------- diff --git a/libcloud/test/storage/fixtures/backblaze_b2/b2_list_buckets.json b/libcloud/test/storage/fixtures/backblaze_b2/b2_list_buckets.json new file mode 100644 index 0000000..5ccefac --- /dev/null +++ b/libcloud/test/storage/fixtures/backblaze_b2/b2_list_buckets.json @@ -0,0 +1,22 @@ +{ + "buckets": [ + { + "accountId": "8c7eea3fe570", + "bucketId": "481c37de2e1ab3bf5e150710", + "bucketName": "test00001", + "bucketType": "allPrivate" + }, + { + "accountId": "8c7eea3fe570", + "bucketId": "886c573e2e1ab3bf5e150710", + "bucketName": "test00002", + "bucketType": "allPublic" + }, + { + "accountId": "8c7eea3fe570", + "bucketId": "c85c97ee2eeab3bf5e150710", + "bucketName": "test00003", + "bucketType": "allPrivate" + } + ] +} http://git-wip-us.apache.org/repos/asf/libcloud/blob/88c22780/libcloud/test/storage/fixtures/backblaze_b2/b2_list_file_names.json ---------------------------------------------------------------------- diff --git a/libcloud/test/storage/fixtures/backblaze_b2/b2_list_file_names.json b/libcloud/test/storage/fixtures/backblaze_b2/b2_list_file_names.json new file mode 100644 index 0000000..a8e3264 --- /dev/null +++ b/libcloud/test/storage/fixtures/backblaze_b2/b2_list_file_names.json @@ -0,0 +1,34 @@ + +{ + "files": [ + { + "action": "upload", + "fileId": "abcd", + "fileName": "2.txt", + "size": 2, + "uploadTimestamp": 1450545966000 + }, + { + "action": "upload", + "fileId": "4_z481c37de2e1ab3bf5e150710_f108012807a71d788_d20160110_m155541_c001_v0001001_t0016", + "fileName": "test5.txt", + "size": 24, + "uploadTimestamp": 1452441341000 + }, + { + "action": "upload", + "fileId": "4_z481c37de2e1ab3bf5e150710_f1071961a62d3426d_d20160110_m155708_c001_v0001013_t0042", + "fileName": "test6.txt", + "size": 24, + "uploadTimestamp": 1452441428000 + }, + { + "action": "upload", + "fileId": "4_z481c37de2e1ab3bf5e150710_f1060bc60382f42a2_d20160111_m063042_c001_v0001012_t0008", + "fileName": "test8.txt", + "size": 24, + "uploadTimestamp": 1452493842000 + } + ], + "nextFileName": null +} http://git-wip-us.apache.org/repos/asf/libcloud/blob/88c22780/libcloud/test/storage/fixtures/backblaze_b2/b2_upload_file.json ---------------------------------------------------------------------- diff --git a/libcloud/test/storage/fixtures/backblaze_b2/b2_upload_file.json b/libcloud/test/storage/fixtures/backblaze_b2/b2_upload_file.json new file mode 100644 index 0000000..2f3cdb0 --- /dev/null +++ b/libcloud/test/storage/fixtures/backblaze_b2/b2_upload_file.json @@ -0,0 +1,10 @@ +{ + "accountId": "8c7eea3fe570", + "bucketId": "481c37de2e1ab3bf5e150710", + "contentLength": 24, + "contentSha1": "23d23d43d4ecad793c049c81c4bc436ba1e8531e", + "contentType": "text/plain", + "fileId": "abcde", + "fileInfo": {}, + "fileName": "test0007.txt" +} http://git-wip-us.apache.org/repos/asf/libcloud/blob/88c22780/libcloud/test/storage/test_backblaze_b2.py ---------------------------------------------------------------------- diff --git a/libcloud/test/storage/test_backblaze_b2.py b/libcloud/test/storage/test_backblaze_b2.py new file mode 100644 index 0000000..6ce01e8 --- /dev/null +++ b/libcloud/test/storage/test_backblaze_b2.py @@ -0,0 +1,172 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import sys +import tempfile + +import mock + +from libcloud.storage.drivers.backblaze_b2 import BackblazeB2StorageDriver +from libcloud.utils.py3 import httplib +from libcloud.test import unittest +from libcloud.test import StorageMockHttp +from libcloud.test import MockRawResponse +from libcloud.test import MockHttpTestCase +from libcloud.test.file_fixtures import StorageFileFixtures + + +class MockAuthConn(mock.Mock): + account_id = 'abcdefgh' + + +class BackblazeB2StorageDriverTestCase(unittest.TestCase): + driver_klass = BackblazeB2StorageDriver + driver_args = ('a', 'b') + + def setUp(self): + self.driver_klass.connectionCls.authCls = MockAuthConn() + self.driver_klass.connectionCls.conn_classes = ( + None, BackblazeB2MockHttp) + self.driver_klass.connectionCls.rawResponseCls = \ + BackblazeB2MockRawResponse + BackblazeB2MockHttp.type = None + BackblazeB2MockRawResponse.type = None + self.driver = self.driver_klass(*self.driver_args) + + def test_list_containers(self): + containers = self.driver.list_containers() + self.assertEqual(len(containers), 3) + self.assertEqual(containers[0].name, 'test00001') + self.assertEqual(containers[0].extra['id'], '481c37de2e1ab3bf5e150710') + self.assertEqual(containers[0].extra['bucketType'], 'allPrivate') + + def test_list_container_objects(self): + container = self.driver.list_containers()[0] + objects = self.driver.list_container_objects(container=container) + self.assertEqual(len(objects), 4) + self.assertEqual(objects[0].name, '2.txt') + self.assertEqual(objects[0].size, 2) + self.assertEqual(objects[0].extra['fileId'], 'abcd') + self.assertEqual(objects[0].extra['uploadTimestamp'], 1450545966000) + + def test_create_container(self): + container = self.driver.create_container(container_name='test0005') + self.assertEqual(container.name, 'test0005') + self.assertEqual(container.extra['id'], '681c87aebeaa530f5e250710') + self.assertEqual(container.extra['bucketType'], 'allPrivate') + + def test_delete_container(self): + container = self.driver.list_containers()[0] + result = self.driver.delete_container(container=container) + self.assertTrue(result) + + def test_download_object(self): + container = self.driver.list_containers()[0] + obj = self.driver.list_container_objects(container=container)[0] + _, destination_path = tempfile.mkstemp() + result = self.driver.download_object(obj=obj, destination_path=destination_path, + overwrite_existing=True) + self.assertTrue(result) + + def test_download_object_as_stream(self): + container = self.driver.list_containers()[0] + obj = self.driver.list_container_objects(container=container)[0] + result = self.driver.download_object_as_stream(obj=obj) + result = ''.join(list(result)) + self.assertEqual(result, 'ab') + + def test_upload_object(self): + file_path = os.path.abspath(__file__) + container = self.driver.list_containers()[0] + obj = self.driver.upload_object(file_path=file_path, container=container, + object_name='test0007.txt') + self.assertEqual(obj.name, 'test0007.txt') + self.assertEqual(obj.size, 24) + self.assertEqual(obj.extra['fileId'], 'abcde') + + def test_delete_object(self): + container = self.driver.list_containers()[0] + obj = self.driver.list_container_objects(container=container)[0] + result = self.driver.delete_object(obj=obj) + self.assertTrue(result) + + +class BackblazeB2MockHttp(StorageMockHttp, MockHttpTestCase): + fixtures = StorageFileFixtures('backblaze_b2') + + def _b2api_v1_b2_list_buckets(self, method, url, body, headers): + if method == 'GET': + body = self.fixtures.load('b2_list_buckets.json') + else: + raise AssertionError('Unsupported method') + return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + + def _b2api_v1_b2_list_file_names(self, method, url, body, headers): + if method == 'GET': + body = self.fixtures.load('b2_list_file_names.json') + else: + raise AssertionError('Unsupported method') + return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + + def _b2api_v1_b2_create_bucket(self, method, url, body, headers): + if method == 'POST': + body = self.fixtures.load('b2_create_bucket.json') + else: + raise AssertionError('Unsupported method') + return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + + def _b2api_v1_b2_delete_bucket(self, method, url, body, headers): + if method == 'POST': + body = self.fixtures.load('b2_delete_bucket.json') + else: + raise AssertionError('Unsupported method') + return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + + def _b2api_v1_b2_delete_file_version(self, method, url, body, headers): + if method == 'POST': + body = self.fixtures.load('b2_delete_file_version.json') + else: + raise AssertionError('Unsupported method') + return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + + def _b2api_v1_b2_get_upload_url(self, method, url, body, headers): + # test_upload_object + if method == 'GET': + body = self.fixtures.load('b2_get_upload_url.json') + else: + raise AssertionError('Unsupported method') + return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + + def _b2api_v1_b2_upload_file_abcd_defg(self, method, url, body, headers): + # test_upload_object + if method == 'POST': + body = self.fixtures.load('b2_upload_file.json') + else: + raise AssertionError('Unsupported method') + return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + + +class BackblazeB2MockRawResponse(MockRawResponse): + def _file_test00001_2_txt(self, method, url, body, headers): + # test_download_object + if method == 'GET': + body = 'ab' + else: + raise AssertionError('Unsupported method') + return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + +if __name__ == '__main__': + sys.exit(unittest.main())
