def setUpClass(cls):
        super(ObjectVersioningTest, cls).setUpClass()

        cls.container_name = CONTAINER_NAME
        cls.object_name = "{0}_{1}".format(
            Constants.VALID_OBJECT_NAME,
            randstring.get_random_string())
Exemplo n.º 2
0
    def create_archive(self, object_names, compression_type,
                       archive_name=BULK_ARCHIVE_NAME):
        """
        Bulk creates objects in the opencafe's temp directory specified in the
        engine config. Each object's data will be the md5sum of the object's
        name.

        @type  object_names: strings
        @param object_names: a list of object names

        @type  object_names: string
        @param object_names: file compression to apply to the archive

        @rtype:  string
        @return: Returns full path of the archive that was created in
        opencafe's temp directory specified in the engine config
        """
        supported = [None, "gz", "bz2"]
        if compression_type not in supported:
            raise NameError("supported compression: {0}".format(supported))

        ext = ''

        if not compression_type:
            ext = 'tar'
            compression_type = ''
        else:
            ext = 'tar.{0}'.format(compression_type)

        archive_name = '{0}_{1}.{2}'.format(
            archive_name,
            randstring.get_random_string(),
            ext)

        archive_filename = '{0}/{1}'.format(self.temp_dir, archive_name)
        archive = tarfile.open(
            archive_filename,
            'w:{0}'.format(compression_type))

        for object_name in object_names:
            object_data = get_md5_hash(object_name)
            object_size = len(object_data)
            object_time = int(mktime(datetime.now().timetuple()))

            object_buffer = StringIO(object_data)
            object_buffer.seek(0)

            object_info = tarfile.TarInfo(name=object_name)
            object_info.size = object_size
            object_info.mtime = object_time

            archive.addfile(tarinfo=object_info, fileobj=object_buffer)
        archive.close()

        archive_path = "{0}/{1}".format(
            self.temp_dir,
            archive_name)

        return archive_path
Exemplo n.º 3
0
    def ddtest_object_list_with_extended_accept_header(self,
                                                       descriptor=None,
                                                       hdrs=None):
        """
        test for openstack bug #1202453 fix
        and expansion of accept to take 'q' and
        arbitrary extensions
        """
        container_name = '{0}_{1}'.format(self.base_container_name,
                                          randstring.get_random_string())
        self.client.create_container(container_name)
        self.addCleanup(self.behaviors.force_delete_containers,
                        [container_name])

        object_data = 'Test file data'
        content_length = str(len(object_data))
        object_name = '{0}_{1}'.format('extended_accept_header',
                                       randstring.get_random_string())

        headers = {
            'Content-Length': content_length,
            'Content-Type': CONTENT_TYPE_TEXT
        }

        self.client.create_object(container_name,
                                  object_name,
                                  headers=headers,
                                  data=object_data)

        # headers from the data set
        headers = hdrs

        response = self.client.list_objects(container_name, headers=hdrs)

        method = 'list objects'
        expected = 200
        received = response.status_code

        self.assertEqual(expected,
                         received,
                         msg=STATUS_CODE_MSG.format(method=method,
                                                    expected=expected,
                                                    received=str(received),
                                                    headers=str(hdrs)))
    def test_psuedo_dirs_delimiter_test(self):
        container_name = "{0}_{1}".format(CONTAINER_NAME,
                                          randstring.get_random_string())

        obj_names = {
            "/": "music/grok",
            "-": "music-drok",
            ">": "music>foo",
            "|": "music|bar"
        }

        content_length = str(len(self.default_obj_data))
        headers = {
            "Content-Length": content_length,
            "Content-Type": CONTENT_TYPE_TEXT
        }

        for key in obj_names.keys():
            self.behaviors.create_object(container_name,
                                         obj_names.get(key),
                                         data=self.default_obj_data,
                                         headers=headers)

        self.addCleanup(self.behaviors.force_delete_containers,
                        [container_name])

        for delimiter in obj_names.keys():
            prefix = "music{0}".format(delimiter)

            params = {
                "prefix": prefix,
                "delimiter": delimiter,
                "format": "json"
            }
            response = self.client.list_objects(container_name, params=params)

            members = self.get_members(["subdir", "name"], response.content)

            expected = 1
            received = len(members)
            self.assertEqual(
                expected,
                received,
                msg="container list with prefix: {0} and delimiter: {1}"
                " expected: {2} members received: {3}".format(
                    prefix, delimiter, expected, received))

            obj_name = obj_names.get(delimiter)
            self.assertIn(
                obj_name,
                members,
                msg="object name: {0} was not found in response body:"
                " {1}".format(obj_name, members))
    def test_objects_list_with_prefix_delimiter_query_parameters(self):
        container_name = self.behaviors.generate_unique_container_name(
            self.base_container_name)
        self.client.create_container(container_name)

        self.addCleanup(self.behaviors.force_delete_containers,
                        [container_name])

        object_data = 'Test file data'
        content_length = str(len(object_data))
        object_name_prefix = 'delimiter_test/'
        object_name_postfix = 'object_{0}'.format(
            randstring.get_random_string())
        object_name = '{0}{1}'.format(object_name_prefix, object_name_postfix)

        headers = {
            'Content-Length': content_length,
            'Content-Type': CONTENT_TYPE_TEXT
        }

        self.client.create_object(container_name,
                                  object_name,
                                  headers=headers,
                                  data=object_data)

        params = {'delimiter': '/'}
        response = self.client.list_objects(self.container_name, params=params)

        method = "simulated directory list"
        expected = 200
        received = response.status_code

        self.assertEqual(expected,
                         received,
                         msg=STATUS_CODE_MSG.format(method=method,
                                                    expected=expected,
                                                    received=str(received)))

        params = {'prefix': object_name_prefix, 'delimiter': '/'}

        response = self.client.list_objects(container_name, params=params)

        method = "simulated obj in directory list"
        expected = 200
        received = response.status_code

        self.assertEqual(expected,
                         received,
                         msg=STATUS_CODE_MSG.format(method=method,
                                                    expected=expected,
                                                    received=str(received)))
    def test_objects_list_with_path_query_parameter(self):
        """
        This is a depricated feature that has little documentation.
        The following things need to be done for the path parameter to work.
          1. For every 'directory' a 'directory marker' must be added as a
            object. The directory marker does not need to contain data, and
            thus can have a length of 0.
            Example:
            If you want a directory 'foo/bar/', you would upload a object
            named 'foo/bar/' to your container.

          2. You must upload your objects, prefixed with the 'directory' path.
            Example:
            If you wanted to create an object in 'foo/' and another in
            'foo/bar/', you would have to name the objects as follows:
                foo/object1.txt
                foo/bar/object2.txt

          3. Once this has been done, you can use the path query string
            parameter to list the objects in the simulated directory structure.
            Example:
            Using the above examples, setting path to 'foo/' should list
            the following:
                foo/objet1.txt
                foo/bar/
        """
        container_name = self.behaviors.generate_unique_container_name(
            self.base_container_name)

        self.client.create_container(container_name)

        self.addCleanup(self.behaviors.force_delete_containers,
                        [container_name])

        dir_marker = 'path_test/'

        headers = {'Content-Length': '0'}

        self.client.create_object(container_name, dir_marker, headers=headers)

        dir_marker = 'path_test/nested_dir/'

        headers = {'Content-Length': '0'}

        self.client.create_object(container_name, dir_marker, headers=headers)

        object_data = 'Test file data'
        content_length = str(len(object_data))
        object_name_prefix = 'path_test/nested_dir/'
        object_name_postfix = 'object_{0}'.format(
            randstring.get_random_string())
        object_name = '{0}{1}'.format(object_name_prefix, object_name_postfix)

        headers = {
            'Content-Length': content_length,
            'Content-Type': CONTENT_TYPE_TEXT
        }

        self.client.create_object(container_name,
                                  object_name,
                                  headers=headers,
                                  data=object_data)

        params = {'path': 'path_test/'}

        response = self.client.list_objects(container_name, params=params)

        method = "simulated directory list"
        expected = 200
        received = response.status_code

        self.assertEqual(expected,
                         received,
                         msg=STATUS_CODE_MSG.format(method=method,
                                                    expected=expected,
                                                    received=str(received)))

        params = {'path': 'path_test/nested_dir/'}

        response = self.client.list_objects(container_name, params=params)

        method = "simulated obj in directory list"
        expected = 200
        received = response.status_code

        self.assertEqual(expected,
                         received,
                         msg=STATUS_CODE_MSG.format(method=method,
                                                    expected=expected,
                                                    received=str(received)))
    def test_psuedo_dirs_traversal(self):
        container_name = "{0}_{1}".format(CONTAINER_NAME,
                                          randstring.get_random_string())

        content_length = str(len(self.default_obj_data))
        headers = {
            "Content-Length": content_length,
            "Content-Type": CONTENT_TYPE_TEXT
        }

        base_name = "music/the_best_of_grok_and_drok/grok_live/"

        vol1_obj1 = "{0}{1}".format(base_name, "vol_1/rage_against_the_foo")
        vol1_obj2 = "{0}{1}".format(base_name, "vol_1/a_fist_full_of_foo")
        vol1_obj3 = "{0}{1}".format(base_name, "vol_1/nevermind_the_foo")
        vol2_obj1 = "{0}{1}".format(base_name, "vol_2/iron_foo")
        vol2_obj2 = "{0}{1}".format(base_name, "vol_2/foo_master")

        self.behaviors.create_object(container_name,
                                     vol1_obj1,
                                     data=self.default_obj_data,
                                     headers=headers)

        self.behaviors.create_object(container_name,
                                     vol1_obj2,
                                     data=self.default_obj_data,
                                     headers=headers)

        self.behaviors.create_object(container_name,
                                     vol1_obj3,
                                     data=self.default_obj_data,
                                     headers=headers)

        self.behaviors.create_object(container_name,
                                     vol2_obj1,
                                     data=self.default_obj_data,
                                     headers=headers)

        self.behaviors.create_object(container_name,
                                     vol2_obj2,
                                     data=self.default_obj_data,
                                     headers=headers)

        self.addCleanup(self.behaviors.force_delete_containers,
                        [container_name])

        delimiter = "/"

        prefix = "music/"
        params = {"prefix": prefix, "delimiter": delimiter, "format": "json"}
        response = self.client.list_objects(container_name, params=params)

        members = self.get_members(["subdir", "name"], response.content)

        expected = 1
        received = len(members)
        self.assertEqual(
            expected,
            received,
            msg="container list with prefix: {0} and delimiter: {1}"
            " expected: {2} members received: {3}".format(
                prefix, delimiter, expected, received))

        obj_name = "music/the_best_of_grok_and_drok/"
        self.assertIn(obj_name,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(obj_name, members))

        prefix = "music/the_best_of_grok_and_drok/"
        params = {"prefix": prefix, "delimiter": delimiter, "format": "json"}
        response = self.client.list_objects(container_name, params=params)

        members = self.get_members(["subdir", "name"], response.content)

        expected = 1
        received = len(members)
        self.assertEqual(
            expected,
            received,
            msg="container list with prefix: {0} and delimiter: {1}"
            " expected: {2} members received: {3}".format(
                prefix, delimiter, expected, received))

        obj_name = "music/the_best_of_grok_and_drok/grok_live/"
        self.assertIn(obj_name,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(obj_name, members))

        prefix = "music/the_best_of_grok_and_drok/grok_live/"
        params = {"prefix": prefix, "delimiter": delimiter, "format": "json"}
        response = self.client.list_objects(container_name, params=params)

        members = self.get_members(["subdir", "name"], response.content)

        expected = 2
        received = len(members)
        self.assertEqual(
            expected,
            received,
            msg="container list with prefix: {0} and delimiter: {1}"
            " expected: {2} members received: {3}".format(
                prefix, delimiter, expected, received))

        obj_name = "music/the_best_of_grok_and_drok/grok_live/vol_1/"
        self.assertIn(obj_name,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(obj_name, members))

        obj_name = "music/the_best_of_grok_and_drok/grok_live/vol_2/"
        self.assertIn(obj_name,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(obj_name, members))

        prefix = "music/the_best_of_grok_and_drok/grok_live/vol_1/"
        params = {"prefix": prefix, "delimiter": delimiter, "format": "json"}
        response = self.client.list_objects(container_name, params=params)

        members = self.get_members(["subdir", "name"], response.content)

        expected = 3
        received = len(members)
        self.assertEqual(
            expected,
            received,
            msg="container list with prefix: {0} and delimiter: {1}"
            " expected: {2} members received: {3}".format(
                prefix, delimiter, expected, received))

        self.assertIn(vol1_obj1,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(vol1_obj1, members))

        self.assertIn(vol1_obj2,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(vol1_obj2, members))

        self.assertIn(vol1_obj3,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(vol1_obj3, members))

        prefix = "music/the_best_of_grok_and_drok/grok_live/vol_2/"
        params = {"prefix": prefix, "delimiter": delimiter, "format": "json"}
        response = self.client.list_objects(container_name, params=params)

        members = self.get_members(["subdir", "name"], response.content)

        expected = 2
        received = len(members)
        self.assertEqual(
            expected,
            received,
            msg="container list with prefix: {0} and delimiter: {1}"
            " expected: {2} members received: {3}".format(
                prefix, delimiter, expected, received))

        self.assertIn(vol2_obj1,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(vol2_obj1, members))

        self.assertIn(vol2_obj2,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(vol2_obj2, members))
    def test_psuedo_dirs_with_path_query_parameter(self):
        """
        This is a depricated feature that has little documentation.
        The following things need to be done for the path parameter to work.
          1. For every "directory" a "directory marker" must be added as a
            object. The directory marker does not need to contain data, and
            thus can have a length of 0.
            Example:
            If you want a directory "foo/bar/", you would upload a object
            named "foo/bar/" to your container.

          2. You must upload your objects, prefixed with the "directory" path.
            Example:
            If you wanted to create an object in "foo/" and another in
            "foo/bar/", you would have to name the objects as follows:
                foo/object1.txt
                foo/bar/object2.txt

          3. Once this has been done, you can use the path query string
            parameter to list the objects in the simulated directory structure.
            Example:
            Using the above examples, setting path to "foo/" should list
            the following:
                foo/object1.txt
                foo/bar/
        """
        container_name = "{0}_{1}".format(CONTAINER_NAME,
                                          randstring.get_random_string())

        self.behaviors.create_container(container_name)

        self.addCleanup(self.behaviors.force_delete_containers,
                        [container_name])
        """
        create dir markers
        """
        dir_marker_headers = {"Content-Length": "0"}

        lvl1_dir_marker = "level_1/"
        lvl2_dir_marker = "level_1/level_2/"
        lvl3_dir_marker = "level_1/level_2/level_3/"

        self.client.create_object(container_name,
                                  lvl1_dir_marker,
                                  headers=dir_marker_headers)

        self.client.create_object(container_name,
                                  lvl2_dir_marker,
                                  headers=dir_marker_headers)

        self.client.create_object(container_name,
                                  lvl3_dir_marker,
                                  headers=dir_marker_headers)
        """
        create objects
        """
        headers = {
            "Content-Length": str(len(self.default_obj_data)),
            "Content-Type": CONTENT_TYPE_TEXT
        }
        """
        level 1 objects
        """
        lvl1_obj1 = "{0}{1}".format(lvl1_dir_marker, "lvl1_obj1")
        lvl1_obj2 = "{0}{1}".format(lvl1_dir_marker, "lvl1_obj2")

        self.client.create_object(container_name,
                                  lvl1_obj1,
                                  headers=headers,
                                  data=self.default_obj_data)

        self.client.create_object(container_name,
                                  lvl1_obj2,
                                  headers=headers,
                                  data=self.default_obj_data)
        """
        level 2 objects
        """
        lvl2_obj1 = "{0}{1}".format(lvl2_dir_marker, "lvl2_obj1")
        lvl2_obj2 = "{0}{1}".format(lvl2_dir_marker, "lvl2_obj2")

        self.client.create_object(container_name,
                                  lvl2_obj1,
                                  headers=headers,
                                  data=self.default_obj_data)

        self.client.create_object(container_name,
                                  lvl2_obj2,
                                  headers=headers,
                                  data=self.default_obj_data)
        """
        level 3 objects
        """
        lvl3_obj1 = "{0}{1}".format(lvl3_dir_marker, "lvl3_obj1")
        lvl3_obj2 = "{0}{1}".format(lvl3_dir_marker, "lvl3_obj2")

        self.client.create_object(container_name,
                                  lvl3_obj1,
                                  headers=headers,
                                  data=self.default_obj_data)

        self.client.create_object(container_name,
                                  lvl3_obj2,
                                  headers=headers,
                                  data=self.default_obj_data)
        """
        list objects
        """
        params = {"path": lvl1_dir_marker, "format": "json"}
        response = self.client.list_objects(container_name, params=params)

        members = self.get_members(["name"], response.content)

        expected = 3
        received = len(members)
        self.assertEqual(
            expected,
            received,
            msg="container list with path: {0} expected: {1} members"
            " received: {2}".format(lvl1_dir_marker, expected, received))

        self.assertIn(lvl2_dir_marker,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(lvl2_dir_marker, members))

        self.assertIn(lvl1_obj1,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(lvl1_obj1, members))

        self.assertIn(lvl1_obj2,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(lvl1_obj2, members))

        params = {"path": lvl2_dir_marker, "format": "json"}
        response = self.client.list_objects(container_name, params=params)

        members = self.get_members(["name"], response.content)

        expected = 3
        received = len(members)
        self.assertEqual(
            expected,
            received,
            msg="container list with path: {0} expected: {1} members"
            " received: {2}".format(lvl3_dir_marker, expected, received))

        self.assertIn(lvl3_dir_marker,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(lvl3_dir_marker, members))

        self.assertIn(lvl2_obj1,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(lvl2_obj1, members))

        self.assertIn(lvl2_obj2,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(lvl2_obj2, members))

        params = {"path": lvl3_dir_marker, "format": "json"}
        response = self.client.list_objects(container_name, params=params)

        members = self.get_members(["name"], response.content)

        expected = 2
        received = len(members)
        self.assertEqual(
            expected,
            received,
            msg="container list with path: {0} expected: {1} members"
            " received: {2}".format(lvl1_dir_marker, expected, received))

        self.assertIn(lvl3_obj1,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(lvl3_obj1, members))

        self.assertIn(lvl3_obj2,
                      members,
                      msg="object name: {0} was not found in response body:"
                      " {1}".format(lvl3_obj2, members))