...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/workflow/controllers/chaos_node_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/workflow/controllers

     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package controllers
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"strings"
    20  	"time"
    21  
    22  	"sigs.k8s.io/controller-runtime/pkg/client"
    23  
    24  	. "github.com/onsi/ginkgo"
    25  	. "github.com/onsi/gomega"
    26  	corev1 "k8s.io/api/core/v1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/types"
    29  
    30  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    31  )
    32  
    33  // integration tests
    34  var _ = Describe("Workflow", func() {
    35  	var ns string
    36  	BeforeEach(func() {
    37  		ctx := context.TODO()
    38  		newNs := corev1.Namespace{
    39  			ObjectMeta: metav1.ObjectMeta{
    40  				GenerateName: "chaos-mesh-",
    41  			},
    42  			Spec: corev1.NamespaceSpec{},
    43  		}
    44  		Expect(kubeClient.Create(ctx, &newNs)).To(Succeed())
    45  		ns = newNs.Name
    46  		By(fmt.Sprintf("create new namespace %s", ns))
    47  	})
    48  
    49  	AfterEach(func() {
    50  		ctx := context.TODO()
    51  		nsToDelete := corev1.Namespace{}
    52  		Expect(kubeClient.Get(ctx, types.NamespacedName{Name: ns}, &nsToDelete)).To(Succeed())
    53  		Expect(kubeClient.Delete(ctx, &nsToDelete)).To(Succeed())
    54  		By(fmt.Sprintf("cleanup namespace %s", ns))
    55  	})
    56  
    57  	Context("one chaos node", func() {
    58  		It("could spawn one chaos", func() {
    59  			ctx := context.TODO()
    60  			now := time.Now()
    61  			duration := 5 * time.Second
    62  
    63  			By("create simple chaos node with pod chaos")
    64  			startTime := metav1.NewTime(now)
    65  			deadline := metav1.NewTime(now.Add(duration))
    66  			workflowNode := v1alpha1.WorkflowNode{
    67  				ObjectMeta: metav1.ObjectMeta{
    68  					Namespace:    ns,
    69  					GenerateName: "chaos-node-with-chaos-",
    70  				},
    71  				Spec: v1alpha1.WorkflowNodeSpec{
    72  					TemplateName: "",
    73  					WorkflowName: "",
    74  					Type:         v1alpha1.TypePodChaos,
    75  					StartTime:    &startTime,
    76  					Deadline:     &deadline,
    77  					EmbedChaos: &v1alpha1.EmbedChaos{
    78  						PodChaos: &v1alpha1.PodChaosSpec{
    79  							ContainerSelector: v1alpha1.ContainerSelector{
    80  								PodSelector: v1alpha1.PodSelector{
    81  									Selector: v1alpha1.PodSelectorSpec{
    82  										Namespaces: []string{ns},
    83  									},
    84  									Mode: v1alpha1.AllPodMode,
    85  								},
    86  							},
    87  							Action: v1alpha1.PodKillAction,
    88  						},
    89  					},
    90  				},
    91  			}
    92  			Expect(kubeClient.Create(ctx, &workflowNode)).To(Succeed())
    93  			Eventually(func() bool {
    94  				podChaosList := v1alpha1.PodChaosList{}
    95  				Expect(kubeClient.List(ctx, &podChaosList, &client.ListOptions{Namespace: ns})).To(Succeed())
    96  				if len(podChaosList.Items) == 0 {
    97  					return false
    98  				}
    99  				return strings.HasPrefix(podChaosList.Items[0].Name, "chaos-node-with-chaos-")
   100  			}, 10*time.Second, time.Second).Should(BeTrue())
   101  		})
   102  
   103  		It("could spawn one schedule", func() {
   104  			ctx := context.TODO()
   105  			now := time.Now()
   106  			duration := 5 * time.Second
   107  
   108  			By("create simple chaos node with schedule")
   109  			startTime := metav1.NewTime(now)
   110  			deadline := metav1.NewTime(now.Add(duration))
   111  			node := v1alpha1.WorkflowNode{
   112  				ObjectMeta: metav1.ObjectMeta{
   113  					Namespace:    ns,
   114  					GenerateName: "chaos-node-schedule-",
   115  				},
   116  				Spec: v1alpha1.WorkflowNodeSpec{
   117  					WorkflowName: "",
   118  					Type:         v1alpha1.TypeSchedule,
   119  					StartTime:    &startTime,
   120  					Deadline:     &deadline,
   121  					Schedule: &v1alpha1.ScheduleSpec{
   122  						Schedule:                "@every 1s",
   123  						StartingDeadlineSeconds: nil,
   124  						ConcurrencyPolicy:       v1alpha1.AllowConcurrent,
   125  						HistoryLimit:            5,
   126  						Type:                    v1alpha1.ScheduleTypePodChaos,
   127  						ScheduleItem: v1alpha1.ScheduleItem{
   128  							EmbedChaos: v1alpha1.EmbedChaos{
   129  								PodChaos: &v1alpha1.PodChaosSpec{
   130  									ContainerSelector: v1alpha1.ContainerSelector{
   131  										PodSelector: v1alpha1.PodSelector{
   132  											Selector: v1alpha1.PodSelectorSpec{
   133  												Namespaces: []string{ns},
   134  												LabelSelectors: map[string]string{
   135  													"app": "not-actually-exist",
   136  												},
   137  											},
   138  											Mode: v1alpha1.AllPodMode,
   139  										},
   140  										ContainerNames: nil,
   141  									},
   142  									Action: v1alpha1.PodKillAction,
   143  								},
   144  							},
   145  						},
   146  					},
   147  				},
   148  			}
   149  			Expect(kubeClient.Create(ctx, &node)).To(Succeed())
   150  			Eventually(func() bool {
   151  				scheduleList := v1alpha1.ScheduleList{}
   152  				Expect(kubeClient.List(ctx, &scheduleList, &client.ListOptions{Namespace: ns})).To(Succeed())
   153  				if len(scheduleList.Items) == 0 {
   154  					return false
   155  				}
   156  				return strings.HasPrefix(scheduleList.Items[0].Name, "chaos-node-schedule-")
   157  			}, 10*time.Second, time.Second).Should(BeTrue())
   158  		})
   159  	})
   160  })
   161