Git Product home page Git Product logo

cereallkiller / testcontainers-go Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gauravgahlot/testcontainers-go

0.0 0.0 0.0 1.25 MB

Testcontainers is a Golang library that providing a friendly API to run Docker container. It is designed to create runtime environment to use during your automatic tests.

Home Page: https://golang.testcontainers.org/

License: MIT License

Shell 0.27% Go 99.31% Makefile 0.27% Dockerfile 0.15%

testcontainers-go's Introduction

Main pipeline Go Report Card GoDoc Reference

Testcontainers-go is a Go package that makes it simple to create and clean up container-based dependencies for automated integration/smoke tests. The clean, easy-to-use API enables developers to programmatically define containers that should be run as part of a test and clean up those resources when the test is done.

Here's an example of a test that spins up an NGINX container validates that it returns 200 for the status code:

package main

import (
	"context"
	"fmt"
	"net/http"
	"testing"

	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/wait"
)

type nginxContainer struct {
	testcontainers.Container
	URI string
}

func setupNginx(ctx context.Context) (*nginxContainer, error) {
	req := testcontainers.ContainerRequest{
		Image:        "nginx",
		ExposedPorts: []string{"80/tcp"},
		WaitingFor:   wait.ForHTTP("/"),
	}
	container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
		ContainerRequest: req,
		Started:          true,
	})
	if err != nil {
		return nil, err
	}

	ip, err := container.Host(ctx)
	if err != nil {
		return nil, err
	}

	mappedPort, err := container.MappedPort(ctx, "80")
	if err != nil {
		return nil, err
	}

	uri := fmt.Sprintf("http://%s:%s", ip, mappedPort.Port())

	return &nginxContainer{Container: container, URI: uri}, nil
}

func TestIntegrationNginxLatestReturn(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping integration test")
	}

	ctx := context.Background()

	nginxC, err := setupNginx(ctx)
	if err != nil {
		t.Fatal(err)
	}

	// Clean up the container after the test is complete
	defer nginxC.Terminate(ctx)

	resp, err := http.Get(nginxC.URI)
	if resp.StatusCode != http.StatusOK {
		t.Fatalf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
	}
}

Cleaning up your environment after test completion should be accomplished by deferring the container termination, e.g defer nginxC.Terminate(ctx). Reaper (Ryuk) is also enabled by default to help clean up.

Documentation

More information about Testcontainers-go can be found in ./docs, which is rendered at golang.testcontainers.org.

testcontainers-go's People

Contributors

mdelapenya avatar gianarb avatar dependabot-preview[bot] avatar dependabot[bot] avatar 01101101m avatar bablzz avatar prskr avatar jaredpetersen avatar funvit avatar shashank-g172 avatar islishude avatar franklinlindemberg avatar fiftin avatar mraerino avatar mniak avatar hey-xico avatar ikolomiyets avatar claytonnorthey92 avatar purpleclay avatar oriser avatar menedev avatar uportalis avatar vladimirstepanov avatar zregvart avatar gaborszakacs avatar codepitbull avatar e-zhydzetski avatar rieske avatar endpositive avatar erdemtoraman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.