...

Source file src/github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/chaos/stresschaos/cpu.go

Documentation: github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/chaos/stresschaos

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    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  		// diff means the increasing CPU time (in nanosecond)
    58  		// just pick two threshold, 5e8 is a little shorter than one second
    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  		// diff means the increasing CPU time (in nanosecond)
    84  		// just pick two threshold, they are both much shorter than 1 second
    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