...

Source file src/github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/chaos/stresschaos/memory.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  	"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  	// it will raise two pod: stress-peer-0 and stress-peer-1, and we only inject StressChaos into stress-peer-0
    39  
    40  	// approximate equality within 5 MBytes (10% of injected 50M memory chaos)
    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