add unit test for appendEnvVar

This commit is contained in:
Felix Kunde
2022-04-14 11:22:21 +02:00
parent 4b833c2436
commit 4b0602e911
5 changed files with 94 additions and 27 deletions
+11 -11
View File
@@ -937,14 +937,14 @@ func (c *Cluster) generateSpiloPodEnvVars(
}
func appendEnvVars(envs []v1.EnvVar, appEnv ...v1.EnvVar) []v1.EnvVar {
jenvs := envs
collectedEnvs := envs
for _, env := range appEnv {
env.Name = strings.ToUpper(env.Name)
if !isEnvVarPresent(jenvs, env.Name) {
jenvs = append(jenvs, env)
if !isEnvVarPresent(collectedEnvs, env.Name) {
collectedEnvs = append(collectedEnvs, env)
}
}
return jenvs
return collectedEnvs
}
func isEnvVarPresent(envs []v1.EnvVar, key string) bool {
@@ -961,7 +961,7 @@ func (c *Cluster) getPodEnvironmentConfigMapVariables() ([]v1.EnvVar, error) {
configMapPodEnvVarsList := make([]v1.EnvVar, 0)
if c.OpConfig.PodEnvironmentConfigMap.Name == "" {
return configMapPodEnvVarsList, nil
return nil, nil
}
cm, err := c.KubeClient.ConfigMaps(c.OpConfig.PodEnvironmentConfigMap.Namespace).Get(
@@ -977,7 +977,7 @@ func (c *Cluster) getPodEnvironmentConfigMapVariables() ([]v1.EnvVar, error) {
metav1.GetOptions{})
}
if err != nil {
return configMapPodEnvVarsList, fmt.Errorf("could not read PodEnvironmentConfigMap: %v", err)
return nil, fmt.Errorf("could not read PodEnvironmentConfigMap: %v", err)
}
}
@@ -993,7 +993,7 @@ func (c *Cluster) getPodEnvironmentSecretVariables() ([]v1.EnvVar, error) {
secretPodEnvVarsList := make([]v1.EnvVar, 0)
if c.OpConfig.PodEnvironmentSecret == "" {
return secretPodEnvVarsList, nil
return nil, nil
}
secret := &v1.Secret{}
@@ -1019,7 +1019,7 @@ func (c *Cluster) getPodEnvironmentSecretVariables() ([]v1.EnvVar, error) {
err = errors.Wrap(notFoundErr, err.Error())
}
if err != nil {
return secretPodEnvVarsList, errors.Wrap(err, "could not read Secret PodEnvironmentSecretName")
return nil, errors.Wrap(err, "could not read Secret PodEnvironmentSecretName")
}
for k := range secret.Data {
@@ -1859,7 +1859,7 @@ func (c *Cluster) generateCloneEnvironment(description *acidv1.CloneDescription)
c.logger.Debugf("found WALAZStorageAccount %s - will set CLONE_AZURE_STORAGE_ACCOUNT", c.OpConfig.WALAZStorageAccount)
result = append(result, v1.EnvVar{Name: "CLONE_AZURE_STORAGE_ACCOUNT", Value: c.OpConfig.WALAZStorageAccount})
} else {
c.logger.Error("cannot figure out S3 or GS bucket or AZ storage account. All are empty in config.")
c.logger.Error("cannot figure out S3 or GS bucket or AZ storage account. All options are empty in the config.")
}
// append suffix because WAL location name is not the whole path
@@ -1915,7 +1915,7 @@ func (c *Cluster) generateStandbyEnvironment(description *acidv1.StandbyDescript
result := make([]v1.EnvVar, 0)
if description.StandbyHost != "" {
c.logger.Info("preparing standby streaming from remote primary")
c.logger.Info("standby cluster streaming from remote primary")
result = append(result, v1.EnvVar{
Name: "STANDBY_HOST",
Value: description.StandbyHost,
@@ -1927,7 +1927,7 @@ func (c *Cluster) generateStandbyEnvironment(description *acidv1.StandbyDescript
})
}
} else {
c.logger.Info("preparing standby streaming from WAL location")
c.logger.Info("standby cluster streaming from WAL location")
if description.S3WalPath != "" {
result = append(result, v1.EnvVar{
Name: "STANDBY_WALE_S3_PREFIX",
+76 -8
View File
@@ -250,7 +250,6 @@ func TestPodEnvironmentConfigMapVariables(t *testing.T) {
}{
{
subTest: "no PodEnvironmentConfigMap",
envVars: []v1.EnvVar{},
},
{
subTest: "missing PodEnvironmentConfigMap",
@@ -261,8 +260,7 @@ func TestPodEnvironmentConfigMapVariables(t *testing.T) {
},
},
},
envVars: []v1.EnvVar{},
err: fmt.Errorf("could not read PodEnvironmentConfigMap: NotFound"),
err: fmt.Errorf("could not read PodEnvironmentConfigMap: NotFound"),
},
{
subTest: "Pod environment vars configured by PodEnvironmentConfigMap",
@@ -326,7 +324,6 @@ func TestPodEnvironmentSecretVariables(t *testing.T) {
}{
{
subTest: "No PodEnvironmentSecret configured",
envVars: []v1.EnvVar{},
},
{
subTest: "Secret referenced by PodEnvironmentSecret does not exist",
@@ -337,8 +334,7 @@ func TestPodEnvironmentSecretVariables(t *testing.T) {
ResourceCheckTimeout: time.Duration(testResourceCheckTimeout),
},
},
envVars: []v1.EnvVar{},
err: fmt.Errorf("could not read Secret PodEnvironmentSecretName: still failing after %d retries: secret.core %q not found", maxRetries, testPodEnvironmentObjectNotExists),
err: fmt.Errorf("could not read Secret PodEnvironmentSecretName: still failing after %d retries: secret.core %q not found", maxRetries, testPodEnvironmentObjectNotExists),
},
{
subTest: "API error during PodEnvironmentSecret retrieval",
@@ -349,8 +345,7 @@ func TestPodEnvironmentSecretVariables(t *testing.T) {
ResourceCheckTimeout: time.Duration(testResourceCheckTimeout),
},
},
envVars: []v1.EnvVar{},
err: fmt.Errorf("could not read Secret PodEnvironmentSecretName: Secret PodEnvironmentSecret API error"),
err: fmt.Errorf("could not read Secret PodEnvironmentSecretName: Secret PodEnvironmentSecret API error"),
},
{
subTest: "Pod environment vars reference all keys from secret configured by PodEnvironmentSecret",
@@ -945,6 +940,79 @@ func TestCloneEnv(t *testing.T) {
}
}
func TestAppendEnvVar(t *testing.T) {
testName := "TestAppendEnvVar"
tests := []struct {
subTest string
envs []v1.EnvVar
envsToAppend []v1.EnvVar
expectedSize int
}{
{
subTest: "append two variables - one with same key that should get rejected",
envs: []v1.EnvVar{
{
Name: "CUSTOM_VARIABLE",
Value: "test",
},
},
envsToAppend: []v1.EnvVar{
{
Name: "CUSTOM_VARIABLE",
Value: "new-test",
},
{
Name: "ANOTHER_CUSTOM_VARIABLE",
Value: "another-test",
},
},
expectedSize: 2,
},
{
subTest: "append empty slice",
envs: []v1.EnvVar{
{
Name: "CUSTOM_VARIABLE",
Value: "test",
},
},
envsToAppend: []v1.EnvVar{},
expectedSize: 1,
},
{
subTest: "append nil",
envs: []v1.EnvVar{
{
Name: "CUSTOM_VARIABLE",
Value: "test",
},
},
envsToAppend: nil,
expectedSize: 1,
},
}
for _, tt := range tests {
finalEnvs := appendEnvVars(tt.envs, tt.envsToAppend...)
if len(finalEnvs) != tt.expectedSize {
t.Errorf("%s %s: expected %d env variables, got %d",
testName, tt.subTest, tt.expectedSize, len(finalEnvs))
}
for _, env := range tt.envs {
for _, finalEnv := range finalEnvs {
if env.Name == finalEnv.Name {
if env.Value != finalEnv.Value {
t.Errorf("%s %s: expected env value %s of variable %s, got %s instead",
testName, tt.subTest, env.Value, env.Name, finalEnv.Value)
}
}
}
}
}
}
func TestStandbyEnv(t *testing.T) {
testName := "TestStandbyEnv"
tests := []struct {