-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathgrpc_disruption_test.go
126 lines (112 loc) · 3.93 KB
/
grpc_disruption_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025 Datadog, Inc.
package grpc_test
import (
"github.com/DataDog/chaos-controller/api/v1beta1"
"github.com/DataDog/chaos-controller/grpc"
"github.com/DataDog/chaos-controller/grpc/disruptionlistener"
pb "github.com/DataDog/chaos-controller/grpc/disruptionlistener"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"
"google.golang.org/protobuf/types/known/emptypb"
)
var _ = Describe("Test send and clean disruption", func() {
Context("Basic GRPCDisruptionSpec", func() {
// define parameters of NewGRPCDisruptionInjector
spec := v1beta1.GRPCDisruptionSpec{
Port: 2000,
Endpoints: []v1beta1.EndpointAlteration{
{
TargetEndpoint: "/chaosdogfood.ChaosDogfood/order",
ErrorToReturn: "",
OverrideToReturn: "{}",
QueryPercent: 50,
},
{
TargetEndpoint: "/chaosdogfood.ChaosDogfood/getCatalog",
ErrorToReturn: "NOT_FOUND",
OverrideToReturn: "",
QueryPercent: 25,
},
{
TargetEndpoint: "/chaosdogfood.ChaosDogfood/getCatalog",
ErrorToReturn: "ALREADY_EXISTS",
OverrideToReturn: "",
QueryPercent: 50,
},
{
TargetEndpoint: "/chaosdogfood.ChaosDogfood/getCatalog",
ErrorToReturn: "",
OverrideToReturn: "{}",
QueryPercent: 0,
},
},
}
var disruptionListenerClient *disruptionlistener.DisruptionListenerClientMock
BeforeEach(func() {
disruptionListenerClient = disruptionlistener.NewDisruptionListenerClientMock(GinkgoT())
})
Specify("calls Disrupt and ResetDisruptions with expected parameters", func() {
// define expectations
disruptionListenerClient.EXPECT().Disrupt(
mock.Anything,
mock.MatchedBy(func(spec *pb.DisruptionSpec) bool {
endpts := spec.Endpoints
if len(endpts) != 2 {
return false
}
altSpecForOrder := []*pb.AlterationSpec{
{
ErrorToReturn: "",
OverrideToReturn: "{}",
QueryPercent: int32(50),
},
}
altSpecForGetCatalog := []*pb.AlterationSpec{
{
ErrorToReturn: "NOT_FOUND",
OverrideToReturn: "",
QueryPercent: int32(25),
},
{
ErrorToReturn: "ALREADY_EXISTS",
OverrideToReturn: "",
QueryPercent: int32(50),
},
{
ErrorToReturn: "",
OverrideToReturn: "{}",
},
}
// handling that the results of `endpts` is indeterminate
if endpts[0].TargetEndpoint == "/chaosdogfood.ChaosDogfood/order" {
return specsAreEqual(endpts[0].Alterations[0], altSpecForOrder[0]) &&
specsAreEqual(endpts[1].Alterations[0], altSpecForGetCatalog[0]) &&
specsAreEqual(endpts[1].Alterations[1], altSpecForGetCatalog[1]) &&
specsAreEqual(endpts[1].Alterations[2], altSpecForGetCatalog[2])
}
return specsAreEqual(endpts[1].Alterations[0], altSpecForOrder[0]) &&
specsAreEqual(endpts[0].Alterations[0], altSpecForGetCatalog[0]) &&
specsAreEqual(endpts[0].Alterations[1], altSpecForGetCatalog[1]) &&
specsAreEqual(endpts[0].Alterations[2], altSpecForGetCatalog[2])
}),
).Return(&emptypb.Empty{}, nil)
disruptionListenerClient.EXPECT().ResetDisruptions(
mock.Anything,
&emptypb.Empty{},
).Return(&emptypb.Empty{}, nil)
Expect(grpc.SendGrpcDisruption(disruptionListenerClient, spec)).To(Succeed())
Expect(grpc.ClearGrpcDisruptions(disruptionListenerClient)).To(Succeed())
// run test
disruptionListenerClient.AssertExpectations(GinkgoT())
})
})
})
func specsAreEqual(actual *pb.AlterationSpec, expected *pb.AlterationSpec) bool {
return actual.ErrorToReturn == expected.ErrorToReturn &&
actual.OverrideToReturn == expected.OverrideToReturn &&
actual.QueryPercent == expected.QueryPercent
}