From a77e60ff9fbe61f86b94c62fcf6d9bda51a91c10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 30 Apr 2025 14:40:41 +0200 Subject: [PATCH] chore: add tests for the helper module --- .github/workflows/test-teshelpers-redis.yml | 36 ++++ testhelpers/redis/redis_test.go | 192 ++++++++++++++++++++ 2 files changed, 228 insertions(+) create mode 100644 .github/workflows/test-teshelpers-redis.yml create mode 100644 testhelpers/redis/redis_test.go diff --git a/.github/workflows/test-teshelpers-redis.yml b/.github/workflows/test-teshelpers-redis.yml new file mode 100644 index 00000000..d8a5e0c6 --- /dev/null +++ b/.github/workflows/test-teshelpers-redis.yml @@ -0,0 +1,36 @@ +on: + push: + branches: + - master + - main + paths: + - 'testhelpers/redis/**' + pull_request: + paths: + - 'testhelpers/redis/**' +name: "Tests TestHelper Redis" +jobs: + Tests: + runs-on: ubuntu-latest + strategy: + matrix: + go-version: + - 1.23.x + - 1.24.x + redis: + - '6' + - '7' + steps: + - name: Fetch Repository + uses: actions/checkout@v4 + + - name: Install Go + uses: actions/setup-go@v5 + with: + go-version: '${{ matrix.go-version }}' + + - name: Run Test + env: + TEST_REDIS_IMAGE: "docker.io/redis:${{ matrix.redis }}" + working-directory: testhelpers/redis + run: go test ./... -v -race diff --git a/testhelpers/redis/redis_test.go b/testhelpers/redis/redis_test.go new file mode 100644 index 00000000..7e04fdf6 --- /dev/null +++ b/testhelpers/redis/redis_test.go @@ -0,0 +1,192 @@ +package testredis + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestStart(t *testing.T) { + t.Run("default-configuration", func(t *testing.T) { + ctr := Start(t) + require.NotEmpty(t, ctr.URL) + require.True(t, strings.HasPrefix(ctr.URL, "redis://")) + require.Nil(t, ctr.TLSConfig) + require.Empty(t, ctr.Addrs) + require.Empty(t, ctr.Host) + require.Zero(t, ctr.Port) + }) + + t.Run("with-custom-image", func(t *testing.T) { + customImage := "docker.io/redis:6" + ctr := Start(t, WithImage(customImage)) + require.True(t, strings.HasPrefix(ctr.URL, "redis://")) + require.Nil(t, ctr.TLSConfig) + require.Empty(t, ctr.Addrs) + require.Empty(t, ctr.Host) + require.Zero(t, ctr.Port) + }) + + t.Run("with-image-from-env", func(t *testing.T) { + envImage := "docker.io/redis:7" + t.Setenv(ImageEnvVar, envImage) + + ctr := Start(t) + require.True(t, strings.HasPrefix(ctr.URL, "redis://")) + require.Nil(t, ctr.TLSConfig) + require.Empty(t, ctr.Addrs) + require.Empty(t, ctr.Host) + require.Zero(t, ctr.Port) + }) + + t.Run("with-tls", func(t *testing.T) { + t.Run("secure-url", func(t *testing.T) { + t.Run("mtls-disabled", func(t *testing.T) { + t.Parallel() + + ctr := Start(t, WithTLS(true, false)) + require.NotEmpty(t, ctr.URL) + require.True(t, strings.HasPrefix(ctr.URL, "rediss://")) + require.NotNil(t, ctr.TLSConfig) + require.Empty(t, ctr.Addrs) + require.Empty(t, ctr.Host) + require.Zero(t, ctr.Port) + }) + + t.Run("mtls-enabled", func(t *testing.T) { + t.Parallel() + + ctr := Start(t, WithTLS(true, true)) + require.NotEmpty(t, ctr.URL) + require.True(t, strings.HasPrefix(ctr.URL, "rediss://")) + require.NotNil(t, ctr.TLSConfig) + }) + }) + + t.Run("insecure-url", func(t *testing.T) { + t.Run("mtls-disabled", func(t *testing.T) { + t.Parallel() + + ctr := Start(t, WithTLS(false, true)) + require.NotEmpty(t, ctr.URL) + require.True(t, strings.HasPrefix(ctr.URL, "redis://")) + require.NotNil(t, ctr.TLSConfig) + require.Empty(t, ctr.Addrs) + require.Empty(t, ctr.Host) + require.Zero(t, ctr.Port) + }) + + t.Run("mtls-enabled", func(t *testing.T) { + t.Parallel() + + ctr := Start(t, WithTLS(false, false)) + require.NotEmpty(t, ctr.URL) + require.True(t, strings.HasPrefix(ctr.URL, "redis://")) + require.NotNil(t, ctr.TLSConfig) + require.Empty(t, ctr.Addrs) + require.Empty(t, ctr.Host) + require.Zero(t, ctr.Port) + }) + }) + }) + + t.Run("with-host-and-port", func(t *testing.T) { + t.Parallel() + + ctr := Start(t, WithHostPort()) + require.NotEmpty(t, ctr.Host) + require.NotZero(t, ctr.Port) + require.NotEmpty(t, ctr.URL) + require.Empty(t, ctr.Addrs) + }) + + t.Run("with-address", func(t *testing.T) { + t.Parallel() + + ctr := Start(t, WithAddress()) + require.NotEmpty(t, ctr.Addrs) + require.Len(t, ctr.Addrs, 1) + require.NotEmpty(t, ctr.URL) + require.Empty(t, ctr.Host) + require.Zero(t, ctr.Port) + }) + + t.Run("with-url", func(t *testing.T) { + t.Parallel() + + ctr := Start(t, WithURL(true)) + require.NotEmpty(t, ctr.URL) + require.True(t, strings.HasPrefix(ctr.URL, "redis://")) + }) + + t.Run("with-multiple-options", func(t *testing.T) { + t.Run("address/url", func(t *testing.T) { + t.Run("no-tls", func(t *testing.T) { + t.Parallel() + + ctr := Start(t, + WithAddress(), + WithURL(true), + ) + require.NotEmpty(t, ctr.URL) + require.True(t, strings.HasPrefix(ctr.URL, "redis://")) + require.NotEmpty(t, ctr.Addrs) + require.Len(t, ctr.Addrs, 1) + require.Empty(t, ctr.Host) + require.Zero(t, ctr.Port) + }) + + t.Run("tls", func(t *testing.T) { + t.Parallel() + + ctr := Start(t, + WithTLS(true, false), + WithAddress(), + WithURL(true), + ) + require.NotEmpty(t, ctr.URL) + require.True(t, strings.HasPrefix(ctr.URL, "rediss://")) + require.NotEmpty(t, ctr.Addrs) + require.Len(t, ctr.Addrs, 1) + require.Empty(t, ctr.Host) + require.Zero(t, ctr.Port) + }) + }) + }) +} + +func TestConfig(t *testing.T) { + t.Run("with-tls", func(t *testing.T) { + config := &Config{} + WithTLS(true, false)(config) + require.True(t, config.UseTLS) + require.True(t, config.SecureURL) + require.False(t, config.MTLSDisabled) + }) + + t.Run("with-address", func(t *testing.T) { + config := &Config{} + WithAddress()(config) + require.True(t, config.UseAddress) + }) + + t.Run("with-host-port", func(t *testing.T) { + config := &Config{} + WithHostPort()(config) + require.True(t, config.UseHostPort) + }) + + t.Run("with-url", func(t *testing.T) { + config := &Config{} + WithURL(true)(config) + require.True(t, config.UseURL) + }) + + t.Run("with-image", func(t *testing.T) { + customImage := "docker.io/redis:6" + config := &Config{} + WithImage(customImage)(config) + require.Equal(t, customImage, config.Image) + }) +}