Skip to content

Commit

Permalink
Merge pull request #107 from grafana/move-builder-tests
Browse files Browse the repository at this point in the history
move builder test to its package
  • Loading branch information
pablochacin authored Jan 16, 2025
2 parents 245ffca + 685e6d8 commit a76e9b8
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 95 deletions.
89 changes: 85 additions & 4 deletions pkg/local/local_test.go → pkg/builder/builder_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,99 @@
package local
package builder

import (
"context"
"errors"
"fmt"
"net/http/httptest"
"sync"
"testing"

"github.com/grafana/k6build"
"github.com/grafana/k6build/pkg/catalog"
"github.com/grafana/k6build/pkg/store/file"
"github.com/grafana/k6foundry"
"github.com/grafana/k6foundry/pkg/testutils/goproxy"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

// DependencyComp compares two dependencies for ordering
func DependencyComp(a, b catalog.Module) bool { return a.Path < b.Path }

// SetupTestBuilder setups a local build service for testing
func SetupTestBuilder(t *testing.T) (*Builder, error) {
modules := []struct {
path string
version string
source string
}{
{
path: "go.k6.io/k6",
version: "v0.1.0",
source: "testdata/deps/k6",
},
{
path: "go.k6.io/k6",
version: "v0.2.0",
source: "testdata/deps/k6",
},
{
path: "go.k6.io/k6ext",
version: "v0.1.0",
source: "testdata/deps/k6ext",
},
{
path: "go.k6.io/k6ext",
version: "v0.2.0",
source: "testdata/deps/k6ext",
},
{
path: "go.k6.io/k6ext2",
version: "v0.1.0",
source: "testdata/deps/k6ext2",
},
}

// creates a goproxy that serves the given modules
proxy := goproxy.NewGoProxy()
for _, m := range modules {
err := proxy.AddModVersion(m.path, m.version, m.source)
if err != nil {
return nil, fmt.Errorf("setup %w", err)
}
}

goproxySrv := httptest.NewServer(proxy)

catalog, err := catalog.NewCatalogFromFile("testdata/catalog.json")
if err != nil {
return nil, fmt.Errorf("setting up test builder %w", err)
}

store, err := file.NewFileStore(t.TempDir())
if err != nil {
return nil, fmt.Errorf("creating temporary object store %w", err)
}

return New(context.Background(), Config{
Opts: Opts{
GoOpts: k6foundry.GoOpts{
CopyGoEnv: true,
Env: map[string]string{
"GOPROXY": goproxySrv.URL,
"GONOPROXY": "none",
"GOPRIVATE": "go.k6.io",
"GONOSUMDB": "go.k6.io",
},
TmpCache: true,
},
},
Catalog: catalog,
Store: store,
})
}

func TestDependencyResolution(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -72,7 +153,7 @@ func TestDependencyResolution(t *testing.T) {
t.Run(tc.title, func(t *testing.T) {
t.Parallel()

buildsrv, err := SetupTestLocalBuildService(t)
buildsrv, err := SetupTestBuilder(t)
if err != nil {
t.Fatalf("test setup %v", err)
}
Expand Down Expand Up @@ -103,7 +184,7 @@ func TestDependencyResolution(t *testing.T) {

func TestIdempotentBuild(t *testing.T) {
t.Parallel()
buildsrv, err := SetupTestLocalBuildService(t)
buildsrv, err := SetupTestBuilder(t)
if err != nil {
t.Fatalf("test setup %v", err)
}
Expand Down Expand Up @@ -250,7 +331,7 @@ func TestIdempotentBuild(t *testing.T) {
// Attempting to write the same artifact twice will return an error.
func TestConcurrentBuilds(t *testing.T) {
t.Parallel()
buildsrv, err := SetupTestLocalBuildService(t)
buildsrv, err := SetupTestBuilder(t)
if err != nil {
t.Fatalf("test setup %v", err)
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
91 changes: 0 additions & 91 deletions pkg/local/testutils.go

This file was deleted.

0 comments on commit a76e9b8

Please sign in to comment.