Skip to content

Commit

Permalink
Integ Test: Fix zone implementation (#37)
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Emmanuel Jacquier <[email protected]
  • Loading branch information
pierre-emmanuelJ authored Jul 22, 2024
1 parent 34078cc commit a99cba2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
26 changes: 5 additions & 21 deletions internal/integ/cluster/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package cluster

import (
"context"
"flag"
"fmt"
"log/slog"
"os"

"github.com/exoscale/exoscale/csi-driver/internal/integ/flags"
"github.com/exoscale/exoscale/csi-driver/internal/integ/util"
Expand Down Expand Up @@ -41,7 +39,7 @@ func (c *Cluster) getInstanceType(ctx context.Context, family, size string) (*ex
return nil, fmt.Errorf("unable to find instance type %s.%s", family, size)
}

func (c *Cluster) provisionSKSCluster(ctx context.Context, zone string) error {
func (c *Cluster) provisionSKSCluster(ctx context.Context) error {
// do nothing if cluster exists
_, err := c.getCluster(ctx)
if err == nil {
Expand Down Expand Up @@ -90,16 +88,8 @@ func (c *Cluster) provisionSKSCluster(ctx context.Context, zone string) error {
return nil
}

func exitApplication(msg string, err error) {
slog.Error(msg, "err", err)

flag.Usage()

os.Exit(1)
}

func ConfigureCluster(ctx context.Context, createCluster bool, name, zone string) (*Cluster, error) {
client, err := util.CreateEgoscaleClient()
func ConfigureCluster(ctx context.Context, createCluster bool, name string, zone exov3.ZoneName) (*Cluster, error) {
client, err := util.CreateEgoscaleClient(ctx, zone)
if err != nil {
return nil, fmt.Errorf("error creating egoscale v3 client: %w", err)
}
Expand All @@ -110,7 +100,7 @@ func ConfigureCluster(ctx context.Context, createCluster bool, name, zone string
}

if createCluster {
err = cluster.provisionSKSCluster(ctx, zone)
err = cluster.provisionSKSCluster(ctx)
if err != nil {
return nil, fmt.Errorf("error creating SKS cluster: %w", err)
}
Expand Down Expand Up @@ -138,14 +128,8 @@ func ConfigureCluster(ctx context.Context, createCluster bool, name, zone string
func Setup() error {
ctx := context.Background()

if err := flags.ValidateFlags(); err != nil {
exitApplication("invalid flags", err)

return err
}

var err error
testCluster, err = ConfigureCluster(ctx, *flags.CreateCluster, *flags.ClusterName, *flags.Zone)
testCluster, err = ConfigureCluster(ctx, *flags.CreateCluster, *flags.ClusterName, exov3.ZoneName(*flags.Zone))
if err != nil {
return err
}
Expand Down
16 changes: 11 additions & 5 deletions internal/integ/integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"

v3 "github.com/exoscale/egoscale/v3"
"github.com/exoscale/exoscale/csi-driver/internal/integ/cluster"
"github.com/exoscale/exoscale/csi-driver/internal/integ/flags"
"github.com/exoscale/exoscale/csi-driver/internal/integ/k8s"
"github.com/exoscale/exoscale/csi-driver/internal/integ/util"
)
Expand All @@ -31,6 +31,12 @@ func TestMain(m *testing.M) {
slog.Warn("failed to set test timeout", "error", err)
}

if err := flags.ValidateFlags(); err != nil {
slog.Error("invalid flags", "err", err)
flag.Usage()
os.Exit(1)
}

exitCode := 0

err = cluster.Setup()
Expand Down Expand Up @@ -162,7 +168,7 @@ func TestDeleteVolume(t *testing.T) {
testName := "del-vol"
ns := k8s.CreateTestNamespace(t, cluster.Get().K8s, testName)

egoClient, err := util.CreateEgoscaleClient()
egoClient, err := util.CreateEgoscaleClient(ns.CTX, v3.ZoneName(*flags.Zone))
assert.NoError(t, err)

testFunc := func(useRetainStorageClass bool) func(t *testing.T) {
Expand Down Expand Up @@ -341,7 +347,7 @@ func TestSnapshot(t *testing.T) {
snapshotClient := ns.K.DynamicClient.Resource(getSnapshotCRDResource()).Namespace(ns.Name)

awaitExpectation(t, true, func() bool {
crdInstance, err := snapshotClient.Get(ns.CTX, "my-snap-1", v1.GetOptions{})
crdInstance, err := snapshotClient.Get(ns.CTX, "my-snap-1", metav1.GetOptions{})
if !assert.NoError(t, err) {
return false
}
Expand Down Expand Up @@ -375,11 +381,11 @@ func TestSnapshot(t *testing.T) {
})

// delete snapshot
err := snapshotClient.Delete(ns.CTX, "my-snap-1", v1.DeleteOptions{})
err := snapshotClient.Delete(ns.CTX, "my-snap-1", metav1.DeleteOptions{})
assert.NoError(t, err)

awaitExpectation(t, 0, func() int {
snapshots, err := snapshotClient.List(ns.CTX, v1.ListOptions{})
snapshots, err := snapshotClient.List(ns.CTX, metav1.ListOptions{})
if err != nil {
assert.NoError(t, err)
return 0
Expand Down
12 changes: 9 additions & 3 deletions internal/integ/util/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"context"
"fmt"
"os/exec"
"strings"
Expand All @@ -24,11 +25,16 @@ func GetRepoRootDir() string {
return strings.TrimSpace(string(path)) + "/"
}

func CreateEgoscaleClient() (*exov3.Client, error) {
v3Client, err := exov3.NewClient(credentials.NewEnvCredentials(), exov3.ClientOptWithEndpoint(exov3.CHGva2))
func CreateEgoscaleClient(ctx context.Context, zone exov3.ZoneName) (*exov3.Client, error) {
v3Client, err := exov3.NewClient(credentials.NewEnvCredentials())
if err != nil {
return nil, fmt.Errorf("error setting up egoscale client: %w", err)
}

return v3Client, nil
endpoint, err := v3Client.GetZoneAPIEndpoint(ctx, zone)
if err != nil {
return nil, fmt.Errorf("error setting up egoscale client zone: %w", err)
}

return v3Client.WithEndpoint(endpoint), nil
}

0 comments on commit a99cba2

Please sign in to comment.