...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/jvmchaos_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  	"encoding/json"
    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("JVMChaos", func() {
    32  	var (
    33  		key              types.NamespacedName
    34  		created, fetched *JVMChaos
    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 = &JVMChaos{
    53  				ObjectMeta: metav1.ObjectMeta{
    54  					Name:      "foo",
    55  					Namespace: "default",
    56  				},
    57  				Spec: JVMChaosSpec{
    58  					Action: JVMLatencyAction,
    59  					JVMParameter: JVMParameter{
    60  						JVMClassMethodSpec: JVMClassMethodSpec{
    61  							Class:  "Main",
    62  							Method: "print",
    63  						},
    64  						LatencyDuration: 1000,
    65  					},
    66  					ContainerSelector: ContainerSelector{
    67  						PodSelector: PodSelector{
    68  							Mode: OneMode,
    69  						},
    70  					},
    71  				},
    72  			}
    73  
    74  			By("creating an API obj")
    75  			Expect(k8sClient.Create(context.TODO(), created)).To(Succeed())
    76  
    77  			fetched = &JVMChaos{}
    78  			Expect(k8sClient.Get(context.TODO(), key, fetched)).To(Succeed())
    79  			Expect(fetched).To(Equal(created))
    80  
    81  			By("deleting the created object")
    82  			Expect(k8sClient.Delete(context.TODO(), created)).To(Succeed())
    83  			Expect(k8sClient.Get(context.TODO(), key, created)).ToNot(Succeed())
    84  		})
    85  	})
    86  
    87  	Describe("JSON Marshal and Unmarshal", func() {
    88  		object := &JVMChaos{
    89  			ObjectMeta: metav1.ObjectMeta{
    90  				Name:      "foo",
    91  				Namespace: "default",
    92  			},
    93  			Spec: JVMChaosSpec{
    94  				ContainerSelector: ContainerSelector{
    95  					PodSelector: PodSelector{
    96  						Mode:  FixedMode,
    97  						Value: "1337",
    98  					},
    99  				},
   100  				JVMParameter: JVMParameter{
   101  					Name:        "param-name",
   102  					ReturnValue: "param-return-value",
   103  				},
   104  			},
   105  		}
   106  		It("should marshal an object successfully", func() {
   107  			By("marshalling the object")
   108  			data, _ := json.MarshalIndent(object, "", "  ")
   109  			Expect(data).To(MatchJSON(`{
   110    "metadata": {
   111      "name": "foo",
   112      "namespace": "default",
   113      "creationTimestamp": null
   114    },
   115    "spec": {
   116      "selector": {},
   117      "mode": "fixed",
   118      "value": "1337",
   119      "action": "",
   120      "name": "param-name",
   121      "returnValue": "param-return-value",
   122      "exception": "",
   123      "latency": 0,
   124      "ruleData": ""
   125    },
   126    "status": {
   127      "experiment": {}
   128    }
   129  }`))
   130  		})
   131  		It("should unmarshal the object successfully", func() {
   132  			marshalledJson := []byte(`{
   133    "metadata": {
   134      "name": "foo",
   135      "namespace": "default"
   136    },
   137    "spec": {
   138      "name": "param-name",
   139      "returnValue": "param-return-value",
   140      "mode": "fixed",
   141      "value": "1337"
   142    }
   143  }`)
   144  			By("unmarshalling the object")
   145  			var unmarshalledJVMChaos JVMChaos
   146  			Expect(json.Unmarshal(marshalledJson, &unmarshalledJVMChaos)).To(Succeed())
   147  			Expect(&unmarshalledJVMChaos).To(HaveField("ObjectMeta.Name", "foo"))
   148  			Expect(&unmarshalledJVMChaos).To(HaveField("Spec.ContainerSelector.PodSelector.Mode", FixedMode))
   149  			Expect(&unmarshalledJVMChaos).To(HaveField("Spec.ContainerSelector.PodSelector.Value", "1337"))
   150  			Expect(&unmarshalledJVMChaos).To(HaveField("Spec.JVMParameter.Name", "param-name"))
   151  			Expect(&unmarshalledJVMChaos).To(HaveField("Spec.JVMParameter.ReturnValue", "param-return-value"))
   152  		})
   153  	})
   154  })
   155