integration

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2023 License: AGPL-3.0 Imports: 23 Imported by: 0

README

Integration

Integration is a golang library for running integration tests on the local machine and in the Gitlab pipelines. It is responsible for creating spanner emulator as a docker container and it runs provided gRPC server in the background.

Installation

go get gitlab.com/picnic-app/backend/libs/golang/integration@<branch-name/tag>

Getting started

This project includes a Makefile that allows you to test and build the project with simple commands. To see all available options:

make help

Usage


func TestSomething(t *testing.T) {
	ctx := context.Background()
	client := integration.GetGrpcClient(t, v1.NewBlacklistAPIServiceClient)

	_, err := client.Upsert(ctx, &v1.UpsertRequest{
		Id: getRandomString(),
	})

	assert.Error(t, err)
	code, ok := status.FromError(err)
	assert.True(t, ok)
	assert.Equal(t, codes.NotFound, code.Code())
}

func TestMain(m *testing.M) {
	ctx := context.Background()
	// First we should create emulator config.
	// The names of project/instance/database can be random.
	emulatorConfig := integration.EmulatorConfig{
		Project:  "test-project",
		Instance: "test-instance",
		Database: "test-database",
	}

	// Next we should run Setup method which creates emulator as a docker container and 
	// it runs specified func(context.Context, *grpc.Server) in the background.
	emulator, err := integration.Setup(ctx, emulatorConfig, func(ctx context.Context, server *grpc.Server) {
		client, err := repository.NewSpannerClient(ctx, emulatorConfig.URI(), "")
		if err != nil {
			log.Fatalf("Could not create new Spanner client: %v", err)
		}

		svc := handlers.NewGrpcServer(repository.NewSpannerRepository(client))
		v1.RegisterBlacklistAPIServiceServer(server, svc)
	})
	if err != nil {
		log.Fatalf("Could not set up testing environment: %v", err)
	}

	// We should remember about destroying the emulator container.
	defer func() {
		_ = emulator.Terminate(ctx)
	}()

	os.Exit(m.Run())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGrpcClient

func GetGrpcClient[T any](t *testing.T, setupFunc func(cc grpc.ClientConnInterface) T) T

GetGrpcClient creates gRPC client of type T that will be connected to the gRPC server running in the background.

func Setup

func Setup(ctx context.Context, migrationsPath string, emulatorConfig EmulatorConfig, CI bool, opts []grpc.ServerOption, runFunc RunFunction) (testcontainers.Container, error)

Setup creates spanner emulator locally with provided configuration and runs RunFunction in the background.

Types

type EmulatorConfig

type EmulatorConfig struct {
	Project  string
	Instance string
	Database string
}

EmulatorConfig represents configuration used for creating spanner emulator.

func (EmulatorConfig) URI

func (c EmulatorConfig) URI() string

URI returns prepared URI used for connecting to spanner.

type RunFunction

type RunFunction func(ctx context.Context, server *grpc.Server)

RunFunction represents a function that should set up the gRPC server with all the dependencies that will run in the background.

Jump to

Keyboard shortcuts

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