2023-03-16 18:13:49 +00:00
|
|
|
// Copyright 2020-2023 the Pinniped contributors. All Rights Reserved.
|
2020-12-15 18:19:42 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2021-06-22 15:23:19 +00:00
|
|
|
package testlib
|
2020-12-15 18:19:42 +00:00
|
|
|
|
|
|
|
import (
|
2021-03-23 22:17:14 +00:00
|
|
|
"os"
|
2020-12-15 18:19:42 +00:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2021-03-23 17:06:35 +00:00
|
|
|
"time"
|
2020-12-15 18:19:42 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-08-24 21:45:55 +00:00
|
|
|
//nolint:gochecknoglobals
|
2020-12-15 18:19:42 +00:00
|
|
|
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()
|
2021-03-23 22:17:14 +00:00
|
|
|
|
|
|
|
// Allow a pre-built binary passed in via $PINNIPED_TEST_CLI. This is how our tests run in CI for efficiency.
|
|
|
|
if ext, ok := os.LookupEnv("PINNIPED_TEST_CLI"); ok {
|
|
|
|
t.Log("using externally provided pinniped CLI binary")
|
|
|
|
return ext
|
|
|
|
}
|
|
|
|
|
2020-12-15 18:19:42 +00:00
|
|
|
pinnipedCLIBinaryCache.mutex.Lock()
|
|
|
|
defer pinnipedCLIBinaryCache.mutex.Unlock()
|
2023-08-29 22:03:11 +00:00
|
|
|
path := filepath.Join(t.TempDir(), "pinniped")
|
2020-12-15 18:19:42 +00:00
|
|
|
if pinnipedCLIBinaryCache.buf != nil {
|
|
|
|
t.Log("using previously built pinniped CLI binary")
|
2023-03-16 18:13:49 +00:00
|
|
|
//nolint:gosec // this is test code.
|
2022-08-24 21:45:55 +00:00
|
|
|
require.NoError(t, os.WriteFile(path, pinnipedCLIBinaryCache.buf, 0500))
|
2020-12-15 18:19:42 +00:00
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("building pinniped CLI binary")
|
2021-03-23 17:06:35 +00:00
|
|
|
start := time.Now()
|
2020-12-15 18:19:42 +00:00
|
|
|
output, err := exec.Command("go", "build", "-o", path, "go.pinniped.dev/cmd/pinniped").CombinedOutput()
|
|
|
|
require.NoError(t, err, string(output))
|
2021-03-23 17:06:35 +00:00
|
|
|
t.Logf("built CLI binary in %s", time.Since(start).Round(time.Millisecond))
|
2020-12-15 18:19:42 +00:00
|
|
|
|
|
|
|
// Fill our cache so we don't have to do this again.
|
2022-08-24 21:45:55 +00:00
|
|
|
pinnipedCLIBinaryCache.buf, err = os.ReadFile(path)
|
2020-12-15 18:19:42 +00:00
|
|
|
require.NoError(t, err, string(output))
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|