Files
query-orchestration/internal/test/network.go
T

45 lines
956 B
Go
Raw Normal View History

package test
import (
"fmt"
"log/slog"
"sync"
"testing"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/stretchr/testify/require"
)
const (
networkName = "queryorchestration_test"
)
2025-06-02 10:44:45 +00:00
var networkOnce sync.Once
func GetNetwork(t testing.TB) string {
networkOnce.Do(func() {
2025-06-02 10:44:45 +00:00
cli := getDockerClient(t)
defer cli.Close()
2025-06-02 10:44:45 +00:00
_, err := cli.NetworkCreate(t.Context(), networkName, network.CreateOptions{
Driver: network.NetworkBridge,
})
conflictErrorMsg := fmt.Sprintf("Error response from daemon: network with name %s already exists", networkName)
if err != nil && err.Error() != conflictErrorMsg {
require.NoError(t, err)
}
slog.Info("created network", "name", networkName)
})
return networkName
}
2025-06-02 10:44:45 +00:00
func getDockerClient(t testing.TB) *client.Client {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
require.NoError(t, err)
2025-06-03 13:52:10 +00:00
2025-06-02 10:44:45 +00:00
return cli
}