...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/timechaos/timechaos_suite_test.go

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

     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 timechaos
    15  
    16  import (
    17  	"context"
    18  	"errors"
    19  	"testing"
    20  
    21  	"k8s.io/client-go/kubernetes/scheme"
    22  
    23  	. "github.com/onsi/ginkgo"
    24  	. "github.com/onsi/gomega"
    25  
    26  	v1 "k8s.io/api/core/v1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/client-go/tools/record"
    29  	ctrl "sigs.k8s.io/controller-runtime"
    30  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    31  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    32  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    33  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    34  
    35  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    36  	. "github.com/chaos-mesh/chaos-mesh/controllers/test"
    37  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    38  	ctx "github.com/chaos-mesh/chaos-mesh/pkg/router/context"
    39  	. "github.com/chaos-mesh/chaos-mesh/pkg/testutils"
    40  )
    41  
    42  func TestTimechaos(t *testing.T) {
    43  	RegisterFailHandler(Fail)
    44  
    45  	RunSpecsWithDefaultAndCustomReporters(t,
    46  		"TimeChaos Suite",
    47  		[]Reporter{envtest.NewlineReporter{}})
    48  }
    49  
    50  var _ = BeforeSuite(func(done Done) {
    51  	logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
    52  
    53  	Expect(v1.AddToScheme(scheme.Scheme)).To(Succeed())
    54  
    55  	close(done)
    56  }, 60)
    57  
    58  var _ = AfterSuite(func() {
    59  })
    60  
    61  var _ = Describe("TimeChaos", func() {
    62  	Context("TimeChaos", func() {
    63  		podObjects, pods := GenerateNPods("p", 1, PodArg{})
    64  
    65  		duration := "invalid_duration"
    66  
    67  		timechaos := v1alpha1.TimeChaos{
    68  			TypeMeta: metav1.TypeMeta{
    69  				Kind:       "TimeChaos",
    70  				APIVersion: "v1",
    71  			},
    72  			Spec: v1alpha1.TimeChaosSpec{
    73  				Mode:       v1alpha1.AllPodMode,
    74  				Value:      "0",
    75  				Selector:   v1alpha1.SelectorSpec{Namespaces: []string{metav1.NamespaceDefault}},
    76  				TimeOffset: "0s0ns",
    77  				Duration:   &duration,
    78  				Scheduler:  nil,
    79  			},
    80  		}
    81  
    82  		r := endpoint{
    83  			Context: ctx.Context{
    84  				Client:        fake.NewFakeClientWithScheme(scheme.Scheme, podObjects...),
    85  				EventRecorder: &record.FakeRecorder{},
    86  				Log:           ctrl.Log.WithName("controllers").WithName("TimeChaos"),
    87  			},
    88  		}
    89  
    90  		It("TimeChaos Apply", func() {
    91  			defer mock.With("MockSelectAndFilterPods", func() []v1.Pod {
    92  				return pods
    93  			})()
    94  			defer mock.With("MockChaosDaemonClient", &MockChaosDaemonClient{})()
    95  
    96  			err := r.Apply(context.TODO(), ctrl.Request{}, &timechaos)
    97  
    98  			Expect(err).ToNot(HaveOccurred())
    99  		})
   100  
   101  		It("TimeChaos Apply Error", func() {
   102  			defer mock.With("MockSelectAndFilterPods", func() []v1.Pod {
   103  				return pods
   104  			})()
   105  			defer mock.With("MockChaosDaemonClient", &MockChaosDaemonClient{})()
   106  			defer mock.With("MockSetTimeOffsetError", errors.New("SetTimeOffsetError"))()
   107  
   108  			err := r.Apply(context.TODO(), ctrl.Request{}, &timechaos)
   109  
   110  			Expect(err).To(HaveOccurred())
   111  			Expect(err.Error()).To(ContainSubstring("SetTimeOffsetError"))
   112  
   113  		})
   114  
   115  		It("TimeChaos Recover", func() {
   116  			defer mock.With("MockSelectAndFilterPods", func() []v1.Pod {
   117  				return pods
   118  			})()
   119  			defer mock.With("MockChaosDaemonClient", &MockChaosDaemonClient{})()
   120  
   121  			err := r.Recover(context.TODO(), ctrl.Request{}, &timechaos)
   122  			Expect(err).ToNot(HaveOccurred())
   123  		})
   124  
   125  		It("TimeChaos Recover Error", func() {
   126  			defer mock.With("MockSelectAndFilterPods", func() []v1.Pod {
   127  				return pods
   128  			})()
   129  			defer mock.With("MockChaosDaemonClient", &MockChaosDaemonClient{})()
   130  			defer mock.With("MockRecoverTimeOffsetError", errors.New("RecoverTimeOffsetError"))()
   131  
   132  			err := r.Apply(context.TODO(), ctrl.Request{}, &timechaos)
   133  			Expect(err).ToNot(HaveOccurred())
   134  
   135  			err = r.Recover(context.TODO(), ctrl.Request{}, &timechaos)
   136  
   137  			Expect(err).To(HaveOccurred())
   138  			Expect(err.Error()).To(ContainSubstring("RecoverTimeOffsetError"))
   139  		})
   140  	})
   141  })
   142