mirror of
https://github.com/gofiber/storage.git
synced 2025-10-05 16:48:25 +08:00
chore: add tests for the helper module
This commit is contained in:
36
.github/workflows/test-teshelpers-redis.yml
vendored
Normal file
36
.github/workflows/test-teshelpers-redis.yml
vendored
Normal file
@@ -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
|
192
testhelpers/redis/redis_test.go
Normal file
192
testhelpers/redis/redis_test.go
Normal file
@@ -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)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user