diff options
| author | eddie-sheffield <eddie.sheffield@rackspace.com> | 2013-08-19 17:27:07 -0400 |
|---|---|---|
| committer | eddie-sheffield <eddie.sheffield@rackspace.com> | 2013-10-02 13:22:55 -0400 |
| commit | 32d9c42816b608220ae5692e573142dab6534604 (patch) | |
| tree | 45ea0aaa339e0ce61662b7128c2f9c0d2c5357d8 /tests/v2 | |
| parent | 7a4a8a0979fd76203f0cb81622a7f06ee42bb615 (diff) | |
| download | python-glanceclient-32d9c42816b608220ae5692e573142dab6534604.tar.gz | |
Add CLI for V2 image create, update, and upload
Provides command line support for image-create, image-update,
and image-upload using the Glance V2 API. This includes building
help text for create and update based on the image jsonschema
as fetched from the server.
Also fixes bug caused by default warlock patch generation not
matching what Glance expects when updating a core property
which had not originally been set when the image was created.
Related to bp glance-client-v2
Change-Id: I841f9e3d05802f4b794cb6f4849abe03ff0324d9
Diffstat (limited to 'tests/v2')
| -rw-r--r-- | tests/v2/test_schemas.py | 80 | ||||
| -rw-r--r-- | tests/v2/test_shell_v2.py | 114 |
2 files changed, 194 insertions, 0 deletions
diff --git a/tests/v2/test_schemas.py b/tests/v2/test_schemas.py index ff286df..23b881a 100644 --- a/tests/v2/test_schemas.py +++ b/tests/v2/test_schemas.py @@ -14,6 +14,7 @@ # under the License. import testtools +import warlock from glanceclient.v2 import schemas from tests import utils @@ -43,6 +44,15 @@ fixtures = { } +_SCHEMA = schemas.Schema({ + 'name': 'image', + 'properties': { + 'name': {'type': 'string'}, + 'color': {'type': 'string'}, + }, +}) + + class TestSchemaProperty(testtools.TestCase): def test_property_minimum(self): prop = schemas.SchemaProperty('size') @@ -83,3 +93,73 @@ class TestController(testtools.TestCase): schema = self.controller.get('image') self.assertEqual(schema.name, 'image') self.assertEqual([p.name for p in schema.properties], ['name']) + + +class TestSchemaBasedModel(testtools.TestCase): + def setUp(self): + super(TestSchemaBasedModel, self).setUp() + self.model = warlock.model_factory(_SCHEMA.raw(), + schemas.SchemaBasedModel) + + def test_patch_should_replace_missing_core_properties(self): + obj = { + 'name': 'fred' + } + + original = self.model(obj) + original['color'] = 'red' + + patch = original.patch + expected = '[{"path": "/color", "value": "red", "op": "replace"}]' + self.assertEqual(patch, expected) + + def test_patch_should_add_extra_properties(self): + obj = { + 'name': 'fred', + } + + original = self.model(obj) + original['weight'] = '10' + + patch = original.patch + expected = '[{"path": "/weight", "value": "10", "op": "add"}]' + self.assertEqual(patch, expected) + + def test_patch_should_replace_extra_properties(self): + obj = { + 'name': 'fred', + 'weight': '10' + } + + original = self.model(obj) + original['weight'] = '22' + + patch = original.patch + expected = '[{"path": "/weight", "value": "22", "op": "replace"}]' + self.assertEqual(patch, expected) + + def test_patch_should_remove_extra_properties(self): + obj = { + 'name': 'fred', + 'weight': '10' + } + + original = self.model(obj) + del original['weight'] + + patch = original.patch + expected = '[{"path": "/weight", "op": "remove"}]' + self.assertEqual(patch, expected) + + def test_patch_should_remove_core_properties(self): + obj = { + 'name': 'fred', + 'color': 'red' + } + + original = self.model(obj) + del original['color'] + + patch = original.patch + expected = '[{"path": "/color", "op": "remove"}]' + self.assertEqual(patch, expected) diff --git a/tests/v2/test_shell_v2.py b/tests/v2/test_shell_v2.py index 2f8ae91..17ad95d 100644 --- a/tests/v2/test_shell_v2.py +++ b/tests/v2/test_shell_v2.py @@ -102,6 +102,111 @@ class ShellV2Test(testtools.TestCase): mocked_list.assert_called_once_with('pass') utils.print_dict.assert_called_once_with({'id': 'pass'}) + def test_do_image_create_no_user_props(self): + args = self._make_args({'name': 'IMG-01', 'disk_format': 'vhd', + 'container_format': 'bare'}) + with mock.patch.object(self.gc.images, 'create') as mocked_create: + ignore_fields = ['self', 'access', 'file', 'schema'] + expect_image = dict([(field, field) for field in ignore_fields]) + expect_image['id'] = 'pass' + expect_image['name'] = 'IMG-01' + expect_image['disk_format'] = 'vhd' + expect_image['container_format'] = 'bare' + mocked_create.return_value = expect_image + + test_shell.do_image_create(self.gc, args) + + mocked_create.assert_called_once_with(name='IMG-01', + disk_format='vhd', + container_format='bare') + utils.print_dict.assert_called_once_with({ + 'id': 'pass', 'name': 'IMG-01', 'disk_format': 'vhd', + 'container_format': 'bare'}) + + def test_do_image_create_with_user_props(self): + args = self._make_args({'name': 'IMG-01', + 'property': ['myprop=myval']}) + with mock.patch.object(self.gc.images, 'create') as mocked_create: + ignore_fields = ['self', 'access', 'file', 'schema'] + expect_image = dict([(field, field) for field in ignore_fields]) + expect_image['id'] = 'pass' + expect_image['name'] = 'IMG-01' + expect_image['myprop'] = 'myval' + mocked_create.return_value = expect_image + + test_shell.do_image_create(self.gc, args) + + mocked_create.assert_called_once_with(name='IMG-01', + myprop='myval') + utils.print_dict.assert_called_once_with({ + 'id': 'pass', 'name': 'IMG-01', 'myprop': 'myval'}) + + def test_do_image_update_no_user_props(self): + args = self._make_args({'id': 'pass', 'name': 'IMG-01', + 'disk_format': 'vhd', + 'container_format': 'bare'}) + with mock.patch.object(self.gc.images, 'update') as mocked_update: + ignore_fields = ['self', 'access', 'file', 'schema'] + expect_image = dict([(field, field) for field in ignore_fields]) + expect_image['id'] = 'pass' + expect_image['name'] = 'IMG-01' + expect_image['disk_format'] = 'vhd' + expect_image['container_format'] = 'bare' + mocked_update.return_value = expect_image + + test_shell.do_image_update(self.gc, args) + + mocked_update.assert_called_once_with('pass', + None, + name='IMG-01', + disk_format='vhd', + container_format='bare') + utils.print_dict.assert_called_once_with({ + 'id': 'pass', 'name': 'IMG-01', 'disk_format': 'vhd', + 'container_format': 'bare'}) + + def test_do_image_update_with_user_props(self): + args = self._make_args({'id': 'pass', 'name': 'IMG-01', + 'property': ['myprop=myval']}) + with mock.patch.object(self.gc.images, 'update') as mocked_update: + ignore_fields = ['self', 'access', 'file', 'schema'] + expect_image = dict([(field, field) for field in ignore_fields]) + expect_image['id'] = 'pass' + expect_image['name'] = 'IMG-01' + expect_image['myprop'] = 'myval' + mocked_update.return_value = expect_image + + test_shell.do_image_update(self.gc, args) + + mocked_update.assert_called_once_with('pass', + None, + name='IMG-01', + myprop='myval') + utils.print_dict.assert_called_once_with({ + 'id': 'pass', 'name': 'IMG-01', 'myprop': 'myval'}) + + def test_do_image_update_with_remove_props(self): + args = self._make_args({'id': 'pass', 'name': 'IMG-01', + 'disk_format': 'vhd', + 'remove-property': ['container_format']}) + with mock.patch.object(self.gc.images, 'update') as mocked_update: + ignore_fields = ['self', 'access', 'file', 'schema'] + expect_image = dict([(field, field) for field in ignore_fields]) + expect_image['id'] = 'pass' + expect_image['name'] = 'IMG-01' + expect_image['disk_format'] = 'vhd' + + mocked_update.return_value = expect_image + + test_shell.do_image_update(self.gc, args) + + mocked_update.assert_called_once_with('pass', + ['container_format'], + name='IMG-01', + disk_format='vhd') + utils.print_dict.assert_called_once_with({ + 'id': 'pass', 'name': 'IMG-01', 'disk_format': 'vhd'}) + def test_do_explain(self): input = { 'page_size': 18, @@ -115,6 +220,15 @@ class ShellV2Test(testtools.TestCase): self.gc.schemas.get.assert_called_once_with('test') + def test_image_upload(self): + args = self._make_args({'id': 'IMG-01', 'file': 'test'}) + + with mock.patch.object(self.gc.images, 'upload') as mocked_upload: + utils.get_data_file = mock.Mock(return_value='testfile') + mocked_upload.return_value = None + test_shell.do_image_upload(self.gc, args) + mocked_upload.assert_called_once_with('IMG-01', 'testfile') + def test_image_download(self): args = self._make_args( {'id': 'pass', 'file': 'test', 'progress': False}) |
