Skip to content

Commit

Permalink
Updated e2e test to reflect new config params
Browse files Browse the repository at this point in the history
Signed-off-by: Antonin Bas <abas@vmware.com>
  • Loading branch information
antoninbas committed Oct 26, 2021
1 parent f965b30 commit cb00174
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 49 deletions.
4 changes: 2 additions & 2 deletions test/e2e/egress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ func TestEgress(t *testing.T) {
skipIfEncapModeIsNot(t, data, config.TrafficEncapModeEncap)

cc := []configChange{
{"Egress", "true", true},
&configChangeFeatureGate{"Egress", true},
}
ac := []configChange{
{"Egress", "true", true},
&configChangeFeatureGate{"Egress", true},
}

if err := data.mutateAntreaConfigMap(cc, ac, true, true); err != nil {
Expand Down
68 changes: 43 additions & 25 deletions test/e2e/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,40 @@ type TestData struct {
logsDirForTestCase string
}

type configChange struct {
field string
value string
isFeatureGate bool
type configChange interface {
ApplyChange(content string) string
}

type configChangeParam struct {
field string
value string
}

func (cg *configChangeParam) ApplyChange(content string) string {
r := regexp.MustCompile(fmt.Sprintf(`(?m)#?.*%s:.*$`, cg.field))
return r.ReplaceAllString(content, fmt.Sprintf("%s: %s", cg.field, cg.value))
}

type configChangeFeatureGate struct {
name string
enabled bool
}

func (cg *configChangeFeatureGate) ApplyChange(content string) string {
r := regexp.MustCompile(fmt.Sprintf(`(?m)#? %s:.*$`, cg.name))
value := "false"
if cg.enabled {
value = "true"
}
return r.ReplaceAllString(content, fmt.Sprintf(" %s: %s", cg.name, value))
}

type configChangeRaw struct {
fn func(content string) string
}

func (cg *configChangeRaw) ApplyChange(content string) string {
return cg.fn(content)
}

var testData *TestData
Expand Down Expand Up @@ -656,13 +686,13 @@ func (data *TestData) deployAntrea(option deployAntreaOptions) error {
func (data *TestData) deployAntreaFlowExporter(ipfixCollector string) error {
// Enable flow exporter feature and add related config params to antrea agent configmap.
ac := []configChange{
{"FlowExporter", "true", true},
{"flowPollInterval", "\"1s\"", false},
{"activeFlowExportTimeout", fmt.Sprintf("\"%v\"", exporterActiveFlowExportTimeout), false},
{"idleFlowExportTimeout", fmt.Sprintf("\"%v\"", exporterIdleFlowExportTimeout), false},
&configChangeFeatureGate{"FlowExporter", true},
&configChangeParam{"flowPollInterval", "\"1s\""},
&configChangeParam{"activeFlowExportTimeout", fmt.Sprintf("\"%v\"", exporterActiveFlowExportTimeout)},
&configChangeParam{"idleFlowExportTimeout", fmt.Sprintf("\"%v\"", exporterIdleFlowExportTimeout)},
}
if ipfixCollector != "" {
ac = append(ac, configChange{"flowCollectorAddr", fmt.Sprintf("\"%s\"", ipfixCollector), false})
ac = append(ac, &configChangeParam{"flowCollectorAddr", fmt.Sprintf("\"%s\"", ipfixCollector)})
}
return data.mutateAntreaConfigMap(nil, ac, false, true)
}
Expand Down Expand Up @@ -1828,15 +1858,15 @@ func (data *TestData) mutateAntreaConfigMap(controllerChanges []configChange, ag
}

controllerConf := configMap.Data["antrea-controller.conf"]
for _, c := range controllerChanges {
controllerConf = replaceFieldValue(controllerConf, c)
for _, cg := range controllerChanges {
controllerConf = cg.ApplyChange(controllerConf)
}
controllerConfChanged := controllerConf != configMap.Data["antrea-controller.conf"]
configMap.Data["antrea-controller.conf"] = controllerConf

agentConf := configMap.Data["antrea-agent.conf"]
for _, c := range agentChanges {
agentConf = replaceFieldValue(agentConf, c)
for _, cg := range agentChanges {
agentConf = cg.ApplyChange(agentConf)
}
agentConfChanged := agentConf != configMap.Data["antrea-agent.conf"]
configMap.Data["antrea-agent.conf"] = agentConf
Expand Down Expand Up @@ -1865,18 +1895,6 @@ func (data *TestData) mutateAntreaConfigMap(controllerChanges []configChange, ag
return nil
}

func replaceFieldValue(content string, c configChange) string {
var res string
if c.isFeatureGate {
r := regexp.MustCompile(fmt.Sprintf(`(?m)#? %s:.*$`, c.field))
res = r.ReplaceAllString(content, fmt.Sprintf(" %s: %s", c.field, c.value))
} else {
r := regexp.MustCompile(fmt.Sprintf(`(?m)#?.*%s:.*$`, c.field))
res = r.ReplaceAllString(content, fmt.Sprintf("%s: %s", c.field, c.value))
}
return res
}

// gracefulExitAntreaController copies the Antrea controller binary coverage data file out before terminating the Pod
func (data *TestData) gracefulExitAntreaController(covDir string) error {
antreaController, err := data.getAntreaController()
Expand Down
43 changes: 34 additions & 9 deletions test/e2e/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,54 @@
package e2e

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)

func TestReplaceFieldValue(t *testing.T) {
func TestConfigChange(t *testing.T) {
content := `
featureGates:
# featureGateField0:
#field0:
# field1: abc
field2:
nestedField: 7
`
cs := []configChange{
{"featureGateField0", "123", true},
{"field0", "456", false},
{"field1", "789", false},

changeField2 := func(content string) string {
var cfg interface{}
require.NoError(t, yaml.Unmarshal([]byte(content), &cfg))
newField := map[string]interface{}{
"nestedField": 8,
"nestedField2": true,
}
cfg.(map[interface{}]interface{})["field2"] = newField
b, err := yaml.Marshal(&cfg)
require.NoError(t, err)
return string(b)
}

cgs := []configChange{
&configChangeFeatureGate{"featureGateField0", true},
&configChangeParam{"field0", "456"},
&configChangeParam{"field1", "789"},
&configChangeRaw{changeField2},
}
expected := `
featureGateField0: 123
featureGates:
featureGateField0: true
field0: 456
field1: 789
field2:
nestedField: 8
nestedField2: true
`
for _, c := range cs {
content = replaceFieldValue(content, c)
for _, cg := range cgs {
content = cg.ApplyChange(content)
}
assert.Equal(t, expected, content)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(content))
}
29 changes: 23 additions & 6 deletions test/e2e/nodeportlocal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -58,12 +59,28 @@ func skipIfNodePortLocalDisabled(tb testing.TB) {
skipIfFeatureDisabled(tb, features.NodePortLocal, true, false)
}

// setNPLPortRangeInConfigmap will update the nplPortRange in the antrea-agent
// config if needed.
func setNPLPortRangeInConfigmap(t *testing.T, data *TestData, newStartPort, newEndPort int) {
func configureNPLForAgent(t *testing.T, data *TestData, startPort, endPort int) {
configureNPL := func(content string) string {
var cfg interface{}
if err := yaml.Unmarshal([]byte(content), &cfg); err != nil {
t.Fatalf("Failed to unmarshal Agent config: %v", err)
}
nplConfig := map[string]interface{}{
"enable": true,
"portRange": fmt.Sprintf("%d-%d", startPort, endPort),
}
cfg.(map[interface{}]interface{})["nodePortLocal"] = nplConfig
b, err := yaml.Marshal(&cfg)
if err != nil {
t.Fatalf("Failed to marshal Agent config: %v", err)
}
return string(b)
}

ac := []configChange{
{"nplPortRange", fmt.Sprintf("%d-%d", newStartPort, newEndPort), false},
&configChangeRaw{configureNPL},
}

if err := data.mutateAntreaConfigMap(nil, ac, false, true); err != nil {
t.Fatalf("Failed to update NodePortLocal port range: %v", err)
}
Expand All @@ -81,7 +98,7 @@ func TestNodePortLocal(t *testing.T) {
}
defer teardownTest(t, data)
skipIfNodePortLocalDisabled(t)
setNPLPortRangeInConfigmap(t, data, defaultStartPort, defaultEndPort)
configureNPLForAgent(t, data, defaultStartPort, defaultEndPort)
t.Run("testNPLAddPod", func(t *testing.T) { testNPLAddPod(t, data) })
t.Run("testNPLMultiplePodsAgentRestart", func(t *testing.T) { testNPLMultiplePodsAgentRestart(t, data) })
t.Run("testNPLChangePortRangeAgentRestart", func(t *testing.T) { testNPLChangePortRangeAgentRestart(t, data) })
Expand Down Expand Up @@ -558,7 +575,7 @@ func testNPLChangePortRangeAgentRestart(t *testing.T, data *TestData) {
}
}

setNPLPortRangeInConfigmap(t, data, updatedStartPort, updatedEndPort)
configureNPLForAgent(t, data, updatedStartPort, updatedEndPort)

antreaPod, err := data.getAntreaPodOnNode(node)
r.NoError(err, "Error when getting Antrea Agent Pod on Node '%s'", node)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func testUserProvidedCert(t *testing.T, data *TestData) {
// Re-configure antrea-controller to use user-provided cert.
// Note antrea-controller must be restarted to take effect.
cc := []configChange{
{"selfSignedCert", "false", false},
&configChangeParam{"selfSignedCert", "false"},
}
if err := data.mutateAntreaConfigMap(cc, nil, false, false); err != nil {
t.Fatalf("Failed to update ConfigMap: %v", err)
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ func (data *TestData) configureTLS(t *testing.T, cipherSuites []uint16, tlsMinVe
}

cc := []configChange{
{"tlsCipherSuites", cipherSuitesStr, false},
{"tlsMinVersion", tlsMinVersion, false},
&configChangeParam{"tlsCipherSuites", cipherSuitesStr},
&configChangeParam{"tlsMinVersion", tlsMinVersion},
}
ac := []configChange{
{"tlsCipherSuites", cipherSuitesStr, false},
{"tlsMinVersion", tlsMinVersion, false},
&configChangeParam{"tlsCipherSuites", cipherSuitesStr},
&configChangeParam{"tlsMinVersion", tlsMinVersion},
}
if err := data.mutateAntreaConfigMap(cc, ac, true, true); err != nil {
t.Fatalf("Failed to configure Cipher Suites and TLSMinVersion: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/wireguard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ func TestWireGuard(t *testing.T) {

if !providerIsKind {
ac := []configChange{
{"trafficEncryptionMode", "wireguard", false},
&configChangeParam{"trafficEncryptionMode", "wireguard"},
}
if err := data.mutateAntreaConfigMap(nil, ac, false, true); err != nil {
t.Fatalf("Failed to enable WireGuard tunnel: %v", err)
}
defer func() {
ac = []configChange{
{"trafficEncryptionMode", "none", false},
&configChangeParam{"trafficEncryptionMode", "none"},
}
if err := data.mutateAntreaConfigMap(nil, ac, false, true); err != nil {
t.Fatalf("Failed to disable WireGuard tunnel: %v", err)
Expand Down

0 comments on commit cb00174

Please sign in to comment.