Skip to content

Instantly share code, notes, and snippets.

View mainawycliffe's full-sized avatar

Maina Wycliffe mainawycliffe

View GitHub Profile
err = pool.Retry(func() error {
SMTP_PORT = resource.GetPort("1025/tcp")
HTTP_PORT = resource.GetPort("8025/tcp")
// ping to ensure that the server is up and running
_, err := net.Dial("tcp", net.JoinHostPort("localhost", SMTP_PORT))
return err
})
if err != nil {
log.Fatalf("Could not connect to database: %s", err)
code := m.Run()
if err := pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
func TestMain(m *testing.M) {
// uses a sensible default on windows (tcp/http) and linux/osx (socket)
pool, err := dockertest.NewPool("")
if err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
// we will use a dockerfile to build the image for testing
resource, err := pool.BuildAndRun("mailhog-test-server", "./Dockerfile", []string{})
if err != nil {
mail := Mail{
host: SMTP_HOST,
port: SMTP_PORT,
from: "from@example.com",
password: "",
username: "",
}
err := mail.Send([]string{"to@example.com"}, "Test Subject", "Sending an automated test email")
assert.Nil(t, err)
resp, err := http.Get(fmt.Sprintf("http://localhost:%s/api/v2/messages", HTTP_PORT))
assert.Nil(t, err)
defer resp.Body.Close()
assert.Equal(t, 200, resp.StatusCode)
body, err := ioutil.ReadAll(resp.Body)
// hard kill the container in 3 minutes (180 Seconds)
resource.Expire(180)
viper.SetConfigFile("./path/to/config-file.yaml")
viper.SetConfigFile("./path/to/config-file-2.json")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.looped")