|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Announcing TestInfra Action" |
| 4 | +date: 2025-03-31 13:37dw |
| 5 | +comments: true |
| 6 | +categories: |
| 7 | +- GitHub Actions |
| 8 | +- Github |
| 9 | +- Docker |
| 10 | +- Testing |
| 11 | +tags: |
| 12 | +- GitHub Actions |
| 13 | +- Github |
| 14 | +- Docker |
| 15 | +- Testing |
| 16 | +- OCI |
| 17 | +social: |
| 18 | + image_relative: /images/test-infra-output.png |
| 19 | +--- |
| 20 | + |
| 21 | +One of the key parts of [`Building Secure Images with GitHub Actions`](https://blog.wyrihaximus.net/2024/10/building-secure-images-with-github-actions/) |
| 22 | +is unit testing the image just build. While for most of my repositories this has been some bash script in the repo that |
| 23 | +I copied to the next new repo. Always had the desire to make something more clean than that, this action is the outcome |
| 24 | +of that desire. |
| 25 | + |
| 26 | +So here it is the: [`TestInfra Action`](https://github.com/marketplace/actions/testinfra-action). |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +<!-- More --> |
| 31 | + |
| 32 | +# Requirements |
| 33 | + |
| 34 | +With GitHub just barely offering `arm` runners recently, while `x64` runners have been the only option until then. |
| 35 | +(Unless you get the runner software running beyond those two, or use the self hosted Kubernetes runner with only |
| 36 | +supports `arm` and `x64`.) Creating images for both isn’t complicated, and covered in |
| 37 | +[`Building Secure Images with GitHub Actions`](https://blog.wyrihaximus.net/2024/10/building-secure-images-with-github-actions/), |
| 38 | +but to keep maintenance as simple as possible for future CPU architectures I’ve started working on |
| 39 | +[`wyrihaximus/github-action-oci-image-supported-platforms`](https://github.com/WyriHaximus/github-action-oci-image-supported-platforms). |
| 40 | +As a result of that action I don’t have to manually update a matrix for any new CPU arch in the future, it just builds |
| 41 | +all those that are also available upstream. While all of this is no direct requirement for the action, it is for the |
| 42 | +OCI image the action utilizes to run [TestInfra](https://testinfra.readthedocs.io/en/latest/). And as a result of that, |
| 43 | +this action inherits all the CPU archs that image can run on, and is tested on by itself. |
| 44 | + |
| 45 | +Another major requirement is that it should be easy to use. This resulted in some challenges but was successfully achieved. |
| 46 | + |
| 47 | +# Challenges |
| 48 | + |
| 49 | +The biggest issue I had while creating this action was to keep the container running. Not because it was hard to do, |
| 50 | +but because I head to realise it depends on each image if it keeps running after started or not. The |
| 51 | +[original](https://github.com/usabilla/php-docker-template/blob/1e379cfdb90f9b03b4e4e4c6b7212134091040ce/test-cli.sh#L24) |
| 52 | +I took this from, [copied](https://github.com/WyriHaximusNet/docker-php/blob/345e0fca8d7b2099fef71af2c244c7b70f800107/test-zts.sh#L42) |
| 53 | +over again and again, and then taking the script and putting it in this action; took care of that. This is why the |
| 54 | +action has a `cmd` for image that require that. Something that keeps running like `PHP` or `nodejs` REPL is good enough. |
| 55 | + |
| 56 | +The coming challenge is going to be updating my [PHP Docker](https://github.com/WyriHaximusNet/docker-php) workflow. |
| 57 | +It’s build to test all different tags build in a single build run, this action isn’t very keep on that. It can only |
| 58 | +handle a single test suite for the images you pass it. So no looping or any of that, going to have to do a major |
| 59 | +overhaul of that workflow. But hopefully this refactor is one I can use in my other image building workflows until the |
| 60 | +point I can have one general one as a reusable workflow. |
| 61 | + |
| 62 | +# Conclusion |
| 63 | + |
| 64 | +All of this make the action come down to invocation is, taking resolved variables for |
| 65 | +[`WyriHaximusNet/docker-github-action-runner`](https://github.com/WyriHaximusNet/docker-github-action-runner): |
| 66 | + |
| 67 | +```yaml |
| 68 | +- uses: WyriHaximus/github-action-testinfra@v1 |
| 69 | + with: |
| 70 | + image: wyrihaximusnet/github-action-runner:linux-amd64 |
| 71 | + flags: --platform="linux/arm64" |
| 72 | + cmd: node |
| 73 | + testsPath: test |
| 74 | +``` |
| 75 | +
|
| 76 | +Which will run the following tests in `test/test_node20.py`: |
| 77 | + |
| 78 | +```python |
| 79 | +import pytest |
| 80 | +
|
| 81 | +def test_node20(host): |
| 82 | + assert 'v20.' in host.run('node -v').stdout |
| 83 | +``` |
| 84 | + |
| 85 | +Obviously there are more tests in that repository, and several times more in |
| 86 | +[`WyriHaximusNet/docker-php`](https://github.com/WyriHaximusNet/docker-php/tree/master/test/container). |
| 87 | + |
| 88 | +This action is going to make maintaining and ensuring functionality of my own images a whole lot easier, and I hope it |
| 89 | +will helps others as well. |
0 commit comments