...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/utils/recorder/recorder_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/utils/recorder

     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 recorder
    17  
    18  import (
    19  	"fmt"
    20  	"testing"
    21  	"time"
    22  
    23  	. "github.com/onsi/gomega"
    24  )
    25  
    26  func TestGenerateAnnotations(t *testing.T) {
    27  	g := NewGomegaWithT(t)
    28  
    29  	type casePair struct {
    30  		annotations map[string]string
    31  		ev          ChaosEvent
    32  	}
    33  
    34  	missedRun, _ := time.Parse(time.RFC3339Nano, "2021-05-19T18:36:06Z")
    35  	testCases := []casePair{
    36  		{map[string]string{"chaos-mesh.org/id": "", "chaos-mesh.org/type": "applied"}, Applied{}},
    37  		{map[string]string{"chaos-mesh.org/id": "test", "chaos-mesh.org/type": "applied"}, Applied{"test"}},
    38  		{map[string]string{"chaos-mesh.org/id": "test", "chaos-mesh.org/type": "recovered"}, Recovered{"test"}},
    39  
    40  		{map[string]string{"chaos-mesh.org/field": "test", "chaos-mesh.org/type": "updated"}, Updated{"test"}},
    41  
    42  		{map[string]string{"chaos-mesh.org/type": "deleted"}, Deleted{}},
    43  		{map[string]string{"chaos-mesh.org/type": "time-up"}, TimeUp{}},
    44  		{map[string]string{"chaos-mesh.org/type": "paused"}, Paused{}},
    45  		{map[string]string{"chaos-mesh.org/type": "started"}, Started{}},
    46  
    47  		{map[string]string{"chaos-mesh.org/activity": "test1", "chaos-mesh.org/err": "test2", "chaos-mesh.org/type": "failed"}, Failed{"test1", "test2"}},
    48  		{map[string]string{"chaos-mesh.org/type": "not-supported", "chaos-mesh.org/activity": "pausing a workflow schedule"}, NotSupported{Activity: "pausing a workflow schedule"}},
    49  
    50  		{map[string]string{"chaos-mesh.org/type": "finalizer-inited"}, FinalizerInited{}},
    51  		{map[string]string{"chaos-mesh.org/type": "finalizer-removed"}, FinalizerRemoved{}},
    52  
    53  		{map[string]string{"chaos-mesh.org/missed-run": "2021-05-19T18:36:06Z", "chaos-mesh.org/type": "missed-schedule"}, MissedSchedule{MissedRun: missedRun}},
    54  		{map[string]string{"chaos-mesh.org/name": "test", "chaos-mesh.org/type": "schedule-spawn"}, ScheduleSpawn{Name: "test"}},
    55  		{map[string]string{"chaos-mesh.org/running-name": "test", "chaos-mesh.org/type": "schedule-forbid"}, ScheduleForbid{RunningName: "test"}},
    56  		{map[string]string{"chaos-mesh.org/running-name": "test", "chaos-mesh.org/type": "schedule-skip-remove-history"}, ScheduleSkipRemoveHistory{RunningName: "test"}},
    57  		{map[string]string{"chaos-mesh.org/type": "nodes-created", "chaos-mesh.org/child-nodes": "[\"node-a\",\"node-b\"]"}, NodesCreated{ChildNodes: []string{"node-a", "node-b"}}},
    58  	}
    59  
    60  	for _, c := range testCases {
    61  		g.Expect(generateAnnotations(c.ev)).To(Equal(c.annotations))
    62  
    63  		ev, err := FromAnnotations(c.annotations)
    64  		if err != nil {
    65  			fmt.Printf("fail")
    66  		}
    67  		g.Expect(ev).To(Equal(c.ev))
    68  	}
    69  }
    70  
    71  func TestParse(t *testing.T) {
    72  	g := NewGomegaWithT(t)
    73  
    74  	type casePair struct {
    75  		message string
    76  		ev      ChaosEvent
    77  	}
    78  
    79  	missedRun, _ := time.Parse(time.RFC1123Z, "Wed, 19 May 2021 18:36:06 +0000")
    80  	testCases := []casePair{
    81  		{"Successfully apply chaos for test", Applied{"test"}},
    82  		{"Successfully recover chaos for test", Recovered{"test"}},
    83  
    84  		{"Successfully update test of resource", Updated{"test"}},
    85  
    86  		{"Experiment has been deleted", Deleted{}},
    87  		{"Time up according to the duration", TimeUp{}},
    88  		{"Experiment has been paused", Paused{}},
    89  		{"Experiment has started", Started{}},
    90  
    91  		{"Failed to test1: test2", Failed{"test1", "test2"}},
    92  
    93  		{"Finalizer has been inited", FinalizerInited{}},
    94  		{"Finalizer has been removed", FinalizerRemoved{}},
    95  
    96  		{"Missed scheduled time to start a job: Wed, 19 May 2021 18:36:06 +0000", MissedSchedule{MissedRun: missedRun}},
    97  		{"Create new object: test", ScheduleSpawn{Name: "test"}},
    98  		{"Forbid spawning new job because: test is still running", ScheduleForbid{RunningName: "test"}},
    99  		{"Skip removing history: test is still running", ScheduleSkipRemoveHistory{RunningName: "test"}},
   100  	}
   101  
   102  	for _, c := range testCases {
   103  		g.Expect(c.ev.Message()).To(Equal(c.message))
   104  	}
   105  }
   106