Tweak some stdlib usage so we compile under Go 1.14.

Mainly, avoid using some `testing` helpers that were added in 1.14, as well as a couple of other niceties we can live without.

Signed-off-by: Matt Moyer <moyerm@vmware.com>
This commit is contained in:
Matt Moyer 2020-11-24 13:38:28 -06:00
parent 385d2db445
commit 24c4bc0dd4
No known key found for this signature in database
GPG Key ID: EAE88AD172C5AE2D
7 changed files with 60 additions and 19 deletions

View File

@ -58,16 +58,15 @@ func TestAuthorizeCodeStorage(t *testing.T) {
request := &fosite.AuthorizeRequest{ request := &fosite.AuthorizeRequest{
ResponseTypes: fosite.Arguments{"not-code"}, ResponseTypes: fosite.Arguments{"not-code"},
RedirectURI: &url.URL{ RedirectURI: &url.URL{
Scheme: "", Scheme: "",
Opaque: "weee", Opaque: "weee",
User: &url.Userinfo{}, User: &url.Userinfo{},
Host: "", Host: "",
Path: "/callback", Path: "/callback",
RawPath: "", RawPath: "",
ForceQuery: false, ForceQuery: false,
RawQuery: "", RawQuery: "",
Fragment: "", Fragment: "",
RawFragment: "",
}, },
State: "stated", State: "stated",
HandledResponseTypes: fosite.Arguments{"not-type"}, HandledResponseTypes: fosite.Arguments{"not-type"},

View File

@ -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()
}

View File

@ -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
}

View File

@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"go.pinniped.dev/internal/testutil"
"go.pinniped.dev/pkg/oidcclient" "go.pinniped.dev/pkg/oidcclient"
) )
@ -111,7 +112,7 @@ func TestWriteTo(t *testing.T) {
t.Parallel() t.Parallel()
t.Run("io error", func(t *testing.T) { t.Run("io error", func(t *testing.T) {
t.Parallel() t.Parallel()
tmp := t.TempDir() + "/sessions.yaml" tmp := testutil.TempDir(t) + "/sessions.yaml"
require.NoError(t, os.Mkdir(tmp, 0700)) require.NoError(t, os.Mkdir(tmp, 0700))
err := validSession.writeTo(tmp) err := validSession.writeTo(tmp)
require.EqualError(t, err, "open "+tmp+": is a directory") require.EqualError(t, err, "open "+tmp+": is a directory")
@ -119,7 +120,7 @@ func TestWriteTo(t *testing.T) {
t.Run("success", func(t *testing.T) { t.Run("success", func(t *testing.T) {
t.Parallel() t.Parallel()
require.NoError(t, validSession.writeTo(t.TempDir()+"/sessions.yaml")) require.NoError(t, validSession.writeTo(testutil.TempDir(t)+"/sessions.yaml"))
}) })
} }

View File

@ -15,12 +15,13 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"go.pinniped.dev/internal/testutil"
"go.pinniped.dev/pkg/oidcclient" "go.pinniped.dev/pkg/oidcclient"
) )
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
t.Parallel() t.Parallel()
tmp := t.TempDir() + "/sessions.yaml" tmp := testutil.TempDir(t) + "/sessions.yaml"
c := New(tmp) c := New(tmp)
require.NotNil(t, c) require.NotNil(t, c)
require.Equal(t, tmp, c.path) require.Equal(t, tmp, c.path)
@ -186,7 +187,7 @@ func TestGetToken(t *testing.T) {
tt := tt tt := tt
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel() t.Parallel()
tmp := t.TempDir() + "/sessions.yaml" tmp := testutil.TempDir(t) + "/sessions.yaml"
if tt.makeTestFile != nil { if tt.makeTestFile != nil {
tt.makeTestFile(t, tmp) tt.makeTestFile(t, tmp)
} }
@ -417,7 +418,7 @@ func TestPutToken(t *testing.T) {
tt := tt tt := tt
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel() t.Parallel()
tmp := t.TempDir() + "/sessiondir/sessions.yaml" tmp := testutil.TempDir(t) + "/sessiondir/sessions.yaml"
if tt.makeTestFile != nil { if tt.makeTestFile != nil {
tt.makeTestFile(t, tmp) tt.makeTestFile(t, tmp)
} }

View File

@ -26,6 +26,7 @@ import (
"gopkg.in/square/go-jose.v2" "gopkg.in/square/go-jose.v2"
clientauthenticationv1beta1 "k8s.io/client-go/pkg/apis/clientauthentication/v1beta1" clientauthenticationv1beta1 "k8s.io/client-go/pkg/apis/clientauthentication/v1beta1"
"go.pinniped.dev/internal/testutil"
"go.pinniped.dev/pkg/oidcclient" "go.pinniped.dev/pkg/oidcclient"
"go.pinniped.dev/pkg/oidcclient/filesession" "go.pinniped.dev/pkg/oidcclient/filesession"
"go.pinniped.dev/test/library" "go.pinniped.dev/test/library"
@ -187,7 +188,7 @@ func TestCLILoginOIDC(t *testing.T) {
pinnipedExe := buildPinnipedCLI(t) pinnipedExe := buildPinnipedCLI(t)
// Make a temp directory to hold the session cache for this test. // 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. // Start the CLI running the "alpha login oidc [...]" command with stdout/stderr connected to pipes.
cmd := oidcLoginCommand(ctx, t, pinnipedExe, sessionCachePath) cmd := oidcLoginCommand(ctx, t, pinnipedExe, sessionCachePath)
@ -433,7 +434,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 there is a custom CA bundle, pass it via --ca-bundle and a temporary file.
if env.CLITestUpstream.CABundle != "" { 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)) require.NoError(t, ioutil.WriteFile(path, []byte(env.CLITestUpstream.CABundle), 0600))
cmd.Args = append(cmd.Args, "--ca-bundle", path) cmd.Args = append(cmd.Args, "--ca-bundle", path)
} }

View File

@ -231,7 +231,8 @@ func TestSupervisorTLSTerminationWithDefaultCerts(t *testing.T) {
if len(hostAndPortSegments) > 1 { if len(hostAndPortSegments) > 1 {
port = hostAndPortSegments[1] port = hostAndPortSegments[1]
} }
ips, err := net.DefaultResolver.LookupIP(ctx, "ip4", hostname)
ips, err := net.DefaultResolver.LookupIPAddr(ctx, hostname)
require.NoError(t, err) require.NoError(t, err)
ip := ips[0] ip := ips[0]
ipAsString := ip.String() ipAsString := ip.String()
@ -248,7 +249,7 @@ func TestSupervisorTLSTerminationWithDefaultCerts(t *testing.T) {
requireEndpointHasTLSErrorBecauseCertificatesAreNotReady(t, issuerUsingIPAddress) requireEndpointHasTLSErrorBecauseCertificatesAreNotReady(t, issuerUsingIPAddress)
// Create a Secret at the special name which represents the default TLS cert. // 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. // 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) _ = requireDiscoveryEndpointsAreWorking(t, scheme, ipWithPort, string(defaultCA.Bundle()), issuerUsingIPAddress, nil)