ContainerImage.Pinniped/pkg/config/config_test.go

94 lines
2.3 KiB
Go
Raw Normal View History

/*
Copyright 2020 VMware, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package config
import (
"testing"
"github.com/stretchr/testify/require"
2020-08-20 17:54:15 +00:00
"github.com/suzerain-io/pinniped/pkg/config/api"
)
func TestFromPath(t *testing.T) {
tests := []struct {
name string
path string
wantConfig *api.Config
wantError string
}{
{
name: "Happy",
path: "testdata/happy.yaml",
wantConfig: &api.Config{
2020-08-20 17:54:15 +00:00
DiscoveryInfo: api.DiscoveryInfoSpec{
URL: stringPtr("https://some.discovery/url"),
},
WebhookConfig: api.WebhookConfigSpec{
URL: "https://tuna.com/fish?marlin",
CABundle: []byte("-----BEGIN CERTIFICATE-----..."),
},
APIConfig: api.APIConfigSpec{
ServingCertificateConfig: api.ServingCertificateConfigSpec{
DurationSeconds: int64Ptr(3600),
RenewBeforeSeconds: int64Ptr(2400),
},
},
},
},
{
name: "Default",
path: "testdata/default.yaml",
wantConfig: &api.Config{
2020-08-20 17:54:15 +00:00
DiscoveryInfo: api.DiscoveryInfoSpec{
URL: nil,
},
WebhookConfig: api.WebhookConfigSpec{
URL: "https://tuna.com/fish?marlin",
CABundle: []byte("-----BEGIN CERTIFICATE-----..."),
},
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
},
},
},
},
{
name: "InvalidDurationRenewBefore",
path: "testdata/invalid-duration-renew-before.yaml",
wantError: "validate api: durationSeconds cannot be smaller than renewBeforeSeconds",
},
{
name: "NegativeRenewBefore",
path: "testdata/negative-renew-before.yaml",
wantError: "validate api: renewBefore must be positive",
},
{
name: "ZeroRenewBefore",
path: "testdata/zero-renew-before.yaml",
wantError: "validate api: renewBefore must be positive",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
config, err := FromPath(test.path)
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
}