|
| 1 | +package construct |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/mongodb/mongodb-kubernetes-operator/pkg/readiness/config" |
| 5 | + "github.com/stretchr/testify/assert" |
| 6 | + corev1 "k8s.io/api/core/v1" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCollectEnvVars(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + envSetup map[string]string |
| 14 | + expectedEnv []corev1.EnvVar |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "Basic env vars set", |
| 18 | + envSetup: map[string]string{ |
| 19 | + config.ReadinessProbeLoggerBackups: "3", |
| 20 | + config.ReadinessProbeLoggerMaxSize: "10M", |
| 21 | + config.ReadinessProbeLoggerMaxAge: "7", |
| 22 | + config.WithAgentFileLogging: "enabled", |
| 23 | + }, |
| 24 | + expectedEnv: []corev1.EnvVar{ |
| 25 | + { |
| 26 | + Name: config.AgentHealthStatusFilePathEnv, |
| 27 | + Value: "/healthstatus/agent-health-status.json", |
| 28 | + }, |
| 29 | + { |
| 30 | + Name: config.ReadinessProbeLoggerBackups, |
| 31 | + Value: "3", |
| 32 | + }, |
| 33 | + { |
| 34 | + Name: config.ReadinessProbeLoggerMaxSize, |
| 35 | + Value: "10M", |
| 36 | + }, |
| 37 | + { |
| 38 | + Name: config.ReadinessProbeLoggerMaxAge, |
| 39 | + Value: "7", |
| 40 | + }, |
| 41 | + { |
| 42 | + Name: config.WithAgentFileLogging, |
| 43 | + Value: "enabled", |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "Additional env var set", |
| 49 | + envSetup: map[string]string{ |
| 50 | + config.ReadinessProbeLoggerBackups: "3", |
| 51 | + config.ReadinessProbeLoggerMaxSize: "10M", |
| 52 | + config.ReadinessProbeLoggerMaxAge: "7", |
| 53 | + config.ReadinessProbeLoggerCompress: "true", |
| 54 | + config.WithAgentFileLogging: "enabled", |
| 55 | + }, |
| 56 | + expectedEnv: []corev1.EnvVar{ |
| 57 | + { |
| 58 | + Name: config.AgentHealthStatusFilePathEnv, |
| 59 | + Value: "/healthstatus/agent-health-status.json", |
| 60 | + }, |
| 61 | + { |
| 62 | + Name: config.ReadinessProbeLoggerBackups, |
| 63 | + Value: "3", |
| 64 | + }, |
| 65 | + { |
| 66 | + Name: config.ReadinessProbeLoggerMaxSize, |
| 67 | + Value: "10M", |
| 68 | + }, |
| 69 | + { |
| 70 | + Name: config.ReadinessProbeLoggerMaxAge, |
| 71 | + Value: "7", |
| 72 | + }, |
| 73 | + { |
| 74 | + Name: config.ReadinessProbeLoggerCompress, |
| 75 | + Value: "true", |
| 76 | + }, |
| 77 | + { |
| 78 | + Name: config.WithAgentFileLogging, |
| 79 | + Value: "enabled", |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + for _, tt := range tests { |
| 86 | + t.Run(tt.name, func(t *testing.T) { |
| 87 | + // Setup environment variables |
| 88 | + for key, value := range tt.envSetup { |
| 89 | + t.Setenv(key, value) |
| 90 | + } |
| 91 | + |
| 92 | + actualEnvVars := collectEnvVars() |
| 93 | + |
| 94 | + assert.EqualValues(t, tt.expectedEnv, actualEnvVars) |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
0 commit comments