tcbs

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2024 License: MIT Imports: 12 Imported by: 0

README

This golang module offers some basic functions to spin up some containers. It uses testcontainers.

POSTGRESQL

  // create the container
  newpgDB, err := tcbs.NewPostgresContainer("postgres", "password", "postgres")
	if err != nil {
		t.Fatalf("could not create postgres container: %v", err)
	}
  // defer the stop of the container
	defer newpgDB.Terminate(context.Background())

  // Open a connection
	db, err := sql.Open("postgres", newpgDB.GetDSNString())
	if err != nil {
		t.Fatalf("could not open postgres connection: %v", err)
	}
	defer db.Close()
	err = db.Ping()
	if err != nil {
		t.Fatalf("could not ping postgres: %v", err)
	}

REDIS

  redisC, err := tcbs.NewRedisContainer("", "")
	if err != nil {
		t.Fatalf("could not create redis container: %v", err)
	}
	defer redisC.Terminate(context.Background())

	ctx := context.Background()
	redisClient := redis.NewClient(&redis.Options{
		Username: redisC.GetRedisUser(),
		Password: redisC.GetRedisPassword(),
		Addr:     redisC.GetRedisHost() + ":" + redisC.GetRedisPort(),
	})
	defer redisClient.Close()

	_, err = redisClient.Ping(ctx).Result()

SFTP

  container, err := tcbs.NewTestSFTPServer()
	if err!= nil {
		log.Fatal(err)
	}
	defer container.Terminate()
	// init ssh client
	auth := goph.Password(container.GetPassword())
	host := strings.Split(container.GetEndpoint(), ":")[0]
	port := strings.Split(container.GetEndpoint(), ":")[1]
	portUint, err := strconv.ParseUint(port, 10, 32)
	if err != nil {
		log.Fatal(err)
	}
	client, err := goph.NewConn(&goph.Config{
		User:     container.GetUsername(),
		Addr:     host,
		Port:     uint(portUint),
		Auth:     auth,
		Callback: VerifyHost,
	})
	if err != nil {
		log.Fatal(err)
	}
	client.Close()
}

// no fingerprint check
func VerifyHost(host string, remote net.Addr, key ssh.PublicKey) error {
	// return goph.AddKnownHost(host, remote, key, "")
	return nil
}

SMTP

	container, err := tcbs.NewTestSMTPServer()
	if err != nil {
		t.Fatalf("could not create smtp container: %v", err)
	}
	defer container.Terminate()

	// ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
	// defer cancel()
	SMTPClient := mail.NewSMTPClient()
	SMTPClient.Host = container.GetSMTPHost()
	SMTPClient.Port = container.GetSMTPPortInt()
	SMTPClient.Username = container.GetSMTPUser()
	SMTPClient.Password = container.GetSMTPPassword()
	// SMTPClient.Encryption = mail.EncryptionTLS
	SMTPClient.Encryption = mail.EncryptionNone
	SMTPClient.KeepAlive = true
	SMTPClient.ConnectTimeout = 10 * time.Second
	SMTPClient.SendTimeout = 10 * time.Second
	SMTPClient.TLSConfig = &tls.Config{InsecureSkipVerify: true}

	client, err := SMTPClient.Connect()
	if err != nil {
		t.Fatalf("Failed to connect to SMTP server: %v", err)
	}
	defer client.Close()
	...

Documentation

Index

Constants

View Source
const SMTPDDockerImage = "axllent/mailpit:v1.14.0"
View Source
const SSHDDockerImage = "sgaunet/alpine-sshd:latest"

Variables

This section is empty.

Functions

This section is empty.

Types

type PostgresContainer

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

TestDB is a struct that holds the postgresql container and the DSN

func NewPostgresContainer

func NewPostgresContainer(postgresUser, postgresPassword, postgresDBName string) (*PostgresContainer, error)

NewTestDB creates a postgresql database in docker for tests

func (*PostgresContainer) GetDBHost

func (pg *PostgresContainer) GetDBHost() string

GetDBHost returns the host of the database

func (*PostgresContainer) GetDBName

func (pg *PostgresContainer) GetDBName() string

GetDBName returns the name of the database

func (*PostgresContainer) GetDBPassword

func (pg *PostgresContainer) GetDBPassword() string

GetDBPassword returns the password of the database

func (*PostgresContainer) GetDBPort

func (pg *PostgresContainer) GetDBPort() string

GetDBPort returns the port of the database

func (*PostgresContainer) GetDBPortInt

func (pg *PostgresContainer) GetDBPortInt() int

GetDBPortInt returns the port of the database as an integer

func (*PostgresContainer) GetDBUser

func (pg *PostgresContainer) GetDBUser() string

GetDBUser returns the user of the database

func (*PostgresContainer) GetDSN

func (pg *PostgresContainer) GetDSN() dsn.DSN

GetDSN returns the DSN

func (*PostgresContainer) GetDSNString

func (pg *PostgresContainer) GetDSNString() string

GetDSNString returns the DSN string The format is 'host=... port=... user=... password=... dbname=... sslmode=...'

func (*PostgresContainer) Terminate

func (pg *PostgresContainer) Terminate(ctx context.Context) error

Terminate stops the postgresql container

type RedisContainer

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

TestDB is a struct that holds the redis container and the DSN

func NewRedisContainer

func NewRedisContainer(redisUser, redisPassword string) (*RedisContainer, error)

NewTestDB creates a redis database in docker for tests

func (*RedisContainer) DSN

func (r *RedisContainer) DSN() string

DSN returns the DSN of the redis server

func (*RedisContainer) GetRedisHost

func (r *RedisContainer) GetRedisHost() string

GetRedisHost returns the host of the redis server

func (*RedisContainer) GetRedisPassword

func (r *RedisContainer) GetRedisPassword() string

GetRedisPassword returns the password of the redis server

func (*RedisContainer) GetRedisPort

func (r *RedisContainer) GetRedisPort() string

GetRedisPort returns the port of the redis server as a string

func (*RedisContainer) GetRedisPortInt

func (r *RedisContainer) GetRedisPortInt() int

GetRedisPortInt returns the port of the redis server as an integer

func (*RedisContainer) GetRedisUser

func (r *RedisContainer) GetRedisUser() string

GetRedisUser returns the user of the redis server

func (*RedisContainer) Terminate

func (r *RedisContainer) Terminate(ctx context.Context) error

Terminate stops the redis container

type SFTPServer

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

SFTPServer

func NewTestSFTPServer

func NewTestSFTPServer() (*SFTPServer, error)

NewTestSFTPServer creates a new SFTP server for testing with default values

func (*SFTPServer) GetEndpoint

func (t *SFTPServer) GetEndpoint() string

GetEndpoint returns the endpoint of the SFTP server

func (*SFTPServer) GetPassword

func (t *SFTPServer) GetPassword() string

GetPassword returns the password of the SFTP server

func (*SFTPServer) GetPrivateKey

func (t *SFTPServer) GetPrivateKey() string

GetPrivateKey returns the private key of the SFTP server

func (*SFTPServer) GetUsername

func (t *SFTPServer) GetUsername() string

GetUsername returns the username of the SFTP server

func (*SFTPServer) Terminate

func (t *SFTPServer) Terminate() error

Terminate stops the SFTP server

type SMTPServer

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

SMTPServer

func NewTestSMTPServer

func NewTestSMTPServer() (*SMTPServer, error)

NewTestSMTPServer creates a new SMTP server for testing with default values

func (*SMTPServer) GetSMTPEndpoint

func (t *SMTPServer) GetSMTPEndpoint() string

GetEndpoint returns the endpoint of the SFTP server

func (*SMTPServer) GetSMTPHost

func (t *SMTPServer) GetSMTPHost() string

func (*SMTPServer) GetSMTPPassword

func (t *SMTPServer) GetSMTPPassword() string

func (*SMTPServer) GetSMTPPortInt

func (t *SMTPServer) GetSMTPPortInt() int

func (*SMTPServer) GetSMTPPortStr

func (t *SMTPServer) GetSMTPPortStr() string

GetPort returns the port of the SFTP server

func (*SMTPServer) GetSMTPUser

func (t *SMTPServer) GetSMTPUser() string

func (*SMTPServer) Terminate

func (t *SMTPServer) Terminate() error

Terminate stops the SMTP server

Jump to

Keyboard shortcuts

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