Skip to content

Commit

Permalink
update and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ramondeklein committed Aug 27, 2024
1 parent e412118 commit 7fbd1d4
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/minio.min.io/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ type EnvVarSource struct {
SecretKeyRef *SecretKeySelector `json:"secretKeyRef,omitempty" protobuf:"bytes,4,opt,name=secretKeyRef"`
}

// Selects a key from a ConfigMap.
// ConfigMapKeySelector selects a key from a ConfigMap.
// +structType=atomic
type ConfigMapKeySelector struct {
// The ConfigMap to select from.
Expand Down
159 changes: 132 additions & 27 deletions sidecar/pkg/configuration/tenant_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const configSecretName = "config-secret"

func TestEnvVarsToFileContent(t *testing.T) {
type args struct {
envVars []corev1.EnvVar
envVars []miniov2.EnvVar
}
tests := []struct {
name string
Expand All @@ -37,7 +39,7 @@ func TestEnvVarsToFileContent(t *testing.T) {
{
name: "Basic test case",
args: args{
envVars: []corev1.EnvVar{
envVars: []miniov2.EnvVar{
{
Name: "MINIO_UPDATE",
Value: "on",
Expand All @@ -49,7 +51,7 @@ func TestEnvVarsToFileContent(t *testing.T) {
{
name: "Two Vars test case",
args: args{
envVars: []corev1.EnvVar{
envVars: []miniov2.EnvVar{
{
Name: "MINIO_UPDATE",
Value: "on",
Expand Down Expand Up @@ -77,20 +79,20 @@ export MINIO_UPDATE_MINISIGN_PUBKEY="RWTx5Zr1tiHQLwG9keckT0c45M3AGeHD6IvimQHpyRy
func TestGetTenantConfiguration(t *testing.T) {
type args struct {
tenant *miniov2.Tenant
cfgEnvExisting map[string]corev1.EnvVar
cfgEnvExisting map[string]miniov2.EnvVar
}
tests := []struct {
name string
args args
want []corev1.EnvVar
want []miniov2.EnvVar
}{
{
name: "Defaulted Values",
args: args{
tenant: &miniov2.Tenant{},
cfgEnvExisting: nil,
},
want: []corev1.EnvVar{
want: []miniov2.EnvVar{
{
Name: "MINIO_ARGS",
Value: "",
Expand Down Expand Up @@ -118,7 +120,7 @@ func TestGetTenantConfiguration(t *testing.T) {
args: args{
tenant: &miniov2.Tenant{
Spec: miniov2.TenantSpec{
Env: []corev1.EnvVar{
Env: []miniov2.EnvVar{
{
Name: "TEST",
Value: "value",
Expand All @@ -128,7 +130,7 @@ func TestGetTenantConfiguration(t *testing.T) {
},
cfgEnvExisting: nil,
},
want: []corev1.EnvVar{
want: []miniov2.EnvVar{
{
Name: "MINIO_ARGS",
Value: "",
Expand Down Expand Up @@ -173,7 +175,7 @@ func TestParseConfEnvSecret(t *testing.T) {
tests := []struct {
name string
args args
want map[string]corev1.EnvVar
want map[string]miniov2.EnvVar
}{
{
name: "Basic case",
Expand All @@ -185,7 +187,7 @@ export MINIO_STORAGE_CLASS_STANDARD="EC:2"
export MINIO_BROWSER="on"`)},
},
},
want: map[string]corev1.EnvVar{
want: map[string]miniov2.EnvVar{
"MINIO_ROOT_USER": {
Name: "MINIO_ROOT_USER",
Value: "minio",
Expand Down Expand Up @@ -214,7 +216,7 @@ export MINIO_BROWSER="on"`)},
export MINIO_BROWSER="on"`)},
},
},
want: map[string]corev1.EnvVar{
want: map[string]miniov2.EnvVar{
"MINIO_ROOT_USER": {
Name: "MINIO_ROOT_USER",
Value: "minio",
Expand Down Expand Up @@ -245,8 +247,9 @@ export MINIO_BROWSER="on"`)},

func TestGetFullTenantConfig(t *testing.T) {
type args struct {
tenant *miniov2.Tenant
configSecret *corev1.Secret
tenant *miniov2.Tenant
secrets map[string]*corev1.Secret
configMaps map[string]*corev1.ConfigMap
}
tests := []struct {
name string
Expand All @@ -258,19 +261,24 @@ func TestGetFullTenantConfig(t *testing.T) {
args: args{
tenant: &miniov2.Tenant{
Spec: miniov2.TenantSpec{
Env: []corev1.EnvVar{
Configuration: &corev1.LocalObjectReference{
Name: configSecretName,
},
Env: []miniov2.EnvVar{
{
Name: "TEST",
Value: "value",
},
},
},
},
configSecret: &corev1.Secret{
Data: map[string][]byte{"config.env": []byte(`export MINIO_ROOT_USER="minio"
secrets: map[string]*corev1.Secret{
configSecretName: {
Data: map[string][]byte{"config.env": []byte(`export MINIO_ROOT_USER="minio"
export MINIO_ROOT_PASSWORD="minio123"
export MINIO_STORAGE_CLASS_STANDARD="EC:2"
export MINIO_BROWSER="on"`)},
},
},
},
want: `export MINIO_ARGS=""
Expand All @@ -290,7 +298,10 @@ export TEST="value"
args: args{
tenant: &miniov2.Tenant{
Spec: miniov2.TenantSpec{
Env: []corev1.EnvVar{
Configuration: &corev1.LocalObjectReference{
Name: configSecretName,
},
Env: []miniov2.EnvVar{
{
Name: "TEST",
Value: "value",
Expand All @@ -303,11 +314,13 @@ export TEST="value"
},
},
},
configSecret: &corev1.Secret{
Data: map[string][]byte{"config.env": []byte(`export MINIO_ROOT_USER="minio"
secrets: map[string]*corev1.Secret{
configSecretName: {
Data: map[string][]byte{"config.env": []byte(`export MINIO_ROOT_USER="minio"
export MINIO_ROOT_PASSWORD="minio123"
export MINIO_STORAGE_CLASS_STANDARD="EC:2"
export MINIO_BROWSER="on"`)},
},
},
},
want: `export MINIO_ARGS=""
Expand All @@ -332,7 +345,10 @@ export TEST="value"
Namespace: "ns-x",
},
Spec: miniov2.TenantSpec{
Env: []corev1.EnvVar{
Configuration: &corev1.LocalObjectReference{
Name: configSecretName,
},
Env: []miniov2.EnvVar{
{
Name: "TEST",
Value: "value",
Expand All @@ -353,11 +369,102 @@ export TEST="value"
},
},
},
configSecret: &corev1.Secret{
Data: map[string][]byte{"config.env": []byte(`export MINIO_ROOT_USER="minio"
secrets: map[string]*corev1.Secret{
configSecretName: {
Data: map[string][]byte{"config.env": []byte(`export MINIO_ROOT_USER="minio"
export MINIO_ROOT_PASSWORD="minio123"
export MINIO_STORAGE_CLASS_STANDARD="EC:2"
export MINIO_BROWSER="on"`)},
},
},
},
want: `export MINIO_ARGS="https://tenant-pool-0-{0...3}.tenant-hl.ns-x.svc.cluster.local/export{0...3}"
export MINIO_BROWSER="on"
export MINIO_BROWSER_REDIRECT_URL="http://console.minio"
export MINIO_PROMETHEUS_JOB_ID="minio-job"
export MINIO_ROOT_PASSWORD="minio123"
export MINIO_ROOT_USER="minio"
export MINIO_SERVER_URL="https://minio.ns-x.svc.cluster.local:443"
export MINIO_STORAGE_CLASS_STANDARD="EC:2"
export MINIO_UPDATE="on"
export MINIO_UPDATE_MINISIGN_PUBKEY="RWTx5Zr1tiHQLwG9keckT0c45M3AGeHD6IvimQHpyRywVWGbP1aVSGav"
export TEST="value"
`,
},
{
name: "Default with both a config-map and secret reference",
args: args{
tenant: &miniov2.Tenant{
ObjectMeta: metav1.ObjectMeta{
Name: "tenant",
Namespace: "ns-x",
},
Spec: miniov2.TenantSpec{
Configuration: &corev1.LocalObjectReference{
Name: configSecretName,
},
Env: []miniov2.EnvVar{
{
Name: "TEST",
Value: "value",
},
{
Name: "TEST_CONFIGMAP",
ValueFrom: &miniov2.EnvVarSource{
ConfigMapKeyRef: &miniov2.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "test-configmap",
},
Key: "test-configmap-key",
},
},
},
{
Name: "TEST_SECRET",
ValueFrom: &miniov2.EnvVarSource{
SecretKeyRef: &miniov2.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "test-secret",
},
Key: "test-secret-key",
},
},
},
},
Features: &miniov2.Features{
Domains: &miniov2.TenantDomains{
Console: "http://console.minio",
},
},
Pools: []miniov2.Pool{
{
Name: "pool-0",
Servers: 4,
VolumesPerServer: 4,
VolumeClaimTemplate: nil,
},
},
},
},
secrets: map[string]*corev1.Secret{
configSecretName: {
Data: map[string][]byte{"config.env": []byte(`export MINIO_ROOT_USER="minio"
export MINIO_ROOT_PASSWORD="minio123"
export MINIO_STORAGE_CLASS_STANDARD="EC:2"
export MINIO_BROWSER="on"`)},
},
"test-secret": {
Data: map[string][]byte{
"test-secret-key": []byte("test-secret-value"),
},
},
},
configMaps: map[string]*corev1.ConfigMap{
"test-configmap": {
Data: map[string]string{
"test-configmap-key": "test-configmap-value",
},
},
},
},
want: `export MINIO_ARGS="https://tenant-pool-0-{0...3}.tenant-hl.ns-x.svc.cluster.local/export{0...3}"
Expand All @@ -371,18 +478,16 @@ export MINIO_STORAGE_CLASS_STANDARD="EC:2"
export MINIO_UPDATE="on"
export MINIO_UPDATE_MINISIGN_PUBKEY="RWTx5Zr1tiHQLwG9keckT0c45M3AGeHD6IvimQHpyRywVWGbP1aVSGav"
export TEST="value"
export TEST_CONFIGMAP="test-configmap-value"
export TEST_SECRET="test-secret-value"
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.tenant.EnsureDefaults()

var configMaps map[string]*corev1.ConfigMap
secrets := map[string]*corev1.Secret{
tt.args.tenant.ConfigurationSecretName(): tt.args.configSecret,
}
if got, _, _ := GetFullTenantConfig(tt.args.tenant, configMaps, secrets); got != tt.want {
if got, _, _ := GetFullTenantConfig(tt.args.tenant, tt.args.configMaps, tt.args.secrets); got != tt.want {
t.Errorf("GetFullTenantConfig() = `%v`, want `%v`", got, tt.want)
}
})
Expand Down

0 comments on commit 7fbd1d4

Please sign in to comment.