...

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  	g.Expect(v1alpha1.SchemeBuilder.AddToScheme(kubectlscheme.Scheme)).To(BeNil())
    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  			DesiredPhase: v1alpha1.RunningPhase,
   131  		},
   132  	}
   133  
   134  	nodeObjects, _ := utils.GenerateNNodes("node", 2, nil)
   135  	podObjects0, _ := utils.GenerateNPods("pod-node0", 1, utils.PodArg{Labels: map[string]string{"app": "pod"}, Nodename: "node0"})
   136  	podObjects1, _ := utils.GenerateNPods("pod-node1", 1, utils.PodArg{Labels: map[string]string{"app": "pod"}, Nodename: "node1"})
   137  	daemonObjects0, _ := utils.GenerateNPods("daemon-node0", 1, utils.PodArg{Labels: map[string]string{"app.kubernetes.io/component": "chaos-daemon"}, Nodename: "node0"})
   138  	daemonObjects1, _ := utils.GenerateNPods("daemon-node1", 1, utils.PodArg{Labels: map[string]string{"app.kubernetes.io/component": "chaos-daemon"}, Nodename: "node1"})
   139  
   140  	allObjects := append(nodeObjects, daemonObjects0[0], podObjects0[0], daemonObjects1[0], podObjects1[0])
   141  	g.Expect(v1alpha1.SchemeBuilder.AddToScheme(kubectlscheme.Scheme)).To(BeNil())
   142  	client := fake.NewFakeClientWithScheme(kubectlscheme.Scheme, allObjects...)
   143  
   144  	tests := []struct {
   145  		name              string
   146  		chaosSelector     v1alpha1.PodSelectorSpec
   147  		chaosStatus       v1alpha1.ChaosStatus
   148  		wait              bool
   149  		expectedPodNum    int
   150  		expectedDaemonNum int
   151  		expectedErr       bool
   152  	}{
   153  		{
   154  			name:              "chaos on two pods",
   155  			chaosSelector:     v1alpha1.PodSelectorSpec{LabelSelectors: map[string]string{"app": "pod"}},
   156  			chaosStatus:       v1alpha1.ChaosStatus{},
   157  			expectedPodNum:    2,
   158  			expectedDaemonNum: 2,
   159  			expectedErr:       false,
   160  		},
   161  		{
   162  			name: "chaos on one pod",
   163  			chaosSelector: v1alpha1.PodSelectorSpec{
   164  				Nodes:          []string{"node0"},
   165  				LabelSelectors: map[string]string{"app": "pod"},
   166  			},
   167  			chaosStatus:       v1alpha1.ChaosStatus{},
   168  			expectedPodNum:    1,
   169  			expectedDaemonNum: 1,
   170  			expectedErr:       false,
   171  		},
   172  		{
   173  			name:          "wrong selector to get pod",
   174  			chaosSelector: v1alpha1.PodSelectorSpec{LabelSelectors: map[string]string{"app": "oops"}},
   175  			chaosStatus:   v1alpha1.ChaosStatus{},
   176  			expectedErr:   true,
   177  		},
   178  	}
   179  
   180  	for _, test := range tests {
   181  		t.Run(test.name, func(t *testing.T) {
   182  			timeBefore := time.Now()
   183  			pods, daemons, err := GetPods(context.Background(), test.name, test.chaosStatus, test.chaosSelector, client)
   184  			if test.wait {
   185  				g.Expect(time.Now().Add(time.Millisecond * -50).Before(timeBefore)).To(BeTrue())
   186  			}
   187  			if test.expectedErr {
   188  				g.Expect(err).NotTo(BeNil())
   189  			} else {
   190  				g.Expect(err).To(BeNil())
   191  				g.Expect(len(pods)).To(Equal(test.expectedPodNum))
   192  				g.Expect(len(daemons)).To(Equal(test.expectedDaemonNum))
   193  			}
   194  		})
   195  	}
   196  }
   197