2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-08-03 14:17:11 +00:00
|
|
|
|
|
|
|
package library
|
|
|
|
|
|
|
|
import (
|
2020-09-24 22:51:43 +00:00
|
|
|
"io/ioutil"
|
2020-08-03 14:17:11 +00:00
|
|
|
"os"
|
2020-09-24 22:51:43 +00:00
|
|
|
"strings"
|
2020-08-03 14:17:11 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-24 22:51:43 +00:00
|
|
|
"sigs.k8s.io/yaml"
|
|
|
|
|
|
|
|
idpv1alpha1 "go.pinniped.dev/generated/1.19/apis/idp/v1alpha1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestClusterCapability string
|
|
|
|
|
|
|
|
const (
|
|
|
|
ClusterSigningKeyIsAvailable = TestClusterCapability("clusterSigningKeyIsAvailable")
|
2020-08-03 14:17:11 +00:00
|
|
|
)
|
|
|
|
|
2020-09-24 22:51:43 +00:00
|
|
|
// TestEnv captures all the external parameters consumed by our integration tests.
|
|
|
|
type TestEnv struct {
|
|
|
|
t *testing.T
|
|
|
|
|
2020-10-07 00:53:29 +00:00
|
|
|
Namespace string `json:"namespace"`
|
|
|
|
SupervisorNamespace string `json:"supervisorNamespace"`
|
|
|
|
AppName string `json:"appName"`
|
|
|
|
SupervisorAppName string `json:"supervisorAppName"`
|
|
|
|
Capabilities map[TestClusterCapability]bool `json:"capabilities"`
|
|
|
|
TestWebhook idpv1alpha1.WebhookIdentityProviderSpec `json:"testWebhook"`
|
|
|
|
SupervisorAddress string `json:"supervisorAddress"`
|
|
|
|
TestUser struct {
|
2020-09-24 22:51:43 +00:00
|
|
|
Token string `json:"token"`
|
|
|
|
ExpectedUsername string `json:"expectedUsername"`
|
|
|
|
ExpectedGroups []string `json:"expectedGroups"`
|
|
|
|
} `json:"testUser"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntegrationEnv gets the integration test environment from a Kubernetes Secret in the test cluster. This
|
|
|
|
// method also implies SkipUnlessIntegration().
|
|
|
|
func IntegrationEnv(t *testing.T) *TestEnv {
|
2020-08-03 14:17:11 +00:00
|
|
|
t.Helper()
|
2020-09-24 22:51:43 +00:00
|
|
|
SkipUnlessIntegration(t)
|
|
|
|
|
|
|
|
capabilitiesDescriptionYAML := os.Getenv("PINNIPED_CLUSTER_CAPABILITY_YAML")
|
|
|
|
capabilitiesDescriptionFile := os.Getenv("PINNIPED_CLUSTER_CAPABILITY_FILE")
|
|
|
|
require.NotEmptyf(t,
|
|
|
|
capabilitiesDescriptionYAML+capabilitiesDescriptionFile,
|
|
|
|
"must specify either PINNIPED_CLUSTER_CAPABILITY_YAML or PINNIPED_CLUSTER_CAPABILITY_FILE env var for integration tests",
|
|
|
|
)
|
|
|
|
if capabilitiesDescriptionYAML == "" {
|
|
|
|
bytes, err := ioutil.ReadFile(capabilitiesDescriptionFile)
|
|
|
|
capabilitiesDescriptionYAML = string(bytes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result TestEnv
|
|
|
|
err := yaml.Unmarshal([]byte(capabilitiesDescriptionYAML), &result)
|
|
|
|
require.NoErrorf(t, err, "capabilities specification was invalid YAML")
|
|
|
|
|
|
|
|
needEnv := func(key string) string {
|
|
|
|
t.Helper()
|
|
|
|
value := os.Getenv(key)
|
|
|
|
require.NotEmptyf(t, value, "must specify %s env var for integration tests", key)
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Namespace = needEnv("PINNIPED_NAMESPACE")
|
|
|
|
result.AppName = needEnv("PINNIPED_APP_NAME")
|
|
|
|
result.TestUser.ExpectedUsername = needEnv("PINNIPED_TEST_USER_USERNAME")
|
|
|
|
result.TestUser.ExpectedGroups = strings.Split(strings.ReplaceAll(needEnv("PINNIPED_TEST_USER_GROUPS"), " ", ""), ",")
|
|
|
|
result.TestUser.Token = needEnv("PINNIPED_TEST_USER_TOKEN")
|
|
|
|
result.TestWebhook.Endpoint = needEnv("PINNIPED_TEST_WEBHOOK_ENDPOINT")
|
2020-10-07 00:53:29 +00:00
|
|
|
result.SupervisorNamespace = needEnv("PINNIPED_SUPERVISOR_NAMESPACE")
|
|
|
|
result.SupervisorAppName = needEnv("PINNIPED_SUPERVISOR_APP_NAME")
|
|
|
|
result.SupervisorAddress = needEnv("PINNIPED_TEST_SUPERVISOR_ADDRESS")
|
2020-09-24 22:51:43 +00:00
|
|
|
result.TestWebhook.TLS = &idpv1alpha1.TLSSpec{CertificateAuthorityData: needEnv("PINNIPED_TEST_WEBHOOK_CA_BUNDLE")}
|
|
|
|
result.t = t
|
|
|
|
return &result
|
2020-08-03 14:17:11 +00:00
|
|
|
}
|
2020-09-25 14:37:17 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|