#Why exactly can I not run "go test" in a Dagger module ?

1 messages · Page 1 of 1 (latest)

sleek marsh
#

Not trying to test an actual Dagger function, just a simple unit test for pure Go helper function that happens to be in the module, but it fails with DAGGER_SESSION_PORT is not set.
Is this actually the current state of things or am I missing something?

river bone
#

Can you please share your code?

fluid echo
#

@sleek marshyou can't directly run a Go test that calls any of the functions in your module that uses the dag variable because dagger needs a session to be created in order to work. The component that sets up this session is the dagger cli. One thing you can do is to call dagger run go test ./... and that should work

sleek marsh
#

So here's a reproducer. AFAICT the test shouldn't ever hit anything in dag or even a dagger type. (dagger run go test works, thanks)
main.go

package main 
import (
    "fmt"
    "dagger/dagger-test-reproducer/internal/dagger"
)
type DaggerTestReproducer struct{}
// Returns a container that echoes whatever string argument is provided
func (m *DaggerTestReproducer) ContainerEcho(stringArg string) *dagger.Container {
    return dag.Container().From("alpine:latest").WithExec([]string{"echo", stringArg})
}

type config struct {
    SomeField string
}

func (c config) validate() error {
    if c.SomeField == "invalid" {
        return fmt.Errorf("SomeField value not supported: %s", c.SomeField)
    }
    return nil
}

config_test.go

package main
import "testing"
func Test(t *testing.T) {
    config := config{SomeField: "invalid"}
    err := config.validate()
    if err == nil {
        t.Fatal("test failed")
    }
}

go test results in panic: DAGGER_SESSION_PORT is not set

#

Oh I see, I guess it's the var dag = dagger.Connect() global in dagger.gen.go would cause this regardless

#

So technically isolating the code in a separate package would work here as well

fluid echo