1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package networkchaos
17
18 import (
19 "context"
20 "net/http"
21 "time"
22
23 . "github.com/onsi/ginkgo/v2"
24 corev1 "k8s.io/api/core/v1"
25 "k8s.io/apimachinery/pkg/util/wait"
26 "k8s.io/kubernetes/test/e2e/framework"
27 "sigs.k8s.io/controller-runtime/pkg/client"
28
29 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
30 "github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/util"
31 )
32
33
34
35
36 func TestcasePeersCrossover(
37 ns string,
38 cli client.Client,
39 networkPeers []*corev1.Pod,
40 ports []uint16,
41 c http.Client,
42 ) {
43 ctx, cancel := context.WithCancel(context.Background())
44 defer cancel()
45
46 By("prepare experiment playground")
47 for index := range networkPeers {
48 err := util.WaitE2EHelperReady(c, ports[index])
49
50 framework.ExpectNoError(err, "wait e2e helper ready error")
51 }
52
53 result := probeNetworkCondition(c, networkPeers, ports, false)
54 framework.ExpectEqual(len(result[networkConditionBlocked]), 0)
55 framework.ExpectEqual(len(result[networkConditionSlow]), 0)
56
57 var (
58 testDelayTcParam = v1alpha1.TcParameter{
59 Delay: &v1alpha1.DelaySpec{
60 Latency: "200ms",
61 Correlation: "25",
62 Jitter: "0ms",
63 },
64 }
65 )
66
67 By("injecting network chaos between partition 0 and 1")
68 networkDelay := makeNetworkDelayChaos(
69 ns, "network-chaos-1",
70 map[string]string{"partition": "0"},
71 map[string]string{"partition": "1"},
72 v1alpha1.AllMode,
73 v1alpha1.AllMode,
74 v1alpha1.Both,
75 testDelayTcParam,
76 nil,
77 )
78
79 networkDelay.Spec.Direction = v1alpha1.Both
80
81 By("Injecting delay between partition 0 (peer 0,2) with partition 1 (peer 1,3)")
82 err := cli.Create(ctx, networkDelay.DeepCopy())
83 framework.ExpectNoError(err, "create network chaos error")
84
85 err = wait.Poll(time.Second, 15*time.Second, func() (done bool, err error) {
86 result = probeNetworkCondition(c, networkPeers, ports, false)
87 if len(result[networkConditionBlocked]) != 0 || len(result[networkConditionSlow]) != 4 {
88 return false, nil
89 }
90 return true, nil
91 })
92
93 framework.ExpectNoError(err, "failed to waiting condition for chaos injection")
94 framework.ExpectEqual(len(result[networkConditionBlocked]), 0)
95 framework.ExpectEqual(result[networkConditionSlow], [][]int{{0, 1}, {0, 3}, {1, 2}, {2, 3}})
96
97 By("recover")
98 err = cli.Delete(ctx, networkDelay.DeepCopy())
99 framework.ExpectNoError(err, "delete network chaos error")
100
101 err = wait.Poll(time.Second, 15*time.Second, func() (done bool, err error) {
102 result = probeNetworkCondition(c, networkPeers, ports, false)
103 if len(result[networkConditionBlocked]) != 0 || len(result[networkConditionSlow]) != 0 {
104 return false, nil
105 }
106 return true, nil
107 })
108
109 framework.ExpectNoError(err, "failed to waiting condition for chaos recover")
110 framework.ExpectEqual(len(result[networkConditionBlocked]), 0)
111 framework.ExpectEqual(len(result[networkConditionSlow]), 0)
112
113 }
114