3

I was searching for a build system for my Go projects. In the Java world, things look much easier. You have maven and it's so easy to make test/integration test and package the project.

I'm trying to find the solution for starting Redis in docker then run package integration tests and finally stop the Redis.

I don't have problems with the test rule:

go_test(
    name = "go_default_test",
    srcs = ["person_cache_integration_test.go"],
    embed = [":go_default_library"],
    deps = [
        "//internal/models:go_default_library",
        "@com_github_stretchr_testify//assert:go_default_library",
    ],
)

but how can I start Redis in Docker before this rule and stop Redis in any case after successful or fail tests?

Thanks.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
Max Grigoriev
  • 1,021
  • 2
  • 13
  • 29
  • Is your problem more that you need a way to clean up in the event of failure than that starting it is hard? You could just have the test start the container as a part of the test binary itself. – robsiemb Oct 15 '19 at 23:12
  • I did it because I couldn't find the solution with bazel. – Max Grigoriev Oct 19 '19 at 11:22

1 Answers1

1

If your usecase is really just testing I recommend testcontainers. This way your tests don’t depend on the build system and can be locally executed as well as in a CI system.

Here is a go implementation: https://github.com/testcontainers/testcontainers-go

I am not sure if that would work with bazel remote build, but for a typical project it should be fine.

Thomas
  • 11,272
  • 2
  • 24
  • 40
  • 2
    Yes, it's nice solution. I started to use https://github.com/ory/dockertest but it will be nice if I can declare rules in Bazel and don't put it to code. – Max Grigoriev Oct 19 '19 at 11:21
  • Testcontainers was a bad fit for me as it requires my GitLab CI runner (Kubernetes in this case) to run containers in priveleged mode as it would use docker-in-docker. Just a consideration if your company does not allow priveleged mode containers for security reasons. – OscarVanL Jan 09 '23 at 21:33