Documentation ¶
Overview ¶
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) { suite.teardown(t) }
Index ¶
- Constants
- Variables
- func AddServicePort(t *testing.T, svc string, port *ServicePort)
- func DefaultNamespace() string
- func ExtractSimplePath(data interface{}, path string) interface{}
- func GetNodes(t *testing.T) []corev1.Node
- func HTTPDoWithCustomValidation(t *testing.T, url string, validate func(int, string) bool)
- func ProxyURL(t *testing.T, serviceName, portName string, https bool) string
- func RunInfraTests(t *testing.T, suite InfraTestSuite)
- func ServiceToClusterIP(t *testing.T, svc string) error
- func ServiceToNodeport(t *testing.T, svc string, svcPort int16) (bool, string, error)
- func Setup(t *testing.T) func(t *testing.T)
- func WaitUntilPodAvailable(t *testing.T, podName string)
- func WaitUntilServiceAvailable(t *testing.T, serviceName string)
- type HTTPProfile
- type InfraTestSuite
- type RawJSON
- type RawList
- type ServicePort
- type SmokeTestSuite
Constants ¶
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" )
const ( // VarToken contains the name of the environment variable // containing a bearer token to authenticate against the // k8s API VarToken = "TOKEN" )
Variables ¶
var KubectlOpts *k8s.KubectlOptions
KubectlOpts contains the current kubectl config
Functions ¶
func AddServicePort ¶ added in v0.7.0
func AddServicePort(t *testing.T, svc string, port *ServicePort)
appends a service port to the given service
func DefaultNamespace ¶ added in v0.0.6
func DefaultNamespace() 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 interface{}, path string) interface{}
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 HTTPDoWithCustomValidation ¶
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 ¶
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 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 ¶ added in v0.4.0
ServiceToClusterIP patches the given service to type 'ClusterIP' will return error in case something went wrong
func ServiceToNodeport ¶ added in v0.4.0
ServiceToNodeport patches the given service to type 'NodePort' parameters:
svc - the name of the service to patch svcPort - the service port
returns:
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 ¶
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 ¶ added in v0.6.0
WaitUntilPodAvailable wraps the equally named terratest function: will retry 10 times with 5secs wait period in-between
func WaitUntilServiceAvailable ¶
WaitUntilServiceAvailable wraps the equally named terratest function: will retry 10 times with 2secs wait period in-between
Types ¶
type HTTPProfile ¶
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 ¶ added in v0.6.1
type RawJSON = map[string]interface{}
func UnmarshalJSON ¶
UnmarshalJSON does a raw unmarshalling of the given input into a map (of maps actually)
type ServicePort ¶ added in v0.7.0
type ServicePort struct { Name string `json:"name"` TargetPort string `json:"targetPort"` Port uint16 `json:"port"` }
ServicePort maps k8s service port data
type SmokeTestSuite ¶ added in v0.1.1
type SmokeTestSuite struct {
// contains filtered or unexported fields
}
SmokeTestSuite implements InfraTestSuite and will be the receiver of infrastructure test functions
func (*SmokeTestSuite) Setup ¶ added in v0.1.1
func (suite *SmokeTestSuite) Setup(t *testing.T)
Setup implements InfratestSuite.Setup
func (*SmokeTestSuite) TearDown ¶ added in v0.1.1
func (suite *SmokeTestSuite) TearDown(t *testing.T)
TearDown implements InfratestSuite.TearDown