test

package
v3.15.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: Apache-2.0 Imports: 16 Imported by: 0

README

Integration tests

Use these at your own risk.

Reasons why you should be careful with running this locally:

  • it creates a foobar directory which although is reverted in a defer, defers don't seem to work too well in tests
  • it executes go gets from micro run output which might or might not modify your go.mod

The tests in this folder can be ran with go test --tags=integration. It's not being triggered by go test.

Architecture

Key points:

  • tests are being run in parallel, with different micro servers running in different containers
  • local micro run commands will be executed with different env flags, eg. micro -env=testConfigReadFromService run . to connect to the above different servers.

Working with these tests

Although the tests run in docker, the containers and envs are named so you can easily interact with them. Some useful tricks:

First, we have to build a local docker image and check out the services repo:

bash scripts/test-docker.sh
bash scripts/checkout-services.sh

To start a test, cd into the test folder and then:

go clean -testcache && go test --tags=integration  -failfast -v -run TestServerAuth$
$ docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                                                        NAMES
1e6a3003ea94        micro                  "sh /bin/run.sh serv…"   4 seconds ago       Up 1 second         2379-2380/tcp, 4001/tcp, 7001/tcp, 0.0.0.0:14081->8081/tcp   testServerAuth

As it can be seen the container name is the same as the test name. The server output can be seen with docker logs -f testServerAuth.

The tests also add the env into the micro config file:

$ micro env
  local               none
* server              127.0.0.1:8081
  platform            proxy.m3o.com
  testServerAuth      127.0.0.1:14081

This means we can also interact with the server running in the container in the following way:

$ micro -env=testServerAuth status

The loop script can be used to test for flakiness:

cd test; bash loop.sh

or to run all tests once:

go clean -testcache && go test --tags=integration -v ./...

K8s integration tests

We can run a number of integration tests against a k8s cluster rather than the default runtime implementation. We use a Kind (https://kind.sigs.k8s.io/) cluster to run a local cluster and then run the platform install scripts (with a few minor modifications).

Running locally
Pre-reqs

To run the k8s integration tests locally you need to first install the pre-reqs:

Running the tests

The tests can then be run:

  1. kind create cluster - create the cluster
  2. ./scripts/kind-launch.sh - install micro in to the cluster
  3. cd test && go clean -testcache && IN_TRAVIS_CI=yes go test --tags=integration,kind -v -run ./... - run the tests
Adding more tests

Not all integration tests use a server so only a subset of the tests need to run against our Kind cluster. New tests should be defined in the usual way and then added to the testFilter slice defined near the top of kind.go. This is the list of all tests to be run against Kind.

Running a local registry

If you prefer not having to push your images to docker hub for them to be pulled down by your Kind cluster, you can run a local registry and build and push your images to it. We have some handy scripts to get it working.

  1. ./scripts/kind-local-reg.sh - install and run a local registry, set up and launch the cluster to use it
  2. ./scripts/kind-build-micro.sh - build and push micro to the local registry
  3. ./scripts/kind-launch.sh - install micro in to the cluster

When you make any changes you can build and push using kind-build-micro.sh and then bounce all the micro pods kubectl delete po -l micro=runtime to pick up the new version.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChangeNamespace

func ChangeNamespace(cmd *Command, env, namespace string) error

func Login

func Login(serv Server, t *T, email, password string) error

func Try

func Try(blockName string, t *T, f cmdFunc, maxTime time.Duration) error

try is designed with command line executions in mind Error should be checked and a simple `return` from the test case should happen without calling `t.Fatal`. The error value should be disregarded.

func TrySuite

func TrySuite(t *testing.T, f func(t *T), times int)

TrySuite is designed to retry a TestXX function

Types

type Command

type Command struct {
	Env    string
	Config string
	Dir    string

	sync.Mutex
	// contains filtered or unexported fields
}

func (*Command) Exec

func (c *Command) Exec(args ...string) ([]byte, error)

Exec executes a command inline

func (*Command) Output

func (c *Command) Output() ([]byte, error)

Output of a running command

func (*Command) Start

func (c *Command) Start(args ...string) error

Starts a new command

func (*Command) Stop

func (c *Command) Stop() error

Stop a command thats running

type NewServerFunc

type NewServerFunc func(t *T, fname string, opts ...Option) Server

type Option

type Option func(o *Options)

func WithDisableAdmin

func WithDisableAdmin() Option

func WithLogin

func WithLogin() Option

func WithNamespace

func WithNamespace(ns string) Option

type Options

type Options struct {
	// Login specifies whether to login to the server
	Login bool
	// Namespace to use, defaults to the test name
	Namespace string
	// Prevent generating default account
	DisableAdmin bool
}

type Server

type Server interface {
	// Run the server
	Run() error
	// Close shuts down the server
	Close()
	// Command provides a `micro` command for the server
	Command() *Command
	// Name of the environment
	Env() string
	// APIPort is the port the api is exposed on
	APIPort() int
	// PoxyPort is the port the proxy is exposed on
	ProxyPort() int
}

Server is a micro server

func NewServer

func NewServer(t *T, opts ...Option) Server

type ServerBase

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

func (*ServerBase) APIPort

func (s *ServerBase) APIPort() int

func (*ServerBase) Close

func (s *ServerBase) Close()

func (*ServerBase) Command

func (s *ServerBase) Command() *Command

func (*ServerBase) Env

func (s *ServerBase) Env() string

func (*ServerBase) ProxyPort

func (s *ServerBase) ProxyPort() int

func (*ServerBase) Run

func (s *ServerBase) Run() error

error value should not be used but caller should return in the test suite in case of error.

type ServerDefault

type ServerDefault struct {
	ServerBase
}

func (*ServerDefault) Close

func (s *ServerDefault) Close()

func (*ServerDefault) Run

func (s *ServerDefault) Run() error

type T

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

func New

func New(t *testing.T) *T

New returns a new test framework

func (*T) Failed

func (t *T) Failed() bool

Failed indicate whether the test failed

func (*T) Fatal

func (t *T) Fatal(values ...interface{})

Fatal logs and exits immediately. Assumes it has come from a TrySuite() call. If called from within goroutine it does not immediately exit.

func (*T) Fatalf

func (t *T) Fatalf(format string, values ...interface{})

Fatalf logs and exits immediately. Assumes it has come from a TrySuite() call. If called from within goroutine it does not immediately exit.

func (*T) Log

func (t *T) Log(values ...interface{})

func (*T) Logf

func (t *T) Logf(format string, values ...interface{})

func (*T) Parallel

func (t *T) Parallel()

func (*T) T

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

Expose testing.T

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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