diff --git a/internal/testutil/tempdir.go b/internal/testutil/tempdir.go new file mode 100644 index 00000000..4fca27a3 --- /dev/null +++ b/internal/testutil/tempdir.go @@ -0,0 +1,14 @@ +// Copyright 2020 the Pinniped contributors. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// +build !go1.14 + +package testutil + +import ( + "testing" +) + +func TempDir(t *testing.T) string { + return t.TempDir() +} diff --git a/internal/testutil/tempdir_go1.14.go b/internal/testutil/tempdir_go1.14.go new file mode 100644 index 00000000..c51757b2 --- /dev/null +++ b/internal/testutil/tempdir_go1.14.go @@ -0,0 +1,24 @@ +// Copyright 2020 the Pinniped contributors. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// +build go1.14 + +package testutil + +import ( + "io/ioutil" + "os" + "testing" + + "github.com/stretchr/testify/require" +) + +func TempDir(t *testing.T) string { + t.Helper() + dir, err := ioutil.TempDir("", "test-*") + require.NoError(t, err) + t.Cleanup(func() { + require.NoError(t, os.RemoveAll(dir)) + }) + return dir +} diff --git a/pkg/oidcclient/filesession/cachefile_test.go b/pkg/oidcclient/filesession/cachefile_test.go index a881d7d4..b1e1c984 100644 --- a/pkg/oidcclient/filesession/cachefile_test.go +++ b/pkg/oidcclient/filesession/cachefile_test.go @@ -11,6 +11,7 @@ import ( "github.com/stretchr/testify/require" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "go.pinniped.dev/internal/testutil" "go.pinniped.dev/pkg/oidcclient" "go.pinniped.dev/pkg/oidcclient/oidctypes" ) @@ -112,7 +113,7 @@ func TestWriteTo(t *testing.T) { t.Parallel() t.Run("io error", func(t *testing.T) { t.Parallel() - tmp := t.TempDir() + "/sessions.yaml" + tmp := testutil.TempDir(t) + "/sessions.yaml" require.NoError(t, os.Mkdir(tmp, 0700)) err := validSession.writeTo(tmp) require.EqualError(t, err, "open "+tmp+": is a directory") @@ -120,7 +121,7 @@ func TestWriteTo(t *testing.T) { t.Run("success", func(t *testing.T) { t.Parallel() - require.NoError(t, validSession.writeTo(t.TempDir()+"/sessions.yaml")) + require.NoError(t, validSession.writeTo(testutil.TempDir(t)+"/sessions.yaml")) }) } diff --git a/pkg/oidcclient/filesession/filesession_test.go b/pkg/oidcclient/filesession/filesession_test.go index d172af80..2ba7c55b 100644 --- a/pkg/oidcclient/filesession/filesession_test.go +++ b/pkg/oidcclient/filesession/filesession_test.go @@ -15,13 +15,14 @@ import ( "github.com/stretchr/testify/require" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "go.pinniped.dev/internal/testutil" "go.pinniped.dev/pkg/oidcclient" "go.pinniped.dev/pkg/oidcclient/oidctypes" ) func TestNew(t *testing.T) { t.Parallel() - tmp := t.TempDir() + "/sessions.yaml" + tmp := testutil.TempDir(t) + "/sessions.yaml" c := New(tmp) require.NotNil(t, c) require.Equal(t, tmp, c.path) @@ -187,7 +188,7 @@ func TestGetToken(t *testing.T) { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() - tmp := t.TempDir() + "/sessions.yaml" + tmp := testutil.TempDir(t) + "/sessions.yaml" if tt.makeTestFile != nil { tt.makeTestFile(t, tmp) } @@ -418,7 +419,7 @@ func TestPutToken(t *testing.T) { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() - tmp := t.TempDir() + "/sessiondir/sessions.yaml" + tmp := testutil.TempDir(t) + "/sessiondir/sessions.yaml" if tt.makeTestFile != nil { tt.makeTestFile(t, tmp) } diff --git a/test/integration/cli_test.go b/test/integration/cli_test.go index bb6663e5..c4c051eb 100644 --- a/test/integration/cli_test.go +++ b/test/integration/cli_test.go @@ -25,6 +25,7 @@ import ( "gopkg.in/square/go-jose.v2" clientauthenticationv1beta1 "k8s.io/client-go/pkg/apis/clientauthentication/v1beta1" + "go.pinniped.dev/internal/testutil" "go.pinniped.dev/pkg/oidcclient" "go.pinniped.dev/pkg/oidcclient/filesession" "go.pinniped.dev/test/library" @@ -121,7 +122,7 @@ func TestCLILoginOIDC(t *testing.T) { pinnipedExe := buildPinnipedCLI(t) // Make a temp directory to hold the session cache for this test. - sessionCachePath := t.TempDir() + "/sessions.yaml" + sessionCachePath := testutil.TempDir(t) + "/sessions.yaml" // Start the CLI running the "alpha login oidc [...]" command with stdout/stderr connected to pipes. cmd := oidcLoginCommand(ctx, t, pinnipedExe, sessionCachePath) @@ -319,7 +320,7 @@ func oidcLoginCommand(ctx context.Context, t *testing.T, pinnipedExe string, ses // If there is a custom CA bundle, pass it via --ca-bundle and a temporary file. if env.CLITestUpstream.CABundle != "" { - path := filepath.Join(t.TempDir(), "test-ca.pem") + path := filepath.Join(testutil.TempDir(t), "test-ca.pem") require.NoError(t, ioutil.WriteFile(path, []byte(env.CLITestUpstream.CABundle), 0600)) cmd.Args = append(cmd.Args, "--ca-bundle", path) } diff --git a/test/integration/supervisor_discovery_test.go b/test/integration/supervisor_discovery_test.go index b029cc17..24d03c81 100644 --- a/test/integration/supervisor_discovery_test.go +++ b/test/integration/supervisor_discovery_test.go @@ -231,7 +231,8 @@ func TestSupervisorTLSTerminationWithDefaultCerts(t *testing.T) { if len(hostAndPortSegments) > 1 { port = hostAndPortSegments[1] } - ips, err := net.DefaultResolver.LookupIP(ctx, "ip4", hostname) + + ips, err := net.DefaultResolver.LookupIPAddr(ctx, hostname) require.NoError(t, err) ip := ips[0] ipAsString := ip.String() @@ -248,7 +249,7 @@ func TestSupervisorTLSTerminationWithDefaultCerts(t *testing.T) { requireEndpointHasTLSErrorBecauseCertificatesAreNotReady(t, issuerUsingIPAddress) // Create a Secret at the special name which represents the default TLS cert. - defaultCA := createTLSCertificateSecret(ctx, t, ns, "cert-hostname-doesnt-matter", []net.IP{ip}, defaultTLSCertSecretName(env), kubeClient) + defaultCA := createTLSCertificateSecret(ctx, t, ns, "cert-hostname-doesnt-matter", []net.IP{ip.IP}, defaultTLSCertSecretName(env), kubeClient) // Now that the Secret exists, we should be able to access the endpoints by IP address using the CA. _ = requireDiscoveryEndpointsAreWorking(t, scheme, ipWithPort, string(defaultCA.Bundle()), issuerUsingIPAddress, nil)