How to use Down method of testcontainers Package

Best Testcontainers-go code snippet using testcontainers.Down

postgresql_test.go

Source:postgresql_test.go Github

copy

Full Screen

...12 testcontainers "github.com/testcontainers/testcontainers-go"13 "github.com/testcontainers/testcontainers-go/wait"14)15func TestPgStore(t *testing.T) {16 cfg, tearDown, err := pgSetup()17 require.NoError(t, err)18 defer tearDown()19 db, err := pgConnect(cfg)20 require.NoError(t, err)21 defer db.Close()22 store := postgres.New(db.DB)23 t.Run("postgres", func(t *testing.T) {24 testScheduler(t, store)25 })26}27type PgConfig struct {28 Database string29 Host string30 Port int31 Username string32 Password string33}34func pgSetup() (PgConfig, func(), error) {35 dbConfig := PgConfig{36 Database: "eventsourcing",37 Host: "localhost",38 Port: 5432,39 Username: "postgres",40 Password: "postgres",41 }42 tcpPort := strconv.Itoa(dbConfig.Port)43 natPort := nat.Port(tcpPort)44 req := testcontainers.ContainerRequest{45 Image: "postgres:12.3",46 ExposedPorts: []string{tcpPort + "/tcp"},47 Env: map[string]string{48 "POSTGRES_USER": dbConfig.Username,49 "POSTGRES_PASSWORD": dbConfig.Password,50 "POSTGRES_DB": dbConfig.Database,51 },52 WaitingFor: wait.ForListeningPort(natPort),53 }54 ctx := context.Background()55 container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{56 ContainerRequest: req,57 Started: true,58 })59 if err != nil {60 return PgConfig{}, nil, fmt.Errorf("failed to initialise container: %w", err)61 }62 tearDown := func() {63 container.Terminate(ctx)64 }65 ip, err := container.Host(ctx)66 if err != nil {67 tearDown()68 return PgConfig{}, nil, fmt.Errorf("failed to get container host: %w", err)69 }70 port, err := container.MappedPort(ctx, natPort)71 if err != nil {72 tearDown()73 return PgConfig{}, nil, fmt.Errorf("failed to get container port '%s': %w", natPort, err)74 }75 dbConfig.Host = ip76 dbConfig.Port = port.Int()77 db, err := pgConnect(dbConfig)78 if err != nil {79 tearDown()80 return PgConfig{}, nil, fmt.Errorf("failed to connect: %w", err)81 }82 defer db.Close()83 dbSchema(db)84 return dbConfig, tearDown, nil85}86func dbSchema(db *sqlx.DB) {87 db.MustExec(`88 CREATE TABLE IF NOT EXISTS schedules(89 slug VARCHAR (100) PRIMARY KEY,90 kind VARCHAR (100) NOT NULL,91 payload bytea,92 run_at TIMESTAMP NOT NULL,93 version INTEGER NOT NULL,94 retry INTEGER NOT NULL,95 result TEXT,96 locked_until TIMESTAMP97 );98 CREATE INDEX sch_run_at_lock_idx ON schedules (run_at, locked_until);...

Full Screen

Full Screen

firestore_test.go

Source:firestore_test.go Github

copy

Full Screen

...13 testcontainers "github.com/testcontainers/testcontainers-go"14 "github.com/testcontainers/testcontainers-go/wait"15)16func TestFireStore(t *testing.T) {17 cfg, tearDown, err := fireSetup()18 require.NoError(t, err)19 defer tearDown()20 db, err := fireConnect(cfg)21 require.NoError(t, err)22 defer db.Close()23 store := firestore.New(db)24 t.Run("firestore", func(t *testing.T) {25 testScheduler(t, store)26 })27}28type FireConfig struct {29 FirestoreProjectID string30 Host string31 Port int32}33func fireSetup() (FireConfig, func(), error) {34 dbConfig := FireConfig{35 FirestoreProjectID: "dummy-project-id",36 Host: "localhost",37 Port: 8200,38 }39 tcpPort := strconv.Itoa(dbConfig.Port)40 natPort := nat.Port(tcpPort)41 req := testcontainers.ContainerRequest{42 Image: "mtlynch/firestore-emulator",43 ExposedPorts: []string{tcpPort + "/tcp"},44 Env: map[string]string{45 "FIRESTORE_PROJECT_ID": dbConfig.FirestoreProjectID,46 "PORT": strconv.Itoa(dbConfig.Port),47 },48 WaitingFor: wait.ForListeningPort(natPort),49 }50 ctx := context.Background()51 container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{52 ContainerRequest: req,53 Started: true,54 })55 if err != nil {56 return FireConfig{}, nil, fmt.Errorf("failed to initialise container: %w", err)57 }58 tearDown := func() {59 container.Terminate(ctx)60 }61 ip, err := container.Host(ctx)62 if err != nil {63 tearDown()64 return FireConfig{}, nil, fmt.Errorf("failed to get container host: %w", err)65 }66 port, err := container.MappedPort(ctx, natPort)67 if err != nil {68 tearDown()69 return FireConfig{}, nil, fmt.Errorf("failed to get container port '%s': %w", natPort, err)70 }71 dbConfig.Host = ip72 dbConfig.Port = port.Int()73 envVal := fmt.Sprintf("%s:%d", dbConfig.Host, dbConfig.Port)74 err = os.Setenv("FIRESTORE_EMULATOR_HOST", envVal)75 if err != nil {76 return FireConfig{}, nil, fmt.Errorf("failed to set env var FIRESTORE_EMULATOR_HOST to '%s': %w", envVal, err)77 }78 return dbConfig, tearDown, nil79}80func fireConnect(dbConfig FireConfig) (*gfs.Client, error) {81 return gfs.NewClient(context.Background(), dbConfig.FirestoreProjectID)82}...

Full Screen

Full Screen

redis_container.go

Source:redis_container.go Github

copy

Full Screen

...42 Host: containerHost,43 Port: containerPort.Port(),44 }45}46// Down destroy the redis container47func (c RedisContainer) Down() error {48 if c.Container != nil {49 err := c.Container.Terminate(context.Background())50 if err != nil {51 return err52 }53 }54 return nil55}...

Full Screen

Full Screen

Down

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 Cmd: []string{"ash", "-c", "while true; do sleep 1; done"},6 WaitingFor: wait.ForLog("ash"),7 }8 c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 panic(err)12 }13 defer c.Terminate(ctx)14 fmt.Println(c.GetContainerID())15}16require (

Full Screen

Full Screen

Down

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"6379/tcp"},6 WaitingFor: wait.ForListeningPort("6379/tcp"),7 }8 redisContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 redisHost, err := redisContainer.Host(ctx)14 if err != nil {15 log.Fatal(err)16 }17 redisPort, err := redisContainer.MappedPort(ctx, "6379/tcp")18 if err != nil {19 log.Fatal(err)20 }21 log.Printf("Redis container is up! Host: %s, Port: %s", redisHost, redisPort.Port())22 time.Sleep(5 * time.Second)23 if err := redisContainer.Terminate(ctx); err != nil {24 log.Fatal(err)25 }26}

Full Screen

Full Screen

Down

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"5432/tcp"},6 WaitingFor: wait.ForLog("database system is ready to accept connections"),7 }8 postgresContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 defer postgresContainer.Terminate(ctx)14 ip, err := postgresContainer.Host(ctx)15 if err != nil {16 log.Fatal(err)17 }18 port, err := postgresContainer.MappedPort(ctx, "5432")19 if err != nil {20 log.Fatal(err)21 }22", "postgres", "postgres", ip, port.Port(), "postgres")23 time.Sleep(5 * time.Second)24}25import (26func main() {27 ctx := context.Background()28 req := testcontainers.ContainerRequest{29 ExposedPorts: []string{"5432/tcp"},30 WaitingFor: wait.ForLog("database system is ready to accept connections"),31 }32 postgresContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{33 })34 if err != nil {35 log.Fatal(err)36 }37 defer postgresContainer.Terminate(ctx)38 ip, err := postgresContainer.Host(ctx)39 if err != nil {40 log.Fatal(err)41 }42 port, err := postgresContainer.MappedPort(ctx, "5432")43 if err != nil {44 log.Fatal(err)

Full Screen

Full Screen

Down

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"3306/tcp"},6 WaitingFor: wait.ForLog("port: 3306 MySQL Community Server - GPL"),7 }8 mysqlContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 defer mysqlContainer.Terminate(ctx)14 port, err := mysqlContainer.MappedPort(ctx, "3306")15 if err != nil {16 log.Fatal(err)17 }18 ip, err := mysqlContainer.Host(ctx)19 if err != nil {20 log.Fatal(err)21 }22 fmt.Printf("mysql -u root -h %s -P %s23", ip, port.Port())24 err = mysqlContainer.Terminate(ctx)25 if err != nil {26 log.Fatal(err)27 }28}

Full Screen

Full Screen

Down

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"3306/tcp"},6 WaitingFor: wait.ForListeningPort("3306/tcp"),7 }8 mysqlContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 defer mysqlContainer.Terminate(ctx)14 host, err := mysqlContainer.Host(ctx)15 if err != nil {16 log.Fatal(err)17 }18 port, err := mysqlContainer.MappedPort(ctx, "3306")19 if err != nil {20 log.Fatal(err)21 }22 fmt.Println(host)23 fmt.Println(port.Int())24 ip, err := mysqlContainer.Host(ctx)25 if err != nil {26 log.Fatal(err)27 }28 fmt.Println(ip)29 logs, err := mysqlContainer.Logs(ctx)30 if err != nil {31 log.Fatal(err)32 }33 fmt.Println(logs)34 id, err := mysqlContainer.ContainerID(ctx)35 if err != nil {36 log.Fatal(err)37 }38 fmt.Println(id)39 image, err := mysqlContainer.Image(ctx)40 if err != nil {41 log.Fatal(err)42 }43 fmt.Println(image)44 name, err := mysqlContainer.Name(ctx)45 if err != nil {46 log.Fatal(err)47 }48 fmt.Println(name)49 err = mysqlContainer.Stop(ctx)50 if err != nil {51 log.Fatal(err)52 }53 err = mysqlContainer.Terminate(ctx)54 if err != nil {55 log.Fatal(err)56 }57}

Full Screen

Full Screen

Down

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"6379/tcp"},6 WaitingFor: wait.ForLog("Ready to accept connections"),7 }8 redis, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 defer redis.Terminate(ctx)14 ip, err := redis.Host(ctx)15 if err != nil {16 log.Fatal(err)17 }18 port, err := redis.MappedPort(ctx, "6379")19 if err != nil {20 log.Fatal(err)21 }22 fmt.Println("Redis available at port:", port.Int())23 fmt.Println("Redis available at ip:", ip)24 err = redis.Terminate(ctx)25 if err != nil {26 log.Fatal(err)27 }28}29import (30func main() {31 ctx := context.Background()32 req := testcontainers.ContainerRequest{33 ExposedPorts: []string{"6379/tcp"},34 WaitingFor: wait.ForLog("Ready to accept connections"),35 }36 redis, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{37 })38 if err != nil {39 log.Fatal(err)40 }41 defer redis.Terminate(ctx)42 ip, err := redis.Host(ctx)43 if err != nil {44 log.Fatal(err)45 }46 port, err := redis.MappedPort(ctx, "6379")47 if err != nil {48 log.Fatal(err)49 }50 fmt.Println("Redis available at port:", port.Int())51 fmt.Println("Redis available

Full Screen

Full Screen

Down

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"6379/tcp"},6 WaitingFor: wait.ForListeningPort("6379/tcp"),7 }8 redisContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 defer redisContainer.Terminate(ctx)14 ip, err := redisContainer.Host(ctx)15 if err != nil {16 log.Fatal(err)17 }18 port, err := redisContainer.MappedPort(ctx, "6379/tcp")19 if err != nil {20 log.Fatal(err)21 }22 fmt.Println(ip, port.Int())23}24import (25func main() {26 ctx := context.Background()27 req := testcontainers.ContainerRequest{28 ExposedPorts: []string{"6379/tcp"},29 WaitingFor: wait.ForListeningPort("6379/tcp"),30 }31 redisContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{32 })33 if err != nil {34 log.Fatal(err)35 }36 defer redisContainer.Terminate(ctx)37 ip, err := redisContainer.Host(ctx)38 if err != nil {39 log.Fatal(err)40 }41 port, err := redisContainer.MappedPort(ctx, "6379/tcp")42 if err != nil {43 log.Fatal(err)44 }45 fmt.Println(ip, port.Int())46}47import

Full Screen

Full Screen

Down

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 Cmd: []string{"echo", "Hello world!"},6 ExposedPorts: []string{"80/tcp"},7 WaitingFor: wait.ForLog("Hello world!"),8 }9 c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{10 })11 if err != nil {12 panic(err)13 }14 ip, err := c.Host(ctx)15 if err != nil {16 panic(err)17 }18 port, err := c.MappedPort(ctx, "80")19 if err != nil {20 panic(err)21 }22 fmt.Printf("Container IP: %s, mapped port: %s", ip, port.Port())23 err = c.Terminate(ctx)24 if err != nil {25 panic(err)26 }27}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-go automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful