Skip to content

Commit e859473

Browse files
committed
Switch adhoc backends to use same script
1 parent 099eb90 commit e859473

File tree

3 files changed

+83
-43
lines changed

3 files changed

+83
-43
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ To run the whole system via Terraform, juju and charms simulating production and
5555

5656
A charmed Test Observer deployment can be integration tested using `charmcraft test` (which itself internally uses a classically confined copy of [spread](https://github.com/canonical/spread) bundled with `charmcraft`).
5757

58-
The integration tests are runnable either locally (using [lxd](https://documentation.ubuntu.com/lxd/en/latest/installing/) at the time of writing) or using GitHub. Alternative test execution backends are configurable by adding new `adhoc` backends next to `lxd` and `github` in the [spread.yaml](./spread.yaml).
58+
The integration tests are runnable either locally or using GitHub. Alternative test execution backends are configurable by adding new `adhoc` backends next to `lxd-vm` and `localhost` in the [spread.yaml](./spread.yaml).
5959

6060
### Run integration tests locally
6161

6262
Tests can be run locally with either `charmcraft test lxd` or `charmcraft.spread lxd` (the latter gives a bit more verbose log output).
6363

6464
```bash
65-
charmcraft.spread -v lxd
65+
charmcraft.spread -v lxd-vm
6666
```
6767

6868
### Jump into a `spread` provided shell environment
@@ -71,11 +71,11 @@ To debug the charm integration tests, you can jump into a `spread` provided envi
7171

7272
```bash
7373
# open a shell to the execution environment _before_ executing each test task
74-
charmcraft.spread -v -shell-before lxd
74+
charmcraft.spread -v -shell-before lxd-vm
7575

7676
# open a shell to the execution environment _instead of_ executing each test task
77-
charmcraft.spread -v -shell lxd
77+
charmcraft.spread -v -shell lxd-vm
7878

7979
# open a shell to the execution environment _after_ of executing each test task
80-
charmcraft.spread -v -shell-after lxd
80+
charmcraft.spread -v -shell-after lxd-vm
8181
```

spread.yaml

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
project: test-observer-charm-tests
22

3-
# This is the path the project is copied to on the worker systems
4-
# and it is set to the SPREAD_PATH environment variable after allocation
5-
# It must be under /root because spread executes the test scripts
6-
# as root, which means that juju can only see files in root's
7-
# home directory due to snap confinement.
8-
path: /root/project
9-
10-
exclude:
11-
- .coverage
12-
- .git
13-
- .github
14-
- .pytest_cache
15-
- .ruff_cache
16-
- .tox
17-
- .venv
18-
193
backends:
204
# The LXD backend can hit this error, so we define our own lxd backend
215
# https://github.com/canonical/spread/issues/232
@@ -37,25 +21,20 @@ backends:
3721
# so we replace everything else with a hyphen
3822
NAME="$(echo "${NAME}" | sed 's/[^a-zA-Z0-9-]/-/g')"
3923
24+
RUN_UPDATE_SCRIPT="false"
4025
EXISTS="$(lxc list --format csv --columns n name="${NAME}")"
4126
if [[ -z "${EXISTS}" ]]; then
4227
# Get the image portion, e.g. 24.04 from ubuntu-24.04
4328
IMAGE="$(echo "${SPREAD_SYSTEM}" | cut -d '-' -f 2)"
4429
45-
CLOUD_INIT_USER_DATA="$(mktemp)"
46-
cp spread/cloud-init-user-data.yaml "${CLOUD_INIT_USER_DATA}"
47-
sed -i "s|SPREAD_REPLACE_USERNAME|${SPREAD_SYSTEM_USERNAME}|g" "${CLOUD_INIT_USER_DATA}"
48-
sed -i "s|SPREAD_REPLACE_PASSWORD|${SPREAD_PASSWORD}|g" "${CLOUD_INIT_USER_DATA}"
49-
5030
# We need to use the ubuntu: remote, because it provides Ubuntu Server images,
5131
# which work with cloud-init. Ubuntu Desktop images do not.
5232
lxc launch --vm "ubuntu:${IMAGE}" "${NAME}" \
53-
--config cloud-init.user-data="$(cat "${CLOUD_INIT_USER_DATA}")" \
5433
--config limits.cpu="${LIMIT_CPU_CORES}" \
5534
--config limits.memory="${LIMIT_MEMORY_GIB}GiB" \
5635
--device root,size="${ROOT_DISK_GIB}GiB"
5736
58-
rm "${CLOUD_INIT_USER_DATA}"
37+
RUN_UPDATE_SCRIPT="true"
5938
fi
6039
6140
# Wait for the VM to come up and have an IP address
@@ -72,6 +51,17 @@ backends:
7251
sleep 5
7352
done
7453
54+
if [[ "${RUN_UPDATE_SCRIPT}" == "true" ]]; then
55+
UPDATE_SCRIPT="spread/update-adhoc-system.sh"
56+
cp spread/update-adhoc-system-template.sh "${UPDATE_SCRIPT}"
57+
sed -i "s|SPREAD_REPLACE_USERNAME|${SPREAD_SYSTEM_USERNAME}|g" "${UPDATE_SCRIPT}"
58+
sed -i "s|SPREAD_REPLACE_PASSWORD|${SPREAD_PASSWORD}|g" "${UPDATE_SCRIPT}"
59+
60+
lxc file push "${UPDATE_SCRIPT}" "${NAME}"/root/update-adhoc-system.sh
61+
lxc exec "${NAME}" -- bash /root/update-adhoc-system.sh allocate
62+
rm "${UPDATE_SCRIPT}"
63+
fi
64+
7565
# adhoc backends require the ADDRESS command provided by spread
7666
# to set the SSH address
7767
ADDRESS "${address}"
@@ -99,26 +89,34 @@ backends:
9989
# it should only run when explicitly requested
10090
manual: true
10191
allocate: |
102-
sudo sed -i "s|#PasswordAuthentication yes|PasswordAuthentication yes|g" /etc/ssh/sshd_config
103-
sudo sed -i "s|KbdInteractiveAuthentication no|KbdInteractiveAuthentication yes|g" /etc/ssh/sshd_config
104-
sudo cp /etc/ssh/sshd_config.d/60-cloudimg-settings.conf /etc/ssh/sshd_config.d/60-cloudimg-settings.conf.bak || true
105-
sudo rm -f /etc/ssh/sshd_config.d/60-cloudimg-settings.conf || true
106-
sudo systemctl daemon-reload
107-
sudo systemctl restart ssh
92+
UPDATE_SCRIPT="spread/update-adhoc-system.sh"
93+
cp spread/update-adhoc-system-template.sh "${UPDATE_SCRIPT}"
94+
sed -i "s|SPREAD_REPLACE_USERNAME|${SPREAD_SYSTEM_USERNAME}|g" "${UPDATE_SCRIPT}"
95+
sed -i "s|SPREAD_REPLACE_PASSWORD|${SPREAD_PASSWORD}|g" "${UPDATE_SCRIPT}"
10896
109-
sudo useradd ${SPREAD_SYSTEM_USERNAME} --shell /bin/bash --create-home || true
110-
echo "${SPREAD_SYSTEM_USERNAME}:${SPREAD_PASSWORD}" | sudo chpasswd || true
111-
echo "${SPREAD_SYSTEM_USERNAME} ALL=(ALL) NOPASSWD:ALL " | sudo tee /etc/sudoers.d/99-${SPREAD_SYSTEM_USERNAME}-user || true
97+
bash "${UPDATE_SCRIPT}" allocate
11298
11399
ADDRESS "127.0.0.1"
114100
115101
discard: |
116-
sudo userdel -f -r ${SPREAD_SYSTEM_USERNAME} || true
117-
sudo rm -f /etc/sudoers.d/99-${SPREAD_SYSTEM_USERNAME}-user || true
118-
sudo rm -f /etc/ssh/sshd_config.d/60-cloudimg-settings.conf || true
119-
sudo mv /etc/ssh/sshd_config.d/60-cloudimg-settings.conf.bak /etc/ssh/sshd_config.d/60-cloudimg-settings.conf || true
120-
sudo sed -i "s|KbdInteractiveAuthentication yes|KbdInteractiveAuthentication no|g" /etc/ssh/sshd_config
121-
sudo sed -i "s|PasswordAuthentication yes|#PasswordAuthentication yes|g" /etc/ssh/sshd_config
102+
bash spread/update-adhoc-system.sh discard
103+
rm spread/update-adhoc-system.sh
104+
105+
# This is the path the project is copied to on the backend systems
106+
# once they are allocated. It is set to the SPREAD_PATH environment variable.
107+
# It must be under /root because spread executes the test scripts
108+
# as root, which means that the juju snap can only see files in root's
109+
# home directory due to snap confinement.
110+
path: /root/project
111+
112+
exclude:
113+
- .coverage
114+
- .git
115+
- .github
116+
- .pytest_cache
117+
- .ruff_cache
118+
- .tox
119+
- .venv
122120

123121
environment:
124122
CONCIERGE_JUJU_CHANNEL: 3.6/stable
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
# Check if exactly one argument was provided
4+
if [ "$#" -ne 1 ]; then
5+
echo "Usage: $0 <allocate|discard>"
6+
exit 1
7+
fi
8+
9+
SPREAD_SYSTEM_USERNAME=SPREAD_REPLACE_USERNAME
10+
SPREAD_PASSWORD=SPREAD_REPLACE_PASSWORD
11+
12+
MODE=$1
13+
case "${MODE}" in
14+
"allocate")
15+
sudo sed -i "s|#PasswordAuthentication yes|PasswordAuthentication yes|g" /etc/ssh/sshd_config
16+
sudo sed -i "s|KbdInteractiveAuthentication no|KbdInteractiveAuthentication yes|g" /etc/ssh/sshd_config
17+
sudo cp /etc/ssh/sshd_config.d/60-cloudimg-settings.conf /etc/ssh/sshd_config.d/60-cloudimg-settings.conf.bak || true
18+
sudo rm -f /etc/ssh/sshd_config.d/60-cloudimg-settings.conf || true
19+
sudo systemctl daemon-reload
20+
sudo systemctl restart ssh
21+
22+
sudo useradd ${SPREAD_SYSTEM_USERNAME} --shell /bin/bash --create-home || true
23+
echo "${SPREAD_SYSTEM_USERNAME}:${SPREAD_PASSWORD}" | sudo chpasswd || true
24+
echo "${SPREAD_SYSTEM_USERNAME} ALL=(ALL) NOPASSWD:ALL " | sudo tee /etc/sudoers.d/99-${SPREAD_SYSTEM_USERNAME}-user || true
25+
;;
26+
27+
"discard")
28+
sudo userdel -f -r ${SPREAD_SYSTEM_USERNAME} || true
29+
sudo rm -f /etc/sudoers.d/99-${SPREAD_SYSTEM_USERNAME}-user || true
30+
sudo rm -f /etc/ssh/sshd_config.d/60-cloudimg-settings.conf || true
31+
sudo mv /etc/ssh/sshd_config.d/60-cloudimg-settings.conf.bak /etc/ssh/sshd_config.d/60-cloudimg-settings.conf || true
32+
sudo sed -i "s|KbdInteractiveAuthentication yes|KbdInteractiveAuthentication no|g" /etc/ssh/sshd_config
33+
sudo sed -i "s|PasswordAuthentication yes|#PasswordAuthentication yes|g" /etc/ssh/sshd_config
34+
;;
35+
36+
*)
37+
echo "Invalid mode: ${MODE}. Use 'allocate' or 'discard'."
38+
exit 1
39+
;;
40+
esac
41+
42+
exit 0

0 commit comments

Comments
 (0)