Is it possible to run a command in a Docker image as a test in Bazel?

Viewed 303
1 Answers
  • Take a look at the sample test rule here
    • This is a test rule which creates a script (script) that can be invoked in the CLI
    • The script will then exit with a non-zero error-code to indicate that the test failed (or 0 for success)
    • The script is then written as an executable output (ctx.actions.write), declares the list of files it needs available at runtime (runfiles)
    • This python function is then wrapped as a bazel rule (see full guide here)
  • So, how would you proceed towards creating your container test rule?
    1. The script we want to generate above is probably some usage of docker run --rm IMAGE [COMMAND] [ARG...] to create a container from an image, run a command, and remove the container when done
      • Don't forget to set the script exit status based on the exit status of the docker command (as done in the example, where they copy the exit status of grep as the exit status for the overall script)
    2. Update the sample above to use the above docker command, and plant the path to the image accordingly
      • See f.path in the script above showing how they access the path of an individual source file
      • You will need to make sure docker is available when your bazel rules are evaluated

I haven't done this fully myself since I don't have a computer with both bazel and docker, but this should be enough to get you started :)

Good luck!

Related