def test_add_no_container_and_create(self): """ Tests that adding an image with a non-existing container creates the container automatically if flag is set """ options = SWIFT_OPTIONS.copy() options['swift_store_create_container_on_put'] = 'True' options['swift_store_container'] = 'noexist' expected_image_id = 42 expected_swift_size = FIVE_KB expected_swift_contents = "*" * expected_swift_size expected_checksum = hashlib.md5(expected_swift_contents).hexdigest() expected_location = format_swift_location( options['swift_store_user'], options['swift_store_key'], options['swift_store_auth_address'], options['swift_store_container'], expected_image_id) image_swift = StringIO.StringIO(expected_swift_contents) location, size, checksum = SwiftBackend.add(42, image_swift, options) self.assertEquals(expected_location, location) self.assertEquals(expected_swift_size, size) self.assertEquals(expected_checksum, checksum) loc = get_location_from_uri(expected_location) new_image_swift = SwiftBackend.get(loc) new_image_contents = new_image_swift.getvalue() new_image_swift_size = new_image_swift.len self.assertEquals(expected_swift_contents, new_image_contents) self.assertEquals(expected_swift_size, new_image_swift_size)
def test_add(self): """Test that we can add an image via the swift backend""" expected_image_id = 42 expected_swift_size = FIVE_KB expected_swift_contents = "*" * expected_swift_size expected_checksum = hashlib.md5(expected_swift_contents).hexdigest() expected_location = format_swift_location( SWIFT_OPTIONS['swift_store_user'], SWIFT_OPTIONS['swift_store_key'], SWIFT_OPTIONS['swift_store_auth_address'], SWIFT_OPTIONS['swift_store_container'], expected_image_id) image_swift = StringIO.StringIO(expected_swift_contents) location, size, checksum = SwiftBackend.add(42, image_swift, SWIFT_OPTIONS) self.assertEquals(expected_location, location) self.assertEquals(expected_swift_size, size) self.assertEquals(expected_checksum, checksum) loc = get_location_from_uri(expected_location) new_image_swift = SwiftBackend.get(loc) new_image_contents = new_image_swift.getvalue() new_image_swift_size = new_image_swift.len self.assertEquals(expected_swift_contents, new_image_contents) self.assertEquals(expected_swift_size, new_image_swift_size)
def test_add_no_container_no_create(self): """ Tests that adding an image with a non-existing container raises an appropriate exception """ options = SWIFT_OPTIONS.copy() options['swift_store_create_container_on_put'] = 'False' options['swift_store_container'] = 'noexist' image_swift = StringIO.StringIO("nevergonnamakeit") # We check the exception text to ensure the container # missing text is found in it, otherwise, we would have # simply used self.assertRaises here exception_caught = False try: SwiftBackend.add(3, image_swift, options) except BackendException, e: exception_caught = True self.assertTrue("container noexist does not exist " "in Swift" in str(e))
def test_add_auth_url_variations(self): """ Test that we can add an image via the swift backend with a variety of different auth_address values """ variations = ['http://localhost:80', 'http://localhost', 'http://localhost/v1', 'http://localhost/v1/', 'https://localhost', 'https://localhost:8080', 'https://localhost/v1', 'https://localhost/v1/', 'localhost', 'localhost:8080/v1'] i = 42 for variation in variations: expected_image_id = i expected_swift_size = FIVE_KB expected_swift_contents = "*" * expected_swift_size expected_checksum = \ hashlib.md5(expected_swift_contents).hexdigest() new_options = SWIFT_OPTIONS.copy() new_options['swift_store_auth_address'] = variation expected_location = format_swift_location( new_options['swift_store_user'], new_options['swift_store_key'], new_options['swift_store_auth_address'], new_options['swift_store_container'], expected_image_id) image_swift = StringIO.StringIO(expected_swift_contents) location, size, checksum = SwiftBackend.add(i, image_swift, new_options) self.assertEquals(expected_location, location) self.assertEquals(expected_swift_size, size) self.assertEquals(expected_checksum, checksum) loc = get_location_from_uri(expected_location) new_image_swift = SwiftBackend.get(loc) new_image_contents = new_image_swift.getvalue() new_image_swift_size = new_image_swift.len self.assertEquals(expected_swift_contents, new_image_contents) self.assertEquals(expected_swift_size, new_image_swift_size) i = i + 1