In case you want to use an unsupported Packer provisioner like testinfra, you need to make it work with “shell” or “shell (local)”, but you will soon have a dilemma:
You would prefer to run it with “shell (local)” from your build machine and get easy access to your junit log files, but Packer documentation makes you believe you can only run local jobs
You can also use “shell”, but then you need to upload your tests, install the dependencies, run your tests, fetch your test results back and then cleanup after yourself. It seems like a lot of unnecessary extra steps.
Luckily, there are some contextual build variables available, but Hashicorp managed to hide them in a completely random documentation location. You would need usually {{build `SSHPrivateKey`}}, {{build `User`}} and {{build `Host`}} to get access to the temporary ssh private key that packer is using, the temporary host IP and the username used by packer.
Getting back to our testinfra example, then you could do something like this:
{ "type": "shell-local", "inline": [ "echo '{{build `SSHPrivateKey`}}' > packer_rsa", "chmod 600 packer_rsa", "pytest --ssh-identity-file=packer_rsa --ssh-config=ssh_config \ --hosts=ssh://{{build `User`}}@{{build `Host`}} \ --junit-xml junit.xml testinfra_test.py" ] }
where in ssh_config you may want to disable StrictHostKeyChecking, as with every build you’ll get a new HostKey.