From e6b7b9d9d091c1ef32236370183ce2ff22d01956 Mon Sep 17 00:00:00 2001 From: Cory Johns Date: Tue, 7 Mar 2017 14:35:23 -0600 Subject: [PATCH] Unit test for series logic --- juju/model.py | 32 +++++++++++++++++--------------- tests/unit/test_model.py | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/juju/model.py b/juju/model.py index 3df2669..04fb2d4 100644 --- a/juju/model.py +++ b/juju/model.py @@ -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) diff --git a/tests/unit/test_model.py b/tests/unit/test_model.py index f8cced3..67db5ae 100644 --- a/tests/unit/test_model.py +++ b/tests/unit/test_model.py @@ -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' -- 2.25.1