Example #1
0
    def test_get_specific_fields_using_get_http_params(self):
        c = libcdmi.open(self._endpoint, credentials=self._credentials)
        self.addToCleanup(self.cleanup_object, '/testcontainer/')
        self.addToCleanup(self.cleanup_object, '/testcontainer/testobject')
        c.create_container('/testcontainer/')
        object_headers = self._make_headers({'Accept': libcdmi.common.CDMI_OBJECT,
                                             'Content-Type': 'application/octet-stream'})

        with open(self._filename, 'rb') as input_file:
            content = input_file.read()

        response = requests.put(self._endpoint + '/testcontainer/testobject',
                                content,
                                auth=self._credentials,
                                headers=object_headers)

        self.assertEqual(200, response.status_code, response.text)

        response = requests.get(self._endpoint + ('/testcontainer/testobject?'
                                                  'objectID=true&parentURI=true&value=1&value=6'),
                                auth=self._credentials,
                                headers=object_headers)

        self.assertEqual(200, response.status_code, response.text)
        self.assertTrue(len(response.text) > 0, 'Response was empty!')
        data = response.json()
        self.assertEqual(3, len(data), 'There were <> 3 keys in the response data: %s' % response.json())
        self.assertTrue('value' in data.keys(), 'value is not in data (%s)!' % data)
        self.assertTrue('objectID' in data.keys(), 'objectID is not in data (%s)!' % data)
        self.assertTrue('parentURI' in data.keys(), 'parentURI is not in data (%s)!' % data)
        self.assertEqual(content[1:6], base64.b64decode(data['value']))
        self.assertEqual('EgAAYXM=', data['value'], 'Value in response did not match the requested range')
Example #2
0
    def test_container_and_blob_create_and_delete_mock_requests(self):
        c = libcdmi.open(self._endpoint)

        requests_put = self.mockUp(libcdmi.connection.requests, 'put')

        c.create_container('/container/')

        expected_metadata = ('{"metadata": {}}')

        requests_put.assert_called_once_with(self._endpoint + '/container/',
                                             expected_metadata,
                                             headers={'Content-Type': 'application/cdmi-container',
                                                      'Accept': 'application/cdmi-container',
                                                      'X-CDMI-Specification-Version': '1.0.2'},
                                             auth=None)

        expected_metadata = ('{"mimetype": "text/plain", '
                             '"valuetransferencoding": "utf-8", '
                             '"value": "", "metadata": {}}')

        c.create_object('/container/blob', self._filename)
        requests_put.assert_called_with(
            self._endpoint + '/container/blob',
            expected_metadata,
            headers={'Content-Type': 'application/cdmi-object',
                     'Accept': 'application/cdmi-object',
                     'X-CDMI-Specification-Version': '1.0.2'}, auth=None)

        requests_delete = self.mockUp(libcdmi.connection.requests, 'delete')
        c.delete('/container/blob')
        requests_delete.assert_called_once_with(self._endpoint + '/container/blob', auth=None)
Example #3
0
 def test_get_object_mock_requests(self):
     c = libcdmi.open(self._endpoint)
     requests_get = self.mockUp(libcdmi.connection.requests, 'get')
     self.mockUp(libcdmi.connection.requests, 'put')
     c.create_object('/container/blob', self._filename)
     c.get('/container/blob')
     requests_get.assert_called_once_with(self._endpoint + '/container/blob',
                                          headers={'Accept': 'application/cdmi-object',
                                                   'X-CDMI-Specification-Version': '1.0.2'},
                                          auth=None)
Example #4
0
 def test_get_container_mock_requests(self):
     c = libcdmi.open(self._endpoint)
     requests_get = self.mockUp(libcdmi.connection.requests, 'get')
     self.mockUp(libcdmi.connection.requests, 'put')
     c.create_container('/container/')
     c.get('/container/', accept=libcdmi.common.CDMI_CONTAINER)
     requests_get.assert_called_once_with(self._endpoint + '/container/',
                                          headers={'Accept': 'application/cdmi-container',
                                                   'X-CDMI-Specification-Version': '1.0.2'},
                                          auth=None)
Example #5
0
    def test_cdmi_objectid(self):
        c = libcdmi.open(self._endpoint, credentials=self._credentials)
        self.addToCleanup(self.cleanup_object, '/testcontainer/')
        self.addToCleanup(self.cleanup_object, '/testcontainer/testobject')
        c.create_container('/testcontainer/')

        data = {'metadata': {},
                'mimetype': 'text/plain'}

        object_headers = self._make_headers({'Accept': libcdmi.common.CDMI_OBJECT,
                                             'Content-Type': libcdmi.common.CDMI_OBJECT})

        with open(self._filename, 'rb') as input_file:
            try:
                content = input_file.read()
                unicode(content, 'utf-8')
                data['valuetransferencoding'] = 'utf-8'
            except UnicodeDecodeError:
                input_file.seek(0)
                content = base64.b64encode(input_file.read())
                data['valuetransferencoding'] = 'base64'

        data.update({'value': content})

        response = requests.put(self._endpoint + '/testcontainer/testobject',
                                json.dumps(data),
                                auth=self._credentials,
                                headers=object_headers)

        self.assertEqual(200, response.status_code, response.text)
        self.assertEqual(response.headers['X-CDMI-Specification-Version'], '1.0.1')
        self.assertEqual(response.headers['Content-Type'], 'application/cdmi-object')

        result_data = response.json()
        self.assertEqual('application/cdmi-object', result_data['objectType'])
        self.assertEqual('testobject', result_data['objectName'])

        self.assertTrue(len(response.text) > 0, 'Response was empty!')
        data = response.json()

        self.assertTrue('objectID' in data.keys(), 'objectID is not in data (%s)!' % data)

        response = requests.get(self._endpoint + ('/cdmi_objectid/%s/' % data['objectID']),
                                auth=self._credentials,
                                headers=object_headers)

        self.assertEqual(200, response.status_code, response.text)
        self.assertTrue(len(response.text) > 0, 'Response was empty!')
        data = response.json()
        self.assertEqual(9, len(data), 'There were <> 9 keys in the response data: %s' % response.json())
        self.assertTrue('value' in data.keys(), 'value is not in data (%s)!' % data)
        self.assertTrue('objectID' in data.keys(), 'objectID is not in data (%s)!' % data)
        self.assertTrue('parentURI' in data.keys(), 'parentURI is not in data (%s)!' % data)
        self.assertEqual(base64.b64decode(content), base64.b64decode(data['value']), content)
Example #6
0
    def test_swift_create_object(self):
        c = libcdmi.open(self._endpoint, credentials=self._credentials)
        self.addToCleanup(self.cleanup_object, "/swift/")
        self.addToCleanup(self.cleanup_object, "/swift/testobject")
        c.create_container(
            "/swift", metadata={"stoxy_backend": "swift", "stoxy_backend_base_protocol": self._swift_endpoint}
        )

        data = {
            "metadata": {"event name": "SNIA SDC 2013", "event location": "Santa Clara, CA"},
            "mimetype": "text/plain",
        }

        object_headers = self._make_headers(
            {
                "Accept": libcdmi.common.CDMI_OBJECT,
                "Content-Type": libcdmi.common.CDMI_OBJECT,
                "X-Auth-Token": self._swift_token,
            }
        )

        with open(self._filename, "rb") as input_file:
            try:
                content = input_file.read()
                unicode(content, "utf-8")
                data["valuetransferencoding"] = "utf-8"
            except UnicodeDecodeError:
                input_file.seek(0)
                content = base64.b64encode(input_file.read())
                data["valuetransferencoding"] = "base64"

        data.update({"value": content})

        response = requests.put(
            self._endpoint + "/swift/testobject", json.dumps(data), auth=self._credentials, headers=object_headers
        )

        self.assertEqual(200, response.status_code, response.text)
        self.assertEqual(response.headers["X-CDMI-Specification-Version"], "1.0.1")
        self.assertEqual(response.headers["Content-Type"], "application/cdmi-object")

        result_data = response.json()
        self.assertEqual("application/cdmi-object", result_data["objectType"])
        self.assertEqual("testobject", result_data["objectName"])

        time.sleep(3)  # saving is async, wait a bit
        response = requests.get(self._endpoint + "/swift/testobject", auth=self._credentials, headers=object_headers)

        result_data = response.json()
        self.assertTrue("value" in result_data, "No value field in response: %s" % result_data.keys())
        self.assertTrue(len(result_data["value"]) > 0, "Result value length is zero!")
        self.assertEqual(base64.b64decode(content), base64.b64decode(result_data["value"]))
        self.assertEqual(content, result_data["value"])
Example #7
0
    def test_update_container(self):
        c = libcdmi.open(self._endpoint, credentials=self._credentials)

        self.addToCleanup(self.cleanup_object, '/testcontainer')
        container_1 = c.create_container('/testcontainer')
        container_2 = c.update_container('/testcontainer', metadata={'owner': 'nobody'})
        container_get = c.get('/testcontainer/', accept=libcdmi.common.CDMI_CONTAINER)

        self.assertEqual(dict, type(container_2))
        self.assertEqual(dict, type(container_1))
        self.assertEqual(dict, type(container_get))

        for key, value in container_2.iteritems():
            self.assertEqual(value, container_get.get(key, NotThere),
                             'Expected: %s, but received: %s in %s' %
                             (value, container_get.get(key, NotThere), key))
Example #8
0
def run(args, print_=True):
    credentials = tuple(args.auth.split(":")) if args.auth else None
    token = None
    if args.tokenfile:
        if not os.path.exists(args.tokenfile):
            return {"_error": "Specified token filename does not exist: %s" % args.tokenfile}
        else:
            with open(args.tokenfile) as json_token:
                import json

                data = json.load(json_token)
                token = data["access"]["token"]["id"]
    c = libcdmi.open(args.url, credentials=credentials, keystone_token=token)

    # process some commands in a more specific way
    if args.action == "create_object":
        if not args.filename:
            return {"_error": "Filename is mandatory with create_object"}
        if not os.path.exists(args.filename):
            return {"_error": 'File does not exist: "%s"' % args.filename}
        response = getattr(c, args.action)("", args.filename, mimetype=args.mimetype)
    elif args.action in ["create_container", "update_container"]:
        response = c.create_container(
            "",  # url is defining the full path
            metadata={"stoxy_backend": args.container_backend, "stoxy_backend_base_protocol": args.container_base},
        )
    else:
        response = getattr(c, args.action)("")

    if not response:
        return

    if not print_:
        return response

    if args.output == "yaml":
        import yaml

        print yaml.dump(response)
    elif args.output == "json":
        import json

        print json.dumps(response, indent=4, separators=(", ", ": "), sort_keys=True)
    else:
        from pprint import pprint

        pprint(response)
Example #9
0
    def test_create_and_get_container(self):
        c = libcdmi.open(self._endpoint, credentials=self._credentials)

        self.addToCleanup(self.cleanup_object, "/swift")

        self.cleanup_object("/swift")
        container_create = c.create_container(
            "/swift", metadata={"stoxy_backend": "swift", "stoxy_backend_base_protocol": self._swift_endpoint}
        )
        container_get = c.get("/swift/", accept=libcdmi.common.CDMI_CONTAINER)

        self.assertEqual(dict, type(container_create))
        self.assertEqual(dict, type(container_get))

        for key, value in container_create.iteritems():
            self.assertEqual(
                value,
                container_get.get(key, NotThere),
                "Expected: %s, but received: %s in %s" % (value, container_get.get(key, NotThere), key),
            )
Example #10
0
    def test_get_object_non_cdmi_detailed(self):
        c = libcdmi.open(self._endpoint, credentials=self._credentials)
        self.addToCleanup(self.cleanup_object, '/testcontainer/')
        self.addToCleanup(self.cleanup_object, '/testcontainer/testobject')
        c.create_container('/testcontainer/')

        object_headers = self._make_headers({'Accept': libcdmi.common.CDMI_OBJECT,
                                             'Content-Type': 'application/octet-stream'},
                                            cdmi=False)

        with open(self._filename, 'rb') as input_file:
            content = input_file.read()

        response = requests.put(self._endpoint + '/testcontainer/testobject',
                                content,
                                auth=self._credentials,
                                headers=object_headers)

        self.assertEqual(200, response.status_code, response.text)

        # TODO: test non-CDMI request optional Range header functionality
        noncdmi_headers = {'Range': '10-15'}
        response = requests.get(self._endpoint + '/testcontainer/testobject',
                                auth=self._credentials,
                                headers=noncdmi_headers)

        self.assertEqual(response.status_code, 200, response.text)
        self.assertEqual(len(response.text), 5, response.text)
        self.assertEqual(response.text, content[10:15])
        self.assertEqual(response.headers['content-type'], object_headers['Content-Type'])

        # TODO: test non-CDMI request optional Range header functionality
        noncdmi_headers = {'Range': '1-6'}
        response = requests.get(self._endpoint + '/testcontainer/testobject',
                                auth=self._credentials,
                                headers=noncdmi_headers)

        self.assertEqual(5, len(response.text))
        self.assertEqual(response.text, content[1:6])
        self.assertEqual(response.headers['content-type'], object_headers['Content-Type'])
Example #11
0
    def test_create_object_detailed(self):
        c = libcdmi.open(self._endpoint, credentials=self._credentials)
        self.addToCleanup(self.cleanup_object, '/testcontainer/')
        self.addToCleanup(self.cleanup_object, '/testcontainer/testobject')
        c.create_container('/testcontainer/')

        data = {'metadata': {},
                'mimetype': 'text/plain'}

        object_headers = self._make_headers({'Accept': libcdmi.common.CDMI_OBJECT,
                                             'Content-Type': libcdmi.common.CDMI_OBJECT})

        with open(self._filename, 'rb') as input_file:
            try:
                content = input_file.read()
                unicode(content, 'utf-8')
                data['valuetransferencoding'] = 'utf-8'
            except UnicodeDecodeError:
                input_file.seek(0)
                content = base64.b64encode(input_file.read())
                data['valuetransferencoding'] = 'base64'

        data.update({'value': content})

        response = requests.put(self._endpoint + '/testcontainer/testobject',
                                json.dumps(data),
                                auth=self._credentials,
                                headers=object_headers)

        self.assertEqual(200, response.status_code, response.text)
        self.assertEqual(response.headers['X-CDMI-Specification-Version'], '1.0.2')
        self.assertEqual(response.headers['Content-Type'], 'application/cdmi-object')

        result_data = response.json()
        self.assertEqual('application/cdmi-container', result_data['objectType'])
        self.assertEqual('testcontainer', result_data['objectName'])
        self.assertEqual(content, result_data['value'])
Example #12
0
    def test_delete_object(self):
        c = libcdmi.open(self._endpoint, credentials=self._credentials)
        self.addToCleanup(self.cleanup_object, '/testcontainer/')
        self.addToCleanup(self.cleanup_object, '/testcontainer/testobject')
        c.create_container('/testcontainer/')
        object_headers = self._make_headers({'Accept': libcdmi.common.CDMI_OBJECT,
                                             'Content-Type': 'application/octet-stream'})

        with open(self._filename, 'rb') as input_file:
            content = input_file.read()

        response = requests.put(self._endpoint + '/testcontainer/testobject',
                                content,
                                auth=self._credentials,
                                headers=object_headers)

        self.assertEqual(200, response.status_code, response.text)

        # XXX: when used from the unit test, get_config() gives only the OMS config, so need to specify
        # the base path in OMS config too
        stoxy_filepath = '%s/%s' % (get_config().getstring('store', 'file_base_path', '/tmp'),
                                    'testobject')

        self.assertTrue(os.path.exists(stoxy_filepath),
                        'File "%s" does not exist after creation of model object!' % stoxy_filepath)

        delete_response = requests.delete(self._endpoint + '/testcontainer/testobject',
                                          auth=self._credentials,)
        self.assertEqual(200, delete_response.status_code, delete_response.text)

        response = requests.get(self._endpoint + '/testcontainer/testobject',
                                auth=self._credentials,)
        self.assertEqual(404, response.status_code, 'Object was not deleted!')

        self.assertTrue(not os.path.exists(stoxy_filepath),
                        'File "%s" exists after deletion of model object!' % stoxy_filepath)
Example #13
0
    def test_create_object_non_cdmi_detailed(self):
        c = libcdmi.open(self._endpoint, credentials=self._credentials)
        self.addToCleanup(self.cleanup_object, '/testcontainer/')
        self.addToCleanup(self.cleanup_object, '/testcontainer/testobject')
        c.create_container('/testcontainer/')

        object_headers = self._make_headers({'Accept': libcdmi.common.CDMI_OBJECT,
                                             'Content-Type': 'application/binary'},
                                            cdmi=False)

        with open(self._filename, 'rb') as input_file:
            content = input_file.read()

        response = requests.put(self._endpoint + '/testcontainer/testobject',
                                content,
                                auth=self._credentials,
                                headers=object_headers)

        self.assertEqual(200, response.status_code, response.text)
        self.assertEqual(response.headers['X-CDMI-Specification-Version'], '1.0.1')
        self.assertEqual(response.headers['Content-Type'], 'application/cdmi-object')

        result_data = response.text  # Response body is not required
        self.assertTrue(len(result_data) == 0)
Example #14
0
#!/usr/bin/env python
import sys
import httplib
import json

sys.path.append("/Users/ilja/workspace/libcdmi-python/")

import libcdmi

httplib.HTTPConnection.debuglevel = 1

server = "http://localhost:8080"

c = libcdmi.open(server, credentials=("john", "john"))

response = c.create_container(
    "/storage/swift",
    metadata={"stoxy_backend": "swift", "stoxy_backend_base": "swift.example.com/v1/account/container/"},
)

response = c.create_object(
    "/storage/swift/test-cdmi-file",
    "create-object.py",
    metadata={"event name": "SNIA SDC 2013", "event location": "Santa Clara, CA"},
)

print json.dumps(response, sort_keys=True, indent=4, separators=(",", ": "))
Example #15
0
 def test_raises_error_on_bad_status_code(self):
     c = libcdmi.open(self._endpoint)
     requests_put = self.mockUp(libcdmi.connection.requests, 'put')
     response = requests_put.return_value = MagicMock()
     c.create_container('/container/')
     response.raise_for_status.assert_called_once_with()
Example #16
0
 def test_open_connection(self):
     c = libcdmi.open(self._endpoint)
     self.assertTrue(c, 'Could not create a connection with %s!' % self._endpoint)
     self.assertEqual(type(c), libcdmi.connection.Connection)
     self.assertEqual(c.endpoint, self._endpoint)