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
- func DefaultNamespace() string
- func ExtractSimplePath(data interface{}, path string) interface{}
- func HTTPDoWithCustomValidation(t *testing.T, url string, validate func(int, string) bool)
- func ProxyURL(t *testing.T, serviceName, portName string) string
- func RunInfraTests(t *testing.T, suite InfraTestSuite)
- func Setup(t *testing.T) func(t *testing.T)
- func UnmarshalJSON(data []byte) (map[string]interface{}, error)
- func WaitUntilServiceAvailable(t *testing.T, serviceName string)
- type HTTPProfile
- type InfraTestSuite
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 ¶
This section is empty.
Functions ¶
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 slurping in the helmfile and reading from there
NB: this will obviously break if helmfile structure changes
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 reponse 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 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 UnmarshalJSON ¶
UnmarshalJSON does a raw unmarshalling of the given input into a map (of maps actually)
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)