...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package stresschaos
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
30 func TestcaseCPUStressInjectionOnceThenRecover(
31 ns string,
32 cli client.Client,
33 peers []*corev1.Pod,
34 ports []uint16,
35 c http.Client,
36 ) {
37 ctx := context.Background()
38 By("create cpu stress chaos CRD objects")
39 cpuStressChaos := makeCPUStressChaos(ns, "cpu-stress", ns, "stress-peer-0", 1, 100)
40 err := cli.Create(ctx, cpuStressChaos.DeepCopy())
41 framework.ExpectNoError(err, "create stresschaos error")
42
43 lastCPUTime := make([]uint64, 2)
44 diff := make([]uint64, 2)
45 By("waiting for assertion some pods are experiencing cpu stress ")
46 err = wait.Poll(time.Second, 15*time.Second, func() (done bool, err error) {
47 conditions, err := probeStressCondition(c, peers, ports)
48 if err != nil {
49 return false, err
50 }
51
52 diff[0] = conditions[0].CpuTime - lastCPUTime[0]
53 diff[1] = conditions[1].CpuTime - lastCPUTime[1]
54 lastCPUTime[0] = conditions[0].CpuTime
55 lastCPUTime[1] = conditions[1].CpuTime
56 framework.Logf("get CPU: [%d, %d]", diff[0], diff[1])
57
58
59 if diff[0] > 5e8 && diff[1] < 5e6 {
60 return true, nil
61 }
62 return false, nil
63 })
64 framework.ExpectNoError(err, "cpu stress failed")
65 By("delete pod failure chaos CRD objects")
66
67 err = cli.Delete(ctx, cpuStressChaos.DeepCopy())
68 framework.ExpectNoError(err, "delete stresschaos error")
69 By("waiting for assertion recovering")
70 lastCPUTime = make([]uint64, 2)
71 diff = make([]uint64, 2)
72 err = wait.Poll(time.Second, 15*time.Second, func() (done bool, err error) {
73 conditions, err := probeStressCondition(c, peers, ports)
74 if err != nil {
75 return false, err
76 }
77
78 diff[0] = conditions[0].CpuTime - lastCPUTime[0]
79 diff[1] = conditions[1].CpuTime - lastCPUTime[1]
80 lastCPUTime[0] = conditions[0].CpuTime
81 lastCPUTime[1] = conditions[1].CpuTime
82 framework.Logf("get CPU: [%d, %d]", diff[0], diff[1])
83
84
85 if diff[0] < 1e7 && diff[1] < 5e6 {
86 return true, nil
87 }
88 return false, nil
89 })
90 framework.ExpectNoError(err, "fail to recover from cpu stress")
91 }
92