11package v1alpha
22
33import (
4- "bytes"
54 "context"
6- _ "embed"
75 "errors"
86 "fmt"
9- "io"
10- "net/http"
11- "strings"
127 "testing"
138
149 v1alpha "github.com/conduitio/conduit-operator/api/v1alpha"
1510 "github.com/conduitio/conduit-operator/internal/testutil"
1611 "github.com/conduitio/conduit-operator/pkg/conduit"
17- "github.com/conduitio/conduit-operator/pkg/conduit/mock"
1812 "github.com/golang/mock/gomock"
1913 "github.com/matryer/is"
2014 apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -23,9 +17,6 @@ import (
2317 "sigs.k8s.io/controller-runtime/pkg/log"
2418)
2519
26- //go:embed testdata/connector-example.yaml
27- var connectorYAML string
28-
2920func TestWebhookValidate_ConduitVersion (t * testing.T ) {
3021 tests := []struct {
3122 ver string
@@ -47,7 +38,8 @@ func TestWebhookValidate_ConduitVersion(t *testing.T) {
4738 for _ , tc := range tests {
4839 t .Run (testname (tc .expectedErr , tc .ver ), func (t * testing.T ) {
4940 is := is .New (t )
50- v := & ConduitCustomValidator {conduit .NewValidator (log .Log .WithName ("webhook-validation" ))}
41+ ctx := context .Background ()
42+ v := & ConduitCustomValidator {conduit .NewValidator (ctx , log .Log .WithName ("webhook-validation" ))}
5143
5244 fieldErr := v .validateConduitVersion (tc .ver )
5345 if tc .expectedErr != nil {
@@ -60,7 +52,6 @@ func TestWebhookValidate_ConduitVersion(t *testing.T) {
6052 }
6153}
6254
63- //nolint:bodyclose
6455func TestWebhook_ValidateCreate (t * testing.T ) {
6556 tests := []struct {
6657 name string
@@ -70,13 +61,12 @@ func TestWebhook_ValidateCreate(t *testing.T) {
7061 {
7162 name : "validation is successful" ,
7263 setup : func () * v1alpha.Conduit {
73- webClient := setupHTTPMockClient (t )
74- httpResps := getHTTPResps ( )
64+ webClient := conduit . SetupHTTPMockClient (t )
65+ httpResps := conduit . GetHTTPResps ( t )
7566 gomock .InOrder (
76- webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps [0 ]),
77- webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps [1 ]),
78- webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps [0 ]),
79- webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps [1 ]),
67+ webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps ["list" ]),
68+ webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps ["spec" ]),
69+ webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps ["spec" ]),
8070 )
8171
8272 return testutil .SetupSampleConduit (t )
@@ -85,7 +75,7 @@ func TestWebhook_ValidateCreate(t *testing.T) {
8575 {
8676 name : "error occurs on http call" ,
8777 setup : func () * v1alpha.Conduit {
88- webClient := setupHTTPMockClient (t )
78+ webClient := conduit . SetupHTTPMockClient (t )
8979 webClient .EXPECT ().Do (gomock .Any ()).Return (nil , errors .New ("BOOM" )).Times (4 )
9080
9181 return testutil .SetupSampleConduit (t )
@@ -95,12 +85,12 @@ func TestWebhook_ValidateCreate(t *testing.T) {
9585 {
9686 name : "error occurs during validation" ,
9787 setup : func () * v1alpha.Conduit {
98- webClient := setupHTTPMockClient (t )
99- httpResps := getHTTPResps ( )
88+ webClient := conduit . SetupHTTPMockClient (t )
89+ httpResps := conduit . GetHTTPResps ( t )
10090 gomock .InOrder (
101- webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps [0 ]),
102- webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps [1 ]),
103- webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps [1 ]),
91+ webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps ["list" ]),
92+ webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps ["spec" ]),
93+ webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps ["spec" ]),
10494 )
10595
10696 return testutil .SetupBadValidationConduit (t )
@@ -118,10 +108,11 @@ func TestWebhook_ValidateCreate(t *testing.T) {
118108 for _ , tc := range tests {
119109 t .Run (tc .name , func (t * testing.T ) {
120110 is := is .New (t )
121- v := ConduitCustomValidator {conduit .NewValidator (log .Log .WithName ("webhook-validation" ))}
122- conduit := tc .setup ()
111+ ctx := context .Background ()
112+ c := tc .setup ()
113+ v := ConduitCustomValidator {conduit .NewValidator (ctx , log .Log .WithName ("webhook-validation" ))}
123114
124- _ , err := v .ValidateCreate (context .Background (), runtime .Object (conduit ))
115+ _ , err := v .ValidateCreate (context .Background (), runtime .Object (c ))
125116
126117 if tc .wantErr != nil {
127118 is .True (err != nil )
@@ -133,7 +124,6 @@ func TestWebhook_ValidateCreate(t *testing.T) {
133124 }
134125}
135126
136- //nolint:bodyclose
137127func TestWebhook_ValidateUpdate (t * testing.T ) {
138128 tests := []struct {
139129 name string
@@ -143,13 +133,12 @@ func TestWebhook_ValidateUpdate(t *testing.T) {
143133 {
144134 name : "validation is successful" ,
145135 setup : func () * v1alpha.Conduit {
146- webClient := setupHTTPMockClient (t )
147- httpFnResps := getHTTPResps ( )
136+ webClient := conduit . SetupHTTPMockClient (t )
137+ httpFnResps := conduit . GetHTTPResps ( t )
148138 gomock .InOrder (
149- webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpFnResps [0 ]),
150- webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpFnResps [1 ]),
151- webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpFnResps [0 ]),
152- webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpFnResps [1 ]),
139+ webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpFnResps ["list" ]),
140+ webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpFnResps ["spec" ]),
141+ webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpFnResps ["spec" ]),
153142 )
154143
155144 return testutil .SetupSampleConduit (t )
@@ -158,7 +147,7 @@ func TestWebhook_ValidateUpdate(t *testing.T) {
158147 {
159148 name : "error occurs on http call" ,
160149 setup : func () * v1alpha.Conduit {
161- webClient := setupHTTPMockClient (t )
150+ webClient := conduit . SetupHTTPMockClient (t )
162151 webClient .EXPECT ().Do (gomock .Any ()).Return (nil , errors .New ("BOOM" )).Times (4 )
163152
164153 return testutil .SetupSampleConduit (t )
@@ -168,12 +157,12 @@ func TestWebhook_ValidateUpdate(t *testing.T) {
168157 {
169158 name : "error occurs during validation" ,
170159 setup : func () * v1alpha.Conduit {
171- webClient := setupHTTPMockClient (t )
172- httpResps := getHTTPResps ( )
160+ webClient := conduit . SetupHTTPMockClient (t )
161+ httpFnResps := conduit . GetHTTPResps ( t )
173162 gomock .InOrder (
174- webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps [ 0 ]),
175- webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps [ 1 ]),
176- webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpResps [ 1 ]),
163+ webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpFnResps [ "list" ]),
164+ webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpFnResps [ "spec" ]),
165+ webClient .EXPECT ().Do (gomock .Any ()).DoAndReturn (httpFnResps [ "spec" ]),
177166 )
178167
179168 return testutil .SetupBadValidationConduit (t )
@@ -191,10 +180,11 @@ func TestWebhook_ValidateUpdate(t *testing.T) {
191180 for _ , tc := range tests {
192181 t .Run (tc .name , func (t * testing.T ) {
193182 is := is .New (t )
194- v := ConduitCustomValidator {conduit .NewValidator (log .Log .WithName ("webhook-validation" ))}
195- conduit := tc .setup ()
183+ ctx := context .Background ()
184+ c := tc .setup ()
185+ v := ConduitCustomValidator {conduit .NewValidator (ctx , log .Log .WithName ("webhook-validation" ))}
196186
197- _ , err := v .ValidateUpdate (context .Background (), runtime .Object (nil ), runtime .Object (conduit ))
187+ _ , err := v .ValidateUpdate (context .Background (), runtime .Object (nil ), runtime .Object (c ))
198188
199189 if tc .wantErr != nil {
200190 is .True (err != nil )
@@ -205,38 +195,3 @@ func TestWebhook_ValidateUpdate(t *testing.T) {
205195 })
206196 }
207197}
208-
209- func setupHTTPMockClient (t * testing.T ) * mock.MockhttpClient {
210- ctrl := gomock .NewController (t )
211- defer ctrl .Finish ()
212-
213- mockClient := mock .NewMockhttpClient (ctrl )
214- conduit .HTTPClient = mockClient
215-
216- return mockClient
217- }
218-
219- //nolint:bodyclose // Body is closed in the validator, bodyclose is not recognizing this.
220- func getHTTPResps () []func (* http.Request ) (* http.Response , error ) {
221- var resps []func (* http.Request ) (* http.Response , error )
222-
223- respFn := func (_ * http.Request ) (* http.Response , error ) {
224- resp := & http.Response {
225- StatusCode : 200 ,
226- Body : io .NopCloser (bytes .NewBufferString ("{\" tag_name\" : \" v0.10.1\" }" )),
227- }
228- return resp , nil
229- }
230- resps = append (resps , respFn )
231-
232- respFn = func (_ * http.Request ) (* http.Response , error ) {
233- resp := & http.Response {
234- StatusCode : 200 ,
235- Body : io .NopCloser (strings .NewReader (connectorYAML )),
236- }
237- return resp , nil
238- }
239- resps = append (resps , respFn )
240-
241- return resps
242- }
0 commit comments