Skip to content

Commit f0dfa93

Browse files
committed
fix ut
1 parent ae3ab04 commit f0dfa93

File tree

6 files changed

+73
-72
lines changed

6 files changed

+73
-72
lines changed

pkg/manager/controllers/faultinjection/faultinjection_controller_test.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package faultinjection
1919
import (
2020
"encoding/json"
2121
"fmt"
22+
"net/url"
2223
"strings"
2324
"testing"
2425
"time"
@@ -99,6 +100,7 @@ var faultInjection = &ctrlmeshv1alpha1.FaultInjection{
99100
},
100101
},
101102
},
103+
Name: "test-default",
102104
},
103105
},
104106
},
@@ -163,8 +165,10 @@ func TestFaultInjection(t *testing.T) {
163165
},
164166
HttpMatch: []*ctrlmeshv1alpha1.HttpMatch{
165167
{
166-
URL: []string{"aaa.aaa.aaa"},
167-
Method: []string{"GET"},
168+
Host: &ctrlmeshv1alpha1.MatchContent{
169+
Exact: "aaa.aaa.aaa",
170+
},
171+
Method: "GET",
168172
},
169173
},
170174
},
@@ -180,10 +184,12 @@ func TestFaultInjection(t *testing.T) {
180184
return false
181185
}, 5*time.Second, 1*time.Second).Should(gomega.BeTrue())
182186

183-
g.Expect(faultManager.FaultInjectionRest("aaa.aaa.aaa", "GET").Abort).Should(gomega.BeTrue())
184-
g.Expect(faultManager.FaultInjectionRest("aaa.aaa.bbb", "GET").Abort).Should(gomega.BeFalse())
185-
g.Expect(faultManager.FaultInjectionResource("default", "", "Pod", "delete").Abort).Should(gomega.BeTrue())
186-
g.Expect(faultManager.FaultInjectionResource("default", "", "Pod", "create").Abort).Should(gomega.BeFalse())
187+
testUrl, _ := url.Parse("https://aaa.aaa.aaa")
188+
g.Expect(faultManager.GetInjectorByUrl(testUrl, "GET").Abort()).Should(gomega.BeTrue())
189+
testUrl, _ = url.Parse("https://aaa.aaa.bbb")
190+
g.Expect(faultManager.GetInjectorByUrl(testUrl, "GET").Abort()).Should(gomega.BeFalse())
191+
g.Expect(faultManager.GetInjectorByResource("default", "", "Pod", "delete").Abort()).Should(gomega.BeTrue())
192+
g.Expect(faultManager.GetInjectorByResource("default", "", "Pod", "create").Abort()).Should(gomega.BeFalse())
187193
if cb.Labels == nil {
188194
cb.Labels = map[string]string{}
189195
}

pkg/proxy/faultinjection/injector.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ import (
2828

2929
type Injector interface {
3030
Do(w http.ResponseWriter, req *http.Request) (abort bool)
31+
Abort() bool
3132
}
3233

3334
type abortWithDelayInjector struct {
34-
Abort bool
35+
abort bool
3536
delay time.Duration
3637
code int
3738
message string
@@ -49,10 +50,10 @@ func (m *abortWithDelayInjector) Do(w http.ResponseWriter, req *http.Request) bo
4950
klog.Errorf("failed to write api error response: %v", err)
5051
return true
5152
}
52-
klog.Infof("abort by faultInjection, %s, %s, %d, with delay %ss", apiErr.Reason, apiErr.Message, apiErr.Code, m.delay/time.Second)
53+
klog.Infof("abort by faultInjection, %s, %s, %d, with delay %s", apiErr.Reason, apiErr.Message, apiErr.Code, m.delay/time.Second)
5354
return true
5455
}
55-
klog.Infof("delay by faultInjection, %ss", m.delay/time.Second)
56+
klog.Infof("delay by faultInjection, %s", m.delay/time.Second)
5657
return false
5758
}
5859

@@ -61,7 +62,11 @@ func (m *abortWithDelayInjector) AddDelay(d time.Duration) {
6162
}
6263

6364
func (m *abortWithDelayInjector) AddAbort(code int, message string) {
64-
m.Abort = true
65+
m.abort = true
6566
m.code = code
6667
m.message = message
6768
}
69+
70+
func (m *abortWithDelayInjector) Abort() bool {
71+
return m.abort
72+
}

pkg/proxy/faultinjection/manager.go

+24-29
Original file line numberDiff line numberDiff line change
@@ -201,43 +201,38 @@ func (m *manager) GetInjectorByResource(namespace, apiGroup, resource, verb stri
201201

202202
func (m *manager) resourceMatch(matchs []*ctrlmeshproto.ResourceMatch, namespace, apiGroup, resource, verb string) bool {
203203
for _, match := range matchs {
204-
for _, val := range match.ApiGroups {
205-
if val == "*" {
206-
break
207-
}
208-
if val != apiGroup {
209-
continue
210-
}
204+
if !matchInList(match.ApiGroups, apiGroup) {
205+
continue
211206
}
212-
for _, val := range match.Resources {
213-
if val == "*" {
214-
break
215-
}
216-
if val != resource {
217-
continue
218-
}
207+
if !matchInList(match.Resources, resource) {
208+
continue
219209
}
220-
for _, val := range match.Verbs {
221-
if val == "*" {
222-
break
223-
}
224-
if val != verb {
225-
continue
226-
}
210+
if !matchInList(match.Verbs, verb) {
211+
continue
227212
}
228-
for _, val := range match.Namespaces {
229-
if val == "*" {
230-
break
231-
}
232-
if val != namespace {
233-
continue
234-
}
213+
if !matchInList(match.Namespaces, namespace) {
214+
continue
235215
}
236216
return true
237217
}
238218
return false
239219
}
240220

221+
func matchInList(list []string, t string) bool {
222+
if len(list) == 0 {
223+
return true
224+
}
225+
for _, v := range list {
226+
if v == "*" {
227+
return true
228+
}
229+
if t == v {
230+
return true
231+
}
232+
}
233+
return false
234+
}
235+
241236
func withFaultInjection(injectorManager FaultInjectorManager, handler http.Handler) http.Handler {
242237
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
243238
ctx := req.Context()
@@ -264,7 +259,7 @@ func (m *manager) getInjector(faultInjections []*ctrlmeshproto.HTTPFaultInjectio
264259
if faultInjections[idx].Delay != nil && isInPercentRange(faultInjections[idx].Delay.Percent) {
265260
injector.AddDelay(faultInjections[idx].Delay.GetFixedDelay().AsDuration())
266261
}
267-
if !injector.Abort && faultInjections[idx].Abort != nil && isInPercentRange(faultInjections[idx].Abort.Percent) {
262+
if !injector.Abort() && faultInjections[idx].Abort != nil && isInPercentRange(faultInjections[idx].Abort.Percent) {
268263
injector.AddAbort(int(faultInjections[idx].Abort.GetHttpStatus()),
269264
fmt.Sprintf("the fault injection is triggered. Limiting rule name: %s", faultInjections[idx].Name))
270265
}

pkg/proxy/faultinjection/manager_test.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -111,58 +111,58 @@ func TestStringMatch(t *testing.T) {
111111

112112
testUrl, _ := url.Parse("https://test1.com")
113113
injector := mgr.GetInjectorByUrl(testUrl, "POST")
114-
g.Expect(injector.(*abortWithDelayInjector).Abort).Should(gomega.BeTrue())
114+
g.Expect(injector.(*abortWithDelayInjector).Abort()).Should(gomega.BeTrue())
115115

116116
testUrl, _ = url.Parse("https://test1.com")
117117
injector = mgr.GetInjectorByUrl(testUrl, "GET")
118-
g.Expect(injector.(*abortWithDelayInjector).Abort).Should(gomega.BeFalse())
118+
g.Expect(injector.(*abortWithDelayInjector).Abort()).Should(gomega.BeFalse())
119119

120120
testUrl, _ = url.Parse("https://test1.com/a")
121121
injector = mgr.GetInjectorByUrl(testUrl, "POST")
122-
g.Expect(injector.(*abortWithDelayInjector).Abort).Should(gomega.BeTrue())
122+
g.Expect(injector.(*abortWithDelayInjector).Abort()).Should(gomega.BeTrue())
123123

124124
testUrl, _ = url.Parse("https://test2.com/a/b")
125125
injector = mgr.GetInjectorByUrl(testUrl, "POST")
126-
g.Expect(injector.(*abortWithDelayInjector).Abort).Should(gomega.BeTrue())
126+
g.Expect(injector.(*abortWithDelayInjector).Abort()).Should(gomega.BeTrue())
127127

128128
testUrl, _ = url.Parse("https://test2.com")
129129
injector = mgr.GetInjectorByUrl(testUrl, "POST")
130-
g.Expect(injector.(*abortWithDelayInjector).Abort).Should(gomega.BeFalse())
130+
g.Expect(injector.(*abortWithDelayInjector).Abort()).Should(gomega.BeFalse())
131131

132132
testUrl, _ = url.Parse("https://test2.com/a/c")
133133
injector = mgr.GetInjectorByUrl(testUrl, "POST")
134-
g.Expect(injector.(*abortWithDelayInjector).Abort).Should(gomega.BeFalse())
134+
g.Expect(injector.(*abortWithDelayInjector).Abort()).Should(gomega.BeFalse())
135135

136136
testUrl, _ = url.Parse("https://test3x.com/a/c")
137137
injector = mgr.GetInjectorByUrl(testUrl, "POST")
138-
g.Expect(injector.(*abortWithDelayInjector).Abort).Should(gomega.BeTrue())
138+
g.Expect(injector.(*abortWithDelayInjector).Abort()).Should(gomega.BeTrue())
139139

140140
testUrl, _ = url.Parse("https://test3.com")
141141
injector = mgr.GetInjectorByUrl(testUrl, "POST")
142-
g.Expect(injector.(*abortWithDelayInjector).Abort).Should(gomega.BeFalse())
142+
g.Expect(injector.(*abortWithDelayInjector).Abort()).Should(gomega.BeFalse())
143143

144144
testUrl, _ = url.Parse("https://test4.com/abc/d")
145145
injector = mgr.GetInjectorByUrl(testUrl, "POST")
146-
g.Expect(injector.(*abortWithDelayInjector).Abort).Should(gomega.BeTrue())
146+
g.Expect(injector.(*abortWithDelayInjector).Abort()).Should(gomega.BeTrue())
147147

148148
protoFault.Option = ctrlmeshproto.FaultInjection_DELETE
149149
_, err = mgr.Sync(protoFault)
150150

151151
testUrl, _ = url.Parse("https://test1.com/a")
152152
injector = mgr.GetInjectorByUrl(testUrl, "POST")
153-
g.Expect(injector.(*abortWithDelayInjector).Abort).Should(gomega.BeFalse())
153+
g.Expect(injector.(*abortWithDelayInjector).Abort()).Should(gomega.BeFalse())
154154

155155
testUrl, _ = url.Parse("https://test2.com/a/b")
156156
injector = mgr.GetInjectorByUrl(testUrl, "POST")
157-
g.Expect(injector.(*abortWithDelayInjector).Abort).Should(gomega.BeFalse())
157+
g.Expect(injector.(*abortWithDelayInjector).Abort()).Should(gomega.BeFalse())
158158

159159
testUrl, _ = url.Parse("https://test3x.com/a/c")
160160
injector = mgr.GetInjectorByUrl(testUrl, "POST")
161-
g.Expect(injector.(*abortWithDelayInjector).Abort).Should(gomega.BeFalse())
161+
g.Expect(injector.(*abortWithDelayInjector).Abort()).Should(gomega.BeFalse())
162162

163163
testUrl, _ = url.Parse("https://test4.com/abc/d")
164164
injector = mgr.GetInjectorByUrl(testUrl, "POST")
165-
g.Expect(injector.(*abortWithDelayInjector).Abort).Should(gomega.BeFalse())
165+
g.Expect(injector.(*abortWithDelayInjector).Abort()).Should(gomega.BeFalse())
166166
}
167167

168168
func TestIsEffectiveTimeRange(t *testing.T) {

pkg/proxy/grpcserver/faultinject_handler.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import (
2121
"fmt"
2222

2323
"connectrpc.com/connect"
24-
ctrlmeshproto "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto"
25-
"github.com/KusionStack/controller-mesh/pkg/proxy/faultinjection"
2624
"google.golang.org/protobuf/encoding/protojson"
2725
"k8s.io/klog/v2"
26+
27+
ctrlmeshproto "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto"
28+
"github.com/KusionStack/controller-mesh/pkg/proxy/faultinjection"
2829
)
2930

3031
type grpcFaultInjectHandler struct {
@@ -33,7 +34,7 @@ type grpcFaultInjectHandler struct {
3334

3435
func (g *grpcFaultInjectHandler) SendConfig(ctx context.Context, req *connect.Request[ctrlmeshproto.FaultInjection]) (*connect.Response[ctrlmeshproto.FaultInjectConfigResp], error) {
3536

36-
msg := protojson.MarshalOptions{Multiline: true, EmitUnpopulated: true}.Format(req.Msg)
37+
msg := protojson.MarshalOptions{Multiline: false, EmitUnpopulated: true}.Format(req.Msg)
3738
klog.Infof("handle FaultInjection gRPC request %s", msg)
3839
if req.Msg == nil {
3940
return connect.NewResponse(&ctrlmeshproto.FaultInjectConfigResp{Success: false}), fmt.Errorf("nil FaultInjection recieived from client")

pkg/proxy/http/http_proxy_test.go

+11-17
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,10 @@ func StartProxy() {
6464
Match: &ctrlmeshproto.Match{
6565
HttpMatch: []*ctrlmeshproto.HttpMatch{
6666
{
67-
Url: []string{
68-
"https://github.com",
69-
},
70-
Method: []string{
71-
"POST",
72-
"GET",
67+
Host: &ctrlmeshproto.MatchContent{
68+
Exact: "github.com",
7369
},
70+
Method: "GET",
7471
},
7572
},
7673
},
@@ -84,13 +81,13 @@ func StartProxy() {
8481
Match: &ctrlmeshproto.Match{
8582
HttpMatch: []*ctrlmeshproto.HttpMatch{
8683
{
87-
Url: []string{
88-
"https://www.gayhub.com/foo",
84+
Host: &ctrlmeshproto.MatchContent{
85+
Exact: "www.gayhub.com",
8986
},
90-
Method: []string{
91-
"POST",
92-
"GET",
87+
Path: &ctrlmeshproto.MatchContent{
88+
Exact: "/foo",
9389
},
90+
Method: "GET",
9491
},
9592
},
9693
},
@@ -104,13 +101,10 @@ func StartProxy() {
104101
Match: &ctrlmeshproto.Match{
105102
HttpMatch: []*ctrlmeshproto.HttpMatch{
106103
{
107-
Url: []string{
108-
"https://abc.github.com",
109-
},
110-
Method: []string{
111-
"POST",
112-
"GET",
104+
Host: &ctrlmeshproto.MatchContent{
105+
Exact: "abc.github.com",
113106
},
107+
Method: "GET",
114108
},
115109
},
116110
},

0 commit comments

Comments
 (0)