Skip to content

Commit

Permalink
tests: fix TestGatewayDataPlaneNetworkPolicy (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek authored Sep 18, 2024
1 parent bf9588c commit 4516ec6
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 18 deletions.
18 changes: 18 additions & 0 deletions test/helpers/envs/env.go
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,
})
}
57 changes: 57 additions & 0 deletions test/helpers/envs/env_test.go
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))
})
}
}
9 changes: 5 additions & 4 deletions test/integration/test_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
k8sutils "github.com/kong/gateway-operator/pkg/utils/kubernetes"
testutils "github.com/kong/gateway-operator/pkg/utils/test"
"github.com/kong/gateway-operator/test/helpers"
"github.com/kong/gateway-operator/test/helpers/envs"
)

func TestGatewayEssentials(t *testing.T) {
Expand Down Expand Up @@ -940,13 +941,13 @@ func setGatewayConfigurationEnvProxyPort(t *testing.T, gatewayConfiguration *ope
container := k8sutils.GetPodContainerByName(&dpOptions.Deployment.PodTemplateSpec.Spec, consts.DataPlaneProxyContainerName)
require.NotNil(t, container)

container.Env = SetEnvValueByName(container.Env,
container.Env = envs.SetValueByName(container.Env,
"KONG_PROXY_LISTEN",
fmt.Sprintf("0.0.0.0:%d reuseport backlog=16384, 0.0.0.0:%d http2 ssl reuseport backlog=16384", proxyPort, proxySSLPort),
)
container.Env = SetEnvValueByName(container.Env,
container.Env = envs.SetValueByName(container.Env,
"KONG_PORT_MAPS",
fmt.Sprintf("80:%d, 443:%d", proxyPort, proxySSLPort),
fmt.Sprintf("80:%d,443:%d", proxyPort, proxySSLPort),
)

gatewayConfiguration.Spec.DataPlaneOptions = dpOptions
Expand All @@ -963,7 +964,7 @@ func setGatewayConfigurationEnvAdminAPIPort(t *testing.T, gatewayConfiguration *
container := k8sutils.GetPodContainerByName(&dpOptions.Deployment.PodTemplateSpec.Spec, consts.DataPlaneProxyContainerName)
require.NotNil(t, container)

container.Env = SetEnvValueByName(container.Env,
container.Env = envs.SetValueByName(container.Env,
"KONG_ADMIN_LISTEN",
fmt.Sprintf("0.0.0.0:%d ssl reuseport backlog=16384", adminAPIPort),
)
Expand Down
14 changes: 0 additions & 14 deletions test/integration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,6 @@ func GetEnvValueByName(envs []corev1.EnvVar, name string) string {
return value
}

// SetEnvValueByName sets the EnvVar in slice with the provided name and value.
func SetEnvValueByName(envs []corev1.EnvVar, name string, value string) []corev1.EnvVar {
for _, env := range envs {
if env.Name == name {
env.Value = value
return envs
}
}
return append(envs, corev1.EnvVar{
Name: name,
Value: value,
})
}

// GetEnvValueFromByName returns the corresponding ValueFrom pointer of LAST item with given name.
// returns nil if the name not appeared.
func GetEnvValueFromByName(envs []corev1.EnvVar, name string) *corev1.EnvVarSource {
Expand Down

0 comments on commit 4516ec6

Please sign in to comment.