Skip to content

Commit

Permalink
fix ut
Browse files Browse the repository at this point in the history
  • Loading branch information
Eikykun committed Jan 31, 2024
1 parent ae3ab04 commit f0dfa93
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package faultinjection
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -99,6 +100,7 @@ var faultInjection = &ctrlmeshv1alpha1.FaultInjection{
},
},
},
Name: "test-default",
},
},
},
Expand Down Expand Up @@ -163,8 +165,10 @@ func TestFaultInjection(t *testing.T) {
},
HttpMatch: []*ctrlmeshv1alpha1.HttpMatch{
{
URL: []string{"aaa.aaa.aaa"},
Method: []string{"GET"},
Host: &ctrlmeshv1alpha1.MatchContent{
Exact: "aaa.aaa.aaa",
},
Method: "GET",
},
},
},
Expand All @@ -180,10 +184,12 @@ func TestFaultInjection(t *testing.T) {
return false
}, 5*time.Second, 1*time.Second).Should(gomega.BeTrue())

g.Expect(faultManager.FaultInjectionRest("aaa.aaa.aaa", "GET").Abort).Should(gomega.BeTrue())
g.Expect(faultManager.FaultInjectionRest("aaa.aaa.bbb", "GET").Abort).Should(gomega.BeFalse())
g.Expect(faultManager.FaultInjectionResource("default", "", "Pod", "delete").Abort).Should(gomega.BeTrue())
g.Expect(faultManager.FaultInjectionResource("default", "", "Pod", "create").Abort).Should(gomega.BeFalse())
testUrl, _ := url.Parse("https://aaa.aaa.aaa")
g.Expect(faultManager.GetInjectorByUrl(testUrl, "GET").Abort()).Should(gomega.BeTrue())
testUrl, _ = url.Parse("https://aaa.aaa.bbb")
g.Expect(faultManager.GetInjectorByUrl(testUrl, "GET").Abort()).Should(gomega.BeFalse())
g.Expect(faultManager.GetInjectorByResource("default", "", "Pod", "delete").Abort()).Should(gomega.BeTrue())
g.Expect(faultManager.GetInjectorByResource("default", "", "Pod", "create").Abort()).Should(gomega.BeFalse())
if cb.Labels == nil {
cb.Labels = map[string]string{}
}
Expand Down
13 changes: 9 additions & 4 deletions pkg/proxy/faultinjection/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import (

type Injector interface {
Do(w http.ResponseWriter, req *http.Request) (abort bool)
Abort() bool
}

type abortWithDelayInjector struct {
Abort bool
abort bool
delay time.Duration
code int
message string
Expand All @@ -49,10 +50,10 @@ func (m *abortWithDelayInjector) Do(w http.ResponseWriter, req *http.Request) bo
klog.Errorf("failed to write api error response: %v", err)
return true
}
klog.Infof("abort by faultInjection, %s, %s, %d, with delay %ss", apiErr.Reason, apiErr.Message, apiErr.Code, m.delay/time.Second)
klog.Infof("abort by faultInjection, %s, %s, %d, with delay %s", apiErr.Reason, apiErr.Message, apiErr.Code, m.delay/time.Second)
return true

Check warning on line 54 in pkg/proxy/faultinjection/injector.go

View check run for this annotation

Codecov / codecov/patch

pkg/proxy/faultinjection/injector.go#L41-L54

Added lines #L41 - L54 were not covered by tests
}
klog.Infof("delay by faultInjection, %ss", m.delay/time.Second)
klog.Infof("delay by faultInjection, %s", m.delay/time.Second)
return false

Check warning on line 57 in pkg/proxy/faultinjection/injector.go

View check run for this annotation

Codecov / codecov/patch

pkg/proxy/faultinjection/injector.go#L56-L57

Added lines #L56 - L57 were not covered by tests
}

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

func (m *abortWithDelayInjector) AddAbort(code int, message string) {
m.Abort = true
m.abort = true
m.code = code
m.message = message
}

func (m *abortWithDelayInjector) Abort() bool {
return m.abort
}
53 changes: 24 additions & 29 deletions pkg/proxy/faultinjection/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,43 +201,38 @@ func (m *manager) GetInjectorByResource(namespace, apiGroup, resource, verb stri

func (m *manager) resourceMatch(matchs []*ctrlmeshproto.ResourceMatch, namespace, apiGroup, resource, verb string) bool {
for _, match := range matchs {
for _, val := range match.ApiGroups {
if val == "*" {
break
}
if val != apiGroup {
continue
}
if !matchInList(match.ApiGroups, apiGroup) {
continue

Check warning on line 205 in pkg/proxy/faultinjection/manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/proxy/faultinjection/manager.go#L202-L205

Added lines #L202 - L205 were not covered by tests
}
for _, val := range match.Resources {
if val == "*" {
break
}
if val != resource {
continue
}
if !matchInList(match.Resources, resource) {

Check warning on line 207 in pkg/proxy/faultinjection/manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/proxy/faultinjection/manager.go#L207

Added line #L207 was not covered by tests
continue
}
for _, val := range match.Verbs {
if val == "*" {
break
}
if val != verb {
continue
}
if !matchInList(match.Verbs, verb) {
continue

Check warning on line 211 in pkg/proxy/faultinjection/manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/proxy/faultinjection/manager.go#L210-L211

Added lines #L210 - L211 were not covered by tests
}
for _, val := range match.Namespaces {
if val == "*" {
break
}
if val != namespace {
continue
}
if !matchInList(match.Namespaces, namespace) {
continue

Check warning on line 214 in pkg/proxy/faultinjection/manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/proxy/faultinjection/manager.go#L213-L214

Added lines #L213 - L214 were not covered by tests
}
return true

Check warning on line 216 in pkg/proxy/faultinjection/manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/proxy/faultinjection/manager.go#L216

Added line #L216 was not covered by tests
}
return false

Check warning on line 218 in pkg/proxy/faultinjection/manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/proxy/faultinjection/manager.go#L218

Added line #L218 was not covered by tests
}

func matchInList(list []string, t string) bool {
if len(list) == 0 {
return true
}
for _, v := range list {
if v == "*" {
return true
}
if t == v {
return true
}

Check warning on line 231 in pkg/proxy/faultinjection/manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/proxy/faultinjection/manager.go#L221-L231

Added lines #L221 - L231 were not covered by tests
}
return false

Check warning on line 233 in pkg/proxy/faultinjection/manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/proxy/faultinjection/manager.go#L233

Added line #L233 was not covered by tests
}

func withFaultInjection(injectorManager FaultInjectorManager, handler http.Handler) http.Handler {

Check warning on line 236 in pkg/proxy/faultinjection/manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/proxy/faultinjection/manager.go#L236

Added line #L236 was not covered by tests
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
Expand All @@ -264,7 +259,7 @@ func (m *manager) getInjector(faultInjections []*ctrlmeshproto.HTTPFaultInjectio
if faultInjections[idx].Delay != nil && isInPercentRange(faultInjections[idx].Delay.Percent) {
injector.AddDelay(faultInjections[idx].Delay.GetFixedDelay().AsDuration())

Check warning on line 260 in pkg/proxy/faultinjection/manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/proxy/faultinjection/manager.go#L260

Added line #L260 was not covered by tests
}
if !injector.Abort && faultInjections[idx].Abort != nil && isInPercentRange(faultInjections[idx].Abort.Percent) {
if !injector.Abort() && faultInjections[idx].Abort != nil && isInPercentRange(faultInjections[idx].Abort.Percent) {
injector.AddAbort(int(faultInjections[idx].Abort.GetHttpStatus()),
fmt.Sprintf("the fault injection is triggered. Limiting rule name: %s", faultInjections[idx].Name))
}
Expand Down
26 changes: 13 additions & 13 deletions pkg/proxy/faultinjection/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,58 +111,58 @@ func TestStringMatch(t *testing.T) {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

func TestIsEffectiveTimeRange(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/proxy/grpcserver/faultinject_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
"fmt"

"connectrpc.com/connect"
ctrlmeshproto "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto"
"github.com/KusionStack/controller-mesh/pkg/proxy/faultinjection"
"google.golang.org/protobuf/encoding/protojson"
"k8s.io/klog/v2"

ctrlmeshproto "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto"
"github.com/KusionStack/controller-mesh/pkg/proxy/faultinjection"
)

type grpcFaultInjectHandler struct {
Expand All @@ -33,7 +34,7 @@ type grpcFaultInjectHandler struct {

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

msg := protojson.MarshalOptions{Multiline: true, EmitUnpopulated: true}.Format(req.Msg)
msg := protojson.MarshalOptions{Multiline: false, EmitUnpopulated: true}.Format(req.Msg)

Check warning on line 37 in pkg/proxy/grpcserver/faultinject_handler.go

View check run for this annotation

Codecov / codecov/patch

pkg/proxy/grpcserver/faultinject_handler.go#L37

Added line #L37 was not covered by tests
klog.Infof("handle FaultInjection gRPC request %s", msg)
if req.Msg == nil {
return connect.NewResponse(&ctrlmeshproto.FaultInjectConfigResp{Success: false}), fmt.Errorf("nil FaultInjection recieived from client")
Expand Down
28 changes: 11 additions & 17 deletions pkg/proxy/http/http_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,10 @@ func StartProxy() {
Match: &ctrlmeshproto.Match{
HttpMatch: []*ctrlmeshproto.HttpMatch{
{
Url: []string{
"https://github.com",
},
Method: []string{
"POST",
"GET",
Host: &ctrlmeshproto.MatchContent{
Exact: "github.com",
},
Method: "GET",
},
},
},
Expand All @@ -84,13 +81,13 @@ func StartProxy() {
Match: &ctrlmeshproto.Match{
HttpMatch: []*ctrlmeshproto.HttpMatch{
{
Url: []string{
"https://www.gayhub.com/foo",
Host: &ctrlmeshproto.MatchContent{
Exact: "www.gayhub.com",
},
Method: []string{
"POST",
"GET",
Path: &ctrlmeshproto.MatchContent{
Exact: "/foo",
},
Method: "GET",
},
},
},
Expand All @@ -104,13 +101,10 @@ func StartProxy() {
Match: &ctrlmeshproto.Match{
HttpMatch: []*ctrlmeshproto.HttpMatch{
{
Url: []string{
"https://abc.github.com",
},
Method: []string{
"POST",
"GET",
Host: &ctrlmeshproto.MatchContent{
Exact: "abc.github.com",
},
Method: "GET",
},
},
},
Expand Down

0 comments on commit f0dfa93

Please sign in to comment.