Skip to content

Commit

Permalink
Juju 3.x support.
Browse files Browse the repository at this point in the history
1. Juju 3.x snap is strictly confined and doesn't have the permission to create juju's local user directory (`~/.local/share/juju`). This PR implements a workaround for this issue
2. Refresh lxd snap to "latest/stable" to support bootstrapping Juju 3.x controller.
3. Added functional tests for Juju 3.x
4. Pin tenacity to workaround LP#2031582

Reviewed-on: https://code.launchpad.net/~txiao/charm-juju-local/+git/charm-juju-local/+merge/449219
Reviewed-by: Eric Chen <[email protected]>
Reviewed-by: Ramesh Sattaru <[email protected]>
Reviewed-by: 🤖 prod-jenkaas-bootstack <[email protected]>
  • Loading branch information
agileshaw authored and Canonical IS Mergebot committed Aug 21, 2023
2 parents fdb6213 + aa4d37d commit d8bf76a
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ help:
@echo ""
@echo " make help - show this text"
@echo " make lint - run flake8"
@echo " make unittests - run the tests defined in the unittest subdirectory"
@echo " make test - run the functests and lint"
@echo " make functional - run the tests defined in the functional subdirectory"
@echo " make release - build the charm"
Expand All @@ -16,6 +17,9 @@ lint:
@echo "Running flake8"
@-tox -e lint

unittests:
@echo "No unit tests available at the moment"

test: lint functional

functional: build
Expand Down
12 changes: 12 additions & 0 deletions lib/lib_charm_juju_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from charmhelpers.core import hookenv, host, templating
from charmhelpers.fetch.ubuntu import apt_cache
from charms.layer import snap


LXD_BRIDGE_TMPL = "lxd-bridge.ini.j2"
Expand Down Expand Up @@ -46,6 +47,9 @@ def lxd_migrate(self):
subprocess.call("sudo lxd.migrate -yes", shell=True)

def lxd_init(self):
# Refresh lxd snap to "latest/stable" channel for Juju 3.x support.
snap.refresh("lxd", channel="latest/stable")

install_sh = textwrap.dedent(
"""
lxd init --auto --storage-backend dir
Expand All @@ -64,6 +68,14 @@ def setup_juju(self):
shell=True,
)
series = host.lsb_release()["DISTRIB_CODENAME"]

# Create juju's local user directory if missing
# This is a workaround for LP #1988355
subprocess.call(
"sudo -u ubuntu mkdir -p /home/ubuntu/.local/share/juju",
shell=True,
)

subprocess.call(
textwrap.dedent(
f"""
Expand Down
1 change: 1 addition & 0 deletions tests/bundles/focal-juju3.yaml
1 change: 1 addition & 0 deletions tests/bundles/jammy-juju3.yaml
5 changes: 5 additions & 0 deletions tests/bundles/overlays/focal-juju3.yaml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
series: focal
applications:
{{ charm_name }}:
options:
juju-channel: "3.1/stable"
5 changes: 5 additions & 0 deletions tests/bundles/overlays/jammy-juju3.yaml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
series: jammy
applications:
{{ charm_name }}:
options:
juju-channel: "3.1/stable"
4 changes: 4 additions & 0 deletions tests/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ charm_name: juju-local
tests:
- tests.tests_juju_local.CharmJujuLocalTest
gate_bundles:
- jammy-juju3
- focal-juju3
- jammy
- focal
- bionic
dev_bundles:
- jammy-juju3
- jammy
smoke_bundles:
- focal-juju3
- focal
target_deploy_status:
juju-local:
Expand Down
22 changes: 17 additions & 5 deletions tests/tests_juju_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,40 @@ def setUpClass(cls):
cls.model_name = model.get_juju_model()
cls.test_config = lifecycle_utils.get_charm_config()
model.block_until_all_units_idle()
# Add a test model in the remote Juju cloud
# This is needed becuase in Juju 3.x there's no default model created
# after bootstrapping the controller
cls.remote_juju(["add-model", "test"], None)

def remote_juju(self, args, format="json"):
@classmethod
def remote_juju(cls, args, format="json"):
if format:
fmt = "--format={}".format(format)
else:
fmt = ""
cmd = """sudo -u ubuntu bash -c '/snap/bin/juju {} {}'""".format(
" ".join(args), fmt
)
res = model.run_on_unit(self.jlocal_unit, cmd)
return res.get("Stdout"), res.get("Stderr")
res = model.run_on_unit(cls.jlocal_unit, cmd)
return_code = int(res["Code"])
if return_code != 0:
raise RuntimeError(
"Failed to execute command in juju-local unit.\nStderr: {}".format(
res.get("Stderr")
)
)
return res.get("Stdout")

def juju_status(self):
stdout, _ = self.remote_juju(["status"])
stdout = self.remote_juju(["status"])
return json.loads(stdout)

def test_juju_status(self):
jstatus = self.juju_status()
self.assertEqual(jstatus["model"]["controller"], "lxd")

def test_juju_users(self):
stdout, _ = self.remote_juju(["users"])
stdout = self.remote_juju(["users"])
userobjects = json.loads(stdout)
usernames = set(u["user-name"] for u in userobjects)
self.assertTrue("admin" in usernames)
Expand Down
1 change: 1 addition & 0 deletions wheelhouse.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Markupsafe<2.0.0 # for xenial support
tenacity<5.0.4 # workaround for LP#2031582

0 comments on commit d8bf76a

Please sign in to comment.