Skip to content

Commit

Permalink
Mock k6foundry (#110)
Browse files Browse the repository at this point in the history
* make k6foundry configurable
* mock foundry in tests


Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin authored Jan 20, 2025
1 parent a76e9b8 commit d2217b5
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 80 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ issues:
- gosec
- noctx
- gochecknoglobals
- revive
- path: js\/modules\/k6\/http\/.*_test\.go
linters:
# k6/http module's tests are quite complex because they often have several nested levels.
Expand Down
98 changes: 98 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
project_name: k6build
version: 2
env:
- IMAGE_OWNER=ghcr.io/grafana
before:
hooks:
- go mod tidy
builds:
- env:
- CGO_ENABLED=0
goos: ["darwin", "linux", "windows"]
goarch: ["amd64", "arm64"]
ldflags:
- "-s -w -X main.version={{.Version}} -X main.appname={{.ProjectName}}"
dir: cmd/k6build
source:
enabled: true
name_template: "{{ .ProjectName }}_{{ .Version }}_source"

archives:
- id: bundle
format: tar.gz
format_overrides:
- goos: windows
format: zip

checksum:
name_template: "{{ .ProjectName }}_{{ .Version }}_checksums.txt"

snapshot:
name_template: "{{ incpatch .Version }}-next+{{.ShortCommit}}{{if .IsGitDirty}}.dirty{{else}}{{end}}"

changelog:
sort: asc
abbrev: -1
filters:
exclude:
- "^chore:"
- "^docs:"
- "^test:"

dockers:
- id: amd64
dockerfile: Dockerfile.goreleaser
use: buildx
image_templates:
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:{{ .Tag }}-amd64"
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}-amd64"
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}.{{ .Minor }}-amd64"
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:latest-amd64"

build_flag_templates:
- "--platform=linux/amd64"
- "--pull"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- "--label=org.opencontainers.image.licenses=AGPL-3.0-only"
- id: arm64
dockerfile: Dockerfile.goreleaser
use: buildx
image_templates:
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:{{ .Tag }}-arm64"
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}-arm64"
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}.{{ .Minor }}-arm64"
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:latest-arm64"

build_flag_templates:
- "--platform=linux/arm64"
- "--pull"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- "--label=org.opencontainers.image.licenses=AGPL-3.0-only"

docker_manifests:
- id: tag
name_template: "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:{{ .Tag }}"
image_templates:
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:{{ .Tag }}-amd64"
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:{{ .Tag }}-arm64"
- id: major
name_template: "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}"
image_templates:
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}-amd64"
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}-arm64"
- id: major-minor
name_template: "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}.{{ .Minor }}"
image_templates:
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}.{{ .Minor }}-amd64"
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:v{{ .Major }}.{{ .Minor }}-arm64"
- id: latest
name_template: "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:latest"
image_templates:
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:latest-amd64"
- "{{ .Env.IMAGE_OWNER }}/{{ .ProjectName }}:latest-arm64"
22 changes: 21 additions & 1 deletion pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ var (
// GoOpts defines the options for the go build environment
type GoOpts = k6foundry.GoOpts

// Foundry is a function that creates a Foundry Builder
type Foundry interface {
NewBuilder(ctx context.Context, opts k6foundry.NativeBuilderOpts) (k6foundry.Builder, error)
}

// FoundryFunction defines a function that implements the Foundry interface
type FoundryFunction func(context.Context, k6foundry.NativeBuilderOpts) (k6foundry.Builder, error)

// NewBuilder implements the Foundry interface
func (f FoundryFunction) NewBuilder(ctx context.Context, opts k6foundry.NativeBuilderOpts) (k6foundry.Builder, error) {
return f(ctx, opts)
}

// Opts defines the options for configuring the builder
type Opts struct {
// Allow semvers with build metadata
Expand All @@ -55,6 +68,7 @@ type Config struct {
Opts Opts
Catalog catalog.Catalog
Store store.ObjectStore
Foundry Foundry
}

// Builder implements the BuildService interface
Expand All @@ -63,6 +77,7 @@ type Builder struct {
catalog catalog.Catalog
store store.ObjectStore
mutexes sync.Map
foundry Foundry
}

// New returns a new instance of Builder given a BuilderConfig
Expand All @@ -75,10 +90,15 @@ func New(_ context.Context, config Config) (*Builder, error) {
return nil, k6build.NewWrappedError(ErrInitializingBuilder, errors.New("store cannot be nil"))
}

foundry := config.Foundry
if foundry == nil {
foundry = FoundryFunction(k6foundry.NewNativeBuilder)
}
return &Builder{
catalog: config.Catalog,
opts: config.Opts,
store: config.Store,
foundry: foundry,
}, nil
}

Expand Down Expand Up @@ -180,7 +200,7 @@ func (b *Builder) Build( //nolint:funlen
builderOpts.Stderr = os.Stderr
}

builder, err := k6foundry.NewNativeBuilder(ctx, builderOpts)
builder, err := b.foundry.NewBuilder(ctx, builderOpts)
if err != nil {
return k6build.Artifact{}, k6build.NewWrappedError(ErrInitializingBuilder, err)
}
Expand Down
99 changes: 42 additions & 57 deletions pkg/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"context"
"errors"
"fmt"
"net/http/httptest"
"io"
"strings"
"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"
Expand All @@ -21,52 +21,47 @@ import (
// 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",
},
}
type mockBuilder struct {
opts k6foundry.NativeBuilderOpts
}

// 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)
}
// Mocks the Faundry's Build method
// Returns the build info for the given platform, k6 version and modules
func (m *mockBuilder) Build(
_ context.Context,
platform k6foundry.Platform,
k6Version string,
mods []k6foundry.Module,
buildOpts []string,
out io.Writer,
) (*k6foundry.BuildInfo, error) {
modVersions := make(map[string]string)
for _, mod := range mods {
modVersions[mod.Path] = mod.Version
}
return &k6foundry.BuildInfo{
Platform: platform.String(),
ModVersions: modVersions,
}, nil
}

func MockFoundryFactory(_ context.Context, opts k6foundry.NativeBuilderOpts) (k6foundry.Builder, error) {
return &mockBuilder{
opts: opts,
}, nil
}

goproxySrv := httptest.NewServer(proxy)
const catalogJSON = `
{
"k6": {"module": "go.k6.io/k6", "versions": ["v0.1.0", "v0.2.0"]},
"k6/x/ext": {"module": "go.k6.io/k6ext", "versions": ["v0.1.0", "v0.2.0"]},
"k6/x/ext2": {"module": "go.k6.io/k6ext2", "versions": ["v0.1.0"]}
}
`

catalog, err := catalog.NewCatalogFromFile("testdata/catalog.json")
// SetupTestBuilder setups a local build service for testing
func SetupTestBuilder(t *testing.T) (*Builder, error) {
catalog, err := catalog.NewCatalogFromJSON(strings.NewReader(catalogJSON))
if err != nil {
return nil, fmt.Errorf("setting up test builder %w", err)
}
Expand All @@ -77,20 +72,10 @@ func SetupTestBuilder(t *testing.T) (*Builder, error) {
}

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,
},
},
Opts: Opts{},
Catalog: catalog,
Store: store,
Foundry: FoundryFunction(MockFoundryFactory),
})
}

Expand Down Expand Up @@ -325,7 +310,7 @@ func TestIdempotentBuild(t *testing.T) {
})
}

// TestConcurrentBuilds tests that is sage to build the same artifact concurrently and that
// TestConcurrentBuilds tests that is safe to build the same artifact concurrently and that
// concurrent builds of different artifacts are not affected.
// The test uses a local test setup backed by a file object store.
// Attempting to write the same artifact twice will return an error.
Expand Down
5 changes: 0 additions & 5 deletions pkg/builder/testdata/catalog.json

This file was deleted.

4 changes: 0 additions & 4 deletions pkg/builder/testdata/deps/k6/cmd/k6.go

This file was deleted.

3 changes: 0 additions & 3 deletions pkg/builder/testdata/deps/k6/go.mod

This file was deleted.

3 changes: 0 additions & 3 deletions pkg/builder/testdata/deps/k6ext/go.mod

This file was deleted.

1 change: 0 additions & 1 deletion pkg/builder/testdata/deps/k6ext/main.go

This file was deleted.

3 changes: 0 additions & 3 deletions pkg/builder/testdata/deps/k6ext2/go.mod

This file was deleted.

1 change: 0 additions & 1 deletion pkg/builder/testdata/deps/k6ext2/main.go

This file was deleted.

2 changes: 0 additions & 2 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func (f buildFunction) Build(
return f(ctx, platform, k6Constrains, deps)
}

//nolint:revive
func buildOk(
ctx context.Context,
platform string,
Expand All @@ -41,7 +40,6 @@ func buildOk(
}, nil
}

//nolint:revive
func buildErr(
ctx context.Context,
platform string,
Expand Down

0 comments on commit d2217b5

Please sign in to comment.