Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand mgmt configmap tests #6987

Merged
merged 11 commits into from
Dec 16, 2024
Merged
14 changes: 13 additions & 1 deletion internal/configs/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/nginxinc/kubernetes-ingress/internal/configs/version1"
nl "github.com/nginxinc/kubernetes-ingress/internal/logger"
"github.com/nginxinc/kubernetes-ingress/internal/validation"
)

const (
Expand Down Expand Up @@ -714,9 +715,20 @@ func ParseMGMTConfigMap(ctx context.Context, cfgm *v1.ConfigMap, eventLog record
mgmtCfgParams.EnforceInitialReport = BoolToPointerBool(enforceInitialReport)
}
}

if endpoint, exists := cfgm.Data["usage-report-endpoint"]; exists {
mgmtCfgParams.Endpoint = strings.TrimSpace(endpoint)
endpoint := strings.TrimSpace(endpoint)
err := validation.ValidateHost(endpoint)
if err != nil {
errorText := fmt.Sprintf("Configmap %s/%s: Invalid value for the usage-report-endpoint key: got %q: %v. Using default endpoint.", cfgm.GetNamespace(), cfgm.GetName(), endpoint, err)
nl.Error(l, errorText)
eventLog.Event(cfgm, v1.EventTypeWarning, invalidValueReason, errorText)
configWarnings = true
} else {
mgmtCfgParams.Endpoint = strings.TrimSpace(endpoint)
}
}

if interval, exists := cfgm.Data["usage-report-interval"]; exists {
i := strings.TrimSpace(interval)
t, err := time.ParseDuration(i)
Expand Down
338 changes: 338 additions & 0 deletions internal/configs/configmaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,78 @@ func TestParseMGMTConfigMapWarnings(t *testing.T) {
},
msg: "enforce-initial-report set empty",
},
{
vepatel marked this conversation as resolved.
Show resolved Hide resolved
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"usage-report-interval": "",
},
},
msg: "usage-report-interval set empty",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"usage-report-interval": "1s",
},
},
msg: "usage-report-interval set below allowed value",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"usage-report-interval": "1s",
},
},
msg: "usage-report-interval set below allowed value",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"ssl-verify": "10",
},
},
msg: "ssl-verify set to an invalid int",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"ssl-verify": "test",
},
},
msg: "ssl-verify set to an invalid value",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"ssl-verify": "",
},
},
msg: "ssl-verify set to an empty string",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"resolver-ipv6": "",
},
},
msg: "resolver-ipv6 set to an empty string",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"resolver-ipv6": "10",
},
},
msg: "resolver-ipv6 set to an invalid int",
},
}

for _, test := range tests {
Expand Down Expand Up @@ -450,6 +522,272 @@ func TestParseMGMTConfigMapEnforceInitialReport(t *testing.T) {
}
}

func TestParseMGMTConfigMapSSLVerify(t *testing.T) {
t.Parallel()
tests := []struct {
configMap *v1.ConfigMap
want *MGMTConfigParams
msg string
}{
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"ssl-verify": "false",
},
},
want: &MGMTConfigParams{
SSLVerify: BoolToPointerBool(false),
Secrets: MGMTSecrets{
License: "license-token",
},
},
msg: "ssl-verify set to false",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"ssl-verify": "true",
},
},
want: &MGMTConfigParams{
SSLVerify: BoolToPointerBool(true),
Secrets: MGMTSecrets{
License: "license-token",
},
},
msg: "ssl-verify set to true",
},
}

for _, test := range tests {
t.Run(test.msg, func(t *testing.T) {
result, warnings, err := ParseMGMTConfigMap(context.Background(), test.configMap, makeEventLogger())
if err != nil {
t.Fatal(err)
}
if warnings {
t.Error("Unexpected warnings")
}

if result.SSLVerify == nil {
t.Errorf("ssl-verify: want %v, got nil", *test.want.SSLVerify)
}
if *result.SSLVerify != *test.want.SSLVerify {
t.Errorf("ssl-verify: want %v, got %v", *test.want.SSLVerify, *result.SSLVerify)
}
})
}
}

func TestParseMGMTConfigMapUsageReportInterval(t *testing.T) {
t.Parallel()
tests := []struct {
configMap *v1.ConfigMap
want *MGMTConfigParams
msg string
}{
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"usage-report-interval": "120s",
},
},
want: &MGMTConfigParams{
Interval: "120s",
Secrets: MGMTSecrets{
License: "license-token",
},
},
msg: "usage report interval set to 120s",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"usage-report-interval": "20m",
},
},
want: &MGMTConfigParams{
Interval: "20m",
Secrets: MGMTSecrets{
License: "license-token",
},
},
msg: "usage report interval set to 20m",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"usage-report-interval": "1h",
},
},
want: &MGMTConfigParams{
Interval: "1h",
Secrets: MGMTSecrets{
License: "license-token",
},
},
msg: "usage report interval set to 1h",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"usage-report-interval": "24h",
},
},
want: &MGMTConfigParams{
Interval: "24h",
Secrets: MGMTSecrets{
License: "license-token",
},
},
msg: "usage report interval set to 24h",
},
}

for _, test := range tests {
t.Run(test.msg, func(t *testing.T) {
result, warnings, err := ParseMGMTConfigMap(context.Background(), test.configMap, makeEventLogger())
if err != nil {
t.Fatal(err)
}
if warnings {
t.Error("Unexpected warnings")
}

if result.Interval == "" {
t.Errorf("UsageReportInterval: want %s, got empty string", test.want.Interval)
}
if result.Interval != test.want.Interval {
t.Errorf("UsageReportInterval: want %v, got %v", test.want.Interval, result.Interval)
}
})
}
}

func TestParseMGMTConfigMapResolverIPV6(t *testing.T) {
t.Parallel()
tests := []struct {
configMap *v1.ConfigMap
want *MGMTConfigParams
msg string
}{
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"resolver-ipv6": "false",
},
},
want: &MGMTConfigParams{
ResolverIPV6: BoolToPointerBool(false),
Secrets: MGMTSecrets{
License: "license-token",
},
},
msg: "resolver-ipv6 set to false",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"resolver-ipv6": "true",
},
},
want: &MGMTConfigParams{
ResolverIPV6: BoolToPointerBool(true),
Secrets: MGMTSecrets{
License: "license-token",
},
},
msg: "resolver-ipv6 set to true",
},
}

for _, test := range tests {
t.Run(test.msg, func(t *testing.T) {
result, warnings, err := ParseMGMTConfigMap(context.Background(), test.configMap, makeEventLogger())
if err != nil {
t.Fatal(err)
}
if warnings {
t.Error("Unexpected warnings")
}

if result.ResolverIPV6 == nil {
t.Errorf("resolver-ipv6: want %v, got nil", *test.want.ResolverIPV6)
}
if *result.ResolverIPV6 != *test.want.ResolverIPV6 {
t.Errorf("resolver-ipv6: want %v, got %v", *test.want.ResolverIPV6, *result.ResolverIPV6)
}
})
}
}

func TestParseMGMTConfigMapUsageReportEndpoint(t *testing.T) {
t.Parallel()
tests := []struct {
configMap *v1.ConfigMap
want *MGMTConfigParams
msg string
}{
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"usage-report-endpoint": "product.connect.nginx.com",
},
},
want: &MGMTConfigParams{
Endpoint: "product.connect.nginx.com",
Secrets: MGMTSecrets{
License: "license-token",
},
},
msg: "usage report endpoint set to product.connect.nginx.com",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"usage-report-endpoint": "product.connect.nginx.com:80",
},
},
want: &MGMTConfigParams{
Endpoint: "product.connect.nginx.com:80",
Secrets: MGMTSecrets{
License: "license-token",
},
},
msg: "usage report endpoint set to product.connect.nginx.com with port 80",
},
}

for _, test := range tests {
t.Run(test.msg, func(t *testing.T) {
result, warnings, err := ParseMGMTConfigMap(context.Background(), test.configMap, makeEventLogger())
if err != nil {
t.Fatal(err)
}
if warnings {
t.Error("Unexpected warnings")
}

if result.Endpoint == "" {
t.Errorf("UsageReportEndpoint: want %s, got empty string", test.want.Endpoint)
}
if result.Endpoint != test.want.Endpoint {
t.Errorf("UsageReportEndpoint: want %v, got %v", test.want.Endpoint, result.Endpoint)
}
})
}
}

func makeEventLogger() record.EventRecorder {
return record.NewFakeRecorder(1024)
}
Loading