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-10-13 15:41:53 +00:00
|
|
|
"strconv"
|
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"
|
|
|
|
)
|
|
|
|
|
2020-10-13 14:13:40 +00:00
|
|
|
type Capability string
|
2020-09-24 22:51:43 +00:00
|
|
|
|
|
|
|
const (
|
2020-10-13 21:09:13 +00:00
|
|
|
ClusterSigningKeyIsAvailable Capability = "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-27 21:57:25 +00:00
|
|
|
ConciergeNamespace string `json:"conciergeNamespace"`
|
|
|
|
SupervisorNamespace string `json:"supervisorNamespace"`
|
|
|
|
ConciergeAppName string `json:"conciergeAppName"`
|
|
|
|
SupervisorAppName string `json:"supervisorAppName"`
|
|
|
|
SupervisorCustomLabels map[string]string `json:"supervisorCustomLabels"`
|
|
|
|
ConciergeCustomLabels map[string]string `json:"conciergeCustomLabels"`
|
|
|
|
Capabilities map[Capability]bool `json:"capabilities"`
|
|
|
|
TestWebhook idpv1alpha1.WebhookIdentityProviderSpec `json:"testWebhook"`
|
|
|
|
SupervisorHTTPAddress string `json:"supervisorHttpAddress"`
|
|
|
|
SupervisorHTTPSAddress string `json:"supervisorHttpsAddress"`
|
|
|
|
SupervisorHTTPSIngressAddress string `json:"supervisorHttpsIngressAddress"`
|
|
|
|
SupervisorHTTPSIngressCABundle string `json:"supervisorHttpsIngressCABundle"`
|
2020-10-09 17:11:47 +00:00
|
|
|
|
|
|
|
TestUser struct {
|
2020-09-24 22:51:43 +00:00
|
|
|
Token string `json:"token"`
|
|
|
|
ExpectedUsername string `json:"expectedUsername"`
|
|
|
|
ExpectedGroups []string `json:"expectedGroups"`
|
|
|
|
} `json:"testUser"`
|
2020-10-13 15:41:53 +00:00
|
|
|
|
|
|
|
OIDCUpstream struct {
|
|
|
|
Issuer string `json:"issuer"`
|
|
|
|
ClientID string `json:"clientID"`
|
|
|
|
LocalhostPort int `json:"localhostPort"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
} `json:"oidcUpstream"`
|
2020-09-24 22:51:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 15:41:53 +00:00
|
|
|
// IntegrationEnv gets the integration test environment from OS environment variables. This
|
2020-09-24 22:51:43 +00:00
|
|
|
// 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)
|
|
|
|
|
2020-10-09 17:11:47 +00:00
|
|
|
capabilitiesDescriptionYAML := os.Getenv("PINNIPED_TEST_CLUSTER_CAPABILITY_YAML")
|
|
|
|
capabilitiesDescriptionFile := os.Getenv("PINNIPED_TEST_CLUSTER_CAPABILITY_FILE")
|
2020-09-24 22:51:43 +00:00
|
|
|
require.NotEmptyf(t,
|
|
|
|
capabilitiesDescriptionYAML+capabilitiesDescriptionFile,
|
2020-10-09 17:11:47 +00:00
|
|
|
"must specify either PINNIPED_TEST_CLUSTER_CAPABILITY_YAML or PINNIPED_TEST_CLUSTER_CAPABILITY_FILE env var for integration tests",
|
2020-09-24 22:51:43 +00:00
|
|
|
)
|
|
|
|
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")
|
|
|
|
|
2020-10-27 21:57:25 +00:00
|
|
|
loadEnvVars(t, &result)
|
2020-09-24 22:51:43 +00:00
|
|
|
|
2020-10-27 21:57:25 +00:00
|
|
|
result.t = t
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
func needEnv(t *testing.T, key string) string {
|
|
|
|
t.Helper()
|
|
|
|
value := os.Getenv(key)
|
|
|
|
require.NotEmptyf(t, value, "must specify %s env var for integration tests", key)
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadEnvVars(t *testing.T, result *TestEnv) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
result.ConciergeNamespace = needEnv(t, "PINNIPED_TEST_CONCIERGE_NAMESPACE")
|
|
|
|
result.ConciergeAppName = needEnv(t, "PINNIPED_TEST_CONCIERGE_APP_NAME")
|
|
|
|
result.TestUser.ExpectedUsername = needEnv(t, "PINNIPED_TEST_USER_USERNAME")
|
|
|
|
result.TestUser.ExpectedGroups = strings.Split(strings.ReplaceAll(needEnv(t, "PINNIPED_TEST_USER_GROUPS"), " ", ""), ",")
|
|
|
|
result.TestUser.Token = needEnv(t, "PINNIPED_TEST_USER_TOKEN")
|
|
|
|
result.TestWebhook.Endpoint = needEnv(t, "PINNIPED_TEST_WEBHOOK_ENDPOINT")
|
|
|
|
result.SupervisorNamespace = needEnv(t, "PINNIPED_TEST_SUPERVISOR_NAMESPACE")
|
|
|
|
result.SupervisorAppName = needEnv(t, "PINNIPED_TEST_SUPERVISOR_APP_NAME")
|
|
|
|
result.TestWebhook.TLS = &idpv1alpha1.TLSSpec{CertificateAuthorityData: needEnv(t, "PINNIPED_TEST_WEBHOOK_CA_BUNDLE")}
|
2020-10-13 15:41:53 +00:00
|
|
|
|
2020-10-20 22:57:10 +00:00
|
|
|
result.SupervisorHTTPAddress = os.Getenv("PINNIPED_TEST_SUPERVISOR_HTTP_ADDRESS")
|
2020-10-27 21:57:25 +00:00
|
|
|
result.SupervisorHTTPSIngressAddress = os.Getenv("PINNIPED_TEST_SUPERVISOR_HTTPS_INGRESS_ADDRESS")
|
|
|
|
result.SupervisorHTTPSIngressCABundle = os.Getenv("PINNIPED_TEST_SUPERVISOR_HTTPS_INGRESS_CA_BUNDLE") // optional
|
2020-10-20 22:57:10 +00:00
|
|
|
require.NotEmptyf(t,
|
2020-10-27 21:57:25 +00:00
|
|
|
result.SupervisorHTTPAddress+result.SupervisorHTTPSIngressAddress,
|
|
|
|
"must specify either PINNIPED_TEST_SUPERVISOR_HTTP_ADDRESS or PINNIPED_TEST_SUPERVISOR_HTTPS_INGRESS_ADDRESS env var (or both) for integration tests",
|
|
|
|
)
|
|
|
|
result.SupervisorHTTPSAddress = needEnv(t, "PINNIPED_TEST_SUPERVISOR_HTTPS_ADDRESS")
|
|
|
|
require.NotRegexp(t, "^[0-9]", result.SupervisorHTTPSAddress,
|
|
|
|
"PINNIPED_TEST_SUPERVISOR_HTTPS_ADDRESS must be a hostname with an optional port and cannot be an IP address",
|
2020-10-20 22:57:10 +00:00
|
|
|
)
|
|
|
|
|
2020-10-27 21:57:25 +00:00
|
|
|
conciergeCustomLabelsYAML := needEnv(t, "PINNIPED_TEST_CONCIERGE_CUSTOM_LABELS")
|
2020-10-15 17:14:23 +00:00
|
|
|
var conciergeCustomLabels map[string]string
|
2020-10-27 21:57:25 +00:00
|
|
|
err := yaml.Unmarshal([]byte(conciergeCustomLabelsYAML), &conciergeCustomLabels)
|
2020-10-15 17:14:23 +00:00
|
|
|
require.NoErrorf(t, err, "PINNIPED_TEST_CONCIERGE_CUSTOM_LABELS must be a YAML map of string to string")
|
|
|
|
result.ConciergeCustomLabels = conciergeCustomLabels
|
|
|
|
require.NotEmpty(t, result.ConciergeCustomLabels, "PINNIPED_TEST_CONCIERGE_CUSTOM_LABELS cannot be empty")
|
2020-10-27 21:57:25 +00:00
|
|
|
supervisorCustomLabelsYAML := needEnv(t, "PINNIPED_TEST_SUPERVISOR_CUSTOM_LABELS")
|
2020-10-15 17:14:23 +00:00
|
|
|
var supervisorCustomLabels map[string]string
|
|
|
|
err = yaml.Unmarshal([]byte(supervisorCustomLabelsYAML), &supervisorCustomLabels)
|
|
|
|
require.NoErrorf(t, err, "PINNIPED_TEST_SUPERVISOR_CUSTOM_LABELS must be a YAML map of string to string")
|
|
|
|
result.SupervisorCustomLabels = supervisorCustomLabels
|
|
|
|
require.NotEmpty(t, result.SupervisorCustomLabels, "PINNIPED_TEST_SUPERVISOR_CUSTOM_LABELS cannot be empty")
|
|
|
|
|
2020-10-27 21:57:25 +00:00
|
|
|
result.OIDCUpstream.Issuer = needEnv(t, "PINNIPED_TEST_CLI_OIDC_ISSUER")
|
|
|
|
result.OIDCUpstream.ClientID = needEnv(t, "PINNIPED_TEST_CLI_OIDC_CLIENT_ID")
|
|
|
|
result.OIDCUpstream.LocalhostPort, _ = strconv.Atoi(needEnv(t, "PINNIPED_TEST_CLI_OIDC_LOCALHOST_PORT"))
|
|
|
|
result.OIDCUpstream.Username = needEnv(t, "PINNIPED_TEST_CLI_OIDC_USERNAME")
|
|
|
|
result.OIDCUpstream.Password = needEnv(t, "PINNIPED_TEST_CLI_OIDC_PASSWORD")
|
2020-08-03 14:17:11 +00:00
|
|
|
}
|
2020-09-25 14:37:17 +00:00
|
|
|
|
2020-10-13 14:13:40 +00:00
|
|
|
func (e *TestEnv) HasCapability(cap Capability) bool {
|
2020-09-25 14:37:17 +00:00
|
|
|
e.t.Helper()
|
|
|
|
isCapable, capabilityWasDescribed := e.Capabilities[cap]
|
2020-10-13 14:13:40 +00:00
|
|
|
require.Truef(e.t, capabilityWasDescribed, "the %q capability of the test environment was not described", cap)
|
2020-09-25 14:37:17 +00:00
|
|
|
return isCapable
|
|
|
|
}
|
|
|
|
|
2020-10-13 14:13:40 +00:00
|
|
|
func (e *TestEnv) WithCapability(cap Capability) *TestEnv {
|
2020-09-25 14:37:17 +00:00
|
|
|
e.t.Helper()
|
|
|
|
if !e.HasCapability(cap) {
|
2020-10-13 14:13:40 +00:00
|
|
|
e.t.Skipf("skipping integration test because test environment lacks the %q capability", cap)
|
2020-09-25 14:37:17 +00:00
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2020-10-13 14:13:40 +00:00
|
|
|
func (e *TestEnv) WithoutCapability(cap Capability) *TestEnv {
|
2020-09-25 14:37:17 +00:00
|
|
|
e.t.Helper()
|
|
|
|
if e.HasCapability(cap) {
|
2020-10-13 14:13:40 +00:00
|
|
|
e.t.Skipf("skipping integration test because test environment has the %q capability", cap)
|
2020-09-25 14:37:17 +00:00
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|