Skip to content

Instantly share code, notes, and snippets.

@ghostsquad
Created August 15, 2019 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghostsquad/1ad0bc470004e99ab18cfa46ad6a51e5 to your computer and use it in GitHub Desktop.
Save ghostsquad/1ad0bc470004e99ab18cfa46ad6a51e5 to your computer and use it in GitHub Desktop.
docker-test.go
func setup() func() {
pool, err := dockertest.NewPool("")
if err != nil {
fmt.Printf("Could not connect to docker: %s\n", err)
os.Exit(1)
}
pool.MaxWait = time.Second * 120
// pulls an image, creates a container based on it and runs it
// if you update this, ensure the docker-compose.yml is also updated
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "vault",
Tag: "1.2.0",
CapAdd: []string{
"IPC_LOCK",
},
Env: devEnvSlice,
Cmd: []string{"server", "-dev"},
})
if err != nil {
fmt.Printf("Could not start resource: %s\n", err)
os.Exit(1)
}
shutdownFunc := func() {
if err := pool.Purge(resource); err != nil {
fmt.Printf("Could not purge resource: %s\n", err)
os.Exit(1)
}
}
resourcePort = resource.GetPort("8200/tcp")
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
if err := pool.Retry(func() error {
conn, err := net.Dial("tcp", fmt.Sprintf(":%s", resourcePort))
if err != nil {
return errors.Wrap(err, "Could not connect to Vault")
}
defer conn.Close()
//var err error
//db, err = sql.Open("mysql", fmt.Sprintf("root:secret@(localhost:%s)/mysql", resource.GetPort("3306/tcp")))
//if err != nil {
// return err
//}
//return db.Ping()
fmt.Printf("Connected to vault on port: %s\n", resourcePort)
return nil
}); err != nil {
fmt.Printf("Could not connect to container: %s\n", err)
os.Exit(1)
}
return shutdownFunc
}
func TestMain(m *testing.M) {
shutdown := setup()
defer shutdown()
code := m.Run()
os.Exit(code)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment