How to manage your test environment

Whether you are a charm user looking to try out some charms or a charm author looking to develop a charm, you will likely need / want all of the following:

  • juju,

  • a localhost Kubernetes / machine cloud, and possibly also

  • further deploy or develop tools,

ideally in an isolated test environment and with the confidence that you know not just how to set things up but also tear things down.

This document shows two basic ways to achieve all of that, one automatic (using an Ubuntu VM pre-equipped with most/all you’ll need) and one manual (where you have the option to go without a Multipass VM).

Tip

The automatic path should be faster and safer for most.

Set things up

Get an isolated test environment; install the juju CLI client; get a cloud, add the cloud to juju and bootstrap a controller into the cloud; add a model on the controller:

  1. Install Multipass, then use it with the charm-dev blueprint to launch a Juju-ready Ubuntu VM (below, my-juju-vm), and launch a shell into the VM:

Note

If on Windows: Note that Multipass can only be installed on Windows 10 Pro or Enterprise. If you are using a different version, please follow the manual path, omitting the Multipass step.

Note

This step may take a few minutes to complete (e.g., 10 mins).

This is because the command downloads, installs, (updates,) and configures a number of packages, and the speed will be affected by network bandwidth (not just your own, but also that of the package sources).

However, once it’s done, you’ll have everything you’ll need – all in a nice isolated environment that you can clean up easily.

Troubleshooting: If this fails, run multipass delete --purge my-juju-vm to clean up, then try the launch line again.

# Install Multipass. E.g., on a Linux with snapd:
sudo snap install multipass

# Launch a Multipass VM from the charm-dev blueprint:
$ multipass launch --cpus 4 --memory 8G --disk 50G --name my-juju-vm charm-dev 

# Open a shell into the VM:
$ multipass shell my-juju-vm
Welcome to Ubuntu 22.04.4 LTS (GNU/Linux 5.15.0-100-generic x86_64)
# ...

# Type any further commands after the VM shell prompt:
ubuntu@my-juju-vm:~$ 

Tip

At any point:

  • To exit the shell, press mod + C (e.g., Ctrl+C) or type exit.

  • To stop the VM after exiting the VM shell, run multipass stop charm-dev-vm.

  • To restart the VM and re-open a shell into it, type multipass shell charm-dev-vm.

  1. Verify that the VM already has everything that you’ll need to deploy charms with Juju: a localhost cloud (microk8s - a MicroK8s-based Kubernetes cloud for Kubernetes charms; localhost – a LXD-based machine cloud for machine charms); the cloud is already known to juju; juju already has a controller bootstrapped on the cloud`; and there is a workload model on that controller that you can go ahead and deploy things to:

# Verify that you have juju:
juju

# Verify that you have a Kubernetes and a machine cloud
# and they're already known to juju:
juju clouds

# Verify that you already have a controller bootstrapped into each:
juju controllers

# Switch to the preexisting workload model on the controller:
## For the MicroK8s cloud:
ubuntu@my-juju-vm:~$ juju switch microk8s:welcome-k8s

## For the LXD cloud:
ubuntu@my-juju-vm:~$ juju switch lxd:welcome-lxd
  1. (If you are developing a charm or planning to also use a different Juju client:)

3a. Ensure you have all the necessary tools.

Example: Charming tools
# Verify that you have Charmcraft:
ubuntu@my-juju-vm:~$ charmcraft

# Verify that you have a version of Python that meets the requirements for Ops:
ubuntu@my-juju-vm:~$ python3 --version

# Take stock of ay other pre-installed Python packages:
ubuntu@my-juju-vm:~$ pip list # should show, e.g., requests, tox, toml, virtualenv

# Install anything else that's missing, e.g., docker:
ubuntu@my-juju-vm:~$ sudo addgroup --system docker
ubuntu@my-juju-vm:~$ sudo adduser $USER docker
ubuntu@my-juju-vm:~$ newgrp docker
ubuntu@my-juju-vm:~$ sudo snap install docker

3b. On your workstation, create a directory for your files, then mount it to your Ubuntu VM:

Example: Create and mount a charm directory
# Create the charm directory:
$ mkdir ~/my-charm

# Mount it to the Multipass VM:
$ multipass mount --type native ~/my-charm charm-dev-vm:~/my-charm

# Verify that it's indeed on the VM:
ubuntu@charm-dev-vm:~$ ls
my-charm  snap

# Going forward:
# - Use your host machine (on Linux, `cd ~/my-charm`) to create and edit your charm files. This will allow you to use your favorite local editor.
# - Use the Multipass VM shell (on Linux, `ubuntu@charm-dev-vm:~$ cd ~/my-charm`) to run Charmcraft and Juju commands.

Tear things down

Delete the Multipass VM:

$ multipass delete --purge my-juju-vm

Uninstall Multipass.