...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/common/fx_test.go

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

     1  // Copyright 2022 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 common
    17  
    18  import (
    19  	"testing"
    20  
    21  	. "github.com/onsi/gomega"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"sigs.k8s.io/controller-runtime/pkg/event"
    24  
    25  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    26  )
    27  
    28  func TestStatusRecordEventsChangePredicateEventsChange(t *testing.T) {
    29  	g := NewWithT(t)
    30  	// should skip event, if we Only the object.status.experiment.records[].events
    31  	// notice: ResourceVersion and Generation will be changed by k8s itself
    32  	newObj := &v1alpha1.HTTPChaos{
    33  		TypeMeta: metav1.TypeMeta{},
    34  		ObjectMeta: metav1.ObjectMeta{
    35  			ResourceVersion: "1",
    36  			Generation:      1,
    37  		},
    38  		Spec: v1alpha1.HTTPChaosSpec{},
    39  		Status: v1alpha1.HTTPChaosStatus{
    40  			ChaosStatus: v1alpha1.ChaosStatus{
    41  				Conditions: nil,
    42  				Experiment: v1alpha1.ExperimentStatus{
    43  					DesiredPhase: "",
    44  					Records: []*v1alpha1.Record{
    45  						{
    46  							Id:             "",
    47  							SelectorKey:    "",
    48  							Phase:          "",
    49  							InjectedCount:  0,
    50  							RecoveredCount: 0,
    51  							Events: []v1alpha1.RecordEvent{
    52  								{
    53  									Type:      v1alpha1.TypeFailed,
    54  									Operation: v1alpha1.Apply,
    55  									Message:   "apply failed",
    56  									Timestamp: nil,
    57  								},
    58  							},
    59  						},
    60  					},
    61  				},
    62  			},
    63  			Instances: nil,
    64  		},
    65  	}
    66  	oldObj := &v1alpha1.HTTPChaos{
    67  		TypeMeta: metav1.TypeMeta{},
    68  		ObjectMeta: metav1.ObjectMeta{
    69  			ResourceVersion: "0",
    70  			Generation:      0,
    71  		},
    72  		Spec: v1alpha1.HTTPChaosSpec{},
    73  		Status: v1alpha1.HTTPChaosStatus{
    74  			ChaosStatus: v1alpha1.ChaosStatus{
    75  				Conditions: nil,
    76  				Experiment: v1alpha1.ExperimentStatus{
    77  					DesiredPhase: "",
    78  					Records: []*v1alpha1.Record{
    79  						{
    80  							Id:             "",
    81  							SelectorKey:    "",
    82  							Phase:          "",
    83  							InjectedCount:  0,
    84  							RecoveredCount: 0,
    85  							Events:         nil,
    86  						},
    87  					},
    88  				},
    89  			},
    90  			Instances: nil,
    91  		},
    92  	}
    93  
    94  	predicate := StatusRecordEventsChangePredicate{}
    95  	updateEvent := event.UpdateEvent{ObjectOld: oldObj, ObjectNew: newObj}
    96  	pick := predicate.Update(updateEvent)
    97  	g.Expect(pick).Should(Equal(false))
    98  }
    99  
   100  func TestStatusRecordEventsChangePredicatePhaseChange(t *testing.T) {
   101  	g := NewWithT(t)
   102  
   103  	newObj := &v1alpha1.HTTPChaos{
   104  		TypeMeta: metav1.TypeMeta{},
   105  		ObjectMeta: metav1.ObjectMeta{
   106  			ResourceVersion: "1",
   107  			Generation:      1,
   108  		},
   109  		Spec: v1alpha1.HTTPChaosSpec{},
   110  		Status: v1alpha1.HTTPChaosStatus{
   111  			ChaosStatus: v1alpha1.ChaosStatus{
   112  				Conditions: nil,
   113  				Experiment: v1alpha1.ExperimentStatus{
   114  					DesiredPhase: v1alpha1.StoppedPhase,
   115  					Records:      nil,
   116  				},
   117  			},
   118  			Instances: nil,
   119  		},
   120  	}
   121  	oldObj := &v1alpha1.HTTPChaos{
   122  		TypeMeta: metav1.TypeMeta{},
   123  		ObjectMeta: metav1.ObjectMeta{
   124  			ResourceVersion: "0",
   125  			Generation:      0,
   126  		},
   127  		Spec: v1alpha1.HTTPChaosSpec{},
   128  		Status: v1alpha1.HTTPChaosStatus{
   129  			ChaosStatus: v1alpha1.ChaosStatus{
   130  				Conditions: nil,
   131  				Experiment: v1alpha1.ExperimentStatus{
   132  					DesiredPhase: v1alpha1.RunningPhase,
   133  					Records:      nil,
   134  				},
   135  			},
   136  			Instances: nil,
   137  		},
   138  	}
   139  
   140  	predicate := StatusRecordEventsChangePredicate{}
   141  	updateEvent := event.UpdateEvent{ObjectOld: oldObj, ObjectNew: newObj}
   142  	pick := predicate.Update(updateEvent)
   143  	g.Expect(pick).Should(Equal(true))
   144  }
   145  
   146  func TestStatusRecordEventsChangePredicateChildCRDs(t *testing.T) {
   147  	g := NewWithT(t)
   148  
   149  	// should allow the event about "Update" on certain Chaos Resource, v1alpha1.StatefulObject
   150  	newObj := &v1alpha1.PodIOChaos{
   151  		TypeMeta:   metav1.TypeMeta{},
   152  		ObjectMeta: metav1.ObjectMeta{},
   153  		Spec:       v1alpha1.PodIOChaosSpec{},
   154  		Status:     v1alpha1.PodIOChaosStatus{},
   155  	}
   156  	oldObj := &v1alpha1.PodIOChaos{
   157  		TypeMeta:   metav1.TypeMeta{},
   158  		ObjectMeta: metav1.ObjectMeta{},
   159  		Spec:       v1alpha1.PodIOChaosSpec{},
   160  		Status:     v1alpha1.PodIOChaosStatus{},
   161  	}
   162  
   163  	predicate := StatusRecordEventsChangePredicate{}
   164  	updateEvent := event.UpdateEvent{ObjectOld: oldObj, ObjectNew: newObj}
   165  	pick := predicate.Update(updateEvent)
   166  	g.Expect(pick).Should(Equal(false))
   167  }
   168