Coder Social home page Coder Social logo

ory / dockertest Goto Github PK

View Code? Open in Web Editor NEW
4.0K 30.0 236.0 1.48 MB

Write better integration tests! Dockertest helps you boot up ephermal docker images for your Go tests with minimal work.

Home Page: https://www.ory.sh/?utm_source=github&utm_medium=banner&utm_campaign=dockertest

License: Apache License 2.0

Go 99.87% Makefile 0.13%
go docker ci tests automation unit-testing integration-testing

dockertest's Introduction

dockertest's People

Contributors

ackar avatar aeneasr avatar alexandear avatar anhello avatar arekkas avatar bjorn-tf avatar calvn avatar ddrboxman avatar dennwc avatar dependabot[bot] avatar ewilde avatar fuchiao avatar hsinhoyeh avatar ibrt avatar jackwhelpton avatar kaign avatar kevgo avatar ninnemana avatar paultyng avatar pmenglund avatar potix2 avatar robertogyn19 avatar ronhopper avatar sryoya avatar ss-stackid avatar steven-sheehy avatar stianovrevage avatar straysh avatar tainguyentt avatar tullo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dockertest's Issues

Ability to specify container to host port mappings

I notice the ExposedPorts functionality, that allows for a container port to be specified such that it is mapped to a random host port e.g 0.0.0.0:32911->9092/tcp. Is there a way to specify the port mappings such that the container port is mapped to a specific host port e.g 0.0.0.0:9092->9092/tcp?

I'm attempting to use dockertest to spin up an Apache Kafka container spotify/kafka, for use in an integration test. The app is making use of the Shopify/sarama Kafka client. As expected, dockertest spins up the Kafka container successfully. In fact, all works fine up until Sarama attempts to consume from the Kafka topic. The reason for this is that the Kafka broker advertises its port number to Sarama, which Sarama then uses, but it's the container port and not the host port. For example, dockertest spins up a Kafka container with port mappings 0.0.0.0:32911->9092/tcp, Sarama connects initially on port 32911 and queries for the broker information, "localhost:9092" is returned to Sarama, Sarama attempts to connect but fails as port 9092 is not available.

I'm sure there's probably a more elegant solution rather than ensuring that the container port is mapped to the exact same host port, but specific port mappings would certainly be a very useful feature in dockertest.

For reference, the code I'm working on can be found here

Replace docker backend with containerd

Hi!

I've just watched a talk about containerd at FOSDEM and it seems to me that it should be possible to replace the docker backend with containerd, for a much neater dependency stack and call stack. I realise this might have to be done in a fork, but is this something you would consider?

API v3

  • Should use docker inspect to let docker allocate a bound port
  • Should avoid encouraging callbacks and globals in using code
  • Should allow pluggable runner-types (including implementing in using code)
  • Should allow pluggable container-types (including implementing in using code)

Something in the line of:

type Container interface {
    Destroy()
    ConnectURL() string  // "Main" service URL in service-specific format
    URLs() map[string]string // Should at least contain "main". May contain extra
    Log() io.Reader
}

type ContainerImage struct {
  Name string
  ImageArguments []string
  Inspector interface {
    // Inspect Container and wait for it to be ready
    WaitForReady(Container)
  }
}

type Monitor interface {
    IsAlive() bool
}

type Runner interface {
   GetMonitor() Monitor
   Deploy(ContainerImage) (Container, error)
}

// Set from init
var Impl Runner

type RegexStager *regexp.Regexp // Implementing ContainerImage.Inspector

// Usage:
container, err := dockertest.Deploy(dockertest.MySQL56)
defer container.Destroy()

db, err := sql.Open(container.ConnectURL())
aux, err := net.Dial("tcp", container.URLs()["aux"])

Configurable memory

Hey @arekkas, love using dockertest, I'm just hitting an issue using it with some containers that require more memory. It would be handy if you could set the memory limit from the run options. I'm happy to work it in quick. Thanks

[mysql] allow to use a customized database instead of default one

hi,

thanks for creating such great tool. It is really saved lots of time to do integration test.
However, we found that you use default "mysql" database (it also called mysql) as your default database.
https://github.com/ory-am/dockertest/blob/master/mysql.go#L35

url := fmt.Sprintf("%s:%s@tcp(%s:%d)/mysql", MySQLUsername, MySQLPassword, ip, port)

As you may knew, mysql stored user's information and privileges into that database as well.
That would be an issue if we share the same table names.
I think it is better to let caller decide which one of databases should be used.
And, when mysql container is up, we can create the database if it is not default one.

Let me know your concerns.
If you accept PR, I can send one.

#97 broke NewPool() for docker-machine

When DOCKER_URL was changed to DOCKER_HOST in #97 , it broke auto-configuration for docker-machine env ... as it sets DOCKER_HOST to use tcp:// as scheme.

E.g.

DOCKER_HOST=tcp://172.16.3.128:2376
DOCKER_MACHINE_NAME=docker
DOCKER_TLS_VERIFY=1
DOCKER_CERT_PATH=/Users/englund/.docker/machine/machines/docker

If we switch the checks to look for DOCKER_MACHINE_NAME first, I think it will work, but since I don't have minikube setup to test on I don't know if it'll break something. I'll create a PR, but would like input from someone who

Tests fail on travis

$ go test -v ./...

=== RUN   TestMongo

--- FAIL: TestMongo (0.02s)

    Error Trace:    docker_test.go:15

    Error:      Expected nil, but got: &errors.errorString{s:"Error checking for docker image mongo: exit status 1"}



    docker_test.go:17: Error checking for docker image mongo: exit status 1

=== RUN   TestMySQL

--- FAIL: TestMySQL (0.02s)

    Error Trace:    docker_test.go:35

    Error:      Expected nil, but got: &errors.errorString{s:"Error checking for docker image mysql: exit status 1"}



    docker_test.go:37: Error checking for docker image mysql: exit status 1

=== RUN   TestPostgres

--- FAIL: TestPostgres (0.02s)

    Error Trace:    docker_test.go:55

    Error:      Expected nil, but got: &errors.errorString{s:"Error checking for docker image postgres: exit status 1"}



    docker_test.go:57: Error checking for docker image postgres: exit status 1

FAIL

exit status 1

FAIL    github.com/ory-am/dockertest    0.062s

Possible regression with docker-machine 0.5.0? Starting already existing vm does not work.

I'm not exactly sure what the circumstances are, but when I run the following:

$ docker-machine start default
Machine "default" is already running.
echo $?
1

docker-machine returns an error code. After skimming the dockertest code, I noticed that startDockerMachine() would always return false for the exit-code 1. It would also always be called.

Am I correct with my assumptions?

I thought of 2 different ways to tackle this issue:

  • Give preference to the docker command in runLongTest(). This would allow a correctly set up environment to run directly against the docker command, even if docker-machine is present.
  • Remove the initial DockerMachineAvailable = false inside runLongTest() and only start the docker machine if DockerMachineAvailable is false and haveDockerMachine. This would allow the tests to force docker machines to not be recreated.

Cheers

Tries to use https:// even though I specify tcp://

Thank you for this great Project!
Sadly, connecting to docker recently stopped working for me.
Docker sets my DOCKER_HOST to tcp://127.0.0.1:2375 and now when I try to run my tests, dockertest tries to connect using https.

Could not start resource: : Post https://127.0.0.1:2375/images/create?fromImage=postgres&tag=10.0-alpine: http: server gave HTTP response to HTTPS clientFAIL

I'm currently running Docker 17.09.1-ce on Windows.

Also, shouldn't this only use https if in https://github.com/ory/dockertest/blob/v3/dockertest.go#L104 DOCKER_CERT_PATH has a value?

Add tests

Might require some rewriting because we'd have to mock exec.*

sql: Register called twice for driver postgres

I'm trying to use dockertest to run tests on my gin app. I'm using dat to interact with my postgresql database.

However, when I try to run my tests it panics: "sql: Register called twice for driver postgres"

panic(0x558ca0, 0xc8202aab60)
    /usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:464 +0x3e6
database/sql.Register(0x74e240, 0x8, 0xf48c10, 0xabb598)
    /usr/local/Cellar/go/1.6/libexec/src/database/sql/sql.go:45 +0x181
github.com/lib/pq.init.1()
    /Volumes/Macintosh/Manu/Documents/Programming/Go/src/github.com/lib/pq/conn.go:44 +0x71
github.com/lib/pq.init()
    /Volumes/Macintosh/Manu/Documents/Programming/Go/src/github.com/lib/pq/user_posix.go:24 +0x4ee
gopkg.in/ory-am/dockertest%2ev2.init()
    /Volumes/Macintosh/Manu/Documents/Programming/Go/src/gopkg.in/ory-am/dockertest.v2/vars.go:69 +0x79
github.com/processout/api/database.init()
    /Volumes/Macintosh/Manu/Documents/Programming/Go/src/github.com/processout/api/database/setup.go:63 +0x59
github.com/processout/api/controllers.init()
    /Volumes/Macintosh/Manu/Documents/Programming/Go/src/github.com/processout/api/controllers/invoices_test.go:19 +0x94
main.init()

Any idea on what's happening? I'm using the same driver as you, github.com/lib/pq. Thanks!

Doesn't work with Gorm


import (
	"testing"
	_ "github.com/lib/pq"
	"os"
	"github.com/ory/dockertest"
	"log"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/postgres"
	"fmt"
)

func TestMain(m *testing.M) {
	var db *gorm.DB
	var err error

	pool, err := dockertest.NewPool("")
	if err != nil {
		log.Fatalf("Could not connect to docker: %s", err)
	}

	resource, err := pool.Run("postgres", "9.6.5-alpine", []string{"POSTGRES_PASSWORD=secret", "POSTGRES_DB=test"})
	if err != nil {
		log.Fatalf("Could not start resource: %s", err)
	}

	if err = pool.Retry(func() error {
		var err error
		hostPort := resource.GetHostPort("5432/tcp")
		dbConnStr := fmt.Sprintf("postgres://postgres:secret@%s/test?sslmode=disable", hostPort)
		db, err = gorm.Open("postgres", dbConnStr)
		if err != nil {
			panic(fmt.Sprintf("Failed to connect database: %v", err))
		}
		return db.DB().Ping()
	}); err != nil {
		log.Fatalf("Could not connect to docker: %s", err)
	}

	code := m.Run()
	if err = pool.Purge(resource); err != nil {
		log.Fatalf("Could not purge resource %s", err)
	}
	os.Exit(code)
}

func TestGet(t *testing.T) {
	t.Fail()
}

It will panic with Failed to connect database: EOF.

Support for specifying mounts in v3 of the api

In v2 of the api the ConnectToCassandra function took a optionalParams parameter. Using this parameter I was able to create a mount with a custom cassandra config.

It would be nice for this same functionality to exist in v3.

Container networking

Hello there,

I'm trying to use this library as a replacement for docker-compose but I'm failing to see how I can connect two containers to the same network so that they can talk to each other without host port mappings. Is this use case accounted for already?

remove images as well

for now clean up with: docker run --rm -v /var/run/docker.sock:/var/run/docker.sock:ro -v /var/lib/docker:/var/lib/docker martin/docker-cleanup-volumes

Vendoring issue when importing from gopkg.in

I am seeing vendoring issues after the dockerclient package was moved internally. Vendoring is done with govendor and the import is based off gopkg.in.

package github.com/hashicorp/vault/builtin/logical/mysql (test)
	imports gopkg.in/ory-am/dockertest.v3
	imports github.com/ory/dockertest/docker: cannot find package "github.com/ory/dockertest/docker" in any of:
	/Users/cleung/go/src/github.com/hashicorp/vault/vendor/github.com/ory/dockertest/docker (vendor tree)
	/usr/local/Cellar/go/1.10.1/libexec/src/github.com/ory/dockertest/docker (from $GOROOT)
	/Users/cleung/go/src/github.com/ory/dockertest/docker (from $GOPATH)
FAIL	github.com/hashicorp/vault/builtin/logical/mysql [setup failed]

The package is also not able to be downloaded with go get

go get -d github.com/ory/dockertest/docker
package github.com/ory/dockertest/docker: cannot find package "github.com/ory/dockertest/docker" in any of:
	/usr/local/Cellar/go/1.10.1/libexec/src/github.com/ory/dockertest/docker (from $GOROOT)
	/Users/cleung/go/src/github.com/ory/dockertest/docker (from $GOPATH)

I think the issue might be that package docker is not an actual fetch-able package so anything that is external to dockertest will not be able to resolve dockertest/docker.

Minio example doesn't work

The minio example references the docker client in RunOptions.PortBindings, which isn't available unless you import it.

It seems counterproductive to import the docker client library that dockertest uses.

Is there a deeper meaning here that I'm not getting?
If not, I wouldn't mind having a go at it and submit a pull-request to fix it.

parseTime dns addition breaks mysql startup

Regarding

edb9d94

this is not cool; it breaks the mysql startup.

  
  * ...e2e_test.go
  Line 37: - Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?parseTime=true.jobs a, db0002?parseTime=true.users b
      WHERE
          a.owner ' at line 25
  goroutine 5 [running]:

Some suggestions and an alternative to Dockertest

Hi Aeneas, I'm opening an issue in absence of other ways to contact you :) I just wanted to let you know I have been working on an alternative approach to achieve the same function of this library, and published it at: https://github.com/ibrt/go-compose. I believe this approach is simpler and has few advantages (listed below), maybe it could be interesting for you to have a look and possibly borrow some ideas!

  • It uses a Docker Compose config, which is standard and supports all Docker options
  • It is smaller but actually more flexible
  • It pulls in way less dependencies
  • It completely delegates the Docker environment configuration to whoever runs the tests
  • It results in less Go code to start/stop containers (i.e. one call to start and one call to kill everything)

On the downside, it doesn't have any support for standard or predefined services. I am planning to add them in separate subpackages, so callers can actually import only the dependencies they need.

Best,
Ivan

Error when creating docker container: no such host

(Docker version is 1.13.1)

I was using the example MongoDB code to create a mongo container in my test case and encountered this no such host error when connecting:

Post http://unix.sock/containers/create?: dial tcp: lookup unix.sock: no such host

Suspected a OS X platform issue, I also tried to run the test case on Travis CI with the following configuration:

language: go
sudo: required

go:
- 1.7

services:
- docker

but got the same error.

Here is the code that caused the problem, it is essentially same with the example code:

func connectToDockerMongo(t *testing.T) (*dockertest.Pool, *dockertest.Resource) {
	pool, err := dockertest.NewPool("")
	fatalIfError(t, err)

	dockerResource, err := pool.Run("mongo", "3.3", []string{""})
	fatalIfError(t, err)

	err = pool.Retry(func() error {
		var e error
		session, e := mgo.Dial(fmt.Sprintf("localhost:%s", dockerResource.GetPort("27017")))
		if e != nil {
			t.Error(e)
			return e
		}
		return session.Ping()
	})
	fatalIfError(t, err)

	return pool, dockerResource
}

Basically, pool.Run("mongo", "3.3", []string{""}) is failing.

Could someone please help. Thanks!

Is this still in active development?

I am planning to use dockertest to code my test base for my new golang microservices.
This is a great tool for that.
Besides, are you planning to put kafka into dockertest?

Container unreachable

Hi ,
Im trying to run some tests and containers but i getting this error for all the db`s that i tried to run ...

Im running the mongo example and the error is

Could not connect to database: Could not set up MongoDB container: 172.17.0.2:36125 unreachable for 10s.
Test are also producing this error

  `go test docker_integration_test.go  --run TestConnectToMongoDB
--- FAIL: TestConnectToMongoDB (10.93s)
        Error Trace:    docker_integration_test.go:130
    Error:      Expected nil, but got: &errors.errorString{s:"Could not set up MongoDB container: 172.17.0.2:36126 unreachable for 10s"}

FAIL
FAIL    command-line-arguments  10.938s
`

Starting mongo on OSX results in error

When trying the provided example, it returns this error:

Could not start resource: : API error (500): {"message":"invalid environment variable: "}

Using the official CE-Docker for Mac

docker version:
Client:
Version: 17.03.0-ce
API version: 1.26
Go version: go1.7.5
Git commit: 60ccb22
Built: Thu Feb 23 10:40:59 2017
OS/Arch: darwin/amd64

Server:
Version: 17.03.0-ce
API version: 1.26 (minimum version 1.12)
Go version: go1.7.5
Git commit: 3a232c8
Built: Tue Feb 28 07:52:04 2017
OS/Arch: linux/amd64
Experimental: true

Ability to specify database version

First, thanks for this. It is definitely useful!

How hard would it be to add version support to this? Ideally I'd like to run unit/integration tests using the same version of MySQL/Redis/PQ/etc. as the version in production.

Do not publish ports on Linux

Currently dockertest publishes all ports for the host. It's the only way to make it work for Windows, but on Linux there is no need to publish any ports since container can be accessed via Docker network (ex: 172.17.0.2).

I propose to detect if the host OS is Linux, disable ports publishing and change the host address that is returned (see #126).

It might also fix the problem with #94.

Cassandra

Hello,
Does it planned the support of cassandra?
Thx in adv

Expose setupContainer

Was there any explicit reason as to why setupContainer is private and not public? I'd like to use this to spin up some private images for testing without having to manage my fork of dockertest.

Thanks again for the awesome package.

Not working for docker-machine on Mac

Not working after this commit. b01b564

env

docker-machine env xxx
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/path/to/xxx"
export DOCKER_MACHINE_NAME="xxx"
# Run this command to configure your shell:
# eval $(docker-machine env xxx)

code

resource, err := pool.Run("imageName", "latest", []string{})
// err: Post http://192.168.99.100:2376/images/create?fromImage=xxxxx&tag=latest: net/http: HTTP/1.x transport connection broken: malformed HTTP response "\x15\x03\x01\x00\x02\x02\x16"

Not working after docker 1.13.1 update

I have this code that was working before today's docker update:

        resource, err := pool.Run("mongo", "latest", []string{""})
	if err != nil {
		log.Fatalf("Could not start resource: %s", err)
	}

Starting today I get: 2017/02/13 14:07:21 Could not start resource: : API error (500): {"message":"invalid environment variable: "}

Docker version:
Client:
Version: 1.13.1
API version: 1.26
Go version: go1.7.5
Git commit: 092cba3727
Built: Sun Feb 12 02:40:56 2017
OS/Arch: linux/amd64

Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Go version: go1.7.5
Git commit: 092cba3727
Built: Sun Feb 12 02:40:56 2017
OS/Arch: linux/amd64
Experimental: false

Mistakes in Readme

in Start a container section
db, err := mgo.Dial(url)
here db should be a global variable. Also line
return db.Ping() != nil
should be changed to return db.Ping() == nil

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.