Skip to content

Commit

Permalink
Adding the validate dagger function
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Oct 17, 2024
1 parent 9d9cce7 commit fd0fe10
Show file tree
Hide file tree
Showing 20 changed files with 203 additions and 38 deletions.
18 changes: 13 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,24 @@ define LDFLAGS
"
endef

.PHONY: test
test:
# pacts and unit
go test -tags "$(BUILDTAGS)" -v ./...
.PHONY: test-unit
test-unit:
go test -v `go list ./... | grep -v /pact` -tags "$(BUILDTAGS)"

.PHONY: test-pact
test-pact:
go test -v ./pact/... -tags "$(BUILDTAGS)"

.PNONY: test-e2e
test-e2e:
# integration and e2e
docker build -t replicated-cli-test -f hack/Dockerfile.testing .
docker run --rm --name replicated-cli-tests \
-v `pwd`:/go/src/github.com/replicatedhq/replicated \
replicated-cli-test \
replicated-cli-test

.PHONY: test
test: test-unit test-pact test-e2e

.PHONY: publish-pact
publish-pact:
Expand Down
15 changes: 15 additions & 0 deletions dagger/compatibility.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"context"
"dagger/replicated/internal/dagger"
)

func validateCompatibility(
ctx context.Context,

// +defaultPath="./"
source *dagger.Directory,
) error {
return nil
}
20 changes: 20 additions & 0 deletions dagger/exec_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"dagger/replicated/internal/dagger"
"time"
)

// CacheBustingExec is a helper function that will add a cache busting env var automatically
// to the container. This is useful when Exec target is a dynamic event acting on an entity outside
// of the container that you absolutely want to re-run every time.
//
// Temporary hack until cache controls are a thing: https://docs.dagger.io/cookbook/#invalidate-cache
func CacheBustingExec(args []string, opts ...dagger.ContainerWithExecOpts) dagger.WithContainerFunc {
return func(c *dagger.Container) *dagger.Container {
if c == nil {
panic("CacheBustingExec requires a container, but container was nil")
}
return c.WithEnvVariable("DAGGER_CACHEBUSTER_CBE", time.Now().String()).WithExec(args, opts...)
}
}
34 changes: 34 additions & 0 deletions dagger/functionality.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"context"
"dagger/replicated/internal/dagger"
)

func validateFunctionality(
ctx context.Context,

// +defaultPath="./"
source *dagger.Directory,
) error {
goModCache := dag.CacheVolume("replicated-go-mod-122")
goBuildCache := dag.CacheVolume("replicated-go-build-121")

// unit tests
unitTest := dag.Container().
From("golang:1.22").
WithMountedDirectory("/go/src/github.com/replicatedhq/replicated", source).
WithWorkdir("/go/src/github.com/replicatedhq/replicated").
WithMountedCache("/go/pkg/mod", goModCache).
WithEnvVariable("GOMODCACHE", "/go/pkg/mod").
WithMountedCache("/go/build-cache", goBuildCache).
WithEnvVariable("GOCACHE", "/go/build-cache").
With(CacheBustingExec([]string{"make", "test-unit"}))

_, err := unitTest.Stderr(ctx)
if err != nil {
return err
}

return nil
}
15 changes: 15 additions & 0 deletions dagger/performance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"context"
"dagger/replicated/internal/dagger"
)

func validatePerformance(
ctx context.Context,

// +defaultPath="./"
source *dagger.Directory,
) error {
return nil
}
34 changes: 34 additions & 0 deletions dagger/security.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"context"
"dagger/replicated/internal/dagger"
)

func validateSecurity(
ctx context.Context,

// +defaultPath="./"
source *dagger.Directory,
) error {
goModCache := dag.CacheVolume("replicated-go-mod-122")
goBuildCache := dag.CacheVolume("replicated-go-build-121")

// run semgrep
semgrep := dag.Container().
From("returntocorp/semgrep").
WithMountedDirectory("/go/src/github.com/replicatedhq/replicated", source).
WithWorkdir("/go/src/github.com/replicatedhq/replicated").
WithMountedCache("/go/pkg/mod", goModCache).
WithEnvVariable("GOMODCACHE", "/go/pkg/mod").
WithMountedCache("/go/build-cache", goBuildCache).
WithEnvVariable("GOCACHE", "/go/build-cache").
With(CacheBustingExec([]string{"semgrep", "scan", "--config=p/golang", "."}))

_, err := semgrep.Stderr(ctx)
if err != nil {
return err
}

return nil
}
31 changes: 31 additions & 0 deletions dagger/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"context"
"dagger/replicated/internal/dagger"
)

func (r *Replicated) Validate(
ctx context.Context,

// +defaultPath="./"
source *dagger.Directory,
) error {
if err := validateSecurity(ctx, source); err != nil {
return err
}

if err := validateFunctionality(ctx, source); err != nil {
return err
}

if err := validateCompatibility(ctx, source); err != nil {
return err
}

if err := validatePerformance(ctx, source); err != nil {
return err
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/pact-foundation/pact-go/dsl"
realkotsclient "github.com/replicatedhq/replicated/pkg/kotsclient"
"github.com/replicatedhq/replicated/pkg/platformclient"
"github.com/stretchr/testify/assert"
)
Expand All @@ -14,7 +15,7 @@ func Test_AppCreate(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-app-create-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

createdApp, err := client.CreateKOTSApp("app-create-1")
assert.Nil(t, err)
Expand Down
5 changes: 3 additions & 2 deletions pkg/kotsclient/app_test.go → pact/kotsclient/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/pact-foundation/pact-go/dsl"
realkotsclient "github.com/replicatedhq/replicated/pkg/kotsclient"
"github.com/replicatedhq/replicated/pkg/platformclient"
"github.com/stretchr/testify/assert"
)
Expand All @@ -14,7 +15,7 @@ func Test_ListApps(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-list-apps-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

apps, err := client.ListApps(false)
assert.Nil(t, err)
Expand Down Expand Up @@ -68,7 +69,7 @@ func Test_RemoveApp(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-rm-app-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

err = client.DeleteKOTSApp("replicated-cli-rm-app-app")
assert.Nil(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/pact-foundation/pact-go/dsl"
realkotsclient "github.com/replicatedhq/replicated/pkg/kotsclient"
"github.com/replicatedhq/replicated/pkg/platformclient"
"github.com/replicatedhq/replicated/pkg/types"
"github.com/stretchr/testify/assert"
Expand All @@ -15,7 +16,7 @@ func Test_CreateChannel(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-create-channel-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

channel, err := client.CreateChannel("replicated-cli-create-channel-app", "New Channel", "Description")
assert.Nil(t, err)
Expand Down Expand Up @@ -64,7 +65,7 @@ func Test_ListChannels(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-list-channels-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

channels, err := client.ListChannels("replicated-cli-list-channels-app", "")
assert.Nil(t, err)
Expand Down Expand Up @@ -111,7 +112,7 @@ func Test_GetChannel(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-get-channel-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

channel, err := client.GetChannel("replicated-cli-get-channel-app", "replicated-cli-get-channel-unstable")
assert.Nil(t, err)
Expand Down Expand Up @@ -153,7 +154,7 @@ func Test_RemoveChannels(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-rm-channel-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

err = client.ArchiveChannel("replicated-cli-rm-channel-app", "replicated-cli-rm-channel-beta")
assert.Nil(t, err)
Expand Down Expand Up @@ -216,7 +217,7 @@ func Test_AddRemoveSemver(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-semver-channel-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

channel := types.Channel{
Name: "Unstable",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/pact-foundation/pact-go/dsl"
realkotsclient "github.com/replicatedhq/replicated/pkg/kotsclient"
"github.com/replicatedhq/replicated/pkg/platformclient"
"github.com/stretchr/testify/assert"
)
Expand All @@ -14,7 +15,7 @@ func Test_CreateInstaller(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "repl-cli-create-installer-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

installerSpec, err := client.CreateInstaller("repl-cli-create-installer-app", testInstallerYAML)
assert.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/pact-foundation/pact-go/dsl"
realkotsclient "github.com/replicatedhq/replicated/pkg/kotsclient"
"github.com/replicatedhq/replicated/pkg/platformclient"
"github.com/stretchr/testify/assert"
)
Expand All @@ -14,9 +15,9 @@ func Test_CreateRegistryDockerHubPassword(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-add-registry-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

req := AddKOTSRegistryRequest{
req := realkotsclient.AddKOTSRegistryRequest{
Provider: "dockerhub",
Endpoint: "index.docker.io",
AuthType: "password",
Expand Down Expand Up @@ -74,9 +75,9 @@ func Test_CreateRegistryDockerHubAccessToken(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-add-registry-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

req := AddKOTSRegistryRequest{
req := realkotsclient.AddKOTSRegistryRequest{
Provider: "dockerhub",
Endpoint: "index.docker.io",
AuthType: "token",
Expand Down Expand Up @@ -134,9 +135,9 @@ func Test_CreateRegistryECR(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-add-registry-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

req := AddKOTSRegistryRequest{
req := realkotsclient.AddKOTSRegistryRequest{
Provider: "ecr",
Endpoint: "0000000000.dkr.ecr.us-east-2.amazonaws.com",
AuthType: "accesskey",
Expand Down Expand Up @@ -194,9 +195,9 @@ func Test_CreateRegistryGCR(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-add-registry-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

req := AddKOTSRegistryRequest{
req := realkotsclient.AddKOTSRegistryRequest{
Provider: "gcr",
Endpoint: "gcr.io",
AuthType: "serviceaccount",
Expand Down Expand Up @@ -254,9 +255,9 @@ func Test_CreateRegistryGARServiceAccount(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-add-registry-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

req := AddKOTSRegistryRequest{
req := realkotsclient.AddKOTSRegistryRequest{
Provider: "gar",
Endpoint: "pkg.dev",
AuthType: "serviceaccount",
Expand Down Expand Up @@ -314,9 +315,9 @@ func Test_CreateRegistryGARAccessToken(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-add-registry-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

req := AddKOTSRegistryRequest{
req := realkotsclient.AddKOTSRegistryRequest{
Provider: "gar",
Endpoint: "pkg.dev",
AuthType: "token",
Expand Down Expand Up @@ -374,7 +375,7 @@ func Test_RemoveRegistryDockerHubPassword(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

api := platformclient.NewHTTPClient(u, "replicated-cli-rm-registry-token")
client := VendorV3Client{HTTPClient: *api}
client := realkotsclient.VendorV3Client{HTTPClient: *api}

err = client.RemoveKOTSRegistry("index.docker.io")
assert.Nil(t, err)
Expand Down
Loading

0 comments on commit fd0fe10

Please sign in to comment.