Skip to content

Commit

Permalink
Add the test case P00002 about default gateway
Browse files Browse the repository at this point in the history
Signed-off-by: bzsuni <[email protected]>
  • Loading branch information
bzsuni committed Dec 6, 2023
1 parent 30001c6 commit a8efa61
Showing 1 changed file with 81 additions and 15 deletions.
96 changes: 81 additions & 15 deletions test/e2e/egresspolicy/egresspolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/spidernet-io/egressgateway/test/e2e/common"
)

var _ = Describe("EgressPolicy", Ordered, func() {
var _ = Describe("EgressPolicy", Ordered, Label("xxz"), func() {
var egw *egressv1.EgressGateway

BeforeAll(func() {
Expand All @@ -42,8 +42,6 @@ var _ = Describe("EgressPolicy", Ordered, func() {
DeferCleanup(func() {
// delete EgressGateway
if egw != nil {
// todo @bzsuni waiting finalizer-feature to be done
time.Sleep(time.Second * 3)
err = common.DeleteObj(ctx, cli, egw)
Expect(err).NotTo(HaveOccurred())
}
Expand Down Expand Up @@ -331,8 +329,7 @@ var _ = Describe("EgressPolicy", Ordered, func() {
egcp.Spec.AppliedTo.PodSubnet = &[]string{"10.10.0.0/16"}
egcp.Spec.AppliedTo.PodSelector = &metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}}
}),
// todo @bzsuni waiting for the bug be fixed
PEntry("should fail when the `Spec.EgressIP.UseNodeIP` of the cluster-policy is set to true and the Spec.EgressIP is not empty", Label("P00017"), true,
Entry("should fail when the `Spec.EgressIP.UseNodeIP` of the cluster-policy is set to true and the Spec.EgressIP is not empty", Label("P00017"), true,
func(egcp *egressv1.EgressClusterPolicy) {
egcp.Spec.EgressGatewayName = egw.Name
egcp.Spec.AppliedTo.PodSubnet = &[]string{"10.10.0.0/16"}
Expand Down Expand Up @@ -458,7 +455,6 @@ var _ = Describe("EgressPolicy", Ordered, func() {
// update the `NodeSelector` of the gateway to change the match from node1 to node2
GinkgoWriter.Printf("update the gateway: %s, to change the match from node: %s to node: %s\n", egw.Name, node1.Name, node2.Name)
egw.Spec.NodeSelector = node2Selector
// todo @bzsuni waiting for the bug to be fixed
Expect(cli.Update(ctx, egw)).NotTo(HaveOccurred(), fmt.Sprintf("failed to update gateway:\n%s\n", common.GetObjYAML(egw)))
// check if the egressgateway synced successfully
expectStatus = &egressv1.EgressGatewayStatus{
Expand Down Expand Up @@ -512,7 +508,6 @@ var _ = Describe("EgressPolicy", Ordered, func() {
// update the `NodeSelector` of the gateway to change the match from node1 to node2
GinkgoWriter.Printf("update the gateway: %s, to change the match from node: %s to node: %s\n", egw.Name, node1.Name, node2.Name)
egw.Spec.NodeSelector = node2Selector
// todo @bzsuni waiting for the bug to be fixed
Expect(cli.Update(ctx, egw)).NotTo(HaveOccurred(), fmt.Sprintf("failed to update gateway:\n%s\n", common.GetObjYAML(egw)))
// check if the egressgateway synced successfully
expectStatus = &egressv1.EgressGatewayStatus{
Expand Down Expand Up @@ -584,8 +579,6 @@ var _ = Describe("EgressPolicy", Ordered, func() {

// delete egw
if egw1 != nil {
// todo @bzsuni waiting for the finalizer-feature to be done
time.Sleep(time.Second * 2)
GinkgoWriter.Printf("Delete egw: %s\n", egw1.Name)
Expect(common.DeleteObj(ctx, cli, egw1)).NotTo(HaveOccurred())
}
Expand Down Expand Up @@ -640,17 +633,20 @@ var _ = Describe("EgressPolicy", Ordered, func() {
Expect(cli.Update(ctx, cpEgp)).To(HaveOccurred())
})

// todo @bzsuni waiting for the bug to be fixed
PIt("cluster-level policy", func() {
It("cluster-level policy", func() {
// create EgressClusterPolicy
newEgressGateway := new(egressv1.EgressGateway)
err := cli.Get(ctx, types.NamespacedName{Name: egw1.Name}, newEgressGateway)
Expect(err).NotTo(HaveOccurred())

egcp, err = common.CreateEgressClusterPolicyCustom(ctx, cli,
func(egcp *egressv1.EgressClusterPolicy) {
egcp.Spec.EgressGatewayName = egw1.Name
if egressConfig.EnableIPv4 {
egcp.Spec.EgressIP.IPv4 = pool.IPv4[0]
egcp.Spec.EgressIP.IPv4 = newEgressGateway.Spec.Ippools.Ipv4DefaultEIP
}
if egressConfig.EnableIPv6 {
egcp.Spec.EgressIP.IPv6 = pool.IPv6[0]
egcp.Spec.EgressIP.IPv6 = newEgressGateway.Spec.Ippools.Ipv6DefaultEIP
}
egcp.Spec.AppliedTo.PodSubnet = &[]string{"10.10.0.0/18"}
})
Expand All @@ -660,10 +656,20 @@ var _ = Describe("EgressPolicy", Ordered, func() {
cpEgcp := egcp.DeepCopy()
// edit policy Spec.EgressIP.IPv4 and Spec.EgressIP.IPv6
if egressConfig.EnableIPv4 {
egcp.Spec.EgressIP.IPv4 = pool.IPv4[1]
for _, val := range pool.IPv4 {
if val != egcp.Spec.EgressIP.IPv4 {
egcp.Spec.EgressIP.IPv4 = val
break
}
}
}
if egressConfig.EnableIPv6 {
egcp.Spec.EgressIP.IPv6 = pool.IPv6[1]
for _, val := range pool.IPv6 {
if val != egcp.Spec.EgressIP.IPv6 {
egcp.Spec.EgressIP.IPv6 = val
break
}
}
}
// update policy EgressIP.IPv4 or EgressIP.IPv6
Expect(cli.Update(ctx, egcp)).To(HaveOccurred())
Expand Down Expand Up @@ -774,4 +780,64 @@ var _ = Describe("EgressPolicy", Ordered, func() {
}
})
})

/*
此用例测试创建 policy 时,使用默认 gateway 的场景
1. 创建集群级别 gateway
2. 创建 policy, clusterPolicy 不指定 gatewayName
3. policy 和 clusterPolicy 可以创建成功,并且 spec.egressGatewayName 为集群默认 gateway,并校验状态正确
4. 创建 命名空间级别的默认 gateway,在此命名空间中创建 policy 不指定 gatewayName, 期望使用此命名空间的默认 gateway,并校验状态正确
*/
Context("Test cluster-level default-egressgateway and namesapce-level default-egressgateway", func() {
var ctx context.Context
var err error
// gateway
var (
defaultClusterEgw *egressv1.EgressGateway
)
// policy
var (
// egp *egressv1.EgressPolicy
egcp *egressv1.EgressClusterPolicy
)
// label
var (
podLabels map[string]string
)

BeforeEach(func() {
ctx = context.TODO()
podLabels = map[string]string{"app": uuid.NewString()}

// create EgressGateway
pool, err := common.GenIPPools(ctx, cli, egressConfig.EnableIPv4, egressConfig.EnableIPv6, 3, 1)
Expect(err).NotTo(HaveOccurred())
nodeSelector := egressv1.NodeSelector{Selector: &metav1.LabelSelector{MatchLabels: nodeLabel}}

defaultClusterEgw, err = common.CreateGatewayNew(ctx, cli, "egw-"+uuid.NewString(), pool, nodeSelector)
Expect(err).NotTo(HaveOccurred())
GinkgoWriter.Printf("Create EgressGateway: %s\n", egw.Name)

// create cluster-level egressgateway
defaultClusterEgw = new(egressv1.EgressGateway)
cli.Get(ctx, types.NamespacedName{Name: egw.Name}, defaultClusterEgw)

Check failure on line 823 in test/e2e/egresspolicy/egresspolicy_test.go

View workflow job for this annotation

GitHub Actions / lint-golang

Error return value of `cli.Get` is not checked (errcheck)
defaultClusterEgw.Spec.ClusterDefault = true
Expect(cli.Update(ctx, defaultClusterEgw)).NotTo(HaveOccurred())
GinkgoWriter.Printf("succeeded to create cluster default gateway: %s\n", defaultClusterEgw.Name)

DeferCleanup(func() {
// delete the policy if it exists

})

})

It("cluster-level policy", Label("P00002"), func() {
// create cluster-level policy
egcp, err = common.CreateEgressClusterPolicy(ctx, cli, egressConfig, "", podLabels)
Expect(err).NotTo(HaveOccurred())
GinkgoWriter.Printf("succeeded to create cluster-level policy: %s\n", egcp.Name)
Expect(egcp.Spec.EgressGatewayName).To(BeEquivalentTo(defaultClusterEgw.Name))
})
})
})

0 comments on commit a8efa61

Please sign in to comment.