From 7a957c94c874d3db9686ebfec87bbc0947b1e467 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Tue, 31 Jan 2017 15:37:16 -0500 Subject: [PATCH] image/appc: fix volume conversion and add tests --- image/appc/__init__.py | 4 ++-- image/appc/test/test_appc.py | 14 ++++++++++++++ util/dict_wrappers.py | 9 ++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/image/appc/__init__.py b/image/appc/__init__.py index 851c4698c..296c9c84e 100644 --- a/image/appc/__init__.py +++ b/image/appc/__init__.py @@ -156,8 +156,8 @@ class DockerV1ToACIManifestTranslator(object): volume_name = DockerV1ToACIManifestTranslator._ac_name(docker_volume_path) return "volume-%s" % volume_name - volume_list = docker_config['Volumes'] or docker_config['volumes'] or [] - for docker_volume_path in volume_list: + volume_list = docker_config['Volumes'] or docker_config['volumes'] or {} + for docker_volume_path in volume_list.iterkeys(): if not docker_volume_path: continue diff --git a/image/appc/test/test_appc.py b/image/appc/test/test_appc.py index 45696f033..de829b4e8 100644 --- a/image/appc/test/test_appc.py +++ b/image/appc/test/test_appc.py @@ -2,6 +2,7 @@ import pytest from image.appc import DockerV1ToACIManifestTranslator from data.interfaces.verbs import RepositoryReference, ImageWithBlob +from util.dict_wrappers import JSONPathDict EXAMPLE_MANIFEST_OBJ = { @@ -96,3 +97,16 @@ def test_legacy_port_conversion(repo_image): ports.sort() assert {'name':'port-8080', 'port':8080, 'protocol':'tcp'} == ports[0] assert {'name':'port-8081', 'port':8081, 'protocol':'tcp'} == ports[1] + + +@pytest.mark.parametrize("vcfg,expected", [ + ({'Volumes': None}, []), + ({'Volumes': {}}, []), + ({'Volumes': {'/bin': {}}}, [{'name': 'volume-bin', 'path': '/bin', 'readOnly': False}]), + ({'volumes': None}, []), + ({'volumes': {}}, []), + ({'volumes': {'/bin': {}}}, [{'name': 'volume-bin', 'path': '/bin', 'readOnly': False}]), +]) +def test_volume_version_easy(vcfg, expected): + output = DockerV1ToACIManifestTranslator._build_volumes(JSONPathDict(vcfg)) + assert output == expected diff --git a/util/dict_wrappers.py b/util/dict_wrappers.py index f840f2c36..23a7b3b83 100644 --- a/util/dict_wrappers.py +++ b/util/dict_wrappers.py @@ -59,8 +59,16 @@ class JSONPathDict(object): def __getitem__(self, path): return self.get(path) + def __iter__(self): + return self._object.itervalues() + + def iterkeys(self): + return self._object.iterkeys() + def get(self, path, not_found_handler=None): """ Returns the value found at the given path. Path is a json-path expression. """ + if self._object == {} or self._object is None: + return None jsonpath_expr = parse(path) matches = jsonpath_expr.find(self._object) if not matches: @@ -77,4 +85,3 @@ class JSONPathDict(object): def keys(self): return self._object.keys() -