Skip to content

Commit

Permalink
Add e2e for cluster import functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Danil Grigorev <danil.grigorev@suse.com>
  • Loading branch information
Danil-Grigorev committed Aug 11, 2023
1 parent caad847 commit 3cdee05
Show file tree
Hide file tree
Showing 7 changed files with 5,001 additions and 48 deletions.
3 changes: 2 additions & 1 deletion test/e2e/config/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ managementClusterName: rancher-turtles-e2e

images:
# Use local dev images built source tree;
- name: ghcr.io/rancher-turtles:dev # This should be substituted with operator image
- name: ghcr.io/rancher-sandbox/rancher-turtles-{ARCH}:dev # This should be substituted with operator image
loadBehavior: tryLoad

intervals:
default/wait-controllers: ["3m", "10s"]
default/wait-consistently: ["30s", "5s"]
12 changes: 3 additions & 9 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// +build e2e

/*
Copyright 2022 The Kubernetes Authors.
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,7 +34,6 @@ import (
"k8s.io/klog/v2"
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"

corev1 "k8s.io/api/core/v1"
operatorframework "sigs.k8s.io/cluster-api-operator/test/framework"
"sigs.k8s.io/cluster-api/test/framework"
"sigs.k8s.io/cluster-api/test/framework/bootstrap"
Expand Down Expand Up @@ -239,12 +238,7 @@ func initBootstrapCluster(bootstrapClusterProxy framework.ClusterProxy, config *
Expect(os.MkdirAll(logFolder, 0750)).To(Succeed(), "Invalid argument. Log folder can't be created for initBootstrapCluster")

By("Adding docker variables secret")
bootstrapCluster := bootstrapClusterProxy.GetClient()
secret := &corev1.Secret{}
secretData, err := os.ReadFile(customManifestsFolder + dockerVariablesSecret)
Expect(err).ToNot(HaveOccurred(), "Failed to read the docker provider secret from file")
Expect(yaml.Unmarshal(secretData, &secret)).To(Succeed())
Expect(bootstrapCluster.Create(ctx, secret)).To(Succeed())
Expect(bootstrapClusterProxy.Apply(ctx, dockerVariablesSecret)).To(Succeed())

By("Installing CAPI operator chart")
chart := &operatorframework.HelmChart{
Expand All @@ -255,7 +249,7 @@ func initBootstrapCluster(bootstrapClusterProxy framework.ClusterProxy, config *
Output: operatorframework.Full,
AdditionalFlags: []string{"-n", operatorNamespace, "--create-namespace", "--wait"},
}
_, err = chart.InstallChart(map[string]string{
_, err := chart.InstallChart(map[string]string{
"cert-manager.enabled": "true",
"infrastructure": "docker",
"secretName": "variables",
Expand Down
31 changes: 0 additions & 31 deletions test/e2e/helm_test.go

This file was deleted.

15 changes: 10 additions & 5 deletions test/e2e/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// +build e2e

/*
Copyright 2022 The Kubernetes Authors.
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -21,18 +21,23 @@ package e2e

import (
"context"
_ "embed"
)

var (
ctx = context.Background()

//go:embed resources/fleet-capi-test.yaml
fleetCapiTestdata []byte

//go:embed resources/docker-infra-secret.yaml
dockerVariablesSecret []byte
)

const (
operatorNamespace = "capi-operator-system"
rancherTurtlesNamespace = "rancher-turtles-system"
rancherNamespace = "cattle-system"
customManifestsFolder = "resources/"

dockerVariablesSecret = "docker-infra-secret.yaml"
fleetCapiTestdata = "fleet-capi-test.yaml"
capiClusterName = "test1"
capiClusterNamespace = "fleet-default"
)
85 changes: 85 additions & 0 deletions test/e2e/import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//go:build e2e
// +build e2e

/*
Copyright 2023 The Kubernetes Authors.
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 e2e

import (
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rancher-sandbox/rancher-turtles/internal/rancher"
"sigs.k8s.io/controller-runtime/pkg/client"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

var _ = Describe("Create and delete CAPI cluster functionality should work", func() {
var (
bootstrapCluster client.Client
rancherClusterHandler *rancher.ClusterHandler
capiCluster *clusterv1.Cluster
rancherClusterKey client.ObjectKey
)

BeforeEach(func() {
bootstrapCluster = bootstrapClusterProxy.GetClient()
rancherClusterHandler = rancher.NewClusterHandler(ctx, bootstrapCluster)
capiCluster = &clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{
Namespace: capiClusterNamespace,
Name: capiClusterName,
}}
rancherClusterKey = client.ObjectKey{
Namespace: capiClusterNamespace,
Name: fmt.Sprintf("%s-capi", capiClusterName),
}
})

It("should successfully create a rancher cluster from CAPI cluster", func() {
By("Creating a CAPI docker provider cluster setup")
Eventually(func() error {
return bootstrapClusterProxy.Apply(ctx, fleetCapiTestdata)
}, e2eConfig.GetIntervals(bootstrapClusterProxy.GetName(), "wait-controllers")...).Should(Succeed())

By("Waiting for rancher cluster record to appear")
Eventually(func() error {
_, err := rancherClusterHandler.Get(rancherClusterKey)
return err
}, e2eConfig.GetIntervals(bootstrapClusterProxy.GetName(), "wait-controllers")...).Should(Succeed())
})

It("should successfully remove a rancher cluster after CAPI cluster removal and no longer re-import it", func() {
By("Removing CAPI cluster record")
Expect(bootstrapCluster.Delete(ctx, capiCluster)).To(Succeed())

By("Waiting for rancher cluster to be removed")
Eventually(func() bool {
_, err := rancherClusterHandler.Get(rancherClusterKey)
return apierrors.IsNotFound(err)
}, e2eConfig.GetIntervals(bootstrapClusterProxy.GetName(), "wait-controllers")...).Should(BeTrue())

By("Checking if rancher cluster record will not be created again")
Consistently(func() bool {
_, err := rancherClusterHandler.Get(rancherClusterKey)
return apierrors.IsNotFound(err)
}, e2eConfig.GetIntervals(bootstrapClusterProxy.GetName(), "wait-consistently")...).Should(BeTrue())
})
})
Loading

0 comments on commit 3cdee05

Please sign in to comment.