Merged in feature/testingopts (pull request #129)
Testing Options and Clean Up * bitsandbobs * lint * healthchecks * imgintests * singlegenerate * testclean * network * networkclean * cmds
This commit is contained in:
@@ -12,7 +12,7 @@ import (
|
||||
func TestCreateAPI(t *testing.T) {
|
||||
t.Parallel()
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := t.Context()
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ type TestAWSConfig struct {
|
||||
func TestCreateQueueContainer(t *testing.T) {
|
||||
t.Parallel()
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := context.Background()
|
||||
cfg := &TestAWSConfig{}
|
||||
|
||||
@@ -5,12 +5,16 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/gruntwork-io/terratest/modules/shell"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
@@ -31,6 +35,48 @@ type containerConfig struct {
|
||||
MockHTTP string
|
||||
}
|
||||
|
||||
const (
|
||||
imageTag = "queryorchestration:latest"
|
||||
)
|
||||
|
||||
var (
|
||||
imageBuild sync.Once
|
||||
)
|
||||
|
||||
func buildImage(t testing.TB) {
|
||||
imageBuild.Do(func() {
|
||||
dockerContextPath, err := os.Getwd()
|
||||
require.NoError(t, err, "Failed to get current directory")
|
||||
|
||||
dockerfilePath := "build/Dockerfile"
|
||||
|
||||
for {
|
||||
candidatePath := filepath.Join(dockerContextPath, dockerfilePath)
|
||||
|
||||
t.Logf("Checking for Dockerfile at: %s", dockerContextPath)
|
||||
if _, err := os.Stat(candidatePath); err == nil {
|
||||
dockerfilePath = candidatePath
|
||||
t.Logf("Found Dockerfile at: %s", candidatePath)
|
||||
break
|
||||
}
|
||||
|
||||
dockerContextPath = filepath.Join(dockerContextPath, "..")
|
||||
}
|
||||
|
||||
dockerBuildCmd := shell.Command{
|
||||
Command: "docker",
|
||||
Args: []string{
|
||||
"build",
|
||||
"--file", dockerfilePath,
|
||||
"--tag", imageTag,
|
||||
dockerContextPath,
|
||||
},
|
||||
}
|
||||
err = shell.RunCommandE(t, dockerBuildCmd)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func createContainer(t testing.TB, ctx context.Context, network string, cfg *containerConfig) (testcontainers.Container, func()) {
|
||||
env := map[string]string{
|
||||
"PGUSER": cfg.Cfg.GetDBUser(),
|
||||
@@ -65,8 +111,10 @@ func createContainer(t testing.TB, ctx context.Context, network string, cfg *con
|
||||
env[string(GetRunnerEnvFromName(e))] = GetQueueURL(t, cfg.Cfg, e)
|
||||
}
|
||||
|
||||
buildImage(t)
|
||||
|
||||
req := testcontainers.ContainerRequest{
|
||||
Image: "queryorchestration:latest",
|
||||
Image: imageTag,
|
||||
Env: env,
|
||||
WaitingFor: wait.ForLog(cfg.WaitForMsg),
|
||||
Entrypoint: []string{fmt.Sprintf("./%s", cfg.Name)},
|
||||
|
||||
@@ -5,23 +5,39 @@ import (
|
||||
"testing"
|
||||
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/gruntwork-io/terratest/modules/docker"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBuildImage(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.SkipNow()
|
||||
}
|
||||
buildImage(t)
|
||||
|
||||
exists := docker.DoesImageExist(t, imageTag, nil)
|
||||
assert.True(t, exists)
|
||||
}
|
||||
|
||||
type Cfg struct {
|
||||
serviceconfig.BaseConfig
|
||||
objectstore.ObjectStoreConfig
|
||||
}
|
||||
|
||||
func TestCreateContainer(t *testing.T) {
|
||||
t.Parallel()
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg := &Cfg{}
|
||||
|
||||
net := DepNetwork.Get(t, ctx)
|
||||
|
||||
_ = CreateDB(t, ctx, cfg, net, &CreateDatabaseConfig{NoMigrations: true})
|
||||
deps, clean := CreateFullDependencies(t, ctx, cfg)
|
||||
defer clean()
|
||||
|
||||
ccfg := &containerConfig{
|
||||
Name: string(QueryAPIName),
|
||||
@@ -32,7 +48,7 @@ func TestCreateContainer(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
container, cleanup := createContainer(t, ctx, net, ccfg)
|
||||
container, cleanup := createContainer(t, ctx, deps.Network, ccfg)
|
||||
assert.NotNil(t, container)
|
||||
assert.NotNil(t, cleanup)
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
func TestCreateDB(t *testing.T) {
|
||||
t.Parallel()
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -29,7 +29,7 @@ func TestCreateDB(t *testing.T) {
|
||||
func TestCreateDBWithMigrations(t *testing.T) {
|
||||
t.Parallel()
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ func TestCreateDetectDocumentTextExpectation(t *testing.T) {
|
||||
func TestWaitForMockEndpoint(t *testing.T) {
|
||||
t.Parallel()
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
|
||||
+34
-26
@@ -2,6 +2,7 @@ package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -29,34 +30,41 @@ func (nm *NetworkManager) Get(t testing.TB, ctx context.Context) string {
|
||||
nm.mutex.Lock()
|
||||
defer nm.mutex.Unlock()
|
||||
|
||||
if !nm.initialized {
|
||||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
||||
require.NoError(t, err)
|
||||
|
||||
nm.networkName = networkName
|
||||
|
||||
current, err := cli.NetworkList(ctx, network.ListOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
found := false
|
||||
for _, c := range current {
|
||||
if c.Name == nm.networkName {
|
||||
found = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
_, err = cli.NetworkCreate(ctx, nm.networkName, network.CreateOptions{
|
||||
Driver: "bridge",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
nm.initialized = true
|
||||
if nm.initialized {
|
||||
return nm.networkName
|
||||
}
|
||||
|
||||
slog.Info("get network", "name", nm.networkName)
|
||||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
||||
require.NoError(t, err)
|
||||
|
||||
nm.networkName = networkName
|
||||
|
||||
current, err := cli.NetworkList(ctx, network.ListOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
found := false
|
||||
for _, c := range current {
|
||||
if c.Name == nm.networkName {
|
||||
found = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if found {
|
||||
return nm.networkName
|
||||
}
|
||||
|
||||
_, err = cli.NetworkCreate(ctx, nm.networkName, network.CreateOptions{
|
||||
Driver: "bridge",
|
||||
})
|
||||
conflictErrorMsg := fmt.Sprintf("Error response from daemon: network with name %s already exists", nm.networkName)
|
||||
if err != nil && err.Error() != conflictErrorMsg {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
slog.Info("created network", "name", nm.networkName)
|
||||
|
||||
nm.initialized = true
|
||||
|
||||
return nm.networkName
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ type StoreConfig struct {
|
||||
func TestCreateBucket(t *testing.T) {
|
||||
t.Parallel()
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -40,7 +40,7 @@ func TestCreateBucket(t *testing.T) {
|
||||
func TestCreateStoreClient(t *testing.T) {
|
||||
t.Parallel()
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ type TestConfig struct {
|
||||
func TestCreateQueue(t *testing.T) {
|
||||
t.Parallel()
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -44,7 +44,7 @@ func TestCreateQueue(t *testing.T) {
|
||||
func TestAssertMessageWait(t *testing.T) {
|
||||
t.Parallel()
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -71,7 +71,7 @@ func TestAssertMessageWait(t *testing.T) {
|
||||
func TestAssertMessageBodyWait(t *testing.T) {
|
||||
t.Parallel()
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -95,7 +95,7 @@ func TestAssertMessageBodyWait(t *testing.T) {
|
||||
func TestAssertMessageAttrWait(t *testing.T) {
|
||||
t.Parallel()
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
func TestCreateRunner(t *testing.T) {
|
||||
t.Parallel()
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user