ClickHouse/programs/diagnostics/internal/platform/manager_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
2.8 KiB
Go
Raw Normal View History

//go:build !no_docker
2022-04-27 12:22:20 +00:00
package platform_test
import (
"context"
"fmt"
"os"
"path"
"testing"
"github.com/ClickHouse/ClickHouse/programs/diagnostics/internal/platform"
"github.com/ClickHouse/ClickHouse/programs/diagnostics/internal/platform/test"
2022-11-25 07:52:49 +00:00
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
2022-04-27 12:22:20 +00:00
)
2022-11-25 07:52:49 +00:00
// create a ClickHouse container
func createClickHouseContainer(t *testing.T, ctx context.Context) (testcontainers.Container, nat.Port) {
2022-04-27 12:22:20 +00:00
cwd, err := os.Getwd()
if err != nil {
fmt.Println("unable to read current directory", err)
os.Exit(1)
}
// for now, we test against a hardcoded database-server version but we should make this a property
req := testcontainers.ContainerRequest{
Image: fmt.Sprintf("clickhouse/clickhouse-server:%s", test.GetClickHouseTestVersion()),
ExposedPorts: []string{"9000/tcp"},
WaitingFor: wait.ForLog("Ready for connections"),
Mounts: testcontainers.ContainerMounts{
{
Source: testcontainers.GenericBindMountSource{
HostPath: path.Join(cwd, "../../testdata/docker/custom.xml"),
},
Target: "/etc/clickhouse-server/config.d/custom.xml",
},
{
Source: testcontainers.GenericBindMountSource{
HostPath: path.Join(cwd, "../../testdata/docker/admin.xml"),
},
Target: "/etc/clickhouse-server/users.d/admin.xml",
},
2022-04-27 12:22:20 +00:00
},
}
clickhouseContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
// can't test without container
panic(err)
}
2022-11-25 07:52:49 +00:00
p, err := clickhouseContainer.MappedPort(ctx, "9000")
if err != nil {
// can't test without a port
panic(err)
}
2022-04-27 12:22:20 +00:00
2022-11-25 07:52:49 +00:00
return clickhouseContainer, p
2022-04-27 12:22:20 +00:00
}
func TestConnect(t *testing.T) {
t.Run("can only connect once", func(t *testing.T) {
2022-11-25 07:52:49 +00:00
ctx := context.Background()
clickhouseContainer, mappedPort := createClickHouseContainer(t, ctx)
defer clickhouseContainer.Terminate(ctx) //nolint
t.Setenv("CLICKHOUSE_DB_PORT", mappedPort.Port())
port := mappedPort.Int()
2022-04-27 12:22:20 +00:00
// get before connection
manager := platform.GetResourceManager()
require.Nil(t, manager.DbClient)
// init connection
2022-11-25 07:52:49 +00:00
err := manager.Connect("localhost", uint16(port), "", "")
2022-04-27 12:22:20 +00:00
require.Nil(t, err)
require.NotNil(t, manager.DbClient)
// try and re-fetch connection
2022-11-25 07:52:49 +00:00
err = manager.Connect("localhost", uint16(port), "", "")
2022-04-27 12:22:20 +00:00
require.NotNil(t, err)
require.Equal(t, "connect can only be called once", err.Error())
})
}
func TestGetResourceManager(t *testing.T) {
t.Run("get resource manager", func(t *testing.T) {
manager := platform.GetResourceManager()
require.NotNil(t, manager)
manager2 := platform.GetResourceManager()
require.NotNil(t, manager2)
require.Equal(t, &manager, &manager2)
})
}