Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Lowercase all image names for Docker driver
Browse files Browse the repository at this point in the history
Docker repository names must be lowercase. When
an image is requested or a snapshot is created,
the requested name should be lowercased such that
it may be given a valid name in the Docker registry.

Adds tests for snapshots and get_image_name.

Change-Id: I97b281d30454e33ba32e428901bb5da58400308f
Closes-bug: 1284761
  • Loading branch information
Eric Windisch committed Apr 2, 2014
1 parent cc84dd6 commit e85ebeb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
73 changes: 73 additions & 0 deletions novadocker/tests/virt/docker/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

import mock

from nova.compute import task_states
from nova import context
from nova import exception
from nova.openstack.common import jsonutils
from nova.openstack.common import units
from nova import test
import nova.tests.image.fake
from nova.tests import matchers
from nova.tests import utils
from nova.tests.virt.test_virt_drivers import _VirtDriverTestCase
from novadocker.tests.virt.docker import mock_client
Expand Down Expand Up @@ -255,3 +258,73 @@ def test_list_instances_no_inspect_race(self):
return_value={}):
instances = self.connection.list_instances()
self.assertFalse(instances)

@mock.patch.object(novadocker.tests.virt.docker.mock_client.MockClient,
'push_repository')
@mock.patch.object(novadocker.virt.docker.driver.DockerDriver,
'_find_container_by_name', return_value={'id': 'fake_id'})
def test_snapshot(self, byname_mock, repopush_mock):
# Use mix-case to test that mixed-case image names succeed.
snapshot_name = 'tEsT-SnAp'

expected_calls = [
{'args': (),
'kwargs':
{'task_state': task_states.IMAGE_PENDING_UPLOAD}},
{'args': (),
'kwargs':
{'task_state': task_states.IMAGE_UPLOADING,
'expected_state': task_states.IMAGE_PENDING_UPLOAD}}]
func_call_matcher = matchers.FunctionCallMatcher(expected_calls)

instance_ref = utils.get_test_instance()
properties = {'instance_id': instance_ref['id'],
'user_id': str(self.context.user_id)}
sent_meta = {'name': snapshot_name, 'is_public': False,
'status': 'creating', 'properties': properties}

# Because the docker driver doesn't push directly into Glance, we
# cannot check that the images are correctly configured in the
# fake image service, but we can ensuring naming and other
# conventions are accurate.
image_service = nova.tests.image.fake.FakeImageService()
recv_meta = image_service.create(context, sent_meta)

self.connection.snapshot(self.context, instance_ref, recv_meta['id'],
func_call_matcher.call)

(repopush_calls, repopush_kwargs) = repopush_mock.call_args
repo = repopush_calls[0]

# Assure the image_href is correctly placed into the headers.
headers_image_href = repopush_kwargs.get('headers', {}).get(
'X-Meta-Glance-Image-Id')
self.assertEqual(recv_meta['id'], headers_image_href)

# Assure the repository name pushed into the docker registry is valid.
self.assertIn(":" + str(self.connection._get_registry_port()) + "/",
repo)
self.assertEqual(repo.count(":"), 1)
self.assertEqual(repo.count("/"), 1)

# That the lower-case snapshot name matches the name pushed
image_name = repo.split("/")[1]
self.assertEqual(snapshot_name.lower(), image_name)

def test_get_image_name(self):
instance_ref = utils.get_test_instance()
image_info = utils.get_test_image_info(None, instance_ref)
image_info['container_format'] = 'docker'
image_info['name'] = 'MiXeDcAsE-image'
repo = self.connection._get_image_name(self.context,
instance_ref, image_info)

# Assure the repository name pushed into the docker registry is valid.
self.assertIn(":" + str(self.connection._get_registry_port()) + "/",
repo)
self.assertEqual(repo.count(":"), 1)
self.assertEqual(repo.count("/"), 1)

# That the lower-case snapshot name matches the name pushed
image_name = repo.split("/")[1]
self.assertEqual(image_info['name'].lower(), image_name)
4 changes: 2 additions & 2 deletions novadocker/virt/docker/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def _get_image_name(self, context, instance, image):
instance_id=instance['name'])
return '{0}:{1}/{2}'.format(CONF.my_ip,
self._registry_port,
image['name'])
image['name'].lower())

def _get_default_cmd(self, image_name):
default_cmd = ['sh']
Expand Down Expand Up @@ -378,7 +378,7 @@ def snapshot(self, context, instance, image_href, update_task_state):
(image_service, image_id) = glance.get_remote_image_service(
context, image_href)
image = image_service.show(context, image_id)
name = image['name']
name = image['name'].lower()
default_tag = (':' not in name)
name = '{0}:{1}/{2}'.format(CONF.my_ip,
self._registry_port,
Expand Down

0 comments on commit e85ebeb

Please sign in to comment.