minor refactor e2e cluster test

This commit is contained in:
Pavel Sviderski
2024-12-02 20:37:01 +10:00
parent 6eb373e4de
commit 21d55dc69a
2 changed files with 14 additions and 14 deletions
+4
View File
@@ -33,6 +33,10 @@ type Machine struct {
APIAddress netip.AddrPort APIAddress netip.AddrPort
} }
func (m *Machine) Connect(ctx context.Context) (*client.Client, error) {
return client.New(ctx, connector.NewTCPConnector(m.APIAddress))
}
type CreateMachineOptions struct { type CreateMachineOptions struct {
Name string Name string
Image string Image string
+10 -14
View File
@@ -10,12 +10,11 @@ import (
"testing" "testing"
"time" "time"
"uncloud/internal/cli/client" "uncloud/internal/cli/client"
"uncloud/internal/cli/client/connector"
"uncloud/internal/machine/api/pb" "uncloud/internal/machine/api/pb"
"uncloud/internal/ucind" "uncloud/internal/ucind"
) )
func createTestCluster(t *testing.T, name string, opts ucind.CreateClusterOptions) (*ucind.Provisioner, ucind.Cluster) { func createTestCluster(t *testing.T, name string, opts ucind.CreateClusterOptions) (ucind.Cluster, *ucind.Provisioner) {
dockerCli, err := dockerclient.NewClientWithOpts(dockerclient.FromEnv, dockerclient.WithAPIVersionNegotiation()) dockerCli, err := dockerclient.NewClientWithOpts(dockerclient.FromEnv, dockerclient.WithAPIVersionNegotiation())
require.NoError(t, err) require.NoError(t, err)
@@ -26,25 +25,22 @@ func createTestCluster(t *testing.T, name string, opts ucind.CreateClusterOption
c, err := p.CreateCluster(ctx, name, opts) c, err := p.CreateCluster(ctx, name, opts)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, name, c.Name)
require.Len(t, c.Machines, opts.Machines)
t.Cleanup(func() { t.Cleanup(func() {
require.NoError(t, p.RemoveCluster(ctx, name)) require.NoError(t, p.RemoveCluster(ctx, name))
}) })
return p, c return c, p
} }
func TestClusterLifecycle(t *testing.T) { func TestClusterLifecycle(t *testing.T) {
t.Parallel() t.Parallel()
var ( name := "ucind-test.cluster-lifecycle"
name = "test-cluster-lifecycle" ctx := context.Background()
ctx = context.Background() c, p := createTestCluster(t, name, ucind.CreateClusterOptions{Machines: 3})
)
p, c := createTestCluster(t, name, ucind.CreateClusterOptions{Machines: 3})
require.Equal(t, name, c.Name)
require.Len(t, c.Machines, 3)
t.Run("each machine reconciled cluster store", func(t *testing.T) { t.Run("each machine reconciled cluster store", func(t *testing.T) {
var err error var err error
@@ -52,14 +48,14 @@ func TestClusterLifecycle(t *testing.T) {
clients := make([]*client.Client, len(c.Machines)) clients := make([]*client.Client, len(c.Machines))
for i, m := range c.Machines { for i, m := range c.Machines {
require.NoError(t, p.WaitMachineReady(ctx, m)) require.NoError(t, p.WaitMachineReady(ctx, m))
clients[i], err = client.New(ctx, connector.NewTCPConnector(m.APIAddress)) clients[i], err = m.Connect(ctx)
require.NoError(t, err) require.NoError(t, err)
//goland:noinspection GoDeferInLoop //goland:noinspection GoDeferInLoop
defer clients[i].Close() defer clients[i].Close()
} }
// Any machine should work as a cluster API endpoint, e.g. be able to list all machines in the cluster. // Any machine should work as a cluster API endpoint, e.g. be able to list all machines in the cluster.
for _, cli := range clients { for i, cli := range clients {
// Wait for the machine to reconcile the cluster store. // Wait for the machine to reconcile the cluster store.
require.Eventually(t, func() bool { require.Eventually(t, func() bool {
machines, err := cli.ListMachines(ctx, &emptypb.Empty{}) machines, err := cli.ListMachines(ctx, &emptypb.Empty{})
@@ -84,7 +80,7 @@ func TestClusterLifecycle(t *testing.T) {
} }
return true return true
}, 15*time.Second, 50*time.Millisecond) }, 15*time.Second, 50*time.Millisecond, "cluster store not reconciled on machine #%d", i+1)
} }
}) })