v0.25.5 Latest Latest

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

Go to latest
Published: Jun 23, 2023 License: Apache-2.0 Imports: 33 Imported by: 0



Package common provides frequently used metaphors to be used in infrastructure tests:

- means to define test suites with common setup and teardown functions

- setting up and tearing down test environments

- implicit handling of k8s access control

- convenience for dealing with http connections and result verification

How to define a test suite:

// define a custom type
type SampleTestSuite struct {

// Setup is called once per suite
// before running any test
func (suite *SampleTestSuite) Setup(t *testing.T) {
	// suite setup code goes here

// TearDown is called once per suite
// after running all the tests
func (suite *SampleTestSuite) TearDown(t *testing.T) {
	// suite teardown code goes here

// individual tests must be prefixed with `InfraTest`

func (suite *SampleTestSuite) InfraTestSomething(t *testing.T) {
	// test code goes here

func (suite *SampleTestSuite) InfraTestSomethingElse(t *testing.T) {
	// test code goes here

// finally, add the main test function
func TestMain(t *testing.T) {
	s11.RunInfraTests(t, &SampleTestSuite{})

How to deal with setup/teardown:

import "code.syseleven.de/syseleven/common"

type SampleTestSuite struct {
	// keep the reference to the teardown
	// function returned by common.Setup
	teardown func(t *testing.T)

func (suite *SampleTestSuite) Setup(t *testing.T) {
	suite.teardown = common.Setup(t)

func (suite *SampleTestSuite) TearDown(t *testing.T) {



View Source
const (
	// VarSkipSetup contains the name of the environment variable
	// controlling environment setup, set to yes or true to skip the helmfile
	// setup / teardown
	VarSkipSetup = "SKIP_SETUP"
	// VarHelmfilePath contains the name of the environment variable
	// containing a path to a helmfile
	VarHelmfilePath = "HELMFILE_PATH"
View Source
const (
	// VarToken contains the name of the environment variable
	// containing a bearer token to authenticate against the
	// k8s API
	VarToken = "TOKEN"


View Source
var KubectlOpts *k8s.KubectlOptions

KubectlOpts contains the current kubectl config


func AddServicePort

func AddServicePort(t *testing.T, svc string, port *ServicePort)

appends a service port to the given service

func DefaultNamespace

func DefaultNamespace(t *testing.T) string

DefaultNamespace tries to find the correct k8s namespace to use for all cluster operations by rendering and slurping in the helmfile and reading from there

returns a string containing the k8s namespace to use for tests or an empty string in case something went wrong

func ExtractSimplePath

func ExtractSimplePath(data any, path string) any

ExtractSimplePath traverses a map of maps (of maps ..) using a simple JSON path of the form 'x.y.z' to extract an (untyped) value (data[x][y][z])

func GetDeployment

func GetDeployment(t *testing.T, name string) *appsv1.Deployment

GetDeployment returns the deployment object for the given name in the default namespace

func GetNodes

func GetNodes(t *testing.T) []corev1.Node

GetNodes wraps the equally named terratest function

func GetStatefulSet

func GetStatefulSet(t *testing.T, name string) *appsv1.StatefulSet

GetStatefulSet returns the stateful set object for the given name in the default namespace

func HTTPDoWithCustomValidation

func HTTPDoWithCustomValidation(t *testing.T, url string, validate func(int, string) bool)

HTTPDoWithCustomValidation is a convenience facade for the equally named terratest function fires a GET request to the given URL and validates the response using the given validation function

func ProxyURL

func ProxyURL(t *testing.T, serviceName, portName string, https bool) string

ProxyURL assembles the k8s API URL to a services proxy subresource see https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-services/#manually-constructing-apiserver-proxy-urls

func RemoveLastServicePort

func RemoveLastServicePort(t *testing.T, svc string)

removes the last (added) service port from the given service

func RunInfraTests

func RunInfraTests(t *testing.T, suite InfraTestSuite)

RunInfraTests must be called explicitly in order to run all infrastructure tests defined for a given test suite

NB: all individual test functions must be prefixed with 'InfraTest'

func ServiceToClusterIP

func ServiceToClusterIP(t *testing.T, svc string) error

ServiceToClusterIP patches the given service to type 'ClusterIP' will return error in case something went wrong

func ServiceToNodeport

func ServiceToNodeport(t *testing.T, svc string, svcPort int16) (bool, string, error)

ServiceToNodeport patches the given service to type 'NodePort' parameters:

svc - the name of the service to patch svcPort - the service port


bool - true if the service was actually patched, false otherwise string - the address for the nodeport service "$external-node-ip:$nodeport" error - will be non-nil in case of error

func Setup

func Setup(t *testing.T) func(t *testing.T)

Setup is the default test suite setup function

if `SKIP_SETUP` is set to true (or yes), virtually nothing will be done otherwise `helmfile apply` using the building-blocks helmfile will be commenced using a randomly generated namespace

`HELMFILE_PATH` can optionally be set to an alternative helmfile a proper teardown function will be returned

Example use:

func TestSomething(t *testing.T) {
	teardown := Setup(t)
	defer teardown(t)
	// test code

func WaitUntilPodAvailable

func WaitUntilPodAvailable(t *testing.T, podName string)

WaitUntilPodAvailable wraps the equally named terratest function: will retry 10 times with 5secs wait period in-between

func WaitUntilServiceAvailable

func WaitUntilServiceAvailable(t *testing.T, serviceName string)

WaitUntilServiceAvailable wraps the equally named terratest function: will retry 10 times with 2secs wait period in-between


type HTTPProfile

type HTTPProfile struct {
	Token, URL string
	TLSConfig  *tls.Config

HTTPProfile encloses necessary data for commencing a request to the k8s API as extracted from the current kubectl configuration

func GetHTTPProfile

func GetHTTPProfile(t *testing.T) *HTTPProfile

GetHTTPProfile extracts all data necessary to commence HTTP requests against the k8s API if TOKEN is set, its value will be used as a bearer token (and has precedence over authentication credentials found in the kubectl configuration)

type InfraTestSuite

type InfraTestSuite interface {
	// will be called once before each test suite
	Setup(t *testing.T)
	// will be called once after each test suite
	TearDown(t *testing.T)

InfraTestSuite defines the basic interface all test suites must implement

type RawJSON

type RawJSON = map[string]interface{}

func UnmarshalJSON

func UnmarshalJSON(data []byte) (RawJSON, error)

UnmarshalJSON does a raw unmarshalling of the given input into a map (of maps actually)

type RawList

type RawList = []interface{}

type ServicePort

type ServicePort struct {
	Name       string `json:"name"`
	TargetPort string `json:"targetPort"`
	Port       uint16 `json:"port"`

ServicePort maps k8s service port data

type SmokeTestSuite

type SmokeTestSuite struct {
	// contains filtered or unexported fields

SmokeTestSuite implements InfraTestSuite and will be the receiver of infrastructure test functions

func (*SmokeTestSuite) Setup

func (suite *SmokeTestSuite) Setup(t *testing.T)

Setup implements InfratestSuite.Setup

func (*SmokeTestSuite) TearDown

func (suite *SmokeTestSuite) TearDown(t *testing.T)

TearDown implements InfratestSuite.TearDown

Jump to

Keyboard shortcuts

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