Remove support for loading test context from a Secret.

Signed-off-by: Matt Moyer <moyerm@vmware.com>
This commit is contained in:
Matt Moyer 2020-09-25 09:37:17 -05:00
parent 434e3fe435
commit 70480260dd
No known key found for this signature in database
GPG Key ID: EAE88AD172C5AE2D
1 changed files with 23 additions and 60 deletions

View File

@ -4,16 +4,12 @@
package library package library
import ( import (
"context"
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
"testing" "testing"
"time"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
idpv1alpha1 "go.pinniped.dev/generated/1.19/apis/idp/v1alpha1" idpv1alpha1 "go.pinniped.dev/generated/1.19/apis/idp/v1alpha1"
@ -46,62 +42,6 @@ func IntegrationEnv(t *testing.T) *TestEnv {
t.Helper() t.Helper()
SkipUnlessIntegration(t) SkipUnlessIntegration(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
secretNamespace := getDefaultedEnv("PINNIPED_NAMESPACE", "integration")
secretName := getDefaultedEnv("PINNIPED_ENVIRONMENT", "pinniped-test-env")
secret, err := NewClientset(t).CoreV1().Secrets(secretNamespace).Get(ctx, secretName, metav1.GetOptions{})
if k8serrors.IsNotFound(err) {
return envFromOSEnviron(t)
}
require.NoErrorf(t, err, "could not fetch test environment from %s/%s", secretNamespace, secretName)
yamlEnv, ok := secret.Data["env.yaml"]
require.True(t, ok, "test environment secret %s/%s did not contain expected 'env.yaml' key", secretNamespace, secretName)
var result TestEnv
err = yaml.Unmarshal(yamlEnv, &result)
require.NoErrorf(t, err, "test environment secret %s/%s contained invalid YAML", secretNamespace, secretName)
result.t = t
return &result
}
func (e *TestEnv) HasCapability(cap TestClusterCapability) bool {
e.t.Helper()
isCapable, capabilityWasDescribed := e.Capabilities[cap]
require.True(e.t, capabilityWasDescribed, `the cluster's "%s" capability was not described`, cap)
return isCapable
}
func (e *TestEnv) WithCapability(cap TestClusterCapability) *TestEnv {
e.t.Helper()
if !e.HasCapability(cap) {
e.t.Skipf(`skipping integration test because cluster lacks the "%s" capability`, cap)
}
return e
}
func (e *TestEnv) WithoutCapability(cap TestClusterCapability) *TestEnv {
e.t.Helper()
if e.HasCapability(cap) {
e.t.Skipf(`skipping integration test because cluster has the "%s" capability`, cap)
}
return e
}
func getDefaultedEnv(name, defaultValue string) string {
if val := os.Getenv(name); val != "" {
return val
}
return defaultValue
}
// envFromOSEnviron is a (temporary?) helper to pull information from os.Environ instead of the test cluster.
func envFromOSEnviron(t *testing.T) *TestEnv {
t.Helper()
capabilitiesDescriptionYAML := os.Getenv("PINNIPED_CLUSTER_CAPABILITY_YAML") capabilitiesDescriptionYAML := os.Getenv("PINNIPED_CLUSTER_CAPABILITY_YAML")
capabilitiesDescriptionFile := os.Getenv("PINNIPED_CLUSTER_CAPABILITY_FILE") capabilitiesDescriptionFile := os.Getenv("PINNIPED_CLUSTER_CAPABILITY_FILE")
require.NotEmptyf(t, require.NotEmptyf(t,
@ -135,3 +75,26 @@ func envFromOSEnviron(t *testing.T) *TestEnv {
result.t = t result.t = t
return &result return &result
} }
func (e *TestEnv) HasCapability(cap TestClusterCapability) bool {
e.t.Helper()
isCapable, capabilityWasDescribed := e.Capabilities[cap]
require.True(e.t, capabilityWasDescribed, `the cluster's "%s" capability was not described`, cap)
return isCapable
}
func (e *TestEnv) WithCapability(cap TestClusterCapability) *TestEnv {
e.t.Helper()
if !e.HasCapability(cap) {
e.t.Skipf(`skipping integration test because cluster lacks the "%s" capability`, cap)
}
return e
}
func (e *TestEnv) WithoutCapability(cap TestClusterCapability) *TestEnv {
e.t.Helper()
if e.HasCapability(cap) {
e.t.Skipf(`skipping integration test because cluster has the "%s" capability`, cap)
}
return e
}