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 15, 2024
1 parent b9b316a commit acef00e
Show file tree
Hide file tree
Showing 19 changed files with 386 additions and 88 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ tidy:

.PHONY: test
test: generate-all fmt vet envtest tidy external-crd ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e/scenarios) -coverprofile cover.out

# E2E_CONFIG_B64 contains the configuration for e2e testing.
E2E_CONFIG_B64 ?= ""
Expand Down
24 changes: 24 additions & 0 deletions test/e2e/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"gopkg.in/yaml.v3"

hmc "github.com/Mirantis/hmc/api/v1alpha1"
"github.com/Mirantis/hmc/test/e2e/templates"
)

type TestingProvider string
Expand Down Expand Up @@ -72,6 +75,27 @@ func Parse() error {
return nil
}

func (c *ClusterTestingConfig) SetDefaults(clusterTemplates map[string][]hmc.AvailableUpgrade, templateType templates.Type) error {
var err error
if !c.Upgrade {
if c.Template == "" {
c.Template, err = templates.FindTemplate(clusterTemplates, templateType)
if err != nil {
return err
}
}
return templates.ValidateTemplate(clusterTemplates, c.Template)
}
if c.Template != "" && c.UpgradeTemplate != "" {
return templates.ValidateUpgradeSequence(clusterTemplates, c.Template, c.UpgradeTemplate)
}
c.Template, c.UpgradeTemplate, err = templates.FindTemplatesToUpgrade(clusterTemplates, templateType, c.Template)
if err != nil {
return err
}
return nil
}

func (c *ProviderTestingConfig) String() string {
prettyConfig, err := yaml.Marshal(c)
Expect(err).NotTo(HaveOccurred())
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
43 changes: 19 additions & 24 deletions test/e2e/managedcluster/managedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/Mirantis/hmc/test/e2e/templates"
"github.com/Mirantis/hmc/test/utils"
)

Expand All @@ -43,17 +44,6 @@ const (
Namespace = "default"
)

type Template 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"
)

//go:embed resources/aws-standalone-cp.yaml.tpl
var awsStandaloneCPManagedClusterTemplateBytes []byte

Expand Down Expand Up @@ -86,38 +76,43 @@ func GetProviderLabel(provider ProviderType) string {
return fmt.Sprintf("%s=%s", providerLabel, provider)
}

func setClusterName(templateName Template) {
func setClusterName(templateType templates.Type) {
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 templates.Type, templateName string) *unstructured.Unstructured {
GinkgoHelper()

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

var managedClusterTemplateBytes []byte
switch templateName {
case TemplateAWSStandaloneCP:
switch templateType {
case templates.TemplateAWSStandaloneCP:
managedClusterTemplateBytes = awsStandaloneCPManagedClusterTemplateBytes
case TemplateAWSHostedCP:
case templates.TemplateAWSHostedCP:
// Validate environment vars that do not have defaults are populated.
// We perform this validation here instead of within a Before block
// since we populate the vars from standalone prior to this step.
Expand All @@ -128,16 +123,16 @@ func GetUnstructured(templateName Template) *unstructured.Unstructured {
EnvVarAWSSecurityGroupID,
})
managedClusterTemplateBytes = awsHostedCPManagedClusterTemplateBytes
case TemplateVSphereStandaloneCP:
case templates.TemplateVSphereStandaloneCP:
managedClusterTemplateBytes = vsphereStandaloneCPManagedClusterTemplateBytes
case TemplateVSphereHostedCP:
case templates.TemplateVSphereHostedCP:
managedClusterTemplateBytes = vsphereHostedCPManagedClusterTemplateBytes
case TemplateAzureHostedCP:
case templates.TemplateAzureHostedCP:
managedClusterTemplateBytes = azureHostedCPManagedClusterTemplateBytes
case TemplateAzureStandaloneCP:
case templates.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
15 changes: 8 additions & 7 deletions test/e2e/managedcluster/providervalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import (
. "github.com/onsi/ginkgo/v2"

"github.com/Mirantis/hmc/test/e2e/kubeclient"
"github.com/Mirantis/hmc/test/e2e/templates"
)

// ProviderValidator is a struct that contains the necessary information to
// 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 templates.Type
// Namespace is the namespace of the cluster to validate.
namespace string
// ClusterName is the name of the cluster to validate.
Expand All @@ -48,7 +49,7 @@ const (
ValidationActionDelete ValidationAction = "delete"
)

func NewProviderValidator(template Template, namespace, clusterName string, action ValidationAction) *ProviderValidator {
func NewProviderValidator(templateType templates.Type, namespace, clusterName string, action ValidationAction) *ProviderValidator {
var (
resourcesToValidate map[string]resourceValidationFunc
resourceOrder []string
Expand All @@ -63,11 +64,11 @@ func NewProviderValidator(template Template, namespace, clusterName string, acti
}
resourceOrder = []string{"clusters", "machines", "control-planes", "csi-driver"}

switch template {
case TemplateAWSStandaloneCP, TemplateAWSHostedCP:
switch templateType {
case templates.TemplateAWSStandaloneCP, templates.TemplateAWSHostedCP:
resourcesToValidate["ccm"] = validateCCM
resourceOrder = append(resourceOrder, "ccm")
case TemplateAzureStandaloneCP, TemplateVSphereStandaloneCP:
case templates.TemplateAzureStandaloneCP, templates.TemplateVSphereStandaloneCP:
delete(resourcesToValidate, "csi-driver")
}
} else {
Expand All @@ -80,7 +81,7 @@ func NewProviderValidator(template Template, namespace, clusterName string, acti
}

return &ProviderValidator{
template: template,
template: templateType,
namespace: namespace,
clusterName: clusterName,
resourcesToValidate: resourcesToValidate,
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
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 e2e
package scenarios

import (
. "github.com/onsi/ginkgo/v2"
Expand Down
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 e2e
package scenarios

import (
"bufio"
Expand All @@ -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/e2e/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 All @@ -74,7 +78,7 @@ var _ = BeforeSuite(func() {
}).WithTimeout(15 * time.Minute).WithPolling(10 * time.Second).Should(Succeed())

By(fmt.Sprintf("applying access rules for ClusterTemplates in %s namespace", managedcluster.Namespace))
clusterTemplates = templates.ApplyClusterTemplateAccessRules(ctx, kc.CrClient)
clusterTemplates = templates.ApplyClusterTemplateAccessRules(ctx, kc.CrClient, managedcluster.Namespace)
})

var _ = AfterSuite(func() {
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 templates.Type, description string) {
GinkgoHelper()
By(fmt.Sprintf("[%s] %s", t, description))
}
Expand Down
Loading

0 comments on commit acef00e

Please sign in to comment.