...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/physical_machine_chaos_types_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  
    21  	. "github.com/onsi/ginkgo/v2"
    22  	. "github.com/onsi/gomega"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/types"
    25  )
    26  
    27  // These tests are written in BDD-style using Ginkgo framework. Refer to
    28  // http://onsi.github.io/ginkgo to learn more.
    29  
    30  var _ = Describe("PhysicalMachineChaos", func() {
    31  	BeforeEach(func() {
    32  		// Add any setup steps that needs to be executed before each test
    33  	})
    34  
    35  	AfterEach(func() {
    36  		// Add any teardown steps that needs to be executed after each test
    37  	})
    38  
    39  	Context("Create API", func() {
    40  		It("should create an object successfully", func() {
    41  			testCases := []struct {
    42  				physicalMachineChaos *PhysicalMachineChaos
    43  				key                  types.NamespacedName
    44  			}{
    45  				{
    46  					physicalMachineChaos: &PhysicalMachineChaos{
    47  						ObjectMeta: metav1.ObjectMeta{
    48  							Name:      "foo",
    49  							Namespace: "default",
    50  						},
    51  						Spec: PhysicalMachineChaosSpec{
    52  							Action: "stress-mem",
    53  							PhysicalMachineSelector: PhysicalMachineSelector{
    54  								Address: []string{"123.123.123.123.123"},
    55  								Mode:    OneMode,
    56  							},
    57  							ExpInfo: ExpInfo{
    58  								StressMemory: &StressMemorySpec{
    59  									Size: "10MB",
    60  								},
    61  							},
    62  						},
    63  					},
    64  					key: types.NamespacedName{
    65  						Name:      "foo",
    66  						Namespace: "default",
    67  					},
    68  				}, {
    69  					physicalMachineChaos: &PhysicalMachineChaos{
    70  						ObjectMeta: metav1.ObjectMeta{
    71  							Name:      "foo1",
    72  							Namespace: "default",
    73  						},
    74  						Spec: PhysicalMachineChaosSpec{
    75  							Action: "stress-mem",
    76  							PhysicalMachineSelector: PhysicalMachineSelector{
    77  								Selector: PhysicalMachineSelectorSpec{
    78  									GenericSelectorSpec: GenericSelectorSpec{
    79  										LabelSelectors: map[string]string{
    80  											"foo1": "bar",
    81  										},
    82  									},
    83  								},
    84  								Mode: OneMode,
    85  							},
    86  							ExpInfo: ExpInfo{
    87  								StressMemory: &StressMemorySpec{
    88  									Size: "10MB",
    89  								},
    90  							},
    91  						},
    92  					},
    93  					key: types.NamespacedName{
    94  						Name:      "foo1",
    95  						Namespace: "default",
    96  					},
    97  				},
    98  			}
    99  
   100  			for _, testCase := range testCases {
   101  				By("creating an API obj")
   102  				Expect(k8sClient.Create(context.TODO(), testCase.physicalMachineChaos)).To(Succeed())
   103  
   104  				fetched := &PhysicalMachineChaos{}
   105  				Expect(k8sClient.Get(context.TODO(), testCase.key, fetched)).To(Succeed())
   106  				Expect(fetched).To(Equal(testCase.physicalMachineChaos))
   107  
   108  				By("deleting the created object")
   109  				Expect(k8sClient.Delete(context.TODO(), testCase.physicalMachineChaos)).To(Succeed())
   110  				Expect(k8sClient.Get(context.TODO(), testCase.key, testCase.physicalMachineChaos)).ToNot(Succeed())
   111  			}
   112  		})
   113  	})
   114  })
   115