testutil

package
v18.7.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2018 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package testutil provides helpers for running the linkerd integration tests.

All helpers are defined as functions on the TestHelper struct, which you should instantiate once per test, using the NewTestHelper function. Since that function also parses command line flags, it should be called as part of your test's TestMain function. For example:

package mytest

import (
	"os"
	"testing"

	"github.com/linkerd/linkerd2/testutil"
)

var TestHelper *util.TestHelper

func TestMain(m *testing.M) {
  TestHelper = util.NewTestHelper()
  os.Exit(m.Run())
}

func TestMyTest(t *testing.T) {
	// add test code here
}

Calling NewTestHelper adds the following command line flags:

-linkerd string
		path to the linkerd binary to test
-linkerd-namespace string
		the namespace where linkerd is installed (default "linkerd")
-integration-tests
	must be provided to run the integration tests

Note that the -integration-tests flag must be set when running tests, so that the tests aren't inadvertently executed when unit tests for the project are run.

TestHelper embeds KubernetesHelper, so all functions defined on KubernetesHelper are also available to instances of TestHelper. See the individual function definitions for details on how to use each helper in tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KubernetesHelper

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

KubernetesHelper provides Kubernetes-related test helpers. It connects to the Kubernetes API using the environment's configured kubeconfig file.

func NewKubernetesHelper

func NewKubernetesHelper() (*KubernetesHelper, error)

NewKubernetesHelper creates a new instance of KubernetesHelper.

func (*KubernetesHelper) CheckDeployment

func (h *KubernetesHelper) CheckDeployment(namespace string, deploymentName string, replicas int) error

CheckDeployment checks that a deployment in a namespace contains the expected number of replicas.

func (*KubernetesHelper) CheckIfNamespaceExists

func (h *KubernetesHelper) CheckIfNamespaceExists(namespace string) error

CheckIfNamespaceExists checks if a namespace exists.

func (*KubernetesHelper) CheckPods

func (h *KubernetesHelper) CheckPods(namespace string, deploymentName string, replicas int) error

CheckPods checks that a deployment in a namespace contains the expected number of pods in the Running state.

func (*KubernetesHelper) CheckService

func (h *KubernetesHelper) CheckService(namespace string, serviceName string) error

CheckService checks that a service exists in a namespace.

func (*KubernetesHelper) CreateNamespaceIfNotExists

func (h *KubernetesHelper) CreateNamespaceIfNotExists(namespace string) error

CreateNamespaceIfNotExists creates a namespace if it does not already exist.

func (*KubernetesHelper) GetPodsForDeployment added in v0.5.0

func (h *KubernetesHelper) GetPodsForDeployment(namespace string, deploymentName string) ([]string, error)

GetPodsForDeployment returns all pods for the given deployment

func (*KubernetesHelper) KubectlApply

func (h *KubernetesHelper) KubectlApply(stdin string, namespace string) (string, error)

KubectlApply applies a given configuration string in a namespace. If the namespace does not exist, it creates it first. If no namespace is provided, it uses the default namespace.

func (*KubernetesHelper) ParseNamespacedResource

func (h *KubernetesHelper) ParseNamespacedResource(resource string) (string, string, error)

ParseNamespacedResource extracts a namespace and resource name from a string that's in the format namespace/resource. If the strings is in a different format it returns an error.

func (*KubernetesHelper) ProxyURLFor added in v0.4.4

func (h *KubernetesHelper) ProxyURLFor(namespace, service, port string) (string, error)

ProxyURLFor creates a kubernetes proxy, runs it, and returns the URL that tests can use for access to the given service. Note that the proxy remains running for the duration of the test.

type Stream

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

Stream provides the ability of read the output of an executing process while it is still running

func (*Stream) ReadUntil

func (s *Stream) ReadUntil(lineCount int, timeout time.Duration) ([]string, error)

ReadUntil reads from the process output until specified number of lines has been reached, or until a timeout

func (*Stream) Stop

func (s *Stream) Stop()

Stop closes the stream and kills the process

type TestHelper

type TestHelper struct {
	KubernetesHelper
	// contains filtered or unexported fields
}

TestHelper provides helpers for running the linkerd integration tests.

func NewTestHelper

func NewTestHelper() *TestHelper

NewTestHelper creates a new instance of TestHelper for the current test run. The new TestHelper can be configured via command line flags.

func (*TestHelper) CheckVersion

func (h *TestHelper) CheckVersion(serverVersion string) error

CheckVersion validates the the output of the "linkerd version" command.

func (*TestHelper) CombinedOutput

func (h *TestHelper) CombinedOutput(name string, arg ...string) (string, error)

CombinedOutput executes a shell command and returns the output.

func (*TestHelper) GetLinkerdNamespace

func (h *TestHelper) GetLinkerdNamespace() string

GetLinkerdNamespace returns the namespace where linkerd is installed. Set the namespace using the -linkerd-namespace command line flag.

func (*TestHelper) GetTestNamespace

func (h *TestHelper) GetTestNamespace(testName string) string

GetTestNamespace returns the namespace for the given test. The test namespace is prefixed with the linkerd namespace.

func (*TestHelper) GetVersion

func (h *TestHelper) GetVersion() string

GetVersion returns the version of linkerd to test. This version corresponds to the client version of the linkerd binary provided via the -linkerd command line flag.

func (*TestHelper) HTTPGetURL

func (h *TestHelper) HTTPGetURL(url string) (string, error)

HTTPGetURL sends a GET request to the given URL. It returns the response body in the event of a successful 200 response. In the event of a non-200 response, it returns an error. It retries requests for up to 30 seconds, giving pods time to start.

func (*TestHelper) LinkerdRun

func (h *TestHelper) LinkerdRun(arg ...string) (string, error)

LinkerdRun executes a linkerd command appended with the --linkerd-namespace flag.

func (*TestHelper) LinkerdRunStream

func (h *TestHelper) LinkerdRunStream(arg ...string) (*Stream, error)

LinkerdRunStream initiates a linkerd command appended with the --linkerd-namespace flag, and returns a Stream that can be used to read the command's output while it is still executing.

func (*TestHelper) RetryFor

func (h *TestHelper) RetryFor(timeout time.Duration, fn func() error) error

RetryFor retries a given function every second until the function returns without an error, or a timeout is reached. If the timeout is reached, it returns the last error received from the function.

func (*TestHelper) TLS added in v0.5.0

func (h *TestHelper) TLS() bool

TLS returns whether or not TLS is enabled for the given test.

func (*TestHelper) ValidateOutput

func (h *TestHelper) ValidateOutput(out, fixtureFile string) error

ValidateOutput validates a string against the contents of a file in the test's testdata directory.

Jump to

Keyboard shortcuts

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