Skip to content

Commit 65e2d91

Browse files
committed
feat: developer plugin
1 parent 31cefae commit 65e2d91

13 files changed

+784
-0
lines changed
Lines changed: 327 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
/*
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
4+
Lice/*
5+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License").
8+
You may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package test
21+
22+
import (
23+
"database/sql/driver"
24+
"errors"
25+
"testing"
26+
27+
mock_driver_infrastructure "github.com/aws/aws-advanced-go-wrapper/.test/test/mocks/awssql/driver_infrastructure"
28+
mock_error_simulator "github.com/aws/aws-advanced-go-wrapper/.test/test/mocks/awssql/plugins/error_simulator"
29+
"github.com/aws/aws-advanced-go-wrapper/awssql/host_info_util"
30+
"github.com/aws/aws-advanced-go-wrapper/awssql/plugins"
31+
"github.com/aws/aws-advanced-go-wrapper/awssql/plugins/error_simulator"
32+
"github.com/aws/aws-advanced-go-wrapper/awssql/utils"
33+
"github.com/golang/mock/gomock"
34+
"github.com/stretchr/testify/assert"
35+
)
36+
37+
var (
38+
testError = errors.New("test")
39+
hostInfo = &host_info_util.HostInfo{Host: "pg.testdb.us-east-2.rds.amazonaws.com", Port: 1234}
40+
)
41+
42+
func TestDeveloperConnectionPlugin(t *testing.T) {
43+
t.Run("testRaiseError", func(t *testing.T) {
44+
ctrl := gomock.NewController(t)
45+
defer ctrl.Finish()
46+
error_simulator.ResetErrorSimulatorManager()
47+
48+
mockPluginService := mock_driver_infrastructure.NewMockPluginService(ctrl)
49+
properties := utils.NewRWMap[string, string]()
50+
properties.Put("plugins", "dev")
51+
plugin := plugins.NewDeveloperConnectionPlugin(mockPluginService, properties)
52+
mockConnectFunc := func(props *utils.RWMap[string, string]) (driver.Conn, error) {
53+
return nil, nil
54+
}
55+
mockExecuteFunc := func() (any, any, bool, error) {
56+
return nil, nil, true, nil
57+
}
58+
59+
// Should connect successfully
60+
_, err := plugin.Connect(hostInfo, properties, false, mockConnectFunc)
61+
assert.NoError(t, err)
62+
63+
// Should execute successfully
64+
_, _, _, err = plugin.Execute(nil, "query", mockExecuteFunc)
65+
assert.NoError(t, err)
66+
67+
// Raise error on next call
68+
devPlugin := plugin.(*plugins.DeveloperConnectionPlugin)
69+
devPlugin.RaiseErrorOnNextCall(testError, "")
70+
71+
// Should fail with test error
72+
_, _, _, err = plugin.Execute(nil, "query", mockExecuteFunc)
73+
assert.Equal(t, testError, err)
74+
})
75+
76+
t.Run("testRaiseErrorForMethodName", func(t *testing.T) {
77+
ctrl := gomock.NewController(t)
78+
defer ctrl.Finish()
79+
error_simulator.ResetErrorSimulatorManager()
80+
81+
mockPluginService := mock_driver_infrastructure.NewMockPluginService(ctrl)
82+
properties := utils.NewRWMap[string, string]()
83+
properties.Put("plugins", "dev")
84+
plugin := plugins.NewDeveloperConnectionPlugin(mockPluginService, properties)
85+
mockConnectFunc := func(props *utils.RWMap[string, string]) (driver.Conn, error) {
86+
return nil, nil
87+
}
88+
mockExecuteFunc := func() (any, any, bool, error) {
89+
return nil, nil, true, nil
90+
}
91+
92+
// Should connect successfully
93+
_, err := plugin.Connect(hostInfo, properties, false, mockConnectFunc)
94+
assert.NoError(t, err)
95+
96+
// Should execute successfully
97+
_, _, _, err = plugin.Execute(nil, "query", mockExecuteFunc)
98+
assert.NoError(t, err)
99+
100+
// Raise error on next call for specific method
101+
devPlugin := plugin.(*plugins.DeveloperConnectionPlugin)
102+
devPlugin.RaiseErrorOnNextCall(testError, "query")
103+
104+
// Should fail with test error
105+
_, _, _, err = plugin.Execute(nil, "query", mockExecuteFunc)
106+
assert.Equal(t, testError, err)
107+
})
108+
109+
t.Run("testRaiseErrorForAnyMethodName", func(t *testing.T) {
110+
ctrl := gomock.NewController(t)
111+
defer ctrl.Finish()
112+
error_simulator.ResetErrorSimulatorManager()
113+
114+
mockPluginService := mock_driver_infrastructure.NewMockPluginService(ctrl)
115+
properties := utils.NewRWMap[string, string]()
116+
properties.Put("plugins", "dev")
117+
plugin := plugins.NewDeveloperConnectionPlugin(mockPluginService, properties)
118+
mockConnectFunc := func(props *utils.RWMap[string, string]) (driver.Conn, error) {
119+
return nil, nil
120+
}
121+
mockExecuteFunc := func() (any, any, bool, error) {
122+
return nil, nil, true, nil
123+
}
124+
125+
// Should connect successfully
126+
_, err := plugin.Connect(hostInfo, properties, false, mockConnectFunc)
127+
assert.NoError(t, err)
128+
129+
// Should execute successfully
130+
_, _, _, err = plugin.Execute(nil, "query", mockExecuteFunc)
131+
assert.NoError(t, err)
132+
133+
// Raise error on next call for any method
134+
devPlugin := plugin.(*plugins.DeveloperConnectionPlugin)
135+
devPlugin.RaiseErrorOnNextCall(testError, "*")
136+
137+
// Should fail with test error
138+
_, _, _, err = plugin.Execute(nil, "query", mockExecuteFunc)
139+
assert.Equal(t, testError, err)
140+
})
141+
142+
t.Run("testRaiseErrorForWrongMethodName", func(t *testing.T) {
143+
ctrl := gomock.NewController(t)
144+
defer ctrl.Finish()
145+
error_simulator.ResetErrorSimulatorManager()
146+
147+
mockPluginService := mock_driver_infrastructure.NewMockPluginService(ctrl)
148+
properties := utils.NewRWMap[string, string]()
149+
properties.Put("plugins", "dev")
150+
plugin := plugins.NewDeveloperConnectionPlugin(mockPluginService, properties)
151+
mockConnectFunc := func(props *utils.RWMap[string, string]) (driver.Conn, error) {
152+
return nil, nil
153+
}
154+
mockExecuteFunc := func() (any, any, bool, error) {
155+
return nil, nil, true, nil
156+
}
157+
158+
// Should connect successfully
159+
_, err := plugin.Connect(hostInfo, properties, false, mockConnectFunc)
160+
assert.NoError(t, err)
161+
162+
// Should execute successfully
163+
_, _, _, err = plugin.Execute(nil, "query", mockExecuteFunc)
164+
assert.NoError(t, err)
165+
166+
// Raise error on next call for different method
167+
devPlugin := plugin.(*plugins.DeveloperConnectionPlugin)
168+
devPlugin.RaiseErrorOnNextCall(testError, "close")
169+
170+
// Should execute successfully (wrong method name)
171+
_, _, _, err = plugin.Execute(nil, "query", mockExecuteFunc)
172+
assert.NoError(t, err)
173+
})
174+
175+
t.Run("testRaiseErrorWithCallback", func(t *testing.T) {
176+
ctrl := gomock.NewController(t)
177+
defer ctrl.Finish()
178+
error_simulator.ResetErrorSimulatorManager()
179+
180+
mockPluginService := mock_driver_infrastructure.NewMockPluginService(ctrl)
181+
properties := utils.NewRWMap[string, string]()
182+
properties.Put("plugins", "dev")
183+
plugin := plugins.NewDeveloperConnectionPlugin(mockPluginService, properties)
184+
mockMethodCallback := mock_error_simulator.NewMockErrorSimulatorMethodCallback(ctrl)
185+
mockConnectFunc := func(props *utils.RWMap[string, string]) (driver.Conn, error) {
186+
return nil, nil
187+
}
188+
mockExecuteFunc := func() (any, any, bool, error) {
189+
return nil, nil, true, nil
190+
}
191+
192+
devPlugin := plugin.(*plugins.DeveloperConnectionPlugin)
193+
devPlugin.SetCallback(mockMethodCallback)
194+
195+
mockArgs := []any{"test", "employees"}
196+
mockMethodCallback.EXPECT().GetErrorToRaise("query", gomock.Any()).Return(testError)
197+
mockMethodCallback.EXPECT().GetErrorToRaise("query", gomock.Any()).Return(nil)
198+
199+
// Should connect successfully
200+
_, err := plugin.Connect(hostInfo, properties, false, mockConnectFunc)
201+
assert.NoError(t, err)
202+
203+
// Should fail with test error
204+
_, _, _, err = plugin.Execute(nil, "query", mockExecuteFunc, mockArgs...)
205+
assert.Equal(t, testError, err)
206+
207+
// Should execute successfully with different args
208+
_, _, _, err = plugin.Execute(nil, "query", mockExecuteFunc, "test", "admin")
209+
assert.NoError(t, err)
210+
})
211+
212+
t.Run("testRaiseNoErrorWithCallback", func(t *testing.T) {
213+
ctrl := gomock.NewController(t)
214+
defer ctrl.Finish()
215+
error_simulator.ResetErrorSimulatorManager()
216+
217+
mockPluginService := mock_driver_infrastructure.NewMockPluginService(ctrl)
218+
properties := utils.NewRWMap[string, string]()
219+
properties.Put("plugins", "dev")
220+
plugin := plugins.NewDeveloperConnectionPlugin(mockPluginService, properties)
221+
mockMethodCallback := mock_error_simulator.NewMockErrorSimulatorMethodCallback(ctrl)
222+
mockConnectFunc := func(props *utils.RWMap[string, string]) (driver.Conn, error) {
223+
return nil, nil
224+
}
225+
mockExecuteFunc := func() (any, any, bool, error) {
226+
return nil, nil, true, nil
227+
}
228+
229+
devPlugin := plugin.(*plugins.DeveloperConnectionPlugin)
230+
devPlugin.SetCallback(mockMethodCallback)
231+
232+
mockArgs := []any{"test", "employees"}
233+
mockMethodCallback.EXPECT().GetErrorToRaise("close", gomock.Any()).Return(nil).Times(2)
234+
235+
// Should connect successfully
236+
_, err := plugin.Connect(hostInfo, properties, false, mockConnectFunc)
237+
assert.NoError(t, err)
238+
239+
// Should execute successfully (different method)
240+
_, _, _, err = plugin.Execute(nil, "close", mockExecuteFunc, mockArgs...)
241+
assert.NoError(t, err)
242+
243+
// Should execute successfully
244+
_, _, _, err = plugin.Execute(nil, "close", mockExecuteFunc, "test", "admin")
245+
assert.NoError(t, err)
246+
})
247+
248+
t.Run("testRaiseErrorOnConnect", func(t *testing.T) {
249+
ctrl := gomock.NewController(t)
250+
defer ctrl.Finish()
251+
error_simulator.ResetErrorSimulatorManager()
252+
253+
mockPluginService := mock_driver_infrastructure.NewMockPluginService(ctrl)
254+
properties := utils.NewRWMap[string, string]()
255+
properties.Put("plugins", "dev")
256+
plugin := plugins.NewDeveloperConnectionPlugin(mockPluginService, properties)
257+
mockConnectFunc := func(props *utils.RWMap[string, string]) (driver.Conn, error) {
258+
return nil, nil
259+
}
260+
261+
errorSimulatorManager := error_simulator.GetErrorSimulatorManager()
262+
errorSimulatorManager.RaiseErrorOnNextConnect(testError)
263+
264+
// Should fail with test error
265+
_, err := plugin.Connect(hostInfo, properties, false, mockConnectFunc)
266+
assert.Equal(t, testError, err)
267+
268+
// Should connect successfully on second attempt
269+
_, err = plugin.Connect(hostInfo, properties, false, mockConnectFunc)
270+
assert.NoError(t, err)
271+
})
272+
273+
t.Run("testNoErrorOnConnectWithCallback", func(t *testing.T) {
274+
ctrl := gomock.NewController(t)
275+
defer ctrl.Finish()
276+
error_simulator.ResetErrorSimulatorManager()
277+
278+
mockPluginService := mock_driver_infrastructure.NewMockPluginService(ctrl)
279+
properties := utils.NewRWMap[string, string]()
280+
properties.Put("plugins", "dev")
281+
plugin := plugins.NewDeveloperConnectionPlugin(mockPluginService, properties)
282+
mockConnectCallback := mock_error_simulator.NewMockErrorSimulatorConnectCallback(ctrl)
283+
mockConnectFunc := func(props *utils.RWMap[string, string]) (driver.Conn, error) {
284+
return nil, nil
285+
}
286+
287+
errorSimulatorManager := error_simulator.GetErrorSimulatorManager()
288+
errorSimulatorManager.SetCallback(mockConnectCallback)
289+
290+
mockConnectCallback.EXPECT().GetErrorToRaise(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
291+
292+
// Should connect successfully
293+
_, err := plugin.Connect(hostInfo, properties, false, mockConnectFunc)
294+
assert.NoError(t, err)
295+
})
296+
297+
t.Run("testRaiseErrorOnConnectWithCallback", func(t *testing.T) {
298+
ctrl := gomock.NewController(t)
299+
defer ctrl.Finish()
300+
error_simulator.ResetErrorSimulatorManager()
301+
302+
mockPluginService := mock_driver_infrastructure.NewMockPluginService(ctrl)
303+
properties := utils.NewRWMap[string, string]()
304+
properties.Put("plugins", "dev")
305+
plugin := plugins.NewDeveloperConnectionPlugin(mockPluginService, properties)
306+
mockConnectCallback := mock_error_simulator.NewMockErrorSimulatorConnectCallback(ctrl)
307+
mockConnectFunc := func(props *utils.RWMap[string, string]) (driver.Conn, error) {
308+
return nil, nil
309+
}
310+
311+
errorSimulatorManager := error_simulator.GetErrorSimulatorManager()
312+
errorSimulatorManager.SetCallback(mockConnectCallback)
313+
314+
gomock.InOrder(
315+
mockConnectCallback.EXPECT().GetErrorToRaise(gomock.Any(), gomock.Any(), gomock.Any()).Return(testError),
316+
mockConnectCallback.EXPECT().GetErrorToRaise(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil),
317+
)
318+
319+
// Should fail with test error
320+
_, err := plugin.Connect(hostInfo, properties, false, mockConnectFunc)
321+
assert.Equal(t, testError, err)
322+
323+
// Should connect successfully on second attempt
324+
_, err = plugin.Connect(hostInfo, properties, false, mockConnectFunc)
325+
assert.NoError(t, err)
326+
})
327+
}

.test/test/implementations_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ func TestImplementations(t *testing.T) {
9191
var _ driver_infrastructure.ConnectionPlugin = (*bg.BlueGreenPlugin)(nil)
9292
var _ driver_infrastructure.ConnectionPlugin = (*plugins.ConnectTimePlugin)(nil)
9393
var _ driver_infrastructure.ConnectionPlugin = (*plugins.ExecutionTimePlugin)(nil)
94+
var _ driver_infrastructure.ConnectionPlugin = (*plugins.AuroraConnectionTrackerPlugin)(nil)
95+
var _ driver_infrastructure.ConnectionPlugin = (*plugins.DeveloperConnectionPlugin)(nil)
96+
var _ driver_infrastructure.ConnectionPluginFactory = (*plugins.AuroraConnectionTrackerPluginFactory)(nil)
97+
var _ driver_infrastructure.ConnectionPluginFactory = (*plugins.DeveloperConnectionPluginFactory)(nil)
9498
var _ driver_infrastructure.ConnectionPluginFactory = (*plugins.ConnectTimePluginFactory)(nil)
9599
var _ driver_infrastructure.ConnectionPluginFactory = (*plugins.ExecutionTimePluginFactory)(nil)
96100
var _ driver_infrastructure.ConnectionPluginFactory = (*efm.HostMonitoringPluginFactory)(nil)

.test/test/mocks/awssql/plugins/error_simulator/mock_error_simulator_connect_callback.go

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)