From 01563c08910ddaba4077fd9dc691df541e045165 Mon Sep 17 00:00:00 2001
From: xiadanni <xiadanni1@huawei.com>
Date: Tue, 18 Aug 2020 17:05:32 +0800
Subject: [PATCH 04/16] agent: add IPVS test

Signed-off-by: xiadanni <xiadanni1@huawei.com>
---
 grpc_test.go | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 172 insertions(+)

diff --git a/grpc_test.go b/grpc_test.go
index e69102b..d58c0b6 100644
--- a/grpc_test.go
+++ b/grpc_test.go
@@ -1840,3 +1840,175 @@ func getPipeMaxSize() (uint32, error) {
 	u, err := strconv.ParseUint(s, 10, 32)
 	return uint32(u), err
 }
+
+func TestUpdateIPVSRule(t *testing.T) {
+	assert := assert.New(t)
+
+	// add IPVS rule successfully
+	a := &agentGRPC{
+		sandbox: &sandbox{
+			containers: make(map[string]*container),
+		},
+	}
+
+	req := &pb.UpdateIPVSRequest{
+		IPVSReq: "ipvsadm -A -t 17.2.0.7:80 -s rr -p 3000",
+	}
+
+	_, err := a.UpdateIPVSRule(context.Background(), req)
+	assert.NoError(err)
+
+	// delete ipvs rule successfully
+	req = &pb.UpdateIPVSRequest{
+		IPVSReq: "ipvsadm -D -t 17.2.0.7:80",
+	}
+
+	_, err = a.UpdateIPVSRule(context.Background(), req)
+	assert.NoError(err)
+
+	// update ipvs rule error because exec failed
+	req = &pb.UpdateIPVSRequest{
+		IPVSReq: "ipvsadm -A -t 17.2.0.7:80 -s rr -p -3000",
+	}
+
+	_, err = a.UpdateIPVSRule(context.Background(), req)
+	assert.Error(err)
+	assert.Contains(err.Error(), "exec IPVS command failed")
+
+	// update IPVS rule error because rule less than validHeadLength
+	req = &pb.UpdateIPVSRequest{
+		IPVSReq: "ipvsa",
+	}
+
+	_, err = a.UpdateIPVSRule(context.Background(), req)
+	assert.Error(err)
+	assert.Contains(err.Error(), "invalid IPVS rule")
+
+	// update ipvs rule error because invalid command
+	req = &pb.UpdateIPVSRequest{
+		IPVSReq: "abcabcabc ipvsadm",
+	}
+
+	_, err = a.UpdateIPVSRule(context.Background(), req)
+	assert.Error(err)
+	assert.Contains(err.Error(), "invalid IPVS rule")
+
+	// add ipvs rule error because rule count exceeds
+	a = &agentGRPC{
+		sandbox: &sandbox{
+			containers: make(map[string]*container),
+			ipvsadm: ipvsAdm{
+				ipvsRuleCnt: 20000,
+			},
+		},
+	}
+
+	req = &pb.UpdateIPVSRequest{
+		IPVSReq: "ipvsadm -A -t 17.2.0.7:80 -s rr -p 3000",
+	}
+	_, err = a.UpdateIPVSRule(context.Background(), req)
+	assert.Error(err)
+	assert.Errorf(err, "rules exceed limit")
+
+	// add ipvs rule error because ipvs request item less than 2
+	a = &agentGRPC{
+		sandbox: &sandbox{
+			containers: make(map[string]*container),
+			ipvsadm: ipvsAdm{
+				ipvsRuleCnt: 0,
+			},
+		},
+	}
+
+	req = &pb.UpdateIPVSRequest{
+		IPVSReq: "ipvsadm",
+	}
+
+	_, err = a.UpdateIPVSRule(context.Background(), req)
+	assert.Error(err)
+	assert.Contains(err.Error(), "invalid IPVS rule")
+
+	// add ipvs rule error because ipvs rule nil
+	req = nil
+
+	_, err = a.UpdateIPVSRule(context.Background(), req)
+	assert.Error(err)
+	assert.Contains(err.Error(), "IPVS rule is nil")
+
+	// add ipvs rule error because ipvs rule string is empty
+	req = &pb.UpdateIPVSRequest{
+		IPVSReq: "",
+	}
+
+	_, err = a.UpdateIPVSRule(context.Background(), req)
+	assert.Error(err)
+	assert.Contains(err.Error(), "IPVS rule is nil")
+
+	// restore ipvs rule successfully
+	a = &agentGRPC{
+		sandbox: &sandbox{
+			containers: make(map[string]*container),
+		},
+	}
+
+	req = &pb.UpdateIPVSRequest{
+		IPVSReq: "restore|2|-A -t 10.10.11.12:100 -s rr -p 3000\n-a -t 10.10.11.12:100 -r 172.16.0.1:80 -m",
+	}
+
+	_, err = a.UpdateIPVSRule(context.Background(), req)
+	assert.NoError(err)
+
+	// clear IPVS rule successfully
+	req = &pb.UpdateIPVSRequest{
+		IPVSReq: "ipvsadm -C",
+	}
+
+	_, err = a.UpdateIPVSRule(context.Background(), req)
+	assert.NoError(err)
+	assert.Equal(a.sandbox.ipvsadm.ipvsRuleCnt, uint64(0))
+
+	// restore ipvs rule error because rule count invalid
+	req = &pb.UpdateIPVSRequest{
+		IPVSReq: "restore|abc|-A -t 10.10.11.12:100 -s rr -p 3000\n-a -t 10.10.11.12:100 -r 172.16.0.1:80 -m",
+	}
+
+	_, err = a.UpdateIPVSRule(context.Background(), req)
+	assert.Error(err)
+	assert.Contains(err.Error(), "invalid IPVS rule")
+
+	// restore ipvs rule error because other rules exists
+	a = &agentGRPC{
+		sandbox: &sandbox{
+			containers: make(map[string]*container),
+			ipvsadm: ipvsAdm{
+				ipvsRuleCnt: 5,
+			},
+		},
+	}
+
+	req = &pb.UpdateIPVSRequest{
+		IPVSReq: "restore|2|-A -t 10.10.11.12:100 -s rr -p 3000\n-a -t 10.10.11.12:100 -r 172.16.0.1:80 -m",
+	}
+
+	_, err = a.UpdateIPVSRule(context.Background(), req)
+	assert.Error(err)
+	assert.Contains(err.Error(), "exist some rules in system")
+
+	// restore ipvs rule error because ipvs req item less than 3
+	a = &agentGRPC{
+		sandbox: &sandbox{
+			containers: make(map[string]*container),
+			ipvsadm: ipvsAdm{
+				ipvsRuleCnt: 0,
+			},
+		},
+	}
+
+	req = &pb.UpdateIPVSRequest{
+		IPVSReq: "restore|-A -t 10.10.11.12:100 -s rr -p 3000\n-a -t 10.10.11.12:100 -r 172.16.0.1:80 -m",
+	}
+
+	_, err = a.UpdateIPVSRule(context.Background(), req)
+	assert.Error(err)
+	assert.Contains(err.Error(), "invalid IPVS rule")
+}
-- 
2.14.3 (Apple Git-98)

