|
| 1 | +/* |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | +
|
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +*/ |
| 14 | + |
| 15 | +package internal |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "sort" |
| 21 | + "strings" |
| 22 | +) |
| 23 | + |
| 24 | +// EnvVarSpec describes an environment variable used by the test suite |
| 25 | +type EnvVarSpec struct { |
| 26 | + Name string |
| 27 | + Description string |
| 28 | + Required bool |
| 29 | + Default string |
| 30 | +} |
| 31 | + |
| 32 | +var ( |
| 33 | + // envVarRegistry tracks all environment variables used by the test suite |
| 34 | + envVarRegistry = make(map[string]EnvVarSpec) |
| 35 | +) |
| 36 | + |
| 37 | +// RegisterEnvVar registers an environment variable specification. |
| 38 | +// This should be called in init() functions to document environment variables used by the test suite. |
| 39 | +// Example: |
| 40 | +// |
| 41 | +// RegisterEnvVar("MY_VAR", "Description of what this variable does", true) |
| 42 | +func RegisterEnvVar(name, description string, required bool) { |
| 43 | + envVarRegistry[name] = EnvVarSpec{ |
| 44 | + Name: name, |
| 45 | + Description: description, |
| 46 | + Required: required, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// RegisterEnvVarWithDefault registers an environment variable specification with a default value. |
| 51 | +// This should be called in init() functions to document environment variables used by the test suite. |
| 52 | +// Example: |
| 53 | +// |
| 54 | +// RegisterEnvVarWithDefault("MY_VAR", "Description of what this variable does", false, "default-value") |
| 55 | +func RegisterEnvVarWithDefault(name, description string, required bool, defaultValue string) { |
| 56 | + envVarRegistry[name] = EnvVarSpec{ |
| 57 | + Name: name, |
| 58 | + Description: description, |
| 59 | + Required: required, |
| 60 | + Default: defaultValue, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// GetEnvVarValue returns the value of an environment variable, or its default if not set. |
| 65 | +// Panics if the environment variable is not registered in the registry. |
| 66 | +func GetEnvVarValue(name string) string { |
| 67 | + spec, exists := envVarRegistry[name] |
| 68 | + if !exists { |
| 69 | + panic(fmt.Sprintf("environment variable %q is not registered. Use RegisterEnvVar or RegisterEnvVarWithDefault to register it", name)) |
| 70 | + } |
| 71 | + |
| 72 | + value := os.Getenv(name) |
| 73 | + if value == "" && spec.Default != "" { |
| 74 | + return spec.Default |
| 75 | + } |
| 76 | + return value |
| 77 | +} |
| 78 | + |
| 79 | +// PrintEnvVarHelp prints a formatted help message for all registered environment variables |
| 80 | +func PrintEnvVarHelp() { |
| 81 | + if len(envVarRegistry) == 0 { |
| 82 | + fmt.Println("No environment variables are registered.") |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + fmt.Println("Environment Variables:") |
| 87 | + fmt.Println(strings.Repeat("=", 80)) |
| 88 | + |
| 89 | + // Sort by name for consistent output |
| 90 | + var names []string |
| 91 | + for name := range envVarRegistry { |
| 92 | + names = append(names, name) |
| 93 | + } |
| 94 | + sort.Strings(names) |
| 95 | + |
| 96 | + for _, name := range names { |
| 97 | + spec := envVarRegistry[name] |
| 98 | + fmt.Printf("\n%s", name) |
| 99 | + if spec.Required { |
| 100 | + fmt.Print(" (required)") |
| 101 | + } else { |
| 102 | + fmt.Print(" (optional)") |
| 103 | + } |
| 104 | + if spec.Default != "" { |
| 105 | + fmt.Printf(" [default: %s]", spec.Default) |
| 106 | + } |
| 107 | + fmt.Printf("\n %s\n", spec.Description) |
| 108 | + if currentValue := os.Getenv(name); currentValue != "" { |
| 109 | + fmt.Printf(" Current value: %s\n", maskSensitiveValue(name, currentValue)) |
| 110 | + } |
| 111 | + } |
| 112 | + fmt.Println() |
| 113 | +} |
| 114 | + |
| 115 | +// maskSensitiveValue masks potentially sensitive environment variable values |
| 116 | +func maskSensitiveValue(name, value string) string { |
| 117 | + lowerName := strings.ToLower(name) |
| 118 | + if strings.Contains(lowerName, "secret") || |
| 119 | + strings.Contains(lowerName, "password") || |
| 120 | + strings.Contains(lowerName, "token") || |
| 121 | + strings.Contains(lowerName, "key") { |
| 122 | + if len(value) > 8 { |
| 123 | + return value[:4] + "..." + value[len(value)-4:] |
| 124 | + } |
| 125 | + return "****" |
| 126 | + } |
| 127 | + return value |
| 128 | +} |
| 129 | + |
| 130 | +func init() { |
| 131 | + // Register environment variables used by the test suite |
| 132 | + RegisterEnvVar( |
| 133 | + "E2E_HOSTED_CLUSTER_NAME", |
| 134 | + "Name of the HostedCluster to test. Required for tests that interact with a hosted cluster.", |
| 135 | + false, |
| 136 | + ) |
| 137 | + RegisterEnvVar( |
| 138 | + "E2E_HOSTED_CLUSTER_NAMESPACE", |
| 139 | + "Namespace of the HostedCluster to test. Required for tests that interact with a hosted cluster.", |
| 140 | + false, |
| 141 | + ) |
| 142 | + RegisterEnvVar( |
| 143 | + "E2E_SHOW_ENV_HELP", |
| 144 | + "When set to any non-empty value, displays environment variable help and exits without running tests.", |
| 145 | + false, |
| 146 | + ) |
| 147 | + RegisterEnvVarWithDefault( |
| 148 | + "E2E_STRICT_MODE", |
| 149 | + "When set to true, enables strict mode validation that requires all pods to belong to predefined workloads.", |
| 150 | + false, |
| 151 | + "false", |
| 152 | + ) |
| 153 | +} |
0 commit comments