Exemple #1
0
    def test_create_metadata(self):
        api = self.api
        resp = FakeApiResponse()
        resp.status = 201
        api._direct_request = Mock(return_value=(resp, None))

        metadata = {}
        k1 = random_str(32)
        v1 = random_str(32)

        k2 = random_str(32)
        v2 = random_str(32)

        metadata[k1] = v1
        metadata[k2] = v2

        api.create(self.account, self.name, properties=metadata)
        uri = "%s/reference/create" % self.uri_base
        params = {'acct': self.account, 'ref': self.name}

        data = json.dumps({'properties': metadata})
        api._direct_request.assert_called_once_with('POST',
                                                    uri,
                                                    params=params,
                                                    data=data)
    def test_object_show(self):
        api = self.api
        name = random_str(32)
        size = random.randint(1, 1000)
        content_hash = random_str(32)
        content_type = random_str(32)
        resp = FakeApiResponse()
        resp.headers = {
            object_headers["name"]: name,
            object_headers["size"]: str(size),
            object_headers["hash"]: content_hash,
            object_headers["mime_type"]: content_type
        }
        api.container._direct_request = Mock(return_value=(resp, {
            'properties': {}
        }))
        obj = api.object_show(self.account,
                              self.container,
                              name,
                              headers=self.headers)

        uri = "%s/content/get_properties" % self.uri_base
        params = {'acct': self.account, 'ref': self.container, 'path': name}
        api.container._direct_request.assert_called_once_with(
            'POST', uri, params=params, data=None, headers=self.headers)
        self.assertIsNotNone(obj)
 def test_object_list(self):
     api = self.api
     marker = random_str(32)
     delimiter = random_str(32)
     end_marker = random_str(32)
     prefix = random_str(32)
     limit = random.randint(1, 1000)
     name0 = random_str(32)
     name1 = random_str(32)
     resp_body = {"objects": [{"name": name0}, {"name": name1}]}
     resp = FakeApiResponse()
     resp.headers = {}
     api.container._direct_request = Mock(return_value=(resp, resp_body))
     listing = api.object_list(self.account,
                               self.container,
                               limit=limit,
                               marker=marker,
                               prefix=prefix,
                               delimiter=delimiter,
                               end_marker=end_marker,
                               headers=self.headers)
     uri = "%s/container/list" % self.uri_base
     params = {
         'acct': self.account,
         'ref': self.container,
         'marker': marker,
         'max': limit,
         'delimiter': delimiter,
         'prefix': prefix,
         'end_marker': end_marker,
         'properties': False
     }
     api.container._direct_request.assert_called_once_with(
         'GET', uri, params=params, headers=self.headers)
     self.assertEqual(len(listing['objects']), 2)
Exemple #4
0
 def test_from_response_http_status(self):
     fake_resp = FakeApiResponse()
     fake_resp.status = 404
     exc = exceptions.from_response(fake_resp, None)
     self.assertIsInstance(exc, exceptions.NotFound)
     fake_resp.status = 409
     exc = exceptions.from_response(fake_resp, None)
     self.assertIsInstance(exc, exceptions.Conflict)
Exemple #5
0
 def test_from_response(self):
     fake_resp = FakeApiResponse()
     fake_resp.status = 500
     exc = exceptions.from_response(fake_resp, None)
     self.assertIsInstance(exc, exceptions.ClientException)
     self.assertEqual(exc.http_status, fake_resp.status)
     self.assertEqual(exc.message, "n/a")
     self.assertIn("HTTP 500", str(exc))
Exemple #6
0
    def test_create_error(self):
        api = self.api
        resp = FakeApiResponse()
        resp.status_code = 300
        api._direct_request = Mock(return_value=(resp, None))

        self.assertRaises(exceptions.ClientException, api.create, self.account,
                          self.name)
    def test_container_create_exist(self):
        api = self.api
        resp = FakeApiResponse()
        resp.status = 204
        api.container._direct_request = Mock(return_value=(resp, None))

        name = random_str(32)
        result = api.container_create(self.account, name)
        self.assertEqual(result, False)
Exemple #8
0
 def test_from_response_with_body(self):
     fake_resp = FakeApiResponse()
     fake_resp.status = 500
     body = {"status": 300, "message": "Fake error"}
     exc = exceptions.from_response(fake_resp, body)
     self.assertIsInstance(exc, exceptions.ClientException)
     self.assertEqual(exc.http_status, fake_resp.status)
     self.assertEqual(exc.status, 300)
     self.assertEqual(exc.message, "Fake error")
     self.assertIn("HTTP 500", str(exc))
     self.assertIn("STATUS 300", str(exc))
Exemple #9
0
    def test_create_already_exists(self):
        api = self.api
        resp = FakeApiResponse()
        resp.status = 202
        api._direct_request = Mock(return_value=(resp, None))
        api.create(self.account, self.name)
        uri = "%s/reference/create" % self.uri_base
        params = {'acct': self.account, 'ref': self.name}

        data = json.dumps({'properties': {}})
        api._direct_request.assert_called_once_with(
            'POST', uri, params=params, data=data)
Exemple #10
0
 def test_container_show(self):
     api = self.api
     resp = FakeApiResponse()
     name = random_str(32)
     cont_size = random.randint(1, 1000)
     resp.headers = {container_headers["size"]: cont_size}
     api.container._direct_request = Mock(return_value=(resp, {}))
     info = api.container_show(self.account, name, headers=self.headers)
     uri = "%s/container/show" % self.uri_base
     params = {'acct': self.account, 'ref': name}
     api.container._direct_request.assert_called_once_with(
         'GET', uri, params=params, headers=self.headers)
     self.assertEqual(info, {})
Exemple #11
0
    def test_container_delete(self):
        api = self.api

        resp = FakeApiResponse()
        resp.status_code = 204
        api.container._direct_request = Mock(return_value=(resp, None))
        api.directory.unlink = Mock(return_value=None)
        name = random_str(32)
        api.container_delete(self.account, name, headers=self.headers)

        uri = "%s/container/destroy" % self.uri_base
        params = {'acct': self.account, 'ref': name}
        api.container._direct_request.assert_called_once_with(
            'POST', uri, params=params, headers=self.headers)
Exemple #12
0
 def test_container_list(self):
     resp = FakeApiResponse()
     name = random_str(32)
     marker = random_str(32)
     delimiter = random_str(32)
     end_marker = random_str(32)
     prefix = random_str(32)
     limit = random.randint(1, 1000)
     body = {"listing": [[name, 0, 0, 0]]}
     fake_endpoint = 'fake_endpoint'
     self.api.account._direct_request = Mock(return_value=(resp, body))
     self.api.account._get_account_addr = Mock(return_value=fake_endpoint)
     containers = self.api.container_list(self.account,
                                          limit=limit,
                                          marker=marker,
                                          prefix=prefix,
                                          delimiter=delimiter,
                                          end_marker=end_marker,
                                          headers=self.headers)
     params = {
         "id": self.account,
         "prefix": prefix,
         "delimiter": delimiter,
         "marker": marker,
         "end_marker": end_marker,
         "limit": limit
     }
     uri = "http://%s/v1.0/account/containers" % fake_endpoint
     self.api.account._direct_request.assert_called_once_with(
         'GET', uri, params=params, headers=self.headers)
     self.assertEqual(len(containers), 1)
    def test_container_create(self):
        api = self.api
        resp = FakeApiResponse()
        resp.status = 201
        api.container._direct_request = Mock(return_value=(resp, None))

        name = random_str(32)
        result = api.container_create(self.account, name, headers=self.headers)
        self.assertEqual(result, True)

        uri = "%s/container/create" % self.uri_base
        params = {'acct': self.account, 'ref': name}
        self.headers['x-oio-action-mode'] = 'autocreate'
        data = json.dumps({'properties': {}, 'system': {}})
        api.container._direct_request.assert_called_once_with(
            'POST', uri, params=params, data=data,
            headers=self.headers)
Exemple #14
0
 def test_list(self):
     api = self.api
     resp = FakeApiResponse()
     api._direct_request = Mock(return_value=(resp, None))
     uri = "%s/reference/show" % self.uri_base
     params = {'acct': self.account, 'ref': self.name}
     api.list(self.account, self.name)
     api._direct_request.assert_called_once_with('GET', uri, params=params)
Exemple #15
0
 def test_renew(self):
     api = self.api
     service_type = random_str(32)
     resp = FakeApiResponse()
     api._direct_request = Mock(return_value=(resp, None))
     api.renew(self.account, self.name, service_type)
     uri = "%s/reference/renew" % self.uri_base
     params = {'acct': self.account, 'ref': self.name, 'type': service_type}
     api._direct_request.assert_called_once_with('POST', uri, params=params)
Exemple #16
0
 def _build_account_client(self, **kwargs):
     endpoint = "http://1.2.3.4:8000"
     resp = FakeApiResponse()
     body = {"listing": [['ct', 0, 0, 0]]}
     client = AccountClient({'namespace': 'fake'},
                            endpoint=endpoint,
                            proxy_endpoint=endpoint,
                            **kwargs)
     client._direct_request = Mock(return_value=(resp, body))
     client._get_account_addr = Mock(return_value=endpoint)
     return client
Exemple #17
0
 def test_delete_properties(self):
     api = self.api
     properties = [random_str(64)]
     resp = FakeApiResponse()
     api._direct_request = Mock(return_value=(resp, None))
     api.del_properties(self.account, self.name, properties)
     uri = "%s/reference/del_properties" % self.uri_base
     params = {'acct': self.account, 'ref': self.name}
     data = json.dumps(properties)
     api._direct_request.assert_called_once_with(
         'POST', uri, data=data, params=params)
 def test_object_del_properties(self):
     resp = FakeApiResponse()
     self.api.container._direct_request = Mock(return_value=(resp, None))
     self.api.object_del_properties(self.account, self.container, 'a',
                                    ['a'], version='17',
                                    headers=self.headers)
     uri = '%s/content/del_properties' % self.uri_base
     params = {'acct': self.account, 'ref': self.container,
               'path': 'a', 'version': '17'}
     self.api.container._direct_request.assert_called_once_with(
         'POST', uri, data=json.dumps(['a']), params=params,
         headers=self.headers)
Exemple #19
0
 def test_force(self):
     api = self.api
     service_type = random_str(32)
     services = {'seq': 1, 'type': service_type, 'host': '127.0.0.1:8000'}
     resp = FakeApiResponse()
     api._direct_request = Mock(return_value=(resp, None))
     api.force(self.account, self.name, service_type, services)
     uri = "%s/reference/force" % self.uri_base
     params = {'acct': self.account, 'ref': self.name,
               'type': service_type}
     data = json.dumps(services)
     api._direct_request.assert_called_once_with(
         'POST', uri, data=data, params=params, autocreate=False)
Exemple #20
0
    def test_list_type(self):
        api = self.api
        service_type = random_str(32)
        resp = FakeApiResponse()
        resp_body = [{"seq": 1,
                      "type": service_type,
                      "host": "127.0.0.1:6000",
                      "args": ""}]

        api._direct_request = Mock(return_value=(resp, resp_body))
        srv = api.list(self.account, self.name, service_type=service_type)
        uri = "%s/reference/show" % self.uri_base
        params = {'acct': self.account, 'ref': self.name,
                  'type': service_type}
        api._direct_request.assert_called_once_with(
            'GET', uri, params=params)
        self.assertEqual(srv, resp_body)
    def test_container_update(self):
        api = self.api

        name = random_str(32)
        key = random_str(32)
        value = random_str(32)
        meta = {key: value}
        resp = FakeApiResponse()
        api.container._direct_request = Mock(return_value=(resp, None))
        api.container_set_properties(
            self.account, name, meta, headers=self.headers)

        data = json.dumps({'properties': meta, 'system': {}})
        uri = "%s/container/set_properties" % self.uri_base
        params = {'acct': self.account, 'ref': name}
        api.container._direct_request.assert_called_once_with(
            'POST', uri, data=data, params=params, headers=self.headers)
    def test_object_delete(self):
        api = self.api
        name = random_str(32)
        resp_body = [
            chunk("AAAA", "0"), chunk("BBBB", "1"), chunk("CCCC", "2")
        ]
        resp = FakeApiResponse()
        api.container._direct_request = Mock(return_value=(resp, resp_body))

        api.object_delete(
            self.account, self.container, name, headers=self.headers)

        uri = "%s/content/delete" % self.uri_base
        params = {'acct': self.account, 'ref': self.container,
                  'path': name}
        api.container._direct_request.assert_called_once_with(
            'POST', uri, params=params, headers=self.headers)
Exemple #23
0
    def test_object_set_properties(self):
        api = self.api

        name = random_str(32)
        key = random_str(32)
        value = random_str(32)
        meta = {key: value}
        resp = FakeApiResponse()
        api.container._direct_request = Mock(return_value=(resp, None))
        api.object_set_properties(self.account, self.container, name, meta,
                                  **self.common_kwargs)

        data = {'properties': meta}
        data = json.dumps(data)
        uri = "%s/content/set_properties" % self.uri_base
        params = {'acct': self.account, 'ref': self.container, 'path': name}
        api.container._direct_request.assert_called_once_with(
            'POST',
            uri,
            data=data,
            params=params,
            autocreate=True,
            **self.common_kwargs)