-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: fix TestGatewayDataPlaneNetworkPolicy (#609)
- Loading branch information
Showing
4 changed files
with
80 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package envs | ||
|
||
import corev1 "k8s.io/api/core/v1" | ||
|
||
// SetValueByName sets the EnvVar in slice with the provided name and value. | ||
func SetValueByName(envs []corev1.EnvVar, name string, value string) []corev1.EnvVar { | ||
for i := range envs { | ||
env := &envs[i] | ||
if env.Name == name { | ||
env.Value = value | ||
return envs | ||
} | ||
} | ||
return append(envs, corev1.EnvVar{ | ||
Name: name, | ||
Value: value, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package envs | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func TestSetValueByName(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
envs []corev1.EnvVar | ||
setName string | ||
setValue string | ||
expected []corev1.EnvVar | ||
}{ | ||
{ | ||
name: "set new env var", | ||
envs: []corev1.EnvVar{ | ||
{Name: "EXISTING_VAR", Value: "existing_value"}, | ||
}, | ||
setName: "NEW_VAR", | ||
setValue: "new_value", | ||
expected: []corev1.EnvVar{ | ||
{Name: "EXISTING_VAR", Value: "existing_value"}, | ||
{Name: "NEW_VAR", Value: "new_value"}, | ||
}, | ||
}, | ||
{ | ||
name: "update existing env var", | ||
envs: []corev1.EnvVar{ | ||
{Name: "EXISTING_VAR", Value: "existing_value"}, | ||
}, | ||
setName: "EXISTING_VAR", | ||
setValue: "updated_value", | ||
expected: []corev1.EnvVar{ | ||
{Name: "EXISTING_VAR", Value: "updated_value"}, | ||
}, | ||
}, | ||
{ | ||
name: "empty envs slice", | ||
envs: []corev1.EnvVar{}, | ||
setName: "NEW_VAR", | ||
setValue: "new_value", | ||
expected: []corev1.EnvVar{ | ||
{Name: "NEW_VAR", Value: "new_value"}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equal(t, tt.expected, SetValueByName(tt.envs, tt.setName, tt.setValue)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters