support providing TEST_CLUSTER_NAME for e2e tests

This commit is contained in:
Pavel Sviderski
2024-12-03 15:46:19 +10:00
parent 5621a84e99
commit 5da1fc5387
2 changed files with 49 additions and 1 deletions
+34 -1
View File
@@ -9,6 +9,7 @@ import (
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
dockerclient "github.com/docker/docker/client" dockerclient "github.com/docker/docker/client"
"google.golang.org/protobuf/types/known/emptypb" "google.golang.org/protobuf/types/known/emptypb"
"net/netip"
"time" "time"
"uncloud/internal/cli/client" "uncloud/internal/cli/client"
"uncloud/internal/cli/client/connector" "uncloud/internal/cli/client/connector"
@@ -186,7 +187,39 @@ func (p *Provisioner) InspectCluster(ctx context.Context, name string) (Cluster,
} }
c.Name = name c.Name = name
// TODO: list containers (machines) with the cluster name label and include them in the cluster struct. // Include all containers (machines) with the cluster name label.
opts := container.ListOptions{
All: true,
Filters: filters.NewArgs(
filters.Arg("label", ClusterNameLabel+"="+name),
filters.Arg("label", ManagedLabel),
),
}
containers, err := p.dockerCli.ContainerList(ctx, opts)
if err != nil {
return c, fmt.Errorf("list Docker containers with cluster name '%s': %w", name, err)
}
for _, ctr := range containers {
m := Machine{
ClusterName: name,
ContainerName: ctr.Names[0],
Name: ctr.Labels[MachineNameLabel],
}
for _, port := range ctr.Ports {
if port.PrivatePort == UncloudAPIPort {
if ip, err := netip.ParseAddr(port.IP); err == nil {
m.APIAddress = netip.AddrPortFrom(ip, port.PublicPort)
break
}
}
}
if !m.APIAddress.IsValid() {
return c, fmt.Errorf("API binding not found for container '%s'", m.ContainerName)
}
c.Machines = append(c.Machines, m)
}
return c, nil return c, nil
} }
+15
View File
@@ -2,10 +2,12 @@ package e2e
import ( import (
"context" "context"
"errors"
dockerclient "github.com/docker/docker/client" dockerclient "github.com/docker/docker/client"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"os"
"testing" "testing"
"time" "time"
"uncloud/internal/cli/client" "uncloud/internal/cli/client"
@@ -19,6 +21,19 @@ func createTestCluster(t *testing.T, name string, opts ucind.CreateClusterOption
p := ucind.NewProvisioner(dockerCli, nil) p := ucind.NewProvisioner(dockerCli, nil)
ctx := context.Background() ctx := context.Background()
// Use the existing cluster if specified by the environment variable.
envName := os.Getenv("TEST_CLUSTER_NAME")
if envName != "" {
c, err := p.InspectCluster(ctx, envName)
if err == nil {
return c, p
}
if !errors.Is(err, ucind.ErrNotFound) {
require.NoError(t, err)
}
}
// Remove the cluster if it already exists. It could be left from a previous interrupted test run. // Remove the cluster if it already exists. It could be left from a previous interrupted test run.
require.NoError(t, p.RemoveCluster(ctx, name)) require.NoError(t, p.RemoveCluster(ctx, name))