infra

package module
v0.0.0-...-b1af100 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2020 License: MIT Imports: 9 Imported by: 0

README

integration-test-suite

Project with routines that assist in the execution of integration tests.

Requirements

To use all the basic features of this project, it is necessary to have the docker installed, as well as the docker-compose.

Import

Import this project into your go project using the following statement:

import "github.com/zeroberto/integration-test-suite"

Run the command below to download the lib:

go get github.com/zeroberto/integration-test-suite

Usage

Create a docker-compose file containing your infrastructure services:

# Example of docker-compose.yml
version: "3.8"

services:
  db-test:
    image: postgres
    environment: 
      POSTGRES_USER: test
      POSTGRES_PASSWORD: test
      POSTGRES_DB: test
    volumes:
      - ./resources/sql:/docker-entrypoint-initdb.d
    ports: 
      - 65432:5432
    networks: 
      - test-network
  
networks:
  test-network:
    name: test-network
    driver: bridge

Get your infrastructure up before running your tests. At the end of the tests, your infra must be dropped. Your tests must be performed between these different moments. Below is a complete example of how your test file might look partially:

// Example of test file
import (
  "os"
  "testing"
  "time"

  infra "github.com/zeroberto/integration-test-suite"
)

const (
  host                  string = "localhost"
  serverPort            string = "7777"
  dbType                string = "postgres"
  dataSourceName        string = "postgres://test:test@localhost:65432/test?sslmode=disable"
  dockerComposeFileName string = "docker-compose.yml"
)

func TestFindUserInfo(t *testing.T) {
  // Your test code
}

func TestMain(m *testing.M) {
  setup()
  code := m.Run()
  teardown()
  os.Exit(code)
}

func setup() {
  infra.DownInfra(dockerComposeFileName)
  infra.UpInfra(dockerComposeFileName)
  go initServer()
  validateStructure(dockerComposeFileName)
}

func teardown() {
  infra.DownInfra(dockerComposeFileName)
}

func initServer() {
  // Start your server in a personalized way
}

func validateStructure(dockerComposeFileName string) {
  // Validate that all the conditions of your infra are ok. This is just an example.
  for {
    if infra.CheckPortIsOpen(host, serverPort) && infra.CheckDBConnection(dbType, dataSourceName) == nil {
      return
    }
    time.Sleep(100 * time.Millisecond)
  }
}

Limitations

Some features are only available for Unix-based systems, such as the infra.UpInfraWithEnvs().

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckDBConnection

func CheckDBConnection(dbType string, dsn string) error

CheckDBConnection is responsible for checking whether database is accepting connections

func CheckMongoDBConnection

func CheckMongoDBConnection(dsn string) error

CheckMongoDBConnection is responsible for checking whether mongo database is accepting connections

func CheckPortIsOpen

func CheckPortIsOpen(host string, port string) bool

CheckPortIsOpen is responsible for checking that a port is open for connections

func DownInfra

func DownInfra(dockerComposeFileName string)

DownInfra is responsible for bringing down the infrastructure

func GetContainerEnvValue

func GetContainerEnvValue(serviceID string, env string) string

GetContainerEnvValue is responsible for returning a value of env in docker container

func GetServiceID

func GetServiceID(dockerComposeFileName string, serviceName string) string

GetServiceID is responsible for returning a docker service identifier

func StopService

func StopService(serviceID string)

StopService is responsible for stopping a docker service

func UpInfra

func UpInfra(dockerComposeFileName string)

UpInfra is responsible for raising the infrastructure

func UpInfraWithEnvs

func UpInfraWithEnvs(dockerComposeFileName string, envs map[string]string)

UpInfraWithEnvs is responsible for raising the infrastructure with environments

Types

This section is empty.

Jump to

Keyboard shortcuts

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