wait for test cluster to be ready (all machines up)

This commit is contained in:
Pavel Sviderski
2024-12-05 13:45:46 +10:00
parent a40fd613e8
commit 8bddbec7bd
5 changed files with 72 additions and 28 deletions
+12 -5
View File
@@ -4,6 +4,7 @@ import (
"context"
"errors"
dockerclient "github.com/docker/docker/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
@@ -15,7 +16,9 @@ import (
"uncloud/internal/ucind"
)
func createTestCluster(t *testing.T, name string, opts ucind.CreateClusterOptions) (ucind.Cluster, *ucind.Provisioner) {
func createTestCluster(
t *testing.T, name string, opts ucind.CreateClusterOptions, waitReady bool,
) (ucind.Cluster, *ucind.Provisioner) {
dockerCli, err := dockerclient.NewClientWithOpts(dockerclient.FromEnv, dockerclient.WithAPIVersionNegotiation())
require.NoError(t, err)
@@ -39,13 +42,17 @@ func createTestCluster(t *testing.T, name string, opts ucind.CreateClusterOption
c, err := p.CreateCluster(ctx, name, opts)
require.NoError(t, err)
require.Equal(t, name, c.Name)
require.Len(t, c.Machines, opts.Machines)
assert.Equal(t, name, c.Name)
assert.Len(t, c.Machines, opts.Machines)
t.Cleanup(func() {
require.NoError(t, p.RemoveCluster(ctx, name))
})
if waitReady {
require.NoError(t, p.WaitClusterReady(ctx, c, 15*time.Second))
}
return c, p
}
@@ -54,14 +61,14 @@ func TestClusterLifecycle(t *testing.T) {
name := "ucind-test.cluster-lifecycle"
ctx := context.Background()
c, p := createTestCluster(t, name, ucind.CreateClusterOptions{Machines: 3})
c, p := createTestCluster(t, name, ucind.CreateClusterOptions{Machines: 3}, false)
t.Run("each machine reconciled cluster store", func(t *testing.T) {
var err error
// Create a client for each machine and wait for it to be ready.
clients := make([]*client.Client, len(c.Machines))
for i, m := range c.Machines {
require.NoError(t, p.WaitMachineReady(ctx, m))
require.NoError(t, p.WaitMachineReady(ctx, m, 5*time.Second))
clients[i], err = m.Connect(ctx)
require.NoError(t, err)
//goland:noinspection GoDeferInLoop
+11 -8
View File
@@ -14,26 +14,29 @@ import (
func TestRunService(t *testing.T) {
t.Parallel()
name := "ucind-test.run-service"
clusterName := "ucind-test.run-service"
ctx := context.Background()
c, _ := createTestCluster(t, name, ucind.CreateClusterOptions{Machines: 3})
c, _ := createTestCluster(t, clusterName, ucind.CreateClusterOptions{Machines: 3}, true)
cli, err := c.Machines[0].Connect(ctx)
require.NoError(t, err)
t.Run("global mode", func(t *testing.T) {
t.Parallel()
name := "busybox-global"
t.Cleanup(func() {
err := cli.RemoveService(ctx, "busybox-global")
err := cli.RemoveService(ctx, name)
if !dockerclient.IsErrNotFound(err) {
require.NoError(t, err)
}
_, err = cli.InspectService(ctx, "busybox-global")
_, err = cli.InspectService(ctx, name)
require.ErrorIs(t, err, client.ErrNotFound)
})
resp, err := cli.RunService(ctx, api.ServiceSpec{
Name: "busybox-global",
Name: name,
Mode: api.ServiceModeGlobal,
Container: api.ContainerSpec{
Command: []string{"sleep", "infinity"},
@@ -43,14 +46,14 @@ func TestRunService(t *testing.T) {
require.NoError(t, err)
assert.NotEmpty(t, resp.ID)
assert.Equal(t, "busybox-global", resp.Name)
assert.Equal(t, name, resp.Name)
assert.Len(t, resp.Containers, 3, "expected 1 container on each machine")
svc, err := cli.InspectService(ctx, "busybox-global")
svc, err := cli.InspectService(ctx, name)
require.NoError(t, err)
assert.Equal(t, resp.ID, svc.ID)
assert.Equal(t, "busybox-global", svc.Name)
assert.Equal(t, name, svc.Name)
assert.Equal(t, api.ServiceModeGlobal, svc.Mode)
assert.Len(t, svc.Containers, 3, "expected 1 container on each machine")
})