...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/schedule_type_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  // 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 v1alpha1
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/types"
    26  )
    27  
    28  // These tests are written in BDD-style using Ginkgo framework. Refer to
    29  // http://onsi.github.io/ginkgo to learn more.
    30  
    31  var _ = Describe("Schedule", func() {
    32  	var (
    33  		key              types.NamespacedName
    34  		created, fetched *Schedule
    35  	)
    36  
    37  	BeforeEach(func() {
    38  		// Add any setup steps that needs to be executed before each test
    39  	})
    40  
    41  	AfterEach(func() {
    42  		// Add any teardown steps that needs to be executed after each test
    43  	})
    44  
    45  	Context("Create API", func() {
    46  		It("should create an object successfully", func() {
    47  			key = types.NamespacedName{
    48  				Name:      "foo",
    49  				Namespace: "default",
    50  			}
    51  
    52  			created = &Schedule{
    53  				ObjectMeta: metav1.ObjectMeta{
    54  					Name:      "foo",
    55  					Namespace: "default",
    56  				},
    57  				Spec: ScheduleSpec{
    58  					Schedule: "* * * * 1",
    59  					ScheduleItem: ScheduleItem{
    60  						EmbedChaos: EmbedChaos{PodChaos: &PodChaosSpec{
    61  							Action: PodKillAction,
    62  							ContainerSelector: ContainerSelector{
    63  								PodSelector: PodSelector{
    64  									Mode: OneMode,
    65  								},
    66  							},
    67  						},
    68  						},
    69  					},
    70  					ConcurrencyPolicy: ForbidConcurrent,
    71  					Type:              ScheduleTypePodChaos,
    72  				},
    73  				Status: ScheduleStatus{
    74  					LastScheduleTime: metav1.Time{Time: time.Now()},
    75  				},
    76  			}
    77  
    78  			By("creating an API obj")
    79  			Expect(k8sClient.Create(context.TODO(), created)).To(Succeed())
    80  
    81  			fetched = &Schedule{}
    82  			Expect(k8sClient.Get(context.TODO(), key, fetched)).To(Succeed())
    83  			Expect(fetched).To(Equal(created))
    84  
    85  			By("deleting the created object")
    86  			Expect(k8sClient.Delete(context.TODO(), created)).To(Succeed())
    87  			Expect(k8sClient.Get(context.TODO(), key, created)).ToNot(Succeed())
    88  		})
    89  	})
    90  })
    91