1
2
3
4
5
6
7
8
9
10
11
12
13
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
29
30
31 var _ = Describe("JVMChaos", func() {
32 var (
33 key types.NamespacedName
34 created, fetched *JVMChaos
35 )
36
37 BeforeEach(func() {
38
39 })
40
41 AfterEach(func() {
42
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