From 8cdcb89cefcf569a7aedf5943ca6a1010e0373c5 Mon Sep 17 00:00:00 2001 From: Matt Moyer Date: Tue, 15 Dec 2020 12:19:42 -0600 Subject: [PATCH] Add a library.PinnipedCLIPath() test helper, with caching. Caching saves us a little bit of time now that we're using the CLI in more and more tests. Signed-off-by: Matt Moyer --- test/integration/cli_test.go | 24 +--------- .../concierge_credentialrequest_test.go | 2 +- test/library/cli.go | 45 +++++++++++++++++++ 3 files changed, 48 insertions(+), 23 deletions(-) create mode 100644 test/library/cli.go diff --git a/test/integration/cli_test.go b/test/integration/cli_test.go index 33a0cda1..1259ccce 100644 --- a/test/integration/cli_test.go +++ b/test/integration/cli_test.go @@ -42,7 +42,7 @@ func TestCLIGetKubeconfigStaticToken(t *testing.T) { authenticator := library.CreateTestWebhookAuthenticator(ctx, t) // Build pinniped CLI. - pinnipedExe := buildPinnipedCLI(t) + pinnipedExe := library.PinnipedCLIPath(t) for _, tt := range []struct { name string @@ -109,25 +109,6 @@ func TestCLIGetKubeconfigStaticToken(t *testing.T) { } } -func buildPinnipedCLI(t *testing.T) string { - t.Helper() - - pinnipedExeDir, err := ioutil.TempDir("", "pinniped-cli-test-*") - require.NoError(t, err) - t.Cleanup(func() { require.NoError(t, os.RemoveAll(pinnipedExeDir)) }) - - pinnipedExe := filepath.Join(pinnipedExeDir, "pinniped") - output, err := exec.Command( - "go", - "build", - "-o", - pinnipedExe, - "go.pinniped.dev/cmd/pinniped", - ).CombinedOutput() - require.NoError(t, err, string(output)) - return pinnipedExe -} - func runPinnipedCLI(t *testing.T, pinnipedExe string, args ...string) (string, string) { t.Helper() var stdout, stderr bytes.Buffer @@ -145,8 +126,7 @@ func TestCLILoginOIDC(t *testing.T) { defer cancel() // Build pinniped CLI. - t.Logf("building CLI binary") - pinnipedExe := buildPinnipedCLI(t) + pinnipedExe := library.PinnipedCLIPath(t) // Run "pinniped login oidc" to get an ExecCredential struct with an OIDC ID token. credOutput, sessionCachePath := runPinniedLoginOIDC(ctx, t, pinnipedExe) diff --git a/test/integration/concierge_credentialrequest_test.go b/test/integration/concierge_credentialrequest_test.go index 5e2458e0..1a53ef38 100644 --- a/test/integration/concierge_credentialrequest_test.go +++ b/test/integration/concierge_credentialrequest_test.go @@ -65,7 +65,7 @@ func TestSuccessfulCredentialRequest(t *testing.T) { return library.CreateTestJWTAuthenticator(ctx, t) }, token: func(t *testing.T) (string, string, []string) { - pinnipedExe := buildPinnipedCLI(t) + pinnipedExe := library.PinnipedCLIPath(t) credOutput, _ := runPinniedLoginOIDC(ctx, t, pinnipedExe) token := credOutput.Status.Token diff --git a/test/library/cli.go b/test/library/cli.go new file mode 100644 index 00000000..d68e2439 --- /dev/null +++ b/test/library/cli.go @@ -0,0 +1,45 @@ +// Copyright 2020 the Pinniped contributors. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package library + +import ( + "io/ioutil" + "os/exec" + "path/filepath" + "sync" + "testing" + + "github.com/stretchr/testify/require" + + "go.pinniped.dev/internal/testutil" +) + +//nolint: gochecknoglobals +var pinnipedCLIBinaryCache struct { + buf []byte + mutex sync.Mutex +} + +// PinnipedCLIPath returns the path to the Pinniped CLI binary, built on demand and cached between tests. +func PinnipedCLIPath(t *testing.T) string { + t.Helper() + pinnipedCLIBinaryCache.mutex.Lock() + defer pinnipedCLIBinaryCache.mutex.Unlock() + path := filepath.Join(testutil.TempDir(t), "pinniped") + if pinnipedCLIBinaryCache.buf != nil { + t.Log("using previously built pinniped CLI binary") + require.NoError(t, ioutil.WriteFile(path, pinnipedCLIBinaryCache.buf, 0500)) + return path + } + + t.Log("building pinniped CLI binary") + output, err := exec.Command("go", "build", "-o", path, "go.pinniped.dev/cmd/pinniped").CombinedOutput() + require.NoError(t, err, string(output)) + + // Fill our cache so we don't have to do this again. + pinnipedCLIBinaryCache.buf, err = ioutil.ReadFile(path) + require.NoError(t, err, string(output)) + + return path +}