...

Source file src/github.com/chaos-mesh/chaos-mesh/test/e2e/chaos/podchaos/pod_kill.go

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

     1  // Copyright 2020 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package podchaos
    15  
    16  import (
    17  	"context"
    18  	"time"
    19  
    20  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    21  	"github.com/chaos-mesh/chaos-mesh/test/e2e/util"
    22  	"github.com/chaos-mesh/chaos-mesh/test/pkg/fixture"
    23  
    24  	corev1 "k8s.io/api/core/v1"
    25  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/labels"
    28  	"k8s.io/apimachinery/pkg/types"
    29  	"k8s.io/apimachinery/pkg/util/wait"
    30  	"k8s.io/client-go/kubernetes"
    31  	"k8s.io/kubernetes/test/e2e/framework"
    32  	"k8s.io/utils/pointer"
    33  	"sigs.k8s.io/controller-runtime/pkg/client"
    34  )
    35  
    36  func TestcasePodKillOnceThenDelete(ns string, kubeCli kubernetes.Interface, cli client.Client) {
    37  	ctx, cancel := context.WithCancel(context.Background())
    38  	defer cancel()
    39  
    40  	pod := fixture.NewCommonNginxPod("nginx", ns)
    41  	_, err := kubeCli.CoreV1().Pods(ns).Create(pod)
    42  	framework.ExpectNoError(err, "create nginx pod error")
    43  	err = waitPodRunning("nginx", ns, kubeCli)
    44  	framework.ExpectNoError(err, "wait nginx running error")
    45  
    46  	podKillChaos := &v1alpha1.PodChaos{
    47  		ObjectMeta: metav1.ObjectMeta{
    48  			Name:      "nginx-kill",
    49  			Namespace: ns,
    50  		},
    51  		Spec: v1alpha1.PodChaosSpec{
    52  			Selector: v1alpha1.SelectorSpec{
    53  				Namespaces: []string{
    54  					ns,
    55  				},
    56  				LabelSelectors: map[string]string{
    57  					"app": "nginx",
    58  				},
    59  			},
    60  			Action: v1alpha1.PodKillAction,
    61  			Mode:   v1alpha1.OnePodMode,
    62  			Scheduler: &v1alpha1.SchedulerSpec{
    63  				Cron: "@every 10s",
    64  			},
    65  		},
    66  	}
    67  	err = cli.Create(ctx, podKillChaos)
    68  	framework.ExpectNoError(err, "create pod chaos error")
    69  
    70  	err = wait.Poll(5*time.Second, 5*time.Minute, func() (done bool, err error) {
    71  		_, err = kubeCli.CoreV1().Pods(ns).Get("nginx", metav1.GetOptions{})
    72  		if err != nil && apierrors.IsNotFound(err) {
    73  			return true, nil
    74  		}
    75  		return false, nil
    76  	})
    77  	framework.ExpectNoError(err, "Pod kill chaos perform failed")
    78  
    79  }
    80  func TestcasePodKillPauseThenUnPause(ns string, kubeCli kubernetes.Interface, cli client.Client) {
    81  	ctx, cancel := context.WithCancel(context.Background())
    82  	defer cancel()
    83  
    84  	nd := fixture.NewCommonNginxDeployment("nginx", ns, 3)
    85  	_, err := kubeCli.AppsV1().Deployments(ns).Create(nd)
    86  	framework.ExpectNoError(err, "create nginx deployment error")
    87  	err = util.WaitDeploymentReady("nginx", ns, kubeCli)
    88  	framework.ExpectNoError(err, "wait nginx deployment ready error")
    89  
    90  	var pods *corev1.PodList
    91  	var newPods *corev1.PodList
    92  	listOption := metav1.ListOptions{
    93  		LabelSelector: labels.SelectorFromSet(map[string]string{
    94  			"app": "nginx",
    95  		}).String(),
    96  	}
    97  	pods, err = kubeCli.CoreV1().Pods(ns).List(listOption)
    98  	framework.ExpectNoError(err, "get nginx pods error")
    99  
   100  	podKillChaos := &v1alpha1.PodChaos{
   101  		ObjectMeta: metav1.ObjectMeta{
   102  			Name:      "nginx-kill",
   103  			Namespace: ns,
   104  		},
   105  		Spec: v1alpha1.PodChaosSpec{
   106  			Selector: v1alpha1.SelectorSpec{
   107  				Namespaces:     []string{ns},
   108  				LabelSelectors: map[string]string{"app": "nginx"},
   109  			},
   110  			Action:   v1alpha1.PodKillAction,
   111  			Mode:     v1alpha1.OnePodMode,
   112  			Duration: pointer.StringPtr("9m"),
   113  			Scheduler: &v1alpha1.SchedulerSpec{
   114  				Cron: "@every 10m",
   115  			},
   116  		},
   117  	}
   118  	err = cli.Create(ctx, podKillChaos)
   119  	framework.ExpectNoError(err, "create pod chaos error")
   120  
   121  	chaosKey := types.NamespacedName{
   122  		Namespace: ns,
   123  		Name:      "nginx-kill",
   124  	}
   125  
   126  	// some pod is killed as expected
   127  	err = wait.Poll(5*time.Second, 5*time.Minute, func() (done bool, err error) {
   128  		newPods, err = kubeCli.CoreV1().Pods(ns).List(listOption)
   129  		framework.ExpectNoError(err, "get nginx pods error")
   130  		return !fixture.HaveSameUIDs(pods.Items, newPods.Items), nil
   131  	})
   132  	framework.ExpectNoError(err, "wait pod killed failed")
   133  
   134  	// pause experiment
   135  	err = util.PauseChaos(ctx, cli, podKillChaos)
   136  	framework.ExpectNoError(err, "pause chaos error")
   137  
   138  	err = wait.Poll(5*time.Second, 5*time.Minute, func() (done bool, err error) {
   139  		chaos := &v1alpha1.PodChaos{}
   140  		err = cli.Get(ctx, chaosKey, chaos)
   141  		framework.ExpectNoError(err, "get pod chaos error")
   142  		if chaos.Status.Experiment.Phase == v1alpha1.ExperimentPhasePaused {
   143  			return true, nil
   144  		}
   145  		return false, err
   146  	})
   147  	framework.ExpectNoError(err, "check paused chaos failed")
   148  
   149  	// wait for 1 minutes and no pod is killed
   150  	pods, err = kubeCli.CoreV1().Pods(ns).List(listOption)
   151  	framework.ExpectNoError(err, "get nginx pods error")
   152  	err = wait.Poll(5*time.Second, 1*time.Minute, func() (done bool, err error) {
   153  		newPods, err = kubeCli.CoreV1().Pods(ns).List(listOption)
   154  		framework.ExpectNoError(err, "get nginx pods error")
   155  		return !fixture.HaveSameUIDs(pods.Items, newPods.Items), nil
   156  	})
   157  	framework.ExpectError(err, "wait pod not killed failed")
   158  	framework.ExpectEqual(err.Error(), wait.ErrWaitTimeout.Error())
   159  
   160  	// resume experiment
   161  	err = util.UnPauseChaos(ctx, cli, podKillChaos)
   162  	framework.ExpectNoError(err, "resume chaos error")
   163  
   164  	err = wait.Poll(5*time.Second, 5*time.Minute, func() (done bool, err error) {
   165  		chaos := &v1alpha1.PodChaos{}
   166  		err = cli.Get(ctx, chaosKey, chaos)
   167  		framework.ExpectNoError(err, "get pod chaos error")
   168  		if chaos.Status.Experiment.Phase == v1alpha1.ExperimentPhaseRunning {
   169  			return true, nil
   170  		}
   171  		return false, err
   172  	})
   173  	framework.ExpectNoError(err, "check resumed chaos failed")
   174  
   175  	// some pod is killed by resumed experiment
   176  	err = wait.Poll(5*time.Second, 5*time.Minute, func() (done bool, err error) {
   177  		newPods, err = kubeCli.CoreV1().Pods(ns).List(listOption)
   178  		framework.ExpectNoError(err, "get nginx pods error")
   179  		return !fixture.HaveSameUIDs(pods.Items, newPods.Items), nil
   180  	})
   181  	framework.ExpectNoError(err, "wait pod killed failed")
   182  
   183  }
   184