...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/schedule_webhook_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/v1alpha1

     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package v1alpha1
    15  
    16  import (
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    20  )
    21  
    22  var _ = Describe("schedule_webhook", func() {
    23  	Context("webhook.Validator of schedule", func() {
    24  		It("Validate", func() {
    25  
    26  			type TestCase struct {
    27  				name     string
    28  				schedule Schedule
    29  				execute  func(schedule *Schedule) error
    30  				expect   string
    31  			}
    32  			tcs := []TestCase{
    33  				{
    34  					name: "validation for normal chaos",
    35  					schedule: Schedule{
    36  						ObjectMeta: metav1.ObjectMeta{
    37  							Namespace: metav1.NamespaceDefault,
    38  							Name:      "foo1",
    39  						},
    40  						Spec: ScheduleSpec{
    41  							ScheduleItem: ScheduleItem{EmbedChaos: EmbedChaos{PodChaos: &PodChaosSpec{
    42  								Action: ContainerKillAction,
    43  							}}},
    44  							Type:     ScheduleTypePodChaos,
    45  							Schedule: "@every 5s",
    46  						},
    47  					},
    48  					execute: func(schedule *Schedule) error {
    49  						return schedule.ValidateCreate()
    50  					},
    51  					expect: "error",
    52  				},
    53  				{
    54  					name: "validation for schedule",
    55  					schedule: Schedule{
    56  						ObjectMeta: metav1.ObjectMeta{
    57  							Namespace: metav1.NamespaceDefault,
    58  							Name:      "foo2",
    59  						},
    60  						Spec: ScheduleSpec{
    61  							ScheduleItem: ScheduleItem{EmbedChaos: EmbedChaos{PodChaos: &PodChaosSpec{}}},
    62  							Type:         ScheduleTypePodChaos,
    63  							Schedule:     "@every -5s",
    64  						},
    65  					},
    66  					execute: func(schedule *Schedule) error {
    67  						return schedule.ValidateCreate()
    68  					},
    69  					expect: "",
    70  				},
    71  				{
    72  					name: "validation for workflow",
    73  					schedule: Schedule{
    74  						ObjectMeta: metav1.ObjectMeta{
    75  							Namespace: metav1.NamespaceDefault,
    76  							Name:      "foo3",
    77  						},
    78  						Spec: ScheduleSpec{
    79  							ScheduleItem: ScheduleItem{Workflow: &WorkflowSpec{}},
    80  							Type:         ScheduleTypeWorkflow,
    81  							Schedule:     "@every 5s",
    82  						},
    83  					},
    84  					execute: func(schedule *Schedule) error {
    85  						return schedule.ValidateCreate()
    86  					},
    87  					expect: "",
    88  				},
    89  			}
    90  
    91  			for _, tc := range tcs {
    92  				err := tc.execute(&tc.schedule)
    93  				if tc.expect == "error" {
    94  					Expect(err).To(HaveOccurred())
    95  				} else {
    96  					Expect(err).NotTo(HaveOccurred())
    97  				}
    98  			}
    99  		})
   100  	})
   101  })
   102