Skip to content

Commit 93e6ff8

Browse files
authored
Merge pull request #3 from citrix/version2.1.0
Adding more Unit Test cases
2 parents 4fe41e0 + c9e9b35 commit 93e6ff8

File tree

6 files changed

+147
-75
lines changed

6 files changed

+147
-75
lines changed

cmd/citrix-node-controller/inputFeeder.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import (
1313
var (
1414
NetscalerInit = 0x00000008
1515
NetscalerTerminate = 0x00000010
16+
MaxVNID = 0xffffff // 16777215
17+
MinVNID = 1
18+
MaxPort = 0xffff // 65535
19+
MinPort = 1
1620
)
1721

1822
// Node structure keeps the parsed information of a Node Object
@@ -125,6 +129,21 @@ func IsValidIP4(ipAddress string) bool {
125129
return true
126130
}
127131

132+
// This function validate given vxlan Port is valid.
133+
func IsValidVxlanPort(port int) bool {
134+
if (port >= MinPort && port <= MaxPort) {
135+
return true
136+
}
137+
return false
138+
}
139+
140+
// This function validate given vxlan ID is a valid as per RFC.
141+
func IsValidVxlanID(vni int) bool {
142+
if (vni >= MinVNID && vni <= MaxVNID) {
143+
return true
144+
}
145+
return false
146+
}
128147

129148
// FetchCitrixNodeControllerInput parses whole input provided by the user and store into controller input
130149
func FetchCitrixNodeControllerInput() *ControllerInput {
@@ -136,7 +155,7 @@ func FetchCitrixNodeControllerInput() *ControllerInput {
136155
configError = 1
137156
}
138157
if !(IsValidIP4(InputDataBuff.IngressDeviceIP)) {
139-
klog.Error("[ERROR] Invalid IP ")
158+
klog.Error("[ERROR] Invalid IPV4 ")
140159
configError = 1
141160
}
142161
InputDataBuff.IngressDeviceUsername = os.Getenv("NS_USER")
@@ -160,29 +179,40 @@ func FetchCitrixNodeControllerInput() *ControllerInput {
160179
InputDataBuff.IngressDeviceVtepIP = InputDataBuff.IngressDeviceIP
161180
configError = 1
162181
}
182+
if !(IsValidIP4(InputDataBuff.IngressDeviceVtepIP)) {
183+
klog.Error("[ERROR] Invalid IP ")
184+
configError = 1
185+
}
163186
VxlanPort := os.Getenv("VXLAN_PORT")
164187
if len(VxlanPort) == 0 {
165188
fmt.Println("[ERROR] VxlanPort (VXLAN_PORT) is must for extending the route")
166189
configError = 1
167190
}
191+
InputDataBuff.VxlanPort, _ = strconv.Atoi(VxlanPort)
192+
193+
if !(IsValidVxlanPort(InputDataBuff.VxlanPort)) {
194+
klog.Error("[ERROR] VXLAN Port is not in the range, Minimum value: 1, Maximum value: 65535")
195+
configError = 1
196+
}
197+
168198
InputDataBuff.IngressDeviceVxlanIDs = os.Getenv("VNID")
169199
InputDataBuff.IngressDeviceVxlanID, _ = strconv.Atoi(InputDataBuff.IngressDeviceVxlanIDs)
170200
if InputDataBuff.IngressDeviceVxlanID == 0 {
171201
klog.Info("[ERROR] vxlan id (VNID) has Not Given")
172202
configError = 1
173203
}
174-
204+
205+
if !(IsValidVxlanID(InputDataBuff.IngressDeviceVxlanID)) {
206+
klog.Error("[ERROR] VNI not in the range, Minimum value: 1, Maximum value: 16777215")
207+
configError = 1
208+
}
209+
175210
if configError == 1 {
176211
klog.Error("Unable to get the above mentioned input from YAML")
177212
panic("[ERROR] Killing Container.........Please restart Citrix Node Controller with Valid Inputs")
178213
}
179-
if !(IsValidIP4(InputDataBuff.IngressDeviceVtepIP)) {
180-
klog.Error("[ERROR] Invalid IP ")
181-
panic("[ERROR] Killing Container.........Please restart Citrix Node Controller with Valid Inputs")
182-
}
183214
splitString := strings.Split(InputDataBuff.IngressDevicePodCIDR, "/")
184215
InputDataBuff.NodeSubnetMask = PrefixMaskTable[splitString[1]]
185-
InputDataBuff.VxlanPort, _ = strconv.Atoi(VxlanPort)
186216
return &InputDataBuff
187217
}
188218

@@ -201,16 +231,3 @@ func WaitForConfigMapInput(api *KubernetesAPIServer, ControllerInputObj *Control
201231
}
202232
}
203233

204-
// This monitor netscaler and get network details
205-
func MonitorIngressDevice(IngressDeviceClient *NitroClient, ControllerInputObj *ControllerInput) {
206-
vtepMac := getClusterInterfaceMac(IngressDeviceClient)
207-
if vtepMac != "error" && vtepMac != "00:00:00:00:00:00" {
208-
ControllerInputObj.IngressDeviceVtepMAC = vtepMac
209-
} else {
210-
ControllerInputObj.IngressDeviceVtepMAC = os.Getenv("NS_VTEP_MAC")
211-
if len(ControllerInputObj.IngressDeviceVtepMAC) == 0 {
212-
klog.Error("[ERROR] Ingress Device VtepMAC (NS_VTEP_MAC) is required")
213-
panic("[ERROR] Killing Container.........Please restart Citrix Node Controller with NS_VTEP_MAC as CNC could not get it from NS")
214-
}
215-
}
216-
}

cmd/citrix-node-controller/inputFeeder_test.go

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@ import (
88
"testing"
99
//"fmt"
1010
)
11-
1211
func TestFetchCitrixNodeControllerInput(t *testing.T) {
1312
IngressDeviceIP := os.Getenv("NS_IP")
1413
os.Setenv("NS_IP", "")
15-
IngressDeviceVtepMAC := os.Getenv("NS_VTEP_MAC")
16-
os.Setenv("NS_VTEP_MAC", "")
1714
IngressDeviceUsername := os.Getenv("NS_LOGIN")
18-
os.Setenv("NS_LOGIN", "")
15+
os.Setenv("NS_USER", "")
1916
IngressDevicePassword := os.Getenv("NS_PASSWORD")
2017
os.Setenv("NS_PASSWORD", "")
21-
IngressDeviceVtepIP := os.Getenv("NS_SNIP")
22-
os.Setenv("NS_SNIP", "")
18+
IngressDeviceVtepIP := os.Getenv("REMOTE_VTEPIP")
19+
os.Setenv("REMOTE_VTEPIP", "")
20+
Vnid := os.Getenv("VNID")
21+
os.Setenv("VNID", "")
22+
Network := os.Getenv("NETWORK")
23+
os.Setenv("NETWORK", "")
24+
VxlanPort := os.Getenv("VXLAN_PORT")
25+
os.Setenv("VXLAN_PORT", "")
2326
func() {
2427
defer func() {
2528
if r := recover(); r == nil {
@@ -30,14 +33,37 @@ func TestFetchCitrixNodeControllerInput(t *testing.T) {
3033
FetchCitrixNodeControllerInput()
3134
}()
3235

36+
if IngressDeviceIP == "" {
37+
IngressDeviceIP = "127.0.0.1"
38+
}
3339
os.Setenv("NS_IP", IngressDeviceIP)
34-
os.Setenv("NS_VTEP_MAC", IngressDeviceVtepMAC)
35-
os.Setenv("NS_LOGIN", IngressDeviceUsername)
40+
41+
if IngressDeviceUsername == "" {
42+
IngressDeviceUsername = "test"
43+
}
44+
os.Setenv("NS_USER", IngressDeviceUsername)
45+
if IngressDevicePassword == "" {
46+
IngressDevicePassword = "test"
47+
}
3648
os.Setenv("NS_PASSWORD", IngressDevicePassword)
37-
os.Setenv("NS_SNIP", IngressDeviceVtepIP)
49+
if IngressDeviceVtepIP == "" {
50+
IngressDeviceVtepIP = "127.0.0.1"
51+
}
52+
os.Setenv("REMOTE_VTEPIP", IngressDeviceVtepIP)
53+
if Vnid == "" {
54+
Vnid = "999"
55+
}
56+
os.Setenv("VNID", Vnid)
57+
if Network == "" {
58+
Network = "192.128.1.0/24"
59+
}
60+
os.Setenv("NETWORK", Network)
61+
if VxlanPort == "" {
62+
VxlanPort = "8999"
63+
}
64+
os.Setenv("VXLAN_PORT", VxlanPort)
3865
FetchCitrixNodeControllerInput()
3966
}
40-
4167
func TestWaitForConfigMapInput(t *testing.T) {
4268
input, _, api := getClientAndDeviceInfo()
4369
api.Client.CoreV1().ConfigMaps("citrix").Create(&v1.ConfigMap{
@@ -47,13 +73,6 @@ func TestWaitForConfigMapInput(t *testing.T) {
4773
WaitForConfigMapInput(api, input)
4874
}
4975

50-
/*
51-
func TestMonitorIngressDevice(t *testing.T){
52-
controllerInput := FetchCitrixNodeControllerInput()
53-
ingressDevice := createIngressDeviceClient(controllerInput)
54-
55-
MonitorIngressDevice(ingressDevice, controllerInput)
56-
}*/
5776
func TestIsValidIP4(t *testing.T) {
5877
assert := assert.New(t)
5978
assert.Equal(false, IsValidIP4("333.22.1.1"), "Invalid IP")
@@ -64,5 +83,25 @@ func TestIsValidIP4(t *testing.T) {
6483
assert.Equal(false, IsValidIP4("22.1.abc.1"), "Invalid IP")
6584
assert.Equal(false, IsValidIP4("22.1.1.abc"), "Invalid IP")
6685
assert.Equal(false, IsValidIP4("22.1.1.1111"), "Invalid IP")
67-
assert.Equal(true, IsValidIP4("22.1.1.1"), "Invalid IP")
86+
assert.Equal(true, IsValidIP4("22.1.1.1"), "Valid IP")
87+
}
88+
89+
func TestIsValidVxlanPort(t *testing.T) {
90+
assert := assert.New(t)
91+
assert.Equal(true, IsValidVxlanPort(1), "Valid Port")
92+
assert.Equal(true, IsValidVxlanPort(65535), "Valid Port")
93+
assert.Equal(true, IsValidVxlanPort(100), "Valid Port")
94+
assert.Equal(false, IsValidVxlanPort(65537), "Invalid Port")
95+
assert.Equal(false, IsValidVxlanPort(0), "Invalid Port")
96+
assert.Equal(false, IsValidVxlanPort(-1), "Invalid Port")
97+
}
98+
99+
func TestIsValidVxlanID(t *testing.T) {
100+
assert := assert.New(t)
101+
assert.Equal(true, IsValidVxlanID(1), "Valid VNID")
102+
assert.Equal(true, IsValidVxlanID(16777215), "Valid VNID")
103+
assert.Equal(true, IsValidVxlanID(100), "Valid VNID")
104+
assert.Equal(false, IsValidVxlanID(16777216), "Invalid VNID")
105+
assert.Equal(false, IsValidVxlanID(0), "Invalid VNID")
106+
assert.Equal(false, IsValidVxlanID(-1), "Invalid VNID")
68107
}

cmd/citrix-node-controller/k8sInterface.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/binary"
55
"fmt"
66
"k8s.io/api/core/v1"
7-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
87
"k8s.io/apimachinery/pkg/fields"
98
"k8s.io/client-go/kubernetes"
109
restclient "k8s.io/client-go/rest"
@@ -109,17 +108,6 @@ func ConfigMapInputWatcher(api *KubernetesAPIServer, IngressDeviceClient *NitroC
109108
select {}
110109
}
111110

112-
func CheckAndWaitForNetscalerInit(ControllerInputObj *ControllerInput) {
113-
if (ControllerInputObj.State & NetscalerInit) != NetscalerInit {
114-
klog.Info("[DEBUG] Waiting for NetScaler initialization to complete")
115-
}
116-
for {
117-
if (ControllerInputObj.State & NetscalerInit) == NetscalerInit {
118-
break
119-
}
120-
}
121-
}
122-
123111
func HandleConfigMapUpdateEvent(api *KubernetesAPIServer, obj interface{}, newobj interface{}, IngressDeviceClient *NitroClient, ControllerInputObj *ControllerInput) {
124112
node := new(Node)
125113
ConfigMapDataNew := make(map[string]string)
@@ -203,17 +191,6 @@ func HandleConfigMapAddEvent(api *KubernetesAPIServer, obj interface{}, IngressD
203191
}
204192
}
205193

206-
func CLeanupHandler(api *KubernetesAPIServer, DaemonSet string, namespace string) {
207-
ds, err := api.Client.AppsV1().DaemonSets(namespace).Get(DaemonSet, metav1.GetOptions{})
208-
if ds != nil {
209-
falseVar := false
210-
deleteOptions := &metav1.DeleteOptions{OrphanDependents: &falseVar}
211-
err = api.Client.AppsV1().DaemonSets(namespace).Delete(ds.Name, deleteOptions)
212-
}
213-
if err != nil {
214-
fmt.Print(err)
215-
}
216-
}
217194

218195
func HandleConfigMapDeleteEvent(api *KubernetesAPIServer, obj interface{}, IngressDeviceClient *NitroClient, ControllerInputObj *ControllerInput) {
219196
ControllerInputObj.CncOperation = "DELETE"

cmd/citrix-node-controller/k8sInterface_test.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,52 @@ func TestConfigDecider(t *testing.T) {
6363
}
6464
func TestHandleConfigMapAddEvent(t *testing.T) {
6565
input, obj, api := getClientAndDeviceInfo()
66-
HandleConfigMapAddEvent(api, obj, obj, input)
67-
66+
api.Client.CoreV1().ConfigMaps("kube-system").Create(&v1.ConfigMap{
67+
ObjectMeta: metav1.ObjectMeta{Name: "kube-chorus-router"},
68+
Data: map[string]string{"EndpointIP": "1.1.1.1", "CNI-1.1.1.1": "192.168.84.64/26", "Node-1.1.1.1": "1.1.1.1", "Interface-1.1.1.1": "20.20.1.1"},
69+
})
70+
configobj, _ := api.Client.CoreV1().ConfigMaps("kube-system").Get("kube-chorus-router", metav1.GetOptions{})
71+
HandleConfigMapAddEvent(api, configobj, obj, input)
72+
api.Client.CoreV1().ConfigMaps("default").Create(&v1.ConfigMap{
73+
ObjectMeta: metav1.ObjectMeta{Name: "citrix-node-controller"},
74+
Data: map[string]string{"Operation": "ADD"},
75+
})
76+
configobj, _ = api.Client.CoreV1().ConfigMaps("default").Get("citrix-node-controller", metav1.GetOptions{})
77+
HandleConfigMapAddEvent(api, configobj, obj, input)
6878
}
6979
func TestHandleConfigMapDeleteEvent(t *testing.T) {
7080
input, obj, api := getClientAndDeviceInfo()
71-
api.Client.CoreV1().ConfigMaps("citrix").Create(&v1.ConfigMap{
72-
ObjectMeta: metav1.ObjectMeta{Name: "citrix-node-controller"},
73-
Data: map[string]string{"Operation": "ADD"},
81+
api.Client.CoreV1().ConfigMaps("kube-system").Create(&v1.ConfigMap{
82+
ObjectMeta: metav1.ObjectMeta{Name: "kube-chorus-router"},
83+
Data: map[string]string{"EndpointIP": "1.1.1.1", "CNI-1.1.1.1": "192.168.84.64/26", "Node-1.1.1.1": "1.1.1.1", "Interface-1.1.1.1": "20.20.1.1"},
7484
})
75-
configobj, _ := api.Client.CoreV1().ConfigMaps("citrix").Get("citrix-node-controller", metav1.GetOptions{})
85+
configobj, _ := api.Client.CoreV1().ConfigMaps("kube-system").Get("kube-chorus-router", metav1.GetOptions{})
7686
input.State = NetscalerInit
7787
HandleConfigMapDeleteEvent(api, configobj, obj, input)
88+
api.Client.CoreV1().ConfigMaps("default").Create(&v1.ConfigMap{
89+
ObjectMeta: metav1.ObjectMeta{Name: "citrix-node-controller"},
90+
Data: map[string]string{"Operation": "DELETE"},
91+
})
92+
configobj, _ = api.Client.CoreV1().ConfigMaps("default").Get("citrix-node-controller", metav1.GetOptions{})
93+
HandleConfigMapDeleteEvent(api, configobj, obj, input)
7894
}
7995
func TestHandleConfigMapUpdateEvent(t *testing.T) {
8096
input, obj, api := getClientAndDeviceInfo()
81-
api.Client.CoreV1().ConfigMaps("citrix").Create(&v1.ConfigMap{
82-
ObjectMeta: metav1.ObjectMeta{Name: "citrix-node-controller"},
83-
Data: map[string]string{"Operation": "ADD"},
97+
api.Client.CoreV1().ConfigMaps("kube-system").Create(&v1.ConfigMap{
98+
ObjectMeta: metav1.ObjectMeta{Name: "kube-chorus-router"},
99+
Data: map[string]string{"EndpointIP": "1.1.1.1", "CNI-1.1.1.1": "192.168.84.64/26", "Node-1.1.1.1": "1.1.1.1", "Interface-1.1.1.1": "20.20.1.1"},
100+
})
101+
configobj, _ := api.Client.CoreV1().ConfigMaps("kube-system").Get("kube-chorus-router", metav1.GetOptions{})
102+
falseVar := false
103+
deleteOptions := &metav1.DeleteOptions{OrphanDependents: &falseVar}
104+
api.Client.CoreV1().ConfigMaps("kube-system").Delete("kube-chorus-router", deleteOptions)
105+
api.Client.CoreV1().ConfigMaps("kube-system").Create(&v1.ConfigMap{
106+
ObjectMeta: metav1.ObjectMeta{Name: "kube-chorus-router"},
107+
Data: map[string]string{"EndpointIP": "1.1.1.1", "CNI-1.1.1.1": "192.168.84.64/26", "Node-1.1.1.1": "1.1.1.1", "Interface-1.1.1.1": "20.20.1.1", "CNI-2.2.2.2": "192.168.84.64/26", "Node-2.2.2.2": "2.2.2.2", "Interface-2.2.2.2": "30.30.30.30"},
84108
})
85-
configobj, _ := api.Client.CoreV1().ConfigMaps("citrix").Get("citrix-node-controller", metav1.GetOptions{})
86-
HandleConfigMapUpdateEvent(api, configobj, configobj, obj, input)
109+
newconfigobj, _ := api.Client.CoreV1().ConfigMaps("kube-system").Get("kube-chorus-router", metav1.GetOptions{})
110+
CreateK8sApiserverClient()
111+
HandleConfigMapUpdateEvent(api, configobj, newconfigobj, obj, input)
112+
HandleConfigMapUpdateEvent(api, newconfigobj, configobj, obj, input)
113+
87114
}

cmd/citrix-node-controller/netScalerInterface_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
//"net/http/httptest"
99
//"net/http/httptest"
1010
"context"
11+
"encoding/json"
1112
"github.com/stretchr/testify/assert"
1213
"net/http"
1314
"net/http/httptest"
@@ -127,11 +128,18 @@ func testingHTTPClient(handler http.Handler) (*http.Client, func()) {
127128
return cli, s.Close
128129
}
129130
func TestGetPrimaryNodeIP(t *testing.T) {
131+
data := make(map[string]string)
132+
data["hanode"] = "1.1.1.1"
133+
newdata, err := json.Marshal(data)
134+
if err != nil {
135+
panic(err)
136+
}
130137
_, nitro, _ := getClientAndDeviceInfo()
131138
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
132139
assert.Equal(t, "key", r.Header.Get("Key"))
133140
assert.Equal(t, "secret", r.Header.Get("Secret"))
134-
w.Write([]byte("okResponse"))
141+
w.WriteHeader(200)
142+
w.Write([]byte(newdata))
135143
})
136144
httpClient, teardown := testingHTTPClient(h)
137145
defer teardown()
@@ -145,7 +153,8 @@ func TestGetClusterInterfaceMac(t *testing.T) {
145153
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
146154
assert.Equal(t, "key", r.Header.Get("Key"))
147155
assert.Equal(t, "secret", r.Header.Get("Secret"))
148-
w.Write([]byte("okResponse"))
156+
w.WriteHeader(200)
157+
w.Write([]byte("body"))
149158
})
150159
httpClient, teardown := testingHTTPClient(h)
151160
defer teardown()
@@ -158,6 +167,7 @@ func TestGetInterfaceMac(t *testing.T) {
158167
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
159168
assert.Equal(t, "key", r.Header.Get("Key"))
160169
assert.Equal(t, "secret", r.Header.Get("Secret"))
170+
w.WriteHeader(200)
161171
w.Write([]byte("okResponse"))
162172
})
163173
httpClient, teardown := testingHTTPClient(h)
@@ -171,6 +181,7 @@ func TestGetDefaultDatewayInterface(t *testing.T) {
171181
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
172182
assert.Equal(t, "key", r.Header.Get("Key"))
173183
assert.Equal(t, "secret", r.Header.Get("Secret"))
184+
w.WriteHeader(200)
174185
w.Write([]byte("okResponse"))
175186
})
176187
httpClient, teardown := testingHTTPClient(h)
@@ -184,6 +195,7 @@ func TestNsInterfaceConfig(t *testing.T) {
184195
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
185196
assert.Equal(t, "key", r.Header.Get("Key"))
186197
assert.Equal(t, "secret", r.Header.Get("Secret"))
198+
w.WriteHeader(200)
187199
w.Write([]byte("okResponse"))
188200
})
189201
httpClient, teardown := testingHTTPClient(h)

deploy/citrix-k8s-node-controller.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ spec:
8080
- name: NETWORK
8181
value: "192.168.1.0/24"
8282
- name: REMOTE_VTEPIP
83-
value: "10.102.33.209"
83+
value: "xx.xx.xx.xx"
8484
- name: VXLAN_PORT
8585
value: "8472"
8686
- name: VNID

0 commit comments

Comments
 (0)