Skip to content

Commit

Permalink
test: add e2e test suite for management.cattle.io controller
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Salas <carlos.salas@suse.com>
  • Loading branch information
salasberryfin committed Feb 14, 2024
1 parent a9b72a7 commit dcacd4a
Show file tree
Hide file tree
Showing 7 changed files with 670 additions and 7 deletions.
4 changes: 4 additions & 0 deletions test/e2e/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,8 @@ const (
FullTestLabel = "full"
DontRunLabel = "dontrun"
LocalTestLabel = "local"

CapiClusterOwnerLabel = "cluster-api.cattle.io/capi-cluster-owner"
CapiClusterOwnerNamespaceLabel = "cluster-api.cattle.io/capi-cluster-owner-ns"
OwnedLabelName = "cluster-api.cattle.io/owned"
)
2 changes: 1 addition & 1 deletion test/e2e/specs/import_gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strconv"

. "github.com/onsi/ginkgo/v2"

. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -274,7 +275,6 @@ func CreateUsingGitOpsSpec(ctx context.Context, inputGetter func() CreateUsingGi
}, rancherConnectRes)
Expect(rancherConnectRes.Error).NotTo(HaveOccurred(), "Failed getting nodes with Rancher Kubeconfig")
Expect(rancherConnectRes.ExitCode).To(Equal(0), "Getting nodes return non-zero exit code")

})

AfterEach(func() {
Expand Down
315 changes: 315 additions & 0 deletions test/e2e/specs/import_gitops_mgmtv3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
//go:build e2e
// +build e2e

/*
Copyright © 2023 - 2024 SUSE LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package specs

import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"strconv"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/test/framework"
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest/komega"

managementv3 "github.com/rancher-sandbox/rancher-turtles/internal/rancher/management/v3"
"github.com/rancher-sandbox/rancher-turtles/test/e2e"
turtlesframework "github.com/rancher-sandbox/rancher-turtles/test/framework"
"github.com/rancher-sandbox/rancher-turtles/test/testenv"
)

type CreateMgmtV3UsingGitOpsSpecInput struct {
E2EConfig *clusterctl.E2EConfig
BootstrapClusterProxy framework.ClusterProxy
ClusterctlConfigPath string
ArtifactFolder string
RancherServerURL string

ClusterctlBinaryPath string
ClusterTemplate []byte
ClusterName string
AdditionalTemplateVariables map[string]string

CAPIClusterCreateWaitName string
DeleteClusterWaitName string

// ControlPlaneMachineCount defines the number of control plane machines to be added to the workload cluster.
// If not specified, 1 will be used.
ControlPlaneMachineCount *int

// WorkerMachineCount defines number of worker machines to be added to the workload cluster.
// If not specified, 1 will be used.
WorkerMachineCount *int

GitAddr string
GitAuthSecretName string

SkipCleanup bool
SkipDeletionTest bool

LabelNamespace bool

// management.cattle.io specifc
CapiClusterOwnerLabel string
CapiClusterOwnerNamespaceLabel string
OwnedLabelName string
}

// CreateMgmtV3UsingGitOpsSpec implements a spec that will create a cluster via Fleet and test that it
// automatically imports into Rancher Manager.
func CreateMgmtV3UsingGitOpsSpec(ctx context.Context, inputGetter func() CreateMgmtV3UsingGitOpsSpecInput) {
var (
specName = "creategitops"
input CreateMgmtV3UsingGitOpsSpecInput
namespace *corev1.Namespace
repoName string
cancelWatches context.CancelFunc
capiCluster *types.NamespacedName
rancherKubeconfig *turtlesframework.RancherGetClusterKubeconfigResult
originalKubeconfig *turtlesframework.RancherGetClusterKubeconfigResult
rancherConnectRes *turtlesframework.RunCommandResult
rancherCluster *managementv3.Cluster
capiClusterCreateWait []interface{}
deleteClusterWait []interface{}
)

BeforeEach(func() {
Expect(ctx).NotTo(BeNil(), "ctx is required for %s spec", specName)
input = inputGetter()
Expect(input.E2EConfig).ToNot(BeNil(), "Invalid argument. input.E2EConfig can't be nil when calling %s spec", specName)
Expect(input.BootstrapClusterProxy).ToNot(BeNil(), "Invalid argument. input.BootstrapClusterProxy can't be nil when calling %s spec", specName)
Expect(input.ClusterctlConfigPath).To(BeAnExistingFile(), "Invalid argument. input.ClusterctlConfigPath must be an existing file when calling %s spec", specName)
Expect(os.MkdirAll(input.ArtifactFolder, 0750)).To(Succeed(), "Invalid argument. input.ArtifactFolder can't be created for %s spec", specName)

Expect(input.E2EConfig.Variables).To(HaveKey(e2e.KubernetesManagementVersionVar))
namespace, cancelWatches = e2e.SetupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder)
repoName = e2e.CreateRepoName(specName)

capiClusterCreateWait = input.E2EConfig.GetIntervals(input.BootstrapClusterProxy.GetName(), input.CAPIClusterCreateWaitName)
Expect(capiClusterCreateWait).ToNot(BeNil(), "Failed to get wait intervals %s", input.CAPIClusterCreateWaitName)

deleteClusterWait = input.E2EConfig.GetIntervals(input.BootstrapClusterProxy.GetName(), input.DeleteClusterWaitName)
Expect(capiClusterCreateWait).ToNot(BeNil(), "Failed to get wait intervals %s", input.CAPIClusterCreateWaitName)

capiCluster = &types.NamespacedName{
Namespace: namespace.Name,
Name: input.ClusterName,
}

rancherKubeconfig = new(turtlesframework.RancherGetClusterKubeconfigResult)
originalKubeconfig = new(turtlesframework.RancherGetClusterKubeconfigResult)
rancherConnectRes = new(turtlesframework.RunCommandResult)

komega.SetClient(input.BootstrapClusterProxy.GetClient())
komega.SetContext(ctx)
})

It("Should import a cluster using gitops", func() {
controlPlaneMachineCount := 1
if input.ControlPlaneMachineCount != nil {
controlPlaneMachineCount = *input.ControlPlaneMachineCount
}

workerMachineCount := 1
if input.WorkerMachineCount != nil {
workerMachineCount = *input.WorkerMachineCount
}

if input.LabelNamespace {
turtlesframework.AddLabelsToNamespace(ctx, turtlesframework.AddLabelsToNamespaceInput{
ClusterProxy: input.BootstrapClusterProxy,
Name: namespace.Name,
Labels: map[string]string{
"cluster-api.cattle.io/rancher-auto-import": "true",
},
})
}

By("Create Git repository")

repoCloneAddr := turtlesframework.GiteaCreateRepo(ctx, turtlesframework.GiteaCreateRepoInput{
ServerAddr: input.GitAddr,
RepoName: repoName,
Username: input.E2EConfig.GetVariable(e2e.GiteaUserNameVar),
Password: input.E2EConfig.GetVariable(e2e.GiteaUserPasswordVar),
})
repoDir := turtlesframework.GitCloneRepo(ctx, turtlesframework.GitCloneRepoInput{
Address: repoCloneAddr,
Username: input.E2EConfig.GetVariable(e2e.GiteaUserNameVar),
Password: input.E2EConfig.GetVariable(e2e.GiteaUserPasswordVar),
})

By("Create fleet repository structure")

clustersDir := filepath.Join(repoDir, "clusters")
os.MkdirAll(clustersDir, os.ModePerm)

additionalVars := map[string]string{
"CLUSTER_NAME": input.ClusterName,
"WORKER_MACHINE_COUNT": strconv.Itoa(workerMachineCount),
"CONTROL_PLANE_MACHINE_COUNT": strconv.Itoa(controlPlaneMachineCount),
}
for k, v := range input.AdditionalTemplateVariables {
additionalVars[k] = v
}

clusterPath := filepath.Join(clustersDir, fmt.Sprintf("%s.yaml", input.ClusterName))
Expect(turtlesframework.ApplyFromTemplate(ctx, turtlesframework.ApplyFromTemplateInput{
Getter: input.E2EConfig.GetVariable,
Template: input.ClusterTemplate,
OutputFilePath: clusterPath,
AddtionalEnvironmentVariables: additionalVars,
})).To(Succeed())

fleetPath := filepath.Join(clustersDir, "fleet.yaml")
turtlesframework.FleetCreateFleetFile(ctx, turtlesframework.FleetCreateFleetFileInput{
Namespace: namespace.Name,
FilePath: fleetPath,
})

By("Committing changes to fleet repo and pushing")

turtlesframework.GitCommitAndPush(ctx, turtlesframework.GitCommitAndPushInput{
CloneLocation: repoDir,
Username: input.E2EConfig.GetVariable(e2e.GiteaUserNameVar),
Password: input.E2EConfig.GetVariable(e2e.GiteaUserPasswordVar),
CommitMessage: "ci: add clusters bundle",
})

By("Applying GitRepo")

turtlesframework.FleetCreateGitRepo(ctx, turtlesframework.FleetCreateGitRepoInput{
Name: repoName,
Namespace: turtlesframework.FleetLocalNamespace,
Branch: turtlesframework.DefaultBranchName,
Repo: repoCloneAddr,
FleetGeneration: 1,
Paths: []string{"clusters"},
ClientSecretName: input.GitAuthSecretName,
ClusterProxy: input.BootstrapClusterProxy,
})

By("Waiting for the CAPI cluster to appear")
capiCluster := &clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{
Namespace: namespace.Name,
Name: input.ClusterName,
}}
Eventually(
komega.Get(capiCluster),
input.E2EConfig.GetIntervals(input.BootstrapClusterProxy.GetName(), "wait-rancher")...).
Should(Succeed(), "Failed to apply CAPI cluster definition to cluster via Fleet")

By("Waiting for cluster control plane to be Ready")
Eventually(komega.Object(capiCluster), capiClusterCreateWait...).Should(HaveField("Status.ControlPlaneReady", BeTrue()))

By("Waiting for the CAPI cluster to be connectable")
Eventually(func() error {
remoteClient := input.BootstrapClusterProxy.GetWorkloadCluster(ctx, capiCluster.Namespace, capiCluster.Name).GetClient()
namespaces := &corev1.NamespaceList{}

return remoteClient.List(ctx, namespaces)
}, capiClusterCreateWait...).Should(Succeed(), "Failed to connect to workload cluster using CAPI kubeconfig")

By("Storing the original CAPI cluster kubeconfig")
turtlesframework.RancherGetOriginalKubeconfig(ctx, turtlesframework.RancherGetClusterKubeconfigInput{
Getter: input.BootstrapClusterProxy.GetClient(),
SecretName: fmt.Sprintf("%s-kubeconfig", capiCluster.Name),
Namespace: capiCluster.Namespace,
WriteToTempFile: true,
}, originalKubeconfig)

By("Waiting for the rancher cluster record to appear")
rancherClusters := &managementv3.ClusterList{}
selectors := []client.ListOption{
client.MatchingLabels{
input.CapiClusterOwnerLabel: capiCluster.Name,
input.CapiClusterOwnerNamespaceLabel: capiCluster.Namespace,
input.OwnedLabelName: "",
},
}
Eventually(komega.List(rancherClusters, selectors...)).Should(Succeed())
Expect(rancherClusters.Items).To(HaveLen(1))
rancherCluster = &rancherClusters.Items[0]
Eventually(komega.Get(rancherCluster), input.E2EConfig.GetIntervals(input.BootstrapClusterProxy.GetName(), "wait-rancher")...).Should(Succeed())

By("Waiting for the rancher cluster to have a deployed agent")
Eventually(func() bool {
Eventually(komega.Get(rancherCluster), input.E2EConfig.GetIntervals(input.BootstrapClusterProxy.GetName(), "wait-rancher")...).Should(Succeed())
return managementv3.ClusterConditionAgentDeployed.IsTrue(rancherCluster)
}, input.E2EConfig.GetIntervals(input.BootstrapClusterProxy.GetName(), "wait-rancher")...).Should(BeTrue())

By("Waiting for the rancher cluster to be ready")
Eventually(func() bool {
Eventually(komega.Get(rancherCluster), input.E2EConfig.GetIntervals(input.BootstrapClusterProxy.GetName(), "wait-rancher")...).Should(Succeed())
return managementv3.ClusterConditionReady.IsTrue(rancherCluster)
}, input.E2EConfig.GetIntervals(input.BootstrapClusterProxy.GetName(), "wait-rancher")...).Should(BeTrue())

By("Waiting for the CAPI cluster to be connectable using Rancher kubeconfig")
turtlesframework.RancherGetClusterKubeconfig(ctx, turtlesframework.RancherGetClusterKubeconfigInput{
Getter: input.BootstrapClusterProxy.GetClient(),
SecretName: fmt.Sprintf("%s-kubeconfig", rancherCluster.Name),
Namespace: rancherCluster.Spec.FleetWorkspaceName,
RancherServerURL: input.RancherServerURL,
WriteToTempFile: true,
}, rancherKubeconfig)

turtlesframework.RunCommand(ctx, turtlesframework.RunCommandInput{
Command: "kubectl",
Args: []string{
"--kubeconfig",
rancherKubeconfig.TempFilePath,
"get",
"nodes",
"--insecure-skip-tls-verify",
},
}, rancherConnectRes)
Expect(rancherConnectRes.Error).NotTo(HaveOccurred(), "Failed getting nodes with Rancher Kubeconfig")
Expect(rancherConnectRes.ExitCode).To(Equal(0), "Getting nodes return non-zero exit code")
})

AfterEach(func() {
err := testenv.CollectArtifacts(ctx, originalKubeconfig.TempFilePath, path.Join(input.ArtifactFolder, input.BootstrapClusterProxy.GetName(), input.ClusterName))
if err != nil {
fmt.Printf("Failed to collect artifacts for the child cluster: %v\n", err)
}

By("Deleting GitRepo from Rancher")
turtlesframework.FleetDeleteGitRepo(ctx, turtlesframework.FleetDeleteGitRepoInput{
Name: repoName,
Namespace: turtlesframework.FleetLocalNamespace,
ClusterProxy: input.BootstrapClusterProxy,
})

By("Waiting for the rancher cluster record to be removed")
Eventually(komega.Get(rancherCluster), deleteClusterWait...).Should(MatchError(ContainSubstring("not found")), "Rancher cluster should be deleted")

e2e.DumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, capiCluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
})
}
4 changes: 0 additions & 4 deletions test/e2e/suites/import-gitops/import_gitops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
)

var _ = Describe("[Docker] [Kubeadm] Create and delete CAPI cluster functionality should work with namespace auto-import", Label(e2e.ShortTestLabel, e2e.FullTestLabel), func() {

BeforeEach(func() {
SetClient(setupClusterResult.BootstrapClusterProxy.GetClient())
SetContext(ctx)
Expand Down Expand Up @@ -63,7 +62,6 @@ var _ = Describe("[Docker] [Kubeadm] Create and delete CAPI cluster functionalit
})

var _ = Describe("[AWS] [EKS] Create and delete CAPI cluster functionality should work with namespace auto-import", Label(e2e.FullTestLabel), func() {

BeforeEach(func() {
komega.SetClient(setupClusterResult.BootstrapClusterProxy.GetClient())
komega.SetContext(ctx)
Expand Down Expand Up @@ -93,7 +91,6 @@ var _ = Describe("[AWS] [EKS] Create and delete CAPI cluster functionality shoul
})

var _ = Describe("[Azure] [AKS] Create and delete CAPI cluster functionality should work with namespace auto-import", Label(e2e.DontRunLabel), func() {

BeforeEach(func() {
SetClient(setupClusterResult.BootstrapClusterProxy.GetClient())
SetContext(ctx)
Expand Down Expand Up @@ -122,7 +119,6 @@ var _ = Describe("[Azure] [AKS] Create and delete CAPI cluster functionality sho
})

var _ = Describe("[vSphere] [Kubeadm] Create and delete CAPI cluster functionality should work with namespace auto-import", Label(e2e.LocalTestLabel), func() {

BeforeEach(func() {
SetClient(setupClusterResult.BootstrapClusterProxy.GetClient())
SetContext(ctx)
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/suites/import-gitops/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ import (
"fmt"
"os"
"path/filepath"
"testing"

"runtime"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down
Loading

0 comments on commit dcacd4a

Please sign in to comment.