-
Notifications
You must be signed in to change notification settings - Fork 742
/
main_test.go
138 lines (112 loc) · 4.33 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"encoding/json"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
const (
awsConflist = "../../misc/10-aws.conflist"
devNull = "/dev/null"
nodeIP = "10.0.0.0"
)
var getPrimaryIPMock = func(ipv4 bool) (string, error) {
if ipv4 {
return "10.0.0.0", nil
}
return "2600::", nil
}
// Validate that generateJSON runs against default conflist without error
func TestGenerateJSON(t *testing.T) {
err := generateJSON(awsConflist, devNull, getPrimaryIPMock)
assert.NoError(t, err)
}
// Validate that generateJSON runs without error when bandwidth plugin is added to the default conflist
func TestGenerateJSONPlusBandwidth(t *testing.T) {
_ = os.Setenv(envEnBandwidthPlugin, "true")
err := generateJSON(awsConflist, devNull, getPrimaryIPMock)
assert.NoError(t, err)
}
// Validate that generateJSON runs without error when tuning plugin is added to the default conflist
func TestGenerateJSONPlusTuning(t *testing.T) {
_ = os.Setenv(envDisablePodV6, "true")
err := generateJSON(awsConflist, devNull, getPrimaryIPMock)
assert.NoError(t, err)
}
// Validate that generateJSON runs without error when the bandwidth and tuning plugins are added to the default conflist
func TestGenerateJSONPlusBandwidthAndTuning(t *testing.T) {
_ = os.Setenv(envEnBandwidthPlugin, "true")
_ = os.Setenv(envDisablePodV6, "true")
err := generateJSON(awsConflist, devNull, getPrimaryIPMock)
assert.NoError(t, err)
}
// Validate setting environment POD_MTU/AWS_VPC_ENI_MTU, takes effect for egress-cni plugin
func TestEgressCNIPluginIPv4EgressTakesMTUEnvVar(t *testing.T) {
_ = os.Setenv(envEnIPv4Egress, "true")
_ = os.Setenv(envPodMTU, "5000")
// Use a temporary file for the parsed output.
tmpfile, err := os.CreateTemp("", "temp-aws-vpc-cni.conflist")
assert.NoError(t, err)
defer os.Remove(tmpfile.Name())
err = generateJSON(awsConflist, tmpfile.Name(), getPrimaryIPMock)
assert.NoError(t, err)
// Read the json file and verify the MTU value for the egress-cni plugin
var jsonData map[string]interface{}
jsonFile, err := os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
err = json.Unmarshal(jsonFile, &jsonData)
assert.NoError(t, err)
plugins, _ := jsonData["plugins"].([]interface{})
assert.Equal(t, "egress-cni", plugins[1].(map[string]interface{})["type"])
assert.Equal(t, "5000", plugins[1].(map[string]interface{})["mtu"])
}
func TestEgressCNIPluginIPv6EgressTakesMTUEnvVar(t *testing.T) {
_ = os.Setenv(envEnIPv6Egress, "true")
_ = os.Setenv(envPodMTU, "8000")
// Use a temporary file for the parsed output.
tmpfile, err := os.CreateTemp("", "temp-aws-vpc-cni.conflist")
assert.NoError(t, err)
defer os.Remove(tmpfile.Name())
err = generateJSON(awsConflist, tmpfile.Name(), getPrimaryIPMock)
assert.NoError(t, err)
// Read the json file and verify the MTU value for the egress-cni plugin
var jsonData map[string]interface{}
jsonFile, err := os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
err = json.Unmarshal(jsonFile, &jsonData)
assert.NoError(t, err)
plugins, _ := jsonData["plugins"].([]interface{})
assert.Equal(t, "egress-cni", plugins[1].(map[string]interface{})["type"])
assert.Equal(t, "8000", plugins[1].(map[string]interface{})["mtu"])
}
func TestMTUValidation(t *testing.T) {
// By default, ENI MTU and pod MTU should be valid
assert.True(t, validateMTU(envEniMTU))
assert.True(t, validateMTU(envPodMTU))
// Non-integer values should fail
_ = os.Setenv(envEniMTU, "true")
_ = os.Setenv(envPodMTU, "abc")
assert.False(t, validateMTU(envEniMTU))
assert.False(t, validateMTU(envPodMTU))
// Integer values within IPv4 range should succeed
_ = os.Setenv(envEniMTU, "5000")
_ = os.Setenv(envPodMTU, "3000")
assert.True(t, validateMTU(envEniMTU))
assert.True(t, validateMTU(envPodMTU))
// Integer values outside IPv4 range should fail
_ = os.Setenv(envEniMTU, "10000")
_ = os.Setenv(envPodMTU, "500")
assert.False(t, validateMTU(envEniMTU))
assert.False(t, validateMTU(envPodMTU))
// Integer values within IPv6 range should succeed
_ = os.Setenv(envEnIPv6, "true")
_ = os.Setenv(envEniMTU, "5000")
_ = os.Setenv(envPodMTU, "3000")
assert.True(t, validateMTU(envEniMTU))
assert.True(t, validateMTU(envPodMTU))
// Integer values outside IPv6 range should fail
_ = os.Setenv(envEniMTU, "10000")
_ = os.Setenv(envPodMTU, "1200")
assert.False(t, validateMTU(envEniMTU))
assert.False(t, validateMTU(envPodMTU))
}