|
| 1 | +Scripts to test SLS changes in podman containers |
| 2 | +------------------- |
| 3 | + |
| 4 | +The goal is to cover following workflow: |
| 5 | + |
| 6 | +* Change files related to an individual state. |
| 7 | +* Spawn a container with a local standalone salt node and apply the states. |
| 8 | +* Check basic bash commands to verify outcome. |
| 9 | + |
| 10 | +The test is set of bash commands. |
| 11 | +The script relies on shebang to prepare an image and spawn a container with the sls files. |
| 12 | +It is also ready to test salt states using set of commands `salt-call --local`. |
| 13 | + |
| 14 | +###### Example: Run test for mysql states: |
| 15 | + |
| 16 | +```bash |
| 17 | +cd t |
| 18 | +./01-smoke-server.sh |
| 19 | +``` |
| 20 | + |
| 21 | +#### Challenge 1: By default, a container is destroyed when the test finishes. |
| 22 | + |
| 23 | +This is to simplify re-run of tests and do not flood machine with leftover containers after tests. |
| 24 | +To make sure container stays around after faiure - set environment variable *T_PAUSE_ON_FAILURE* to 1 |
| 25 | + |
| 26 | +###### Example: Connect to the container after test failure |
| 27 | + |
| 28 | +```bash |
| 29 | +> # terminal 1 |
| 30 | +> echo fail >> 01-smoke-server.sh |
| 31 | +> T_PAUSE_ON_FAILURE=1 ./01-smoke-server.sh |
| 32 | +... |
| 33 | +bash: line 18: fail: command not found |
| 34 | +Test failed, press any key to finish |
| 35 | +``` |
| 36 | +The terminal will wait for any input to finish the test and clean up the container. |
| 37 | +Now use another terminal window to check the running podman container and get into it for eventual troubleshooting: |
| 38 | + |
| 39 | +```bash |
| 40 | +> # terminal 2 |
| 41 | +> podman ps |
| 42 | +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
| 43 | +2a37d23503fa localhost/mysql.formula.t38c8b1d778efa61c676f1aa99b2aded5.image:latest 4 minutes ago Up 4 minutes ago mysql.formula.t38c8b1d778efa61c676f1aa99b2aded5.01-smoke-server.sh |
| 44 | +> # use copy container id to start bash in it (hint: or bash completion should work for container name as well) |
| 45 | +> podman exec -it mysql.formuls.t38c8b1d778efa61c676f1aa99b2aded5.01-smoke-server.sh bash |
| 46 | +2a37d23503fa:/opt/project # ls |
| 47 | +bin encrypted_pillar_recipients ENCRYPTION.md FORMULAS.yaml gpgkeys pillar README.md salt t test |
| 48 | +2a37d23503fa:/opt/project # # now we are inside the container and can troubleshoot outcome of salt commands |
| 49 | +2a37d23503fa:/opt/project # rcmysql status |
| 50 | +* mariadb.service - MariaDB database server |
| 51 | + Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled) |
| 52 | + Active: active (running) since Fri 2023-03-17 12:45:24 UTC; 10min ago |
| 53 | +``` |
| 54 | + |
| 55 | +#### Challenge 2: Vary OS in container. |
| 56 | + |
| 57 | +Create new file Dockerfile.%osname% similar to existing Docker files in t/lib. |
| 58 | +Use environment variable T_IMAGE=%osname% to let scripts use corresponding Dockerfile. |
| 59 | +By default tests are run with t/Dockerfile.opensuse.leap |
| 60 | + |
| 61 | +#### Challenge 3: Cache packages inside test image. |
| 62 | + |
| 63 | +(Currently works only with default docker image, i.e. T_IMAGE is empty). |
| 64 | +Downloading and installing packages may be time consuming, so sometimes it may be advantageous to have an option to pre-install required packages inside image, in which the test will be run. (So the test will concentrate on verifying other aspects of salt states, without spending time on installing packages). |
| 65 | +At the same time the CI needs to verify salt states related to installation, so it must run the test without such caching. |
| 66 | +Such caching is implemented as optional parameters to shebang command in the test scripts. These parameters are ignored unless global variable *T_CACHE_PACKAGES* is set to 1. |
| 67 | + |
| 68 | +```bash |
| 69 | +> # check parameter in shebang: |
| 70 | +> head -n 1 01-smoke-server.sh |
| 71 | +#!lib/test-in-container-systemd.sh mariadb |
| 72 | +> # run the test in a container with preinstalled mariadb package as specified above: |
| 73 | +> T_CACHE_PACKAGES=1 ./01-smoke-server.sh |
| 74 | +> # run the test in a container without preinstalled mariadb package (will take longer, but will verify package installation as part of test) |
| 75 | +> ./01-smoke-server.sh |
| 76 | +``` |
| 77 | + |
0 commit comments