...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/common/desiredphase_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/common

     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 common
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/types"
    26  	"k8s.io/apimachinery/pkg/util/wait"
    27  	"k8s.io/client-go/util/retry"
    28  
    29  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    30  )
    31  
    32  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    33  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    34  
    35  var _ = Describe("Schedule", func() {
    36  
    37  	BeforeEach(func() {
    38  		// Add any setup steps that needs to be executed before each test
    39  	})
    40  
    41  	AfterEach(func() {
    42  		// Add any teardown steps that needs to be executed after each test
    43  	})
    44  
    45  	Context("Setting phase", func() {
    46  		It("should set phase to running", func() {
    47  			key := types.NamespacedName{
    48  				Name:      "foo1",
    49  				Namespace: "default",
    50  			}
    51  			duration := "10s"
    52  			chaos := &v1alpha1.TimeChaos{
    53  				ObjectMeta: metav1.ObjectMeta{
    54  					Name:      "foo1",
    55  					Namespace: "default",
    56  				},
    57  				Spec: v1alpha1.TimeChaosSpec{
    58  					TimeOffset: "100ms",
    59  					ClockIds:   []string{"CLOCK_REALTIME"},
    60  					Duration:   &duration,
    61  					ContainerSelector: v1alpha1.ContainerSelector{
    62  						PodSelector: v1alpha1.PodSelector{
    63  							Mode: v1alpha1.OneMode,
    64  						},
    65  					},
    66  				},
    67  			}
    68  
    69  			By("creating a chaos")
    70  			{
    71  				Expect(k8sClient.Create(context.TODO(), chaos)).To(Succeed())
    72  			}
    73  
    74  			By("Reconciling desired phase")
    75  			{
    76  				err := wait.Poll(time.Second*1, time.Second*10, func() (ok bool, err error) {
    77  					err = k8sClient.Get(context.TODO(), key, chaos)
    78  					if err != nil {
    79  						return false, err
    80  					}
    81  					return chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.RunningPhase, nil
    82  				})
    83  				Expect(err).ToNot(HaveOccurred())
    84  				err = wait.Poll(time.Second*1, time.Second*10, func() (ok bool, err error) {
    85  					err = k8sClient.Get(context.TODO(), key, chaos)
    86  					if err != nil {
    87  						return false, err
    88  					}
    89  					return chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.StoppedPhase, nil
    90  				})
    91  				Expect(err).ToNot(HaveOccurred())
    92  			}
    93  
    94  			By("deleting the created object")
    95  			{
    96  				Expect(k8sClient.Delete(context.TODO(), chaos)).To(Succeed())
    97  			}
    98  		})
    99  		It("should stop paused chaos", func() {
   100  			key := types.NamespacedName{
   101  				Name:      "foo2",
   102  				Namespace: "default",
   103  			}
   104  			duration := "1000s"
   105  			chaos := &v1alpha1.TimeChaos{
   106  				ObjectMeta: metav1.ObjectMeta{
   107  					Name:      "foo2",
   108  					Namespace: "default",
   109  				},
   110  				Spec: v1alpha1.TimeChaosSpec{
   111  					TimeOffset: "100ms",
   112  					ClockIds:   []string{"CLOCK_REALTIME"},
   113  					Duration:   &duration,
   114  					ContainerSelector: v1alpha1.ContainerSelector{
   115  						PodSelector: v1alpha1.PodSelector{
   116  							Mode: v1alpha1.OneMode,
   117  						},
   118  					},
   119  				},
   120  			}
   121  
   122  			By("creating a chaos")
   123  			{
   124  				Expect(k8sClient.Create(context.TODO(), chaos)).To(Succeed())
   125  			}
   126  
   127  			By("Reconciling desired phase")
   128  			{
   129  				err := wait.Poll(time.Second*1, time.Second*10, func() (ok bool, err error) {
   130  					err = k8sClient.Get(context.TODO(), key, chaos)
   131  					if err != nil {
   132  						return false, err
   133  					}
   134  					return chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.RunningPhase, nil
   135  				})
   136  				Expect(err).ToNot(HaveOccurred())
   137  			}
   138  			By("Pause chaos")
   139  			{
   140  				err := retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
   141  					err = k8sClient.Get(context.TODO(), key, chaos)
   142  					if err != nil {
   143  						return err
   144  					}
   145  					chaos.SetAnnotations(map[string]string{v1alpha1.PauseAnnotationKey: "true"})
   146  					return k8sClient.Update(context.TODO(), chaos)
   147  				})
   148  				Expect(err).ToNot(HaveOccurred())
   149  				err = wait.Poll(time.Second*5, time.Second*60, func() (ok bool, err error) {
   150  					err = k8sClient.Get(context.TODO(), key, chaos)
   151  					if err != nil {
   152  						return false, err
   153  					}
   154  					return chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.StoppedPhase, nil
   155  				})
   156  				Expect(err).ToNot(HaveOccurred())
   157  			}
   158  
   159  			By("Resume chaos")
   160  			{
   161  				err := retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
   162  					err = k8sClient.Get(context.TODO(), key, chaos)
   163  					if err != nil {
   164  						return err
   165  					}
   166  					chaos.SetAnnotations(map[string]string{v1alpha1.PauseAnnotationKey: "false"})
   167  					return k8sClient.Update(context.TODO(), chaos)
   168  				})
   169  				Expect(err).ToNot(HaveOccurred())
   170  				err = wait.Poll(time.Second*5, time.Second*60, func() (ok bool, err error) {
   171  					err = k8sClient.Get(context.TODO(), key, chaos)
   172  					if err != nil {
   173  						return false, err
   174  					}
   175  					return chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.RunningPhase, nil
   176  				})
   177  				Expect(err).ToNot(HaveOccurred())
   178  			}
   179  
   180  			By("deleting the created object")
   181  			{
   182  				Expect(k8sClient.Delete(context.TODO(), chaos)).To(Succeed())
   183  			}
   184  		})
   185  	})
   186  })
   187