Skip to content

Commit

Permalink
Test the controller works with the Go build cache
Browse files Browse the repository at this point in the history
A simple test to check that the controller doesn't cause the go build
cache to grow, unduly. Essentially:

 1. Run the stack
 2. Don't change anything
 3. Run the stack again
 4. Check that the Go build cache didn't change between 1. and 3.

If a stack is built in a fresh tmp directory each time (as it was
previously), this test will fail, because at least the stack's main.go
will look like a new file to the cache.

Signed-off-by: Michael Bridgen <[email protected]>
  • Loading branch information
squaremo committed Feb 2, 2023
1 parent b0dcbf6 commit 581425f
Show file tree
Hide file tree
Showing 5 changed files with 521 additions and 0 deletions.
105 changes: 105 additions & 0 deletions test/stable_build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package tests

import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/pulumi/pulumi-kubernetes-operator/pkg/apis/pulumi/shared"
pulumiv1 "github.com/pulumi/pulumi-kubernetes-operator/pkg/apis/pulumi/v1"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

// The go build cache will grow whenever it compiles a "new" file (new content, or new path; some
// other criteria). This test is to check that, without changing anything, rerunning a Go-based
// stack will _not_ grow the go cache. Working in sympathy with the go build cache is important
// because it means disk use is lower and more predictable, and build times much faster.
var _ = Describe("go build caching", func() {
var (
stack *pulumiv1.Stack
gitdir string
kubeconfig string
)

BeforeEach(func() {
tmpdir, err := os.MkdirTemp("", "pulumi-test")
Expect(err).NotTo(HaveOccurred())
DeferCleanup(os.RemoveAll, tmpdir)

gitdir = filepath.Join(tmpdir, "git")
makeFixtureIntoRepo(gitdir, "testdata/go-build")

// For the duration of the test, set the go build cache to a new location, so we have more
// confidence when it changes (or doesn't change), we know why.
gobuildcache := os.Getenv("GOCACHE")
os.Setenv("GOCACHE", filepath.Join(tmpdir, "gocache"))
DeferCleanup(os.Setenv, "GOCACHE", gobuildcache)

kubeconfig = writeKubeconfig(tmpdir)

stack = &pulumiv1.Stack{
Spec: shared.StackSpec{
Stack: "test",
Backend: fmt.Sprintf("file://%s", tmpdir),
GitSource: &shared.GitSource{
ProjectRepo: gitdir,
RepoDir: "testdata/go-build",
Branch: "default",
},
EnvRefs: map[string]shared.ResourceRef{
"PULUMI_CONFIG_PASSPHRASE": shared.NewLiteralResourceRef("password"),
"KUBECONFIG": shared.NewLiteralResourceRef(kubeconfig),
},
ContinueResyncOnCommitMatch: true,
},
}
stack.Name = "go-build-" + randString()
stack.Namespace = "default"

Expect(k8sClient.Create(context.TODO(), stack)).To(Succeed())
DeferCleanup(deleteAndWaitForFinalization, stack)
})

When("a go stack is run multiple times", func() {
var (
beforeSize string
)

checkCacheSize := func() string {
out, err := exec.Command("go", "env", "GOCACHE").Output()
ExpectWithOffset(1, err).ToNot(HaveOccurred())
cachedir := string(out)
cachedir = strings.TrimSpace(cachedir)
GinkgoWriter.Println("Cache:", cachedir)
// there's just one source file in the project, so the difference between the cache
// working and it not working will be pretty small. `-k` measures in 1KiB blocks, which
// should be fine enough.
out, err = exec.Command("du", "-s", "-k", cachedir).Output()
ExpectWithOffset(1, err).ToNot(HaveOccurred())
s := string(out)
return s
}

BeforeEach(func() {
waitForStackSuccess(stack)
beforeSize = checkCacheSize()
GinkgoWriter.Println("Before:", beforeSize)
})

It("doesn't grow the Go build cache", func() {
// run it again and check that we get the same result from du -sh <cache>
stack.Spec.ResyncFrequencySeconds = 30 // just to provoke reprocessing
resetWaitForStack()
Expect(k8sClient.Update(context.TODO(), stack)).To(Succeed())
waitForStackSuccess(stack)
s := checkCacheSize()
GinkgoWriter.Println("After:", s)
Expect(s).To(Equal(beforeSize))
})
})
})
3 changes: 3 additions & 0 deletions test/testdata/go-build/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: go-build
description: A minimal Kubernetes Go Pulumi program
runtime: go
65 changes: 65 additions & 0 deletions test/testdata/go-build/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module go-build

go 1.18

require (
github.com/pulumi/pulumi-kubernetes/sdk/v3 v3.23.1
github.com/pulumi/pulumi/sdk/v3 v3.53.0
)

require (
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/cheggaaa/pb v1.0.18 // indirect
github.com/djherbis/times v1.2.0 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/gofrs/uuid v3.3.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.0.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/mattn/go-runewidth v0.0.8 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/opentracing/basictracer-go v1.0.0 // indirect
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/term v1.1.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94 // indirect
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/cobra v1.4.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/texttheater/golang-levenshtein v0.0.0-20191208221605-eb6844b05fc6 // indirect
github.com/tweekmonster/luser v0.0.0-20161003172636-3fa38070dbd7 // indirect
github.com/uber/jaeger-client-go v2.22.1+incompatible // indirect
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
github.com/xanzy/ssh-agent v0.3.2 // indirect
go.uber.org/atomic v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/genproto v0.0.0-20200608115520-7c474a2e3482 // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/frand v1.4.2 // indirect
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0 // indirect
)
Loading

0 comments on commit 581425f

Please sign in to comment.