1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package jvmchaos
17
18 import (
19 "testing"
20
21 . "github.com/onsi/gomega"
22
23 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
24 )
25
26 func TestGenerateRuleData(t *testing.T) {
27 g := NewWithT(t)
28
29 testCases := []struct {
30 spec *v1alpha1.JVMChaosSpec
31 ruleData string
32 }{
33 {
34 &v1alpha1.JVMChaosSpec{
35 Action: v1alpha1.JVMExceptionAction,
36 JVMParameter: v1alpha1.JVMParameter{
37 Name: "test",
38 JVMCommonSpec: v1alpha1.JVMCommonSpec{
39 Pid: 1234,
40 },
41 JVMClassMethodSpec: v1alpha1.JVMClassMethodSpec{
42 Class: "testClass",
43 Method: "testMethod",
44 },
45 ThrowException: "java.io.IOException(\"BOOM\")",
46 },
47 },
48 "\nRULE test\nCLASS testClass\nMETHOD testMethod\nAT ENTRY\nIF true\nDO\n\tthrow new java.io.IOException(\"BOOM\");\nENDRULE\n",
49 },
50 {
51 &v1alpha1.JVMChaosSpec{
52 Action: v1alpha1.JVMReturnAction,
53 JVMParameter: v1alpha1.JVMParameter{
54 Name: "test",
55 JVMCommonSpec: v1alpha1.JVMCommonSpec{
56 Pid: 1234,
57 },
58 JVMClassMethodSpec: v1alpha1.JVMClassMethodSpec{
59 Class: "testClass",
60 Method: "testMethod",
61 },
62 ReturnValue: "\"test\"",
63 },
64 },
65 "\nRULE test\nCLASS testClass\nMETHOD testMethod\nAT ENTRY\nIF true\nDO\n\treturn \"test\";\nENDRULE\n",
66 },
67 {
68 &v1alpha1.JVMChaosSpec{
69 Action: v1alpha1.JVMLatencyAction,
70 JVMParameter: v1alpha1.JVMParameter{
71 Name: "test",
72 JVMCommonSpec: v1alpha1.JVMCommonSpec{
73 Pid: 1234,
74 },
75 JVMClassMethodSpec: v1alpha1.JVMClassMethodSpec{
76 Class: "testClass",
77 Method: "testMethod",
78 },
79 LatencyDuration: 5000,
80 },
81 },
82 "\nRULE test\nCLASS testClass\nMETHOD testMethod\nAT ENTRY\nIF true\nDO\n\tThread.sleep(5000);\nENDRULE\n",
83 },
84 {
85 &v1alpha1.JVMChaosSpec{
86 Action: v1alpha1.JVMStressAction,
87 JVMParameter: v1alpha1.JVMParameter{
88 Name: "test",
89 JVMCommonSpec: v1alpha1.JVMCommonSpec{
90 Pid: 1234,
91 },
92 JVMStressCfgSpec: v1alpha1.JVMStressCfgSpec{
93 CPUCount: 1,
94 },
95 },
96 },
97 "\nRULE test\nCLASS org.chaos_mesh.chaos_agent.TriggerThread\nMETHOD triggerFunc\nHELPER org.chaos_mesh.byteman.helper.StressHelper\nAT ENTRY\nBIND flag:boolean=true;\nIF true\nDO\n\tinjectCPUStress(\"test\", 1);\nENDRULE\n",
98 },
99 {
100 &v1alpha1.JVMChaosSpec{
101 Action: v1alpha1.JVMStressAction,
102 JVMParameter: v1alpha1.JVMParameter{
103 Name: "test",
104 JVMCommonSpec: v1alpha1.JVMCommonSpec{
105 Pid: 1234,
106 },
107 JVMStressCfgSpec: v1alpha1.JVMStressCfgSpec{
108 MemoryType: "heap",
109 },
110 },
111 },
112 "\nRULE test\nCLASS org.chaos_mesh.chaos_agent.TriggerThread\nMETHOD triggerFunc\nHELPER org.chaos_mesh.byteman.helper.StressHelper\nAT ENTRY\nBIND flag:boolean=true;\nIF true\nDO\n\tinjectMemStress(\"test\", \"heap\");\nENDRULE\n",
113 },
114 {
115 &v1alpha1.JVMChaosSpec{
116 Action: v1alpha1.JVMGCAction,
117 JVMParameter: v1alpha1.JVMParameter{
118 Name: "test",
119 JVMCommonSpec: v1alpha1.JVMCommonSpec{
120 Pid: 1234,
121 },
122 },
123 },
124 "\nRULE test\nCLASS org.chaos_mesh.chaos_agent.TriggerThread\nMETHOD triggerFunc\nHELPER org.chaos_mesh.byteman.helper.GCHelper\nAT ENTRY\nBIND flag:boolean=true;\nIF true\nDO\n\tgc();\nENDRULE\n",
125 },
126 {
127 &v1alpha1.JVMChaosSpec{
128 Action: v1alpha1.JVMMySQLAction,
129 JVMParameter: v1alpha1.JVMParameter{
130 Name: "test",
131 JVMCommonSpec: v1alpha1.JVMCommonSpec{
132 Pid: 1234,
133 },
134 JVMMySQLSpec: v1alpha1.JVMMySQLSpec{
135 MySQLConnectorVersion: "8",
136 Database: "test",
137 Table: "t1",
138 SQLType: "select",
139 },
140 ThrowException: "BOOM",
141 },
142 },
143 "\nRULE test\nCLASS com.mysql.cj.NativeSession\nMETHOD execSQL\nHELPER org.chaos_mesh.byteman.helper.SQLHelper\nAT ENTRY\nBIND flag:boolean=matchDBTable(\"\", $2, \"test\", \"t1\", \"select\");\nIF flag\nDO\n\tthrow new com.mysql.cj.exceptions.CJException(\"BOOM\");\nENDRULE\n",
144 },
145 {
146 &v1alpha1.JVMChaosSpec{
147 Action: v1alpha1.JVMMySQLAction,
148 JVMParameter: v1alpha1.JVMParameter{
149 Name: "test",
150 JVMCommonSpec: v1alpha1.JVMCommonSpec{
151 Pid: 1234,
152 },
153 JVMMySQLSpec: v1alpha1.JVMMySQLSpec{
154 MySQLConnectorVersion: "8",
155 Database: "test",
156 Table: "t1",
157 SQLType: "select",
158 },
159 LatencyDuration: 5000,
160 },
161 },
162 "\nRULE test\nCLASS com.mysql.cj.NativeSession\nMETHOD execSQL\nHELPER org.chaos_mesh.byteman.helper.SQLHelper\nAT ENTRY\nBIND flag:boolean=matchDBTable(\"\", $2, \"test\", \"t1\", \"select\");\nIF flag\nDO\n\tThread.sleep(5000);\nENDRULE\n",
163 },
164 }
165
166 for _, testCase := range testCases {
167 err := generateRuleData(testCase.spec)
168 g.Expect(err).ShouldNot(HaveOccurred())
169 g.Expect(testCase.spec.RuleData).Should(Equal(testCase.ruleData))
170 }
171 }
172