Skip to content

Commit b9dcaf4

Browse files
authored
Merge pull request #648 from cderici/subordinate-charm-num-unit
#648 #### Description Subordinate charms are by definition don't require additional units. So num-units should be 0 at deploy. Normally in libjuju num_units defaults to 1, this change lets libjuju to check the metadata from charmhub to see if we're deploying a subordinate charm. If that's the case then we adjust the num-units to 0 to be able to deploy it without any problems from Juju's side. Fixes #639 #### QA Steps ```sh tox -e integration -- tests/integration/test_charmhub.py::test_subordinate_charm_zero_units ``` #### Notes & Discussion
2 parents 4f1944c + ae2a35b commit b9dcaf4

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

juju/model.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,16 @@ async def deploy(
16221622
resources = await self._add_charmhub_resources(res.app_name,
16231623
identifier,
16241624
add_charm_res.charm_origin)
1625+
charm_info = await self.charmhub.info(url.name)
1626+
is_subordinate = False
1627+
try:
1628+
is_subordinate = charm_info.result.charm.subordinate
1629+
except AttributeError:
1630+
log.warning('CharmHub.Info : unable to retrieve the subordinate information')
1631+
if is_subordinate:
1632+
if num_units > 1:
1633+
raise JujuError("cannot use num_units with subordinate application")
1634+
num_units = 0
16251635

16261636
if Schema.CHARM_STORE.matches(url.schema):
16271637
resources = await self._add_store_resources(res.app_name,

tests/integration/test_charmhub.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import pytest
22

33
from .. import base
4-
from juju.errors import JujuAPIError
4+
from juju.errors import JujuAPIError, JujuError
5+
from juju import jasyncio
56

67

78
@base.bootstrapped
@@ -69,3 +70,30 @@ async def test_find_all(event_loop):
6970
for resp in result.result:
7071
assert resp.name != ""
7172
assert resp.type_ in ["charm", "bundle"]
73+
74+
75+
@base.bootstrapped
76+
@pytest.mark.asyncio
77+
async def test_subordinate_charm_zero_units(event_loop):
78+
# normally in pylibjuju deploy num_units defaults to 1, we switch
79+
# that to 0 behind the scenes if we see that the charmhub charm
80+
# we're deploying is a subordinate charm
81+
async with base.CleanModel() as model:
82+
83+
# rsyslog-forwarder-ha is a subordinate charm
84+
app = await model.deploy('rsyslog-forwarder-ha')
85+
await jasyncio.sleep(5)
86+
87+
assert len(app.units) == 0
88+
await app.destroy()
89+
await jasyncio.sleep(5)
90+
91+
# note that it'll error if the user tries to use num_units
92+
with pytest.raises(JujuError):
93+
await model.deploy('rsyslog-forwarder-ha', num_units=175)
94+
95+
# (full disclosure: it'll quitely switch to 0 if user enters
96+
# num_units=1, instead of erroring)
97+
app2 = await model.deploy('rsyslog-forwarder-ha', num_units=1)
98+
await jasyncio.sleep(5)
99+
assert len(app2.units) == 0

tests/integration/test_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ async def test_list_models_user_access(event_loop):
149149
# testing all flag
150150
await user.grant(acl='superuser')
151151
models_all = await controller.list_models(username, all=True)
152-
assert models1 == models_all
152+
assert len(models_all) > len(models2)
153153

154154

155155
@base.bootstrapped

0 commit comments

Comments
 (0)