steron

package module
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: MIT Imports: 31 Imported by: 0

README

testosteron is package for easi integrational service test with live environment

How to use

Add package initialization to Test_Main, or create basic Test_Main if not exists
package main

import (
	"fmt"
	"net/http"
	"os"
	"strings"
	"testing"

	steron "github.com/FluorescentTouch/testosteron/v2"
)

func TestMain(m *testing.M) {
	// init package with required options
	cfg, err := steron.Init(steron.AddKafka)
	if err != nil {
		panic(err)
	}

	// add http server, if application requires requests for startup
	srv := steron.HTTP().ServerMain(m)
	srv.HandleFunc("/web/config", func(w http.ResponseWriter, _ *http.Request, ) {
		remoteConfig := fmt.Sprintf(`{"kafka_brokers":[%s]}`, strings.Join(cfg.KafkaBrokers(), ","))
		_, _ = w.Write([]byte(remoteConfig))
	})

	// provide test tools configuration to application
	_ = os.Setenv("REMOTE_SERVER_ADDR", srv.Addr())
	_ = os.Setenv("KAFKA_BROKERS", strings.Join(cfg.KafkaBrokers(), ","))

	// run the app
	code := m.Run()

	steron.Cleanup()
	os.Exit(code)
}
Use required tools while testing application
  • HTTP Server and Client to test application endpoints
func TestHTTPClientDo(t *testing.T) {
	// can be your server
	srv := steron.HTTP().Server(t)
	srv.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(http.StatusOK)
	})
	client := steron.HTTP().Client(t)

	req, err := http.NewRequest(http.MethodGet, srv.Addr(), nil)
	if err != nil {
		t.Fatal(err)
	}

	resp := client.Do(req)
	if resp.StatusCode != http.StatusOK {
		t.Fatal("status code is not 200")
	}
}
  • You may also use helpers to make it easier
func TestHTTPClientGetJSON(t *testing.T) {
	// can be your server
	srv := steron.HTTP().Server(t)
	srv.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(http.StatusOK)
		_, _ = w.Write([]byte(`{"key":"value"}`))
	})
	client := steron.HTTP().Client(t)

	data := make(map[string]any, 0)

	client.GetJSON(srv.Addr(), &data)
	if len(data) == 0 {
		t.Fatal("zero values received")
	}
}
  • Kafka Consume/Produce
func TestKafka(t *testing.T) {
	kafkaClient := steron.Kafka().Client(t)

	kafkaClient.Produce("sample_topic", []byte("msg"))
	
	ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
	defer cancel()
	
	timeout := time.Second * 10
	msg := kafkaClient.Consume(ctx, timeout, "sample_topic")
	if len(msg.Value) == 0 {
		t.Fatal("zero len message received")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyMigrations

func ApplyMigrations(t *testing.T, migrateDirPath, scriptBefore string)

func Cleanup

func Cleanup()

func EnvDebugHandler

func EnvDebugHandler(prefix string) http.HandlerFunc

func JoinHostFromRandomPort

func JoinHostFromRandomPort(urlPath, env string) string

func RandomPortEnv

func RandomPortEnv() string

func SetEnv

func SetEnv(content []byte) error

SetEnv - use "embed" package to receive env file content

func SqlExecFromFile

func SqlExecFromFile(t *testing.T, conn *sql.DB, scriptSql string)

func Unmarshal

func Unmarshal(t *testing.T, data []byte, source any)

Types

type Config

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

func Init

func Init(options ...Option) (Config, error)

func (Config) ElasticSearchAddress

func (c Config) ElasticSearchAddress() string

func (Config) KafkaBrokers

func (c Config) KafkaBrokers() []string

func (Config) MinioConfig added in v2.0.2

func (c Config) MinioConfig() MinioConfig

func (Config) PgConfig

func (c Config) PgConfig() DbConfig

func (Config) Redis added in v2.0.2

func (c Config) Redis() redis.Options

type DbClient

type DbClient interface {
	DB() *sql.DB
	Migrate(migrateDir string) error
}

func PgxClient

func PgxClient(t *testing.T) DbClient

type DbConfig

type DbConfig struct {
	Host       string
	Name       string
	User       string
	Port       int
	Password   string
	Connection string
}

type ElasticSearchClient

type ElasticSearchClient interface {
	Client() *es.Client
}

type ElasticSearchHelper

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

func ElasticSearch

func ElasticSearch() *ElasticSearchHelper

func (*ElasticSearchHelper) Client

type ElasticSearchService

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

func NewElasticSearchService

func NewElasticSearchService(opts ...tc.ContainerCustomizer) *ElasticSearchService

func (*ElasticSearchService) WithHelper

func (e *ElasticSearchService) WithHelper(h *Helper) error

type HTTPHelper

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

func HTTP

func HTTP() *HTTPHelper

func (*HTTPHelper) Client

func (h *HTTPHelper) Client(t *testing.T) WebClient

func (*HTTPHelper) Server

func (h *HTTPHelper) Server(t *testing.T, envs ...string) WebServer

func (*HTTPHelper) ServerMain

func (h *HTTPHelper) ServerMain(m *testing.M, envs ...string) WebServer

type HandlerCollection added in v2.0.4

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

func NewHandlerCollection added in v2.0.5

func NewHandlerCollection(t *testing.T) *HandlerCollection

func (*HandlerCollection) Handle added in v2.0.4

func (h *HandlerCollection) Handle(key string) func(w http.ResponseWriter, r *http.Request)

func (*HandlerCollection) Set added in v2.0.4

func (h *HandlerCollection) Set(key string, fn func(http.ResponseWriter, *http.Request))

type Helper

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

func (*Helper) ElasticSearch

func (h *Helper) ElasticSearch() *ElasticSearchHelper

func (*Helper) HTTP

func (h *Helper) HTTP() *HTTPHelper

func (*Helper) Kafka

func (h *Helper) Kafka() *KafkaHelper

func (*Helper) Minio added in v2.0.2

func (h *Helper) Minio() *MinioHelper

func (*Helper) Postgres

func (h *Helper) Postgres() *PostgresHelper

func (*Helper) Redis added in v2.0.2

func (h *Helper) Redis() *RedisHelper

type KafkaClient

type KafkaClient interface {
	Consume(ctx context.Context, timeout time.Duration, topic string) *sarama.ConsumerMessage
	Produce(topic string, value []byte, h ...sarama.RecordHeader)
	ProduceWithKey(topic string, key []byte, data []byte, headers ...sarama.RecordHeader)
	CreateTopic(name string, detail *sarama.TopicDetail, validateOnly bool)
}

func NewKafkaClient

func NewKafkaClient(t *testing.T) KafkaClient

type KafkaHelper

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

func Kafka

func Kafka() *KafkaHelper

func (*KafkaHelper) Client

func (h *KafkaHelper) Client(t *testing.T) KafkaClient

type KafkaService

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

func NewKafkaService

func NewKafkaService(image string, opts ...tc.ContainerCustomizer) *KafkaService

func (*KafkaService) WithHelper

func (k *KafkaService) WithHelper(h *Helper) error

type MinioClient added in v2.0.2

type MinioClient interface {
	Client() minio.Client
}

type MinioConfig added in v2.0.2

type MinioConfig struct {
	Host     string
	User     string
	Password string
}

type MinioHelper added in v2.0.2

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

func Minio added in v2.0.2

func Minio() *MinioHelper

func (*MinioHelper) Client added in v2.0.2

func (h *MinioHelper) Client(t *testing.T) MinioClient

type MinioService added in v2.0.2

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

func NewMinioService added in v2.0.2

func NewMinioService(image string, opts ...tc.ContainerCustomizer) *MinioService

func (*MinioService) WithHelper added in v2.0.2

func (k *MinioService) WithHelper(h *Helper) error

type Option

type Option interface {
	WithHelper(*Helper) error
}

type PostgresHelper

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

func Postgres

func Postgres() *PostgresHelper

func (*PostgresHelper) Client

func (p *PostgresHelper) Client(t *testing.T) DbClient

type PostgresService

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

func NewPostgresService

func NewPostgresService(image string, opts ...tc.ContainerCustomizer) *PostgresService

func (*PostgresService) WithHelper

func (p *PostgresService) WithHelper(h *Helper) error

type RedisClient

type RedisClient interface {
	Client() redis.UniversalClient
}

type RedisHelper

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

func Redis added in v2.0.2

func Redis() *RedisHelper

func (*RedisHelper) Client

func (h *RedisHelper) Client(t *testing.T) RedisClient

type RedisService

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

func NewRedisService

func NewRedisService(image string, opts ...tc.ContainerCustomizer) *RedisService

func (*RedisService) WithHelper

func (k *RedisService) WithHelper(h *Helper) error

type WebClient

type WebClient interface {
	Do(req *http.Request) *http.Response
	Get(url string) *http.Response
	GetJSON(url string, dst any)
}

type WebServer

type WebServer interface {
	HandleFunc(pattern string, handler http.HandlerFunc)
	Addr() string
	Cleanup()

	// NewDebugHandler - инструмент дебага с безопасной остановкой
	// пример - next := srv.NewDebugHandler(steron.EnvDebugHandler("env_prefix_")); next()
	NewDebugHandler(handlerFunc http.HandlerFunc) func(...string)
}

Directories

Path Synopsis
elastic
http
kafka
minio
postgres
redis

Jump to

Keyboard shortcuts

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