...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package stresschaos
17
18 import (
19 "context"
20 "fmt"
21 "net/http"
22 "time"
23
24 . "github.com/onsi/ginkgo/v2"
25 corev1 "k8s.io/api/core/v1"
26 "k8s.io/apimachinery/pkg/util/wait"
27 "k8s.io/kubernetes/test/e2e/framework"
28 "sigs.k8s.io/controller-runtime/pkg/client"
29 )
30
31 func TestcaseMemoryStressInjectionOnceThenRecover(
32 ns string,
33 cli client.Client,
34 peers []*corev1.Pod,
35 ports []uint16,
36 c http.Client,
37 ) {
38
39
40
41 const allowedJitter = 5 * 1024 * 1024
42
43 ctx := context.Background()
44 By("create memory stress chaos CRD objects")
45 memoryStressChaos := makeMemoryStressChaos(ns, "memory-stress", ns, "stress-peer-0", "50M", 1)
46 err := cli.Create(ctx, memoryStressChaos.DeepCopy())
47 framework.ExpectNoError(err, "create stresschaos error")
48
49 By("waiting for assertion some pods are experiencing memory stress ")
50 err = wait.Poll(time.Second, 15*time.Second, func() (done bool, err error) {
51 conditions, err := probeStressCondition(c, peers, ports)
52 if err != nil {
53 return false, err
54 }
55 framework.Logf("get Memory: [%d, %d]", conditions[0].MemoryUsage, conditions[1].MemoryUsage)
56 if int(conditions[0].MemoryUsage)-int(conditions[1].MemoryUsage) > allowedJitter {
57 return true, nil
58 }
59 return false, nil
60 })
61 framework.ExpectNoError(err, "memory stress failed")
62 By("delete pod failure chaos CRD objects")
63
64 err = cli.Delete(ctx, memoryStressChaos.DeepCopy())
65 framework.ExpectNoError(err, "delete stresschaos error")
66 By("waiting for assertion recovering")
67 err = wait.Poll(time.Second, 15*time.Second, func() (done bool, err error) {
68 conditions, err := probeStressCondition(c, peers, ports)
69 if err != nil {
70 return false, err
71 }
72 By(fmt.Sprintf("get Memory: [%d, %d]", conditions[0].MemoryUsage, conditions[1].MemoryUsage))
73 if conditions[0].MemoryUsage < conditions[1].MemoryUsage+allowedJitter {
74 return true, nil
75 }
76 return false, nil
77 })
78 framework.ExpectNoError(err, "fail to recover from memory stress")
79 }
80