...

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

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

     1  // Copyright 2023 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 condition
    17  
    18  import (
    19  	. "github.com/onsi/ginkgo/v2"
    20  	. "github.com/onsi/gomega"
    21  	corev1 "k8s.io/api/core/v1"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  
    24  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    25  )
    26  
    27  var _ = Describe("Test condition controller", func() {
    28  	reconciler := Reconciler{
    29  		Object: &v1alpha1.PodChaos{
    30  			ObjectMeta: metav1.ObjectMeta{
    31  				Annotations: map[string]string{
    32  					v1alpha1.PauseAnnotationKey: "true",
    33  				},
    34  			},
    35  		},
    36  	}
    37  
    38  	Context("Test diffConditions", func() {
    39  		It("selected/allInjected/allRecovered state should be false when records is empty", func() {
    40  			newConditionMap := diffConditions(reconciler.Object.DeepCopyObject().(v1alpha1.InnerObject))
    41  
    42  			Expect(newConditionMap[v1alpha1.ConditionSelected].Status).To(Equal(corev1.ConditionFalse))
    43  			Expect(newConditionMap[v1alpha1.ConditionAllInjected].Status).To(Equal(corev1.ConditionFalse))
    44  			Expect(newConditionMap[v1alpha1.ConditionAllRecovered].Status).To(Equal(corev1.ConditionFalse))
    45  		})
    46  
    47  		It("Paused state should be true when pause annotation is true", func() {
    48  			obj := reconciler.Object.DeepCopyObject().(v1alpha1.InnerObject)
    49  			obj.SetAnnotations(map[string]string{
    50  				v1alpha1.PauseAnnotationKey: "true",
    51  			})
    52  			newConditionMap := diffConditions(obj)
    53  
    54  			Expect(newConditionMap[v1alpha1.ConditionPaused].Status).To(Equal(corev1.ConditionTrue))
    55  		})
    56  
    57  		It("AllInjected state should be true when all records are injected", func() {
    58  			obj := reconciler.Object.DeepCopyObject().(v1alpha1.InnerObject)
    59  			obj.GetStatus().Experiment.Records = append(obj.GetStatus().Experiment.Records, &v1alpha1.Record{
    60  				Phase: v1alpha1.Injected,
    61  			})
    62  			newConditionMap := diffConditions(obj)
    63  
    64  			Expect(newConditionMap[v1alpha1.ConditionAllInjected].Status).To(Equal(corev1.ConditionTrue))
    65  		})
    66  
    67  		It("AllRecovered state should be true when all records are recovered", func() {
    68  			obj := reconciler.Object.DeepCopyObject().(v1alpha1.InnerObject)
    69  			obj.GetStatus().Experiment.Records = append(obj.GetStatus().Experiment.Records, &v1alpha1.Record{
    70  				Phase: v1alpha1.NotInjected,
    71  			})
    72  			newConditionMap := diffConditions(obj)
    73  
    74  			Expect(newConditionMap[v1alpha1.ConditionAllRecovered].Status).To(Equal(corev1.ConditionTrue))
    75  		})
    76  	})
    77  })
    78