#Writing Tests for Pipeline (not Pipeline Test Step)

1 messages · Page 1 of 1 (latest)

unkempt vale
#

Hallo,

I am currently evaluating Dagger for my company and trying to figure out how to write Tests for my Dagger Functions.
The idea is to write Tests to test the Output of the functions similar to JUnit Tests in Java and Integration Test.

The langauge is Go and I followed all steps in Documentation.
Can someone point me into the right direction how to integration Go Tests into Dagger Module and Functions.

The result should be something like "go test" and I am sure my Pipeline works perfectly.

terse hare
#

we do something like dagger run go test and have files like blabla_test.go in main package

terse hare
unkempt vale
#

@terse hare can you provide me with an example?

I am learing Go as I go 🙂

dagger_test.go```
package main

import (
"testing"
"main"
)

func TestDagger(t *testing.T){
expectedResult := "test";
actualResult := main.ContainerEcho("test");

 // Assert
 if actualResult != expectedResult {
    t.Errorf("Expected %v, but got %v", expectedResult, actualResult)
}
Println("TestDagger passed")

}


main.go```
package main

import (
    "context"
)

type Pipeline struct{}

// Returns a container that echoes whatever string argument is provided
func (m *Pipeline) ContainerEcho(stringArg string) *Container {
    return dag.Container().From("alpine:latest").WithExec([]string{"echo", stringArg})
}

Can you pin point where I am doing it wrong?

Error:

dagger run go test                                                                                                                                                                                                                                  ─╯
✔ connect 1.0s
# dagger/pipeline                           
dagger_test.go:7:8: "/main.go" is not a package path; see 'go help packages'       
FAIL    dagger/pipeline [setup failed]                                                          
exit status 1
terse hare
terse hare
#

@unkempt vale do you have main.go under dagger folder?

unkempt vale
#

@terse hare Yes, I believe I do.

├── LICENSE
├── dagger
│   ├── dagger.gen.go
│   ├── dagger_test.go
│   ├── go.mod
│   ├── go.sum
│   ├── internal
│   │   ├── dagger
│   │   │   └── dagger.gen.go
│   │   ├── querybuilder
│   │   │   ├── marshal.go
│   │   │   └── querybuilder.go
│   │   └── telemetry
│   │       ├── attrs.go
│   │       ├── batch_processor.go
│   │       ├── init.go
│   │       ├── processor.go
│   │       ├── proxy.go
│   │       └── span.go
│   └── main.go
└── dagger.json
unkempt vale
#

@terse hare As I understand you:

  • I need to use --source=. param to put my main.go with ContainerEcho outside of dagger folder.
  • The test files also are outside the dagger folder
  • Importing main pacakge should work then
terse hare
#

basically is that

#

i dont know why but if you do it with /dagger folder does not seem to work 🤔

#

@unkempt vale did you manage to make it work?

unkempt vale
#

It works! Thanks a lot:

Creating structure: dagger init --name=pipeline --sdk=go --source=.

╭─    ~/folder/ci  on   feature/dagger  ··································································································································· 1 ✘  took 5s   .
├── LICENSE
├── dagger.gen.go
├── dagger.json
├── dagger_test.go
├── go.mod
├── go.sum
├── internal
│   ├── dagger
│   │   └── dagger.gen.go
│   ├── querybuilder
│   │   ├── marshal.go
│   │   └── querybuilder.go
│   └── telemetry
│       ├── attrs.go
│       ├── batch_processor.go
│       ├── init.go
│       ├── processor.go
│       ├── proxy.go
│       └── span.go
└── main.go

main.go File

package main

import (
    "context"
)

type Pipeline struct{}

// Returns a container that echoes whatever string argument is provided
func (m *Pipeline) ContainerEcho(stringArg string) *Container {
    return dag.Container().From("alpine:latest").WithExec([]string{"echo", stringArg})
}

func (m *Pipeline) ContainerEchoString(ctx context.Context, stringArg string) (string, error) {
    return dag.Container().From("alpine:latest").WithExec([]string{"echo", stringArg}).Stdout(ctx)
}

dagger_test.go File

package main

import (
    "context"
    "fmt"
    "testing"
    "strings"
)

func TestDaggerTestingFail(t *testing.T){
    expectedResult := "Testing";

    var marshalCtx = context.Background()
    pipeline := new(Pipeline) // Ensure that Pipeline is a type defined in your main package
    actualResult, error := pipeline.ContainerEchoString(marshalCtx, "test")

    if error != nil {
        t.Fatalf("Error: %v", error)
    }   

    // Assert
    // Decode UTF-8 String becasue actualResult is output for STDOUT
    if strings.TrimSpace(actualResult) != expectedResult {
        t.Fatalf("Expected %v, but got %v", expectedResult, actualResult)
    }
    fmt.Println("TestDagger passed") // Use fmt.Println instead of Println
}

Executing: dagger run go test

terse hare
#

nice 🔥

#

one thing to notice when i run tests and it fails somehow my terminal breaks a little bit