clustertest

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2023 License: Apache-2.0 Imports: 14 Imported by: 7

README

clustertest

A test framework for helping with E2E testing of Giant Swarm clusters.

Installation

go get github.com/giantswarm/clustertest

Features

  • Kubernetes client for interacting with the Management Cluster
  • Kubernetes client for interacting with the Workload Clusters
  • Wrapper types around Apps and their respective values ConfigMaps
  • Wrapper types around Cluster apps (and their default-apps)
  • Management (creation and deletion) or Organization resources
  • Wait and polling helpers

Documentation

Documentation can be found at: pkg.go.dev/github.com/giantswarm/clustertest.

Example Usage

ctx := context.Background()

framework, err := clustertest.New("capa_standard")
if err != nil {
  panic(err)
}

cluster := application.NewClusterApp(utils.GenerateRandomName("t"), application.ProviderAWS).
  WithOrg(organization.NewRandomOrg()).
  WithAppValuesFile(
    path.Clean("./test_data/cluster_values.yaml"),
    path.Clean("./test_data/default-apps_values.yaml"),
  )

client, err := framework.ApplyCluster(ctx, cluster)

// Run tests...

err = framework.DeleteCluster(ctx, cluster)

Documentation

Overview

package clustertest provides the main entry point to the framework for E2E cluster testing.

The Framework is configured around a Management Cluster that is used for the creation of test workload clusters.

Example

ctx := context.Background()

framework, err := clustertest.New("context_name")
if err != nil {
	panic(err)
}

cluster := application.NewClusterApp(utils.GenerateRandomName("t"), application.ProviderAWS).
	WithOrg(organization.NewRandomOrg()).
	WithAppVersions("", ""). // If not set, the latest is fetched
	WithAppValuesFile(path.Clean("./test_data/cluster_values.yaml"), path.Clean("./test_data/default-apps_values.yaml"))

client, err := framework.ApplyCluster(ctx, cluster)

// Run tests...

err = framework.DeleteCluster(ctx, cluster)

Example Using Ginkgo

func TestCAPA(t *testing.T) {
	var err error
	ctx := context.Background()

	framework, err = clustertest.New("context_name")
	if err != nil {
		panic(err)
	}
	logger.LogWriter = GinkgoWriter

	cluster = application.NewClusterApp(utils.GenerateRandomName("t"), application.ProviderAWS).
		WithOrg(organization.NewRandomOrg()).
		WithAppVersions("", ""). // If not set, the latest is fetched
		WithAppValuesFile(path.Clean("./test_data/cluster_values.yaml"), path.Clean("./test_data/default-apps_values.yaml"))

	BeforeSuite(func() {
		client, err := framework.ApplyCluster(ctx, cluster)
		Expect(err).To(BeNil())

		Eventually(
			wait.IsNumNodesReady(ctx, client, 3, &cr.MatchingLabels{"node-role.kubernetes.io/control-plane": ""}),
			20*time.Minute,
			30*time.Second,
		).Should(BeTrue())
	})

	AfterSuite(func() {
		err := framework.DeleteCluster(ctx, cluster)
		Expect(err).To(BeNil())
	})

	RegisterFailHandler(Fail)
	RunSpecs(t, "CAPA Suite")
}

Index

Constants

View Source
const (
	KubeconfigEnvVar = "E2E_KUBECONFIG"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Framework

type Framework struct {
	// contains filtered or unexported fields
}

Framework is the overall framework for testing of clusters

func New

func New(contextName string) (*Framework, error)

New initializes a new Framework instance using the provided context from the kubeconfig found in the env var `E2E_KUBECONFIG`

func (*Framework) ApplyCluster

func (f *Framework) ApplyCluster(ctx context.Context, cluster *application.Cluster) (*client.Client, error)

ApplyCluster takes a Cluster object, applies it to the MC in the correct order and then waits for a valid Kubeconfig to be available

A timeout can be provided via the given `ctx` value by using `context.WithTimeout()`

Example:

timeoutCtx, cancelTimeout := context.WithTimeout(context.Background(), 20*time.Minute)
defer cancelTimeout()

cluster := application.NewClusterApp(utils.GenerateRandomName("t"), application.ProviderAWS)

client, err := framework.ApplyCluster(timeoutCtx, cluster)

func (*Framework) CreateOrg

func (f *Framework) CreateOrg(ctx context.Context, org *organization.Org) error

CreateOrg create a new Organization in the MC (which then triggers the creation of the org namespace)

func (*Framework) DeleteCluster

func (f *Framework) DeleteCluster(ctx context.Context, cluster *application.Cluster) error

DeleteCluster removes the Cluster app from the MC

func (*Framework) DeleteOrg

func (f *Framework) DeleteOrg(ctx context.Context, org *organization.Org) error

DeleteOrg deletes an Organization from the MC, waiting for all Clusters in the org namespace to be deleted first

func (*Framework) MC

func (f *Framework) MC() *client.Client

MC returns an initialized client for the Management Cluster

func (*Framework) WC

func (f *Framework) WC(clusterName string) (*client.Client, error)

WC returns an initialized client for the Workload Cluster matching the given name. If no Workload Cluster is found matching the given name an error is returned.

func (*Framework) WaitForClusterReady

func (f *Framework) WaitForClusterReady(ctx context.Context, clusterName string, namespace string) (*client.Client, error)

WaitForClusterReady watches for a Kubeconfig secret to be created on the MC and then waits until that cluster's api-server response successfully

A timeout can be provided via the given `ctx` value by using `context.WithTimeout()`

Example:

timeoutCtx, cancelTimeout := context.WithTimeout(context.Background(), 20*time.Minute)
defer cancelTimeout()

wcClient, err := framework.WaitForClusterReady(timeoutCtx, "test-cluster", "default")

func (*Framework) WaitForControlPlane

func (f *Framework) WaitForControlPlane(ctx context.Context, c *client.Client, expectedNodes int) error

WaitForControlPlane polls the provided cluster and waits until the provided number of Control Plane nodes are reporting as ready

Example:

timeoutCtx, cancelTimeout := context.WithTimeout(context.Background(), 20*time.Minute)
defer cancelTimeout()

err := framework.WaitForControlPlane(timeoutCtx, wcClient, 3)

Directories

Path Synopsis
pkg
application
package application provides wrapper types around the concept of an App and its associated values ConfigMap.
package application provides wrapper types around the concept of an App and its associated values ConfigMap.
client
package client provides a thin wrapper around the controller-runtime client.
package client provides a thin wrapper around the controller-runtime client.
logger
package logger inplements a logging function for use within the test framework and can be used within test cases.
package logger inplements a logging function for use within the test framework and can be used within test cases.
organization
package organization implements types to handle creation and deletion of Organization resources.
package organization implements types to handle creation and deletion of Organization resources.
utils
package utils contain miscellaneous utility functions that can be useful when writing tests or working with this test framework.
package utils contain miscellaneous utility functions that can be useful when writing tests or working with this test framework.
wait
package wait provides functions to help with waiting for certain conditions to be true.
package wait provides functions to help with waiting for certain conditions to be true.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL