steron

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: MIT Imports: 14 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"
)

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 AddKafka

func AddKafka(h *Helper) error

func AddPostgres

func AddPostgres(h *Helper) error

func Cleanup

func Cleanup()

Types

type Config

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

func Init

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

func (Config) KafkaBrokers

func (c Config) KafkaBrokers() []string

func (Config) PgConfig

func (c Config) PgConfig() DbConfig

type DbClient

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

type DbConfig

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

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) WebServer

func (*HTTPHelper) ServerMain

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

type Helper

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

func (*Helper) HTTP

func (h *Helper) HTTP() *HTTPHelper

func (*Helper) Kafka

func (h *Helper) Kafka() *KafkaHelper

func (*Helper) Postgres

func (h *Helper) Postgres() *PostgresHelper

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)
}

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 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 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()
}

Directories

Path Synopsis
http

Jump to

Keyboard shortcuts

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