Skip to content

Commit

Permalink
[e2e] Find and validate templates for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
eromanova committed Nov 14, 2024
1 parent 9d3d1c9 commit 64dc32a
Show file tree
Hide file tree
Showing 16 changed files with 303 additions and 41 deletions.
8 changes: 6 additions & 2 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (

hmc "github.com/Mirantis/hmc/api/v1alpha1"
internalutils "github.com/Mirantis/hmc/internal/utils"
"github.com/Mirantis/hmc/test/e2e/config"
"github.com/Mirantis/hmc/test/e2e/kubeclient"
"github.com/Mirantis/hmc/test/e2e/managedcluster"
"github.com/Mirantis/hmc/test/templates"
Expand All @@ -50,13 +51,16 @@ func TestE2E(t *testing.T) {
var clusterTemplates map[string][]hmc.AvailableUpgrade

var _ = BeforeSuite(func() {
err := config.Parse()
Expect(err).NotTo(HaveOccurred())

GinkgoT().Setenv(managedcluster.EnvVarNamespace, internalutils.DefaultSystemNamespace)

ctx := context.Background()

By("building and deploying the controller-manager")
cmd := exec.Command("make", "kind-deploy")
_, err := utils.Run(cmd)
_, err = utils.Run(cmd)
Expect(err).NotTo(HaveOccurred())
cmd = exec.Command("make", "test-apply")
_, err = utils.Run(cmd)
Expand Down Expand Up @@ -153,7 +157,7 @@ func validateController(kc *kubeclient.KubeClient, labelSelector, name string) e

// templateBy wraps a Ginkgo By with a block describing the template being
// tested.
func templateBy(t managedcluster.Template, description string) {
func templateBy(t managedcluster.TemplateType, description string) {
GinkgoHelper()
By(fmt.Sprintf("[%s] %s", t, description))
}
Expand Down
9 changes: 5 additions & 4 deletions test/e2e/managedcluster/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ package managedcluster

const (
// Common
EnvVarManagedClusterName = "MANAGED_CLUSTER_NAME"
EnvVarControlPlaneNumber = "CONTROL_PLANE_NUMBER"
EnvVarWorkerNumber = "WORKER_NUMBER"
EnvVarNamespace = "NAMESPACE"
EnvVarManagedClusterName = "MANAGED_CLUSTER_NAME"
EnvVarManagedClusterTemplate = "MANAGED_CLUSTER_TEMPLATE"
EnvVarControlPlaneNumber = "CONTROL_PLANE_NUMBER"
EnvVarWorkerNumber = "WORKER_NUMBER"
EnvVarNamespace = "NAMESPACE"
// EnvVarNoCleanup disables After* cleanup in provider specs to allow for
// debugging of test failures.
EnvVarNoCleanup = "NO_CLEANUP"
Expand Down
33 changes: 19 additions & 14 deletions test/e2e/managedcluster/managedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ const (
Namespace = "default"
)

type Template string
type TemplateType string

const (
TemplateAWSStandaloneCP Template = "aws-standalone-cp"
TemplateAWSHostedCP Template = "aws-hosted-cp"
TemplateAzureHostedCP Template = "azure-hosted-cp"
TemplateAzureStandaloneCP Template = "azure-standalone-cp"
TemplateVSphereStandaloneCP Template = "vsphere-standalone-cp"
TemplateVSphereHostedCP Template = "vsphere-hosted-cp"
TemplateAWSStandaloneCP TemplateType = "aws-standalone-cp"
TemplateAWSHostedCP TemplateType = "aws-hosted-cp"
TemplateAzureHostedCP TemplateType = "azure-hosted-cp"
TemplateAzureStandaloneCP TemplateType = "azure-standalone-cp"
TemplateVSphereStandaloneCP TemplateType = "vsphere-standalone-cp"
TemplateVSphereHostedCP TemplateType = "vsphere-hosted-cp"
)

//go:embed resources/aws-standalone-cp.yaml.tpl
Expand Down Expand Up @@ -86,35 +86,40 @@ func GetProviderLabel(provider ProviderType) string {
return fmt.Sprintf("%s=%s", providerLabel, provider)
}

func setClusterName(templateName Template) {
func setClusterName(templateType TemplateType) {
var generatedName string

mcName := os.Getenv(EnvVarManagedClusterName)
if mcName == "" {
mcName = "e2e-test-" + uuid.New().String()[:8]
}

providerName := strings.Split(string(templateName), "-")[0]
providerName := strings.Split(string(templateType), "-")[0]

// Append the provider name to the cluster name to ensure uniqueness between
// different deployed ManagedClusters.
generatedName = fmt.Sprintf("%s-%s", mcName, providerName)
if strings.Contains(string(templateName), "hosted") {
if strings.Contains(string(templateType), "hosted") {
generatedName = fmt.Sprintf("%s-%s", mcName, "hosted")
}

GinkgoT().Setenv(EnvVarManagedClusterName, generatedName)
}

func setTemplate(templateName string) {
GinkgoT().Setenv(EnvVarManagedClusterTemplate, templateName)
}

// GetUnstructured returns an unstructured ManagedCluster object based on the
// provider and template.
func GetUnstructured(templateName Template) *unstructured.Unstructured {
func GetUnstructured(templateType TemplateType, templateName string) *unstructured.Unstructured {
GinkgoHelper()

setClusterName(templateName)
setClusterName(templateType)
setTemplate(templateName)

var managedClusterTemplateBytes []byte
switch templateName {
switch templateType {
case TemplateAWSStandaloneCP:
managedClusterTemplateBytes = awsStandaloneCPManagedClusterTemplateBytes
case TemplateAWSHostedCP:
Expand All @@ -137,7 +142,7 @@ func GetUnstructured(templateName Template) *unstructured.Unstructured {
case TemplateAzureStandaloneCP:
managedClusterTemplateBytes = azureStandaloneCPManagedClusterTemplateBytes
default:
Fail(fmt.Sprintf("Unsupported template: %s", templateName))
Fail(fmt.Sprintf("Unsupported template type: %s", templateType))
}

managedClusterConfigBytes, err := envsubst.Bytes(managedClusterTemplateBytes)
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/managedcluster/providervalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
// validate a provider's resources. Some providers do not support all of the
// resources that can potentially be validated.
type ProviderValidator struct {
// Template is the name of the template being validated.
template Template
// Template is the type of the template being validated.
template TemplateType
// Namespace is the namespace of the cluster to validate.
namespace string
// ClusterName is the name of the cluster to validate.
Expand All @@ -48,7 +48,7 @@ const (
ValidationActionDelete ValidationAction = "delete"
)

func NewProviderValidator(template Template, namespace, clusterName string, action ValidationAction) *ProviderValidator {
func NewProviderValidator(template TemplateType, namespace, clusterName string, action ValidationAction) *ProviderValidator {
var (
resourcesToValidate map[string]resourceValidationFunc
resourceOrder []string
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/managedcluster/resources/aws-hosted-cp.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: ManagedCluster
metadata:
name: ${MANAGED_CLUSTER_NAME}
spec:
template: aws-hosted-cp-0-0-2
template: ${MANAGED_CLUSTER_TEMPLATE}
credential: ${AWS_CLUSTER_IDENTITY}-cred
config:
clusterIdentity:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: ManagedCluster
metadata:
name: ${MANAGED_CLUSTER_NAME}
spec:
template: aws-standalone-cp-0-0-2
template: ${MANAGED_CLUSTER_TEMPLATE}
credential: ${AWS_CLUSTER_IDENTITY}-cred
config:
clusterIdentity:
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/managedcluster/resources/azure-hosted-cp.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: ${MANAGED_CLUSTER_NAME}
namespace: ${NAMESPACE}
spec:
template: azure-hosted-cp-0-0-2
template: ${MANAGED_CLUSTER_TEMPLATE}
credential: ${AZURE_CLUSTER_IDENTITY}-cred
config:
location: "${AZURE_REGION}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: ${MANAGED_CLUSTER_NAME}
namespace: ${NAMESPACE}
spec:
template: azure-standalone-cp-0-0-2
template: ${MANAGED_CLUSTER_TEMPLATE}
credential: ${AZURE_CLUSTER_IDENTITY}-cred
config:
controlPlaneNumber: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: ManagedCluster
metadata:
name: ${MANAGED_CLUSTER_NAME}
spec:
template: vsphere-hosted-cp-0-0-2
template: ${MANAGED_CLUSTER_TEMPLATE}
credential: ${VSPHERE_CLUSTER_IDENTITY}-cred
config:
controlPlaneNumber: ${CONTROL_PLANE_NUMBER:=1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: ManagedCluster
metadata:
name: ${MANAGED_CLUSTER_NAME}
spec:
template: vsphere-standalone-cp-0-0-2
template: ${MANAGED_CLUSTER_TEMPLATE}
credential: ${VSPHERE_CLUSTER_IDENTITY}-cred
config:
controlPlaneNumber: ${CONTROL_PLANE_NUMBER:=1}
Expand Down
23 changes: 19 additions & 4 deletions test/e2e/provider_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
. "github.com/onsi/gomega"

internalutils "github.com/Mirantis/hmc/internal/utils"
"github.com/Mirantis/hmc/test/e2e/config"
"github.com/Mirantis/hmc/test/e2e/kubeclient"
"github.com/Mirantis/hmc/test/e2e/managedcluster"
"github.com/Mirantis/hmc/test/e2e/managedcluster/aws"
Expand All @@ -43,9 +44,22 @@ var _ = Describe("AWS Templates", Label("provider:cloud", "provider:aws"), Order
hostedDeleteFunc func() error
kubecfgDeleteFunc func() error
clusterName string

testingConfig config.ProviderTestingConfig
)

BeforeAll(func() {
By("get testing configuration")
testingConfig = config.Config[config.TestingProviderAWS]

By("find and validate templates")
err := templates.Set(&testingConfig.Standalone, clusterTemplates, managedcluster.TemplateAWSStandaloneCP)
Expect(err).NotTo(HaveOccurred())
err = templates.Set(&testingConfig.Hosted, clusterTemplates, managedcluster.TemplateAWSHostedCP)
Expect(err).NotTo(HaveOccurred())

_, _ = fmt.Fprintf(GinkgoWriter, "Final AWS testing configuration:\n%s\n", testingConfig.String())

By("providing cluster identity")
kc = kubeclient.NewFromLocal(internalutils.DefaultSystemNamespace)
ci := clusteridentity.New(kc, managedcluster.ProviderAWS, managedcluster.Namespace)
Expand Down Expand Up @@ -81,8 +95,9 @@ var _ = Describe("AWS Templates", Label("provider:cloud", "provider:aws"), Order
// hosting the hosted cluster.
GinkgoT().Setenv(managedcluster.EnvVarAWSInstanceType, "t3.xlarge")

templateBy(managedcluster.TemplateAWSStandaloneCP, "creating a ManagedCluster")
sd := managedcluster.GetUnstructured(managedcluster.TemplateAWSStandaloneCP)
templateBy(managedcluster.TemplateAWSStandaloneCP, fmt.Sprintf("creating a ManagedCluster with %s template", testingConfig.Standalone.Template))
sd := managedcluster.GetUnstructured(managedcluster.TemplateAWSStandaloneCP, testingConfig.Standalone.Template)

clusterName = sd.GetName()

standaloneDeleteFunc = kc.CreateManagedCluster(context.Background(), sd, managedcluster.Namespace)
Expand Down Expand Up @@ -138,8 +153,8 @@ var _ = Describe("AWS Templates", Label("provider:cloud", "provider:aws"), Order
// cluster.
aws.PopulateHostedTemplateVars(context.Background(), kc, clusterName)

templateBy(managedcluster.TemplateAWSHostedCP, "creating a ManagedCluster")
hd := managedcluster.GetUnstructured(managedcluster.TemplateAWSHostedCP)
templateBy(managedcluster.TemplateAWSHostedCP, fmt.Sprintf("creating a ManagedCluster with %s template", testingConfig.Hosted.Template))
hd := managedcluster.GetUnstructured(managedcluster.TemplateAWSHostedCP, testingConfig.Hosted.Template)
hdName := hd.GetName()

// Deploy the hosted cluster on top of the standalone cluster.
Expand Down
22 changes: 18 additions & 4 deletions test/e2e/provider_azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
. "github.com/onsi/gomega"

internalutils "github.com/Mirantis/hmc/internal/utils"
"github.com/Mirantis/hmc/test/e2e/config"
"github.com/Mirantis/hmc/test/e2e/kubeclient"
"github.com/Mirantis/hmc/test/e2e/managedcluster"
"github.com/Mirantis/hmc/test/e2e/managedcluster/azure"
Expand All @@ -44,9 +45,22 @@ var _ = Context("Azure Templates", Label("provider:cloud", "provider:azure"), Or
kubecfgDeleteFunc func() error
hostedKubecfgDeleteFunc func() error
sdName string

testingConfig config.ProviderTestingConfig
)

BeforeAll(func() {
By("get testing configuration")
testingConfig = config.Config[config.TestingProviderAzure]

By("find and validate templates")
err := templates.Set(&testingConfig.Standalone, clusterTemplates, managedcluster.TemplateAzureStandaloneCP)
Expect(err).NotTo(HaveOccurred())
err = templates.Set(&testingConfig.Hosted, clusterTemplates, managedcluster.TemplateAzureHostedCP)
Expect(err).NotTo(HaveOccurred())

_, _ = fmt.Fprintf(GinkgoWriter, "Final Azure testing configuration:\n%s\n", testingConfig.String())

By("ensuring Azure credentials are set")
kc = kubeclient.NewFromLocal(internalutils.DefaultSystemNamespace)
ci := clusteridentity.New(kc, managedcluster.ProviderAzure, managedcluster.Namespace)
Expand Down Expand Up @@ -81,8 +95,8 @@ var _ = Context("Azure Templates", Label("provider:cloud", "provider:azure"), Or
})

It("should work with an Azure provider", func() {
templateBy(managedcluster.TemplateAzureStandaloneCP, "creating a ManagedCluster")
sd := managedcluster.GetUnstructured(managedcluster.TemplateAzureStandaloneCP)
templateBy(managedcluster.TemplateAzureStandaloneCP, fmt.Sprintf("creating a ManagedCluster with %s template", testingConfig.Standalone.Template))
sd := managedcluster.GetUnstructured(managedcluster.TemplateAzureStandaloneCP, testingConfig.Standalone.Template)
sdName = sd.GetName()

standaloneDeleteFunc := kc.CreateManagedCluster(context.Background(), sd, managedcluster.Namespace)
Expand All @@ -103,7 +117,7 @@ var _ = Context("Azure Templates", Label("provider:cloud", "provider:azure"), Or
// setup environment variables for deploying the hosted template (subnet name, etc)
azure.SetAzureEnvironmentVariables(sdName, kc)

hd := managedcluster.GetUnstructured(managedcluster.TemplateAzureHostedCP)
hd := managedcluster.GetUnstructured(managedcluster.TemplateAzureHostedCP, testingConfig.Hosted.Template)
hdName := hd.GetName()

var kubeCfgPath string
Expand Down Expand Up @@ -136,7 +150,7 @@ var _ = Context("Azure Templates", Label("provider:cloud", "provider:azure"), Or
By("Create default storage class for azure-disk CSI driver")
azure.CreateDefaultStorageClass(standaloneClient)

templateBy(managedcluster.TemplateAzureHostedCP, "creating a ManagedCluster")
templateBy(managedcluster.TemplateAzureHostedCP, fmt.Sprintf("creating a ManagedCluster with %s template", testingConfig.Hosted.Template))
hostedDeleteFunc = standaloneClient.CreateManagedCluster(context.Background(), hd, managedcluster.Namespace)

templateBy(managedcluster.TemplateAzureHostedCP, "Patching AzureCluster to ready")
Expand Down
20 changes: 18 additions & 2 deletions test/e2e/provider_vsphere_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ package e2e

import (
"context"
"fmt"
"os"
"time"

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

internalutils "github.com/Mirantis/hmc/internal/utils"
"github.com/Mirantis/hmc/test/e2e/config"
"github.com/Mirantis/hmc/test/e2e/kubeclient"
"github.com/Mirantis/hmc/test/e2e/managedcluster"
"github.com/Mirantis/hmc/test/e2e/managedcluster/clusteridentity"
"github.com/Mirantis/hmc/test/e2e/managedcluster/vsphere"
"github.com/Mirantis/hmc/test/templates"
)

var _ = Context("vSphere Templates", Label("provider:onprem", "provider:vsphere"), Ordered, func() {
Expand All @@ -35,9 +38,22 @@ var _ = Context("vSphere Templates", Label("provider:onprem", "provider:vsphere"
deleteFunc func() error
clusterName string
err error

testingConfig config.ProviderTestingConfig
)

BeforeAll(func() {
By("get testing configuration")
testingConfig = config.Config[config.TestingProviderVsphere]

By("find and validate templates")
err := templates.Set(&testingConfig.Standalone, clusterTemplates, managedcluster.TemplateVSphereStandaloneCP)
Expect(err).NotTo(HaveOccurred())
err = templates.Set(&testingConfig.Hosted, clusterTemplates, managedcluster.TemplateVSphereHostedCP)
Expect(err).NotTo(HaveOccurred())

_, _ = fmt.Fprintf(GinkgoWriter, "Final Vsphere testing configuration:\n%s\n", testingConfig.String())

By("ensuring that env vars are set correctly")
vsphere.CheckEnv()
By("creating kube client")
Expand Down Expand Up @@ -80,8 +96,8 @@ var _ = Context("vSphere Templates", Label("provider:onprem", "provider:vsphere"
})

It("should deploy standalone managed cluster", func() {
By("creating a managed cluster")
d := managedcluster.GetUnstructured(managedcluster.TemplateVSphereStandaloneCP)
By(fmt.Sprintf("creating a managed cluster with %s template", testingConfig.Standalone.Template))
d := managedcluster.GetUnstructured(managedcluster.TemplateVSphereStandaloneCP, testingConfig.Standalone.Template)
clusterName = d.GetName()

deleteFunc = kc.CreateManagedCluster(context.Background(), d, managedcluster.Namespace)
Expand Down
2 changes: 1 addition & 1 deletion test/objects/templatechain/templatechain.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package templatemanagement
package templatechain

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
Loading

0 comments on commit 64dc32a

Please sign in to comment.