#Postgres and Dagger Go SDK - connection refused?

1 messages · Page 1 of 1 (latest)

vocal heath
#

I'm trying to run integration tests that need to talk to a postgres db. I'm getting a connection refused - I'm not sure why. Here's my code:

func integration(ctx context.Context, client *dagger.Client) error {

    postgres := client.Container().
        From("postgres:13.3-alpine").
        WithEnvVariable("POSTGRES_USER", "postgres").
        WithEnvVariable("POSTGRES_PASSWORD", "postgres").
        WithEnvVariable("POSTGRES_HOST_AUTH_METHOD", "trust").
        WithEnvVariable("POSTGRES_DB", "bareminimum").
        WithExposedPort(5432).
        // WithExec([]string{"postgres"}).
        AsService()

    _, err := client.
        Container().
        From("golangci/golangci-lint:v1.55.0").
        WithDirectory("/src", client.Host().Directory(".")).WithWorkdir("/src").
        WithExec([]string{"go", "test", "-json", "-race", "-tags=integration", "./..."}).
        WithServiceBinding("postgres", postgres).
        Sync(ctx)
    return err

}

Any ideas?

solar comet
#

Can you share what the postgres connection string in your second container (the one running the integration tests) looks like?

vocal heath
#

Sure thing, here's how I'm connecting:

db, err := sql.Open("postgres", "user=postgres dbname=bareminimum sslmode=disable")
    if err != nil {
        t.Fatal(err)
    }
    defer db.Close()

I can provide the test and the function under test if that would help

verbal kite
#

Can you try if that works for you?

vocal heath
#

trying now ...

verbal kite
#

It's not using the new AsService method since it's running in an older version of Dagger, but that shouldn't affect the test

vocal heath
#

oh, i'm running (version v0.9.3) - do you know which version I'd have to downgrade to?

verbal kite
#

The go.mod in that directory should show

vocal heath
#

oh right, also, does this mean this doesn't work in the latest version?

verbal kite
#

You can also run it in 0.9.3 and just use the AsService method. It should still wrok

verbal kite
final aurora
# verbal kite Yes, it won't compile in 0.9.3. We need to bump it. cc <@933501536624054272>

Yep! I just need to make a PR.

@vocal heath something like this works with v0.9.3

package main

import (
    "context"
    "fmt"
    "os"

    "dagger.io/dagger"
)

func main() {
    ctx := context.Background()

    // create a Dagger client
    client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
    if err != nil {
        panic(err)
    }
    defer client.Close()

    // Database service used for application tests
    database := client.Container().From("postgres:16").
        WithEnvVariable("POSTGRES_PASSWORD", "test").
        WithExec([]string{"postgres"}).
        WithExposedPort(5432).
        AsService()

    // Project to test
    src := client.Host().Directory(".")

    // Run application tests
    out, err := client.Container().From("golang:1.21").
        WithServiceBinding("db", database).     // bind database with the name db
        WithEnvVariable("DB_HOST", "db").       // db refers to the service binding
        WithEnvVariable("DB_PASSWORD", "test"). // password set in db container
        WithEnvVariable("DB_USER", "postgres"). // default user in postgres image
        WithEnvVariable("DB_NAME", "postgres"). // default db name in postgres image
        WithDirectory("/src", src).
        WithWorkdir("/src").
        WithExec([]string{"go", "test"}). // execute go test
        Stdout(ctx)

    if err != nil {
        panic(err)
    }

    fmt.Printf(out)
}
#

Will make the PR in a sec.

final aurora
vocal heath
#

thanks team, got it working with your help 🙏

verbal kite
#

Mind sharing what the issue was? It'll help us improve our docs.

vocal heath
#

not at all, one second, trying to suss out the differences between the snippet non working snippet i posted and the working one

#

looks like it might just have been an ordering problem :

WithExec([]string{"go", "test", "-json", "-race", "-tags=integration", "./..."}).
        WithServiceBinding("postgres", postgres).

vs

    WithServiceBinding("localhost", postgres).
        WithExec([]string{"go", "test", "-json", "-race", "-tags=integration", "./..."}).
verbal kite
#

That makes sense. Order is relevant when assembling your pipeline. Not sure if we highlight this in our docs. @balmy aspen ?

verbal kite