...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/common/common_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/common

     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 common
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  	"time"
    20  
    21  	. "github.com/onsi/gomega"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	kubectlscheme "k8s.io/kubectl/pkg/scheme"
    24  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    25  
    26  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    27  	"github.com/chaos-mesh/chaos-mesh/pkg/utils"
    28  )
    29  
    30  func TestGetChaosList(t *testing.T) {
    31  	logger, _, _ := NewStderrLogger()
    32  	SetupGlobalLogger(logger)
    33  
    34  	g := NewWithT(t)
    35  
    36  	chaos1 := v1alpha1.NetworkChaos{
    37  		TypeMeta: metav1.TypeMeta{
    38  			Kind:       "NetworkChaos",
    39  			APIVersion: "v1",
    40  		},
    41  		ObjectMeta: metav1.ObjectMeta{
    42  			Namespace: metav1.NamespaceDefault,
    43  			Name:      "fakechaos-1",
    44  		},
    45  	}
    46  
    47  	chaos2 := v1alpha1.NetworkChaos{
    48  		TypeMeta: metav1.TypeMeta{
    49  			Kind:       "NetworkChaos",
    50  			APIVersion: "v1",
    51  		},
    52  		ObjectMeta: metav1.ObjectMeta{
    53  			Namespace: metav1.NamespaceDefault,
    54  			Name:      "fakechaos-2",
    55  		},
    56  	}
    57  
    58  	v1alpha1.SchemeBuilder.AddToScheme(kubectlscheme.Scheme)
    59  
    60  	client := fake.NewFakeClientWithScheme(kubectlscheme.Scheme, &chaos1, &chaos2)
    61  
    62  	tests := []struct {
    63  		name        string
    64  		chaosType   string
    65  		chaosName   string
    66  		ns          string
    67  		expectedNum int
    68  		expectedErr bool
    69  	}{
    70  		{
    71  			name:        "Only specify chaosType",
    72  			chaosType:   "networkchaos",
    73  			chaosName:   "",
    74  			ns:          "default",
    75  			expectedNum: 2,
    76  			expectedErr: false,
    77  		},
    78  		{
    79  			name:        "Specify chaos type, chaos name and namespace",
    80  			chaosType:   "networkchaos",
    81  			chaosName:   "fakechaos-1",
    82  			ns:          "default",
    83  			expectedNum: 1,
    84  			expectedErr: false,
    85  		},
    86  		{
    87  			name:        "Specify non-exist chaos name",
    88  			chaosType:   "networkchaos",
    89  			chaosName:   "fakechaos-oops",
    90  			ns:          "default",
    91  			expectedErr: true,
    92  		},
    93  		{
    94  			name:        "Specify non-exist chaos types",
    95  			chaosType:   "stresschaos",
    96  			chaosName:   "fakechaos-1",
    97  			ns:          "default",
    98  			expectedErr: true,
    99  		},
   100  		{
   101  			name:        "Specify non-exist namespace",
   102  			chaosType:   "networkchaos",
   103  			chaosName:   "fakechaos-1",
   104  			ns:          "oops",
   105  			expectedErr: true,
   106  		},
   107  	}
   108  
   109  	for _, test := range tests {
   110  		t.Run(test.name, func(t *testing.T) {
   111  			chaos, _, err := GetChaosList(context.Background(), test.chaosType, test.chaosName, test.ns, client)
   112  			if test.expectedErr {
   113  				g.Expect(err).NotTo(BeNil())
   114  			} else {
   115  				g.Expect(err).To(BeNil())
   116  				g.Expect(len(chaos)).To(Equal(test.expectedNum))
   117  			}
   118  		})
   119  	}
   120  }
   121  
   122  func TestGetPods(t *testing.T) {
   123  	logger, _, _ := NewStderrLogger()
   124  	SetupGlobalLogger(logger)
   125  
   126  	g := NewWithT(t)
   127  
   128  	_ = v1alpha1.ChaosStatus{
   129  		Experiment: v1alpha1.ExperimentStatus{
   130  			Phase: v1alpha1.ExperimentPhaseRunning,
   131  		},
   132  		Scheduler: v1alpha1.ScheduleStatus{},
   133  	}
   134  
   135  	nodeObjects, _ := utils.GenerateNNodes("node", 2, nil)
   136  	podObjects0, _ := utils.GenerateNPods("pod-node0", 1, utils.PodArg{Labels: map[string]string{"app": "pod"}, Nodename: "node0"})
   137  	podObjects1, _ := utils.GenerateNPods("pod-node1", 1, utils.PodArg{Labels: map[string]string{"app": "pod"}, Nodename: "node1"})
   138  	daemonObjects0, _ := utils.GenerateNPods("daemon-node0", 1, utils.PodArg{Labels: map[string]string{"app.kubernetes.io/component": "chaos-daemon"}, Nodename: "node0"})
   139  	daemonObjects1, _ := utils.GenerateNPods("daemon-node1", 1, utils.PodArg{Labels: map[string]string{"app.kubernetes.io/component": "chaos-daemon"}, Nodename: "node1"})
   140  
   141  	allObjects := append(nodeObjects, daemonObjects0[0], podObjects0[0], daemonObjects1[0], podObjects1[0])
   142  
   143  	v1alpha1.SchemeBuilder.AddToScheme(kubectlscheme.Scheme)
   144  	client := fake.NewFakeClientWithScheme(kubectlscheme.Scheme, allObjects...)
   145  
   146  	tests := []struct {
   147  		name              string
   148  		chaosSelector     v1alpha1.SelectorSpec
   149  		chaosStatus       v1alpha1.ChaosStatus
   150  		wait              bool
   151  		expectedPodNum    int
   152  		expectedDaemonNum int
   153  		expectedErr       bool
   154  	}{
   155  		{
   156  			name:              "chaos on two pods",
   157  			chaosSelector:     v1alpha1.SelectorSpec{LabelSelectors: map[string]string{"app": "pod"}},
   158  			chaosStatus:       v1alpha1.ChaosStatus{},
   159  			expectedPodNum:    2,
   160  			expectedDaemonNum: 2,
   161  			expectedErr:       false,
   162  		},
   163  		{
   164  			name: "chaos on one pod",
   165  			chaosSelector: v1alpha1.SelectorSpec{
   166  				Nodes:          []string{"node0"},
   167  				LabelSelectors: map[string]string{"app": "pod"},
   168  			},
   169  			chaosStatus:       v1alpha1.ChaosStatus{},
   170  			expectedPodNum:    1,
   171  			expectedDaemonNum: 1,
   172  			expectedErr:       false,
   173  		},
   174  		{
   175  			name:          "wait for 100ms for chaos to start",
   176  			chaosSelector: v1alpha1.SelectorSpec{LabelSelectors: map[string]string{"app": "pod"}},
   177  			chaosStatus: v1alpha1.ChaosStatus{
   178  				Experiment: v1alpha1.ExperimentStatus{
   179  					Phase: v1alpha1.ExperimentPhaseWaiting,
   180  				},
   181  				Scheduler: v1alpha1.ScheduleStatus{
   182  					NextStart: &metav1.Time{Time: time.Now().Add(time.Millisecond * 50)},
   183  				},
   184  			},
   185  			wait:              true,
   186  			expectedPodNum:    2,
   187  			expectedDaemonNum: 2,
   188  			expectedErr:       false,
   189  		},
   190  		{
   191  			name:          "wrong selector to get pod",
   192  			chaosSelector: v1alpha1.SelectorSpec{LabelSelectors: map[string]string{"app": "oops"}},
   193  			chaosStatus:   v1alpha1.ChaosStatus{},
   194  			expectedErr:   true,
   195  		},
   196  	}
   197  
   198  	for _, test := range tests {
   199  		t.Run(test.name, func(t *testing.T) {
   200  			timeBefore := time.Now()
   201  			pods, daemons, err := GetPods(context.Background(), test.name, test.chaosStatus, test.chaosSelector, client)
   202  			if test.wait {
   203  				g.Expect(time.Now().Add(time.Millisecond * -50).Before(timeBefore)).To(BeTrue())
   204  			}
   205  			if test.expectedErr {
   206  				g.Expect(err).NotTo(BeNil())
   207  			} else {
   208  				g.Expect(err).To(BeNil())
   209  				g.Expect(len(pods)).To(Equal(test.expectedPodNum))
   210  				g.Expect(len(daemons)).To(Equal(test.expectedDaemonNum))
   211  			}
   212  		})
   213  	}
   214  }
   215