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

add license secret and mgmt config map for r33 #6888

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions internal/configs/configmaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,106 @@ func TestParseConfigMapAccessLogDefault(t *testing.T) {
}
}

func TestParseMGMTConfigMapNil(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": "",
},
},
want: nil,
msg: "Must have license-token-secret-name",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{},
},
want: nil,
msg: "Must have license-token-secret-name key",
},
}

for _, test := range tests {
t.Run(test.msg, func(t *testing.T) {
result, _, err := ParseMGMTConfigMap(context.Background(), test.configMap, makeEventLogger())
if err == nil {
t.Fatal(err)
}
if result.Secrets.License != test.want.Secrets.License {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var want here is always nil, correct? Am I right that the table tests here contain values that should make the ParseMGMTConfigMap func return error? If that's true, other returned values are irrelevant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the only way we actually get an error from ParseMGMTConfigMap is from the license

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, if the test inspects the License then error should be not nil. In this example if the err == nil the test fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have refactored the test, can we have another review on that please

t.Errorf("want %q, got %q", test.want.Secrets.License, result.Secrets.License)
}
})
}
}

func TestParseMGMTConfigMap(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",
},
},
want: &MGMTConfigParams{
Secrets: MGMTSecrets{
License: "license-token",
},
},
msg: "Has only license-token-secret-name",
},
{
configMap: &v1.ConfigMap{
Data: map[string]string{
"license-token-secret-name": "license-token",
"enforce-initial-report": "false",
},
},
want: &MGMTConfigParams{
EnforceInitialReport: BoolToPointerBool(false),
Secrets: MGMTSecrets{
License: "license-token",
},
},
msg: "Has license-token-secret-name and enforce-initial-report set to false",
},
}

for _, test := range tests {
t.Run(test.msg, func(t *testing.T) {
result, _, err := ParseMGMTConfigMap(context.Background(), test.configMap, makeEventLogger())
if err != nil {
t.Fatal(err)
}
if result.Secrets.License != test.want.Secrets.License {
t.Errorf("LicenseTokenSecretNane: want %q, got %q", test.want.Secrets.License, result.Secrets.License)
}

if test.want.EnforceInitialReport != nil {
if result.EnforceInitialReport == nil {
t.Errorf("EnforceInitialReport: want %v, got nil", *test.want.EnforceInitialReport)
} else if *result.EnforceInitialReport != *test.want.EnforceInitialReport {
t.Errorf("EnforceInitialReport: want %v, got %v", *test.want.EnforceInitialReport, *result.EnforceInitialReport)
}
} else {
if result.EnforceInitialReport != nil {
t.Errorf("EnforceInitialReport: want nil, got %v", *result.EnforceInitialReport)
}
}
})
}
}

func makeEventLogger() record.EventRecorder {
return record.NewFakeRecorder(1024)
}
26 changes: 26 additions & 0 deletions internal/configs/configurator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package configs

import (
"context"
"encoding/base64"
"encoding/json"
"os"
"reflect"
Expand Down Expand Up @@ -256,6 +257,31 @@ func TestConfiguratorUpdatesConfigWithNilCustomTStemplate(t *testing.T) {
}
}

func TestAddOrUpdateLicenseSecret(t *testing.T) {
t.Parallel()
cnf := createTestConfigurator(t)
cnf.MgmtCfgParams.Secrets.License = "default/license-token"
license := api_v1.Secret{
TypeMeta: meta_v1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: meta_v1.ObjectMeta{
Name: "license-token",
Namespace: "default",
},
Data: map[string][]byte{
"license.jwt": []byte(base64.StdEncoding.EncodeToString([]byte("license-token"))),
},
Type: "nginx.com/license",
}

err := cnf.AddOrUpdateLicenseSecret(&license)
if err != nil {
t.Errorf("AddOrUpdateLicenseSecret returned: \n%v, but expected: \n%v", err, nil)
}
}

func TestAddOrUpdateIngress(t *testing.T) {
t.Parallel()
cnf := createTestConfigurator(t)
Expand Down
59 changes: 59 additions & 0 deletions internal/k8s/secrets/validation_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package secrets

import (
"encoding/base64"
"testing"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -457,6 +458,64 @@ func TestValidateOIDCSecretFails(t *testing.T) {
}
}

func TestValidateLicenseSecret(t *testing.T) {
t.Parallel()
secret := &v1.Secret{
ObjectMeta: meta_v1.ObjectMeta{
Name: "license-token",
Namespace: "default",
},
Type: SecretTypeLicense,
Data: map[string][]byte{
"license.jwt": []byte(base64.StdEncoding.EncodeToString([]byte("license-token"))),
},
}

err := ValidateLicenseSecret(secret)
if err != nil {
t.Errorf("TestValidateLicenseSecret() returned error %v", err)
AlexFenlon marked this conversation as resolved.
Show resolved Hide resolved
}
}

func TestValidateLicenseSecretFails(t *testing.T) {
t.Parallel()
tests := []struct {
secret *v1.Secret
msg string
}{
{
secret: &v1.Secret{
ObjectMeta: meta_v1.ObjectMeta{
Name: "license-token",
Namespace: "default",
},
Type: "some-type",
Data: map[string][]byte{
"license.jwt": []byte(base64.StdEncoding.EncodeToString([]byte("license-token"))),
},
},
msg: "Incorrect type for license secret",
},
{
secret: &v1.Secret{
ObjectMeta: meta_v1.ObjectMeta{
Name: "license-token",
Namespace: "default",
},
Type: SecretTypeLicense,
},
msg: "Missing license.jwt for license secret",
},
}

for _, test := range tests {
err := ValidateLicenseSecret(test.secret)
if err == nil {
t.Errorf("TestValidateLicenseSecretFails() returned no error for the case of %s", test.msg)
AlexFenlon marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

func TestValidateSecret(t *testing.T) {
t.Parallel()
tests := []struct {
Expand Down
Loading