80a520390b
New resource naming conventions: - Do not repeat the Kind in the name, e.g. do not call it foo-cluster-role-binding, just call it foo - Names will generally start with a prefix to identify our component, so when a user lists all objects of that kind, they can tell to which component it is related, e.g. `kubectl get configmaps` would list one named "pinniped-config" - It should be possible for an operator to make the word "pinniped" mostly disappear if they choose, by specifying the app_name in values.yaml, to the extent that is practical (but not from APIService names because those are hardcoded in golang) - Each role/clusterrole and its corresponding binding have the same name - Pinniped resource names that must be known by the server golang code are passed to the code at run time via ConfigMap, rather than hardcoded in the golang code. This also allows them to be prepended with the app_name from values.yaml while creating the ConfigMap. - Since the CLI `get-kubeconfig` command cannot guess the name of the CredentialIssuerConfig resource in advance anymore, it lists all CredentialIssuerConfig in the app's namespace and returns an error if there is not exactly one found, and then uses that one regardless of its name
194 lines
5.0 KiB
Go
194 lines
5.0 KiB
Go
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package config
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/vmware-tanzu/pinniped/internal/here"
|
|
"github.com/vmware-tanzu/pinniped/pkg/config/api"
|
|
)
|
|
|
|
func TestFromPath(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
yaml string
|
|
wantConfig *api.Config
|
|
wantError string
|
|
}{
|
|
{
|
|
name: "Happy",
|
|
yaml: here.Doc(`
|
|
---
|
|
discovery:
|
|
url: https://some.discovery/url
|
|
api:
|
|
servingCertificate:
|
|
durationSeconds: 3600
|
|
renewBeforeSeconds: 2400
|
|
names:
|
|
servingCertificateSecret: pinniped-api-tls-serving-certificate
|
|
credentialIssuerConfig: pinniped-config
|
|
apiService: pinniped-api
|
|
`),
|
|
wantConfig: &api.Config{
|
|
DiscoveryInfo: api.DiscoveryInfoSpec{
|
|
URL: stringPtr("https://some.discovery/url"),
|
|
},
|
|
APIConfig: api.APIConfigSpec{
|
|
ServingCertificateConfig: api.ServingCertificateConfigSpec{
|
|
DurationSeconds: int64Ptr(3600),
|
|
RenewBeforeSeconds: int64Ptr(2400),
|
|
},
|
|
},
|
|
NamesConfig: api.NamesConfigSpec{
|
|
ServingCertificateSecret: "pinniped-api-tls-serving-certificate",
|
|
CredentialIssuerConfig: "pinniped-config",
|
|
APIService: "pinniped-api",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "When only the required fields are present, causes other fields to be defaulted",
|
|
yaml: here.Doc(`
|
|
---
|
|
names:
|
|
servingCertificateSecret: pinniped-api-tls-serving-certificate
|
|
credentialIssuerConfig: pinniped-config
|
|
apiService: pinniped-api
|
|
`),
|
|
wantConfig: &api.Config{
|
|
DiscoveryInfo: api.DiscoveryInfoSpec{
|
|
URL: nil,
|
|
},
|
|
APIConfig: api.APIConfigSpec{
|
|
ServingCertificateConfig: api.ServingCertificateConfigSpec{
|
|
DurationSeconds: int64Ptr(60 * 60 * 24 * 365), // about a year
|
|
RenewBeforeSeconds: int64Ptr(60 * 60 * 24 * 30 * 9), // about 9 months
|
|
},
|
|
},
|
|
NamesConfig: api.NamesConfigSpec{
|
|
ServingCertificateSecret: "pinniped-api-tls-serving-certificate",
|
|
CredentialIssuerConfig: "pinniped-config",
|
|
APIService: "pinniped-api",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Empty",
|
|
yaml: here.Doc(``),
|
|
wantError: "validate names: missing required names: servingCertificateSecret, credentialIssuerConfig, apiService",
|
|
},
|
|
{
|
|
name: "Missing apiService name",
|
|
yaml: here.Doc(`
|
|
---
|
|
names:
|
|
servingCertificateSecret: pinniped-api-tls-serving-certificate
|
|
credentialIssuerConfig: pinniped-config
|
|
`),
|
|
wantError: "validate names: missing required names: apiService",
|
|
},
|
|
{
|
|
name: "Missing credentialIssuerConfig name",
|
|
yaml: here.Doc(`
|
|
---
|
|
names:
|
|
servingCertificateSecret: pinniped-api-tls-serving-certificate
|
|
apiService: pinniped-api
|
|
`),
|
|
wantError: "validate names: missing required names: credentialIssuerConfig",
|
|
},
|
|
{
|
|
name: "Missing servingCertificateSecret name",
|
|
yaml: here.Doc(`
|
|
---
|
|
names:
|
|
credentialIssuerConfig: pinniped-config
|
|
apiService: pinniped-api
|
|
`),
|
|
wantError: "validate names: missing required names: servingCertificateSecret",
|
|
},
|
|
{
|
|
name: "InvalidDurationRenewBefore",
|
|
yaml: here.Doc(`
|
|
---
|
|
api:
|
|
servingCertificate:
|
|
durationSeconds: 2400
|
|
renewBeforeSeconds: 3600
|
|
names:
|
|
servingCertificateSecret: pinniped-api-tls-serving-certificate
|
|
credentialIssuerConfig: pinniped-config
|
|
apiService: pinniped-api
|
|
`),
|
|
wantError: "validate api: durationSeconds cannot be smaller than renewBeforeSeconds",
|
|
},
|
|
{
|
|
name: "NegativeRenewBefore",
|
|
yaml: here.Doc(`
|
|
---
|
|
api:
|
|
servingCertificate:
|
|
durationSeconds: 2400
|
|
renewBeforeSeconds: -10
|
|
names:
|
|
servingCertificateSecret: pinniped-api-tls-serving-certificate
|
|
credentialIssuerConfig: pinniped-config
|
|
apiService: pinniped-api
|
|
`),
|
|
wantError: "validate api: renewBefore must be positive",
|
|
},
|
|
{
|
|
name: "ZeroRenewBefore",
|
|
yaml: here.Doc(`
|
|
---
|
|
api:
|
|
servingCertificate:
|
|
durationSeconds: 2400
|
|
renewBeforeSeconds: -10
|
|
names:
|
|
servingCertificateSecret: pinniped-api-tls-serving-certificate
|
|
credentialIssuerConfig: pinniped-config
|
|
apiService: pinniped-api
|
|
`),
|
|
wantError: "validate api: renewBefore must be positive",
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
test := test
|
|
t.Run(test.name, func(t *testing.T) {
|
|
// Write yaml to temp file
|
|
f, err := ioutil.TempFile("", "pinniped-test-config-yaml-*")
|
|
require.NoError(t, err)
|
|
defer func() {
|
|
err := os.Remove(f.Name())
|
|
require.NoError(t, err)
|
|
}()
|
|
_, err = f.WriteString(test.yaml)
|
|
require.NoError(t, err)
|
|
err = f.Close()
|
|
require.NoError(t, err)
|
|
|
|
// Test FromPath()
|
|
config, err := FromPath(f.Name())
|
|
|
|
if test.wantError != "" {
|
|
require.EqualError(t, err, test.wantError)
|
|
} else {
|
|
require.NoError(t, err)
|
|
require.Equal(t, test.wantConfig, config)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func stringPtr(s string) *string {
|
|
return &s
|
|
}
|