Unit test for series logic
authorCory Johns <johnsca@gmail.com>
Tue, 7 Mar 2017 20:35:23 +0000 (14:35 -0600)
committerCory Johns <johnsca@gmail.com>
Tue, 7 Mar 2017 20:35:23 +0000 (14:35 -0600)
juju/model.py
tests/unit/test_model.py

index 3df2669..04fb2d4 100644 (file)
@@ -935,6 +935,22 @@ class Model(object):
         """
         raise NotImplementedError()
 
+    def _get_series(self, entity_url, entity):
+        # try to get the series from the provided charm URL
+        if entity_url.startswith('cs:'):
+            parts = entity_url[3:].split('/')
+        else:
+            parts = entity_url.split('/')
+        if parts[0].startswith('~'):
+            parts.pop(0)
+        if len(parts) > 1:
+            # series was specified in the URL
+            return parts[0]
+        # series was not supplied at all, so use the newest
+        # supported series according to the charm store
+        ss = entity['Meta']['supported-series']
+        return ss['SupportedSeries'][0]
+
     async def deploy(
             self, entity_url, application_name=None, bind=None, budget=None,
             channel=None, config=None, constraints=None, force=False,
@@ -1018,22 +1034,8 @@ class Model(object):
             if not is_local:
                 if not application_name:
                     application_name = entity['Meta']['charm-metadata']['Name']
-                if not series and '/' in entity_url:
-                    # try to get the series from the provided charm URL
-                    if entity_url.startswith('cs:'):
-                        parts = entity_url[3:].split('/')
-                    else:
-                        parts = entity_url.split('/')
-                    if parts[0].startswith('~'):
-                        parts.pop(0)
-                    if len(parts) > 1:
-                        # series was specified in the URL
-                        series = parts[0]
                 if not series:
-                    # series was not supplied at all, so use the newest
-                    # supported series according to the charm store
-                    ss = entity['Meta']['supported-series']
-                    series = ss['SupportedSeries'][0]
+                    series = self._get_series(entity_url, entity)
                 if not channel:
                     channel = 'stable'
                 await client_facade.AddCharm(channel, entity_id)
index f8cced3..67db5ae 100644 (file)
@@ -92,3 +92,24 @@ class TestModelState(unittest.TestCase):
         self.assertFalse(new)
         self.assertIsInstance(prev, Application)
         self.assertTrue(prev)
+
+
+def test_get_series():
+    from juju.model import Model
+    model = Model()
+    entity = {
+        'Meta': {
+            'supported-series': {
+                'SupportedSeries': [
+                    'xenial',
+                    'trusty',
+                ],
+            },
+        },
+    }
+    assert model._get_series('cs:trusty/ubuntu', entity) == 'trusty'
+    assert model._get_series('xenial/ubuntu', entity) == 'xenial'
+    assert model._get_series('~foo/xenial/ubuntu', entity) == 'xenial'
+    assert model._get_series('~foo/ubuntu', entity) == 'xenial'
+    assert model._get_series('ubuntu', entity) == 'xenial'
+    assert model._get_series('cs:ubuntu', entity) == 'xenial'