forked from googleapis/mcp-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoracle_test.go
More file actions
313 lines (300 loc) · 8.8 KB
/
oracle_test.go
File metadata and controls
313 lines (300 loc) · 8.8 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright © 2025, Oracle and/or its affiliates.
package oracle
import (
"context"
"database/sql"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/googleapis/genai-toolbox/internal/server"
"github.com/googleapis/genai-toolbox/internal/sources"
"github.com/googleapis/genai-toolbox/internal/testutils"
)
func TestParseFromYamlOracle(t *testing.T) {
tcs := []struct {
desc string
in string
want server.SourceConfigs
}{
{
desc: "connection string and useOCI=true",
in: `
kind: sources
name: my-oracle-cs
type: oracle
connectionString: "my-host:1521/XEPDB1"
user: my_user
password: my_pass
useOCI: true
`,
want: map[string]sources.SourceConfig{
"my-oracle-cs": Config{
Name: "my-oracle-cs",
Type: SourceType,
ConnectionString: "my-host:1521/XEPDB1",
User: "my_user",
Password: "my_pass",
UseOCI: true,
},
},
},
{
desc: "host/port/serviceName and default useOCI=false",
in: `
kind: sources
name: my-oracle-host
type: oracle
host: my-host
port: 1521
serviceName: ORCLPDB
user: my_user
password: my_pass
`,
want: map[string]sources.SourceConfig{
"my-oracle-host": Config{
Name: "my-oracle-host",
Type: SourceType,
Host: "my-host",
Port: 1521,
ServiceName: "ORCLPDB",
User: "my_user",
Password: "my_pass",
UseOCI: false,
},
},
},
{
desc: "tnsAlias and TnsAdmin specified with explicit useOCI=true",
in: `
kind: sources
name: my-oracle-tns-oci
type: oracle
tnsAlias: FINANCE_DB
tnsAdmin: /opt/oracle/network/admin
user: my_user
password: my_pass
useOCI: true
`,
want: map[string]sources.SourceConfig{
"my-oracle-tns-oci": Config{
Name: "my-oracle-tns-oci",
Type: SourceType,
TnsAlias: "FINANCE_DB",
TnsAdmin: "/opt/oracle/network/admin",
User: "my_user",
Password: "my_pass",
UseOCI: true,
},
},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
got, _, _, _, _, _, err := server.UnmarshalResourceConfig(context.Background(), testutils.FormatYaml(tc.in))
if err != nil {
t.Fatalf("unable to unmarshal: %s", err)
}
if !cmp.Equal(tc.want, got) {
t.Fatalf("incorrect parse:\nwant: %v\ngot: %v\ndiff: %s", tc.want, got, cmp.Diff(tc.want, got))
}
})
}
}
func TestFailParseFromYaml(t *testing.T) {
tcs := []struct {
desc string
in string
err string
}{
{
desc: "extra field",
in: `
kind: sources
name: my-oracle-instance
type: oracle
host: my-host
serviceName: ORCL
user: my_user
password: my_pass
extraField: value
`,
err: "error unmarshaling sources: unable to parse source \"my-oracle-instance\" as \"oracle\": [1:1] unknown field \"extraField\"\n> 1 | extraField: value\n ^\n 2 | host: my-host\n 3 | name: my-oracle-instance\n 4 | password: my_pass\n 5 | ",
},
{
desc: "missing required password field",
in: `
kind: sources
name: my-oracle-instance
type: oracle
host: my-host
serviceName: ORCL
user: my_user
`,
err: "error unmarshaling sources: unable to parse source \"my-oracle-instance\" as \"oracle\": Key: 'Config.Password' Error:Field validation for 'Password' failed on the 'required' tag",
},
{
desc: "missing connection method fields (validate fails)",
in: `
kind: sources
name: my-oracle-instance
type: oracle
user: my_user
password: my_pass
`,
err: "error unmarshaling sources: unable to parse source \"my-oracle-instance\" as \"oracle\": invalid Oracle configuration: must provide one of: 'tns_alias', 'connection_string', or both 'host' and 'service_name'",
},
{
desc: "multiple connection methods provided (validate fails)",
in: `
kind: sources
name: my-oracle-instance
type: oracle
host: my-host
serviceName: ORCL
connectionString: "my-host:1521/XEPDB1"
user: my_user
password: my_pass
`,
err: "error unmarshaling sources: unable to parse source \"my-oracle-instance\" as \"oracle\": invalid Oracle configuration: provide only one connection method: 'tns_alias', 'connection_string', or 'host'+'service_name'",
},
{
desc: "fail on tnsAdmin with useOCI=false",
in: `
kind: sources
name: my-oracle-fail
type: oracle
tnsAlias: FINANCE_DB
tnsAdmin: /opt/oracle/network/admin
user: my_user
password: my_pass
useOCI: false
`,
err: "error unmarshaling sources: unable to parse source \"my-oracle-fail\" as \"oracle\": invalid Oracle configuration: `tnsAdmin` can only be used when `UseOCI` is true, or use `walletLocation` instead",
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
_, _, _, _, _, _, err := server.UnmarshalResourceConfig(context.Background(), testutils.FormatYaml(tc.in))
if err == nil {
t.Fatalf("expect parsing to fail")
}
errStr := strings.ReplaceAll(err.Error(), "\r", "")
if errStr != tc.err {
t.Fatalf("unexpected error:\ngot:\n%q\nwant:\n%q\n", errStr, tc.err)
}
})
}
}
// TestRunSQLExecutesDML verifies that RunSQL correctly routes operations to
// ExecContext instead of QueryContext when the readOnly flag is set to false.
func TestRunSQLExecutesDML(t *testing.T) {
// Initialize a mock database connection.
// This connection is not established with a real backend but
// satisfies the interface requirements for the test.
db, err := sql.Open("oracle", "oracle://user:pass@localhost:1521/service")
if err != nil {
t.Fatalf("failed to open mock db: %v", err)
}
defer db.Close()
src := &Source{
Config: Config{
Name: "test-dml-source",
Type: SourceType,
User: "test-user",
},
DB: db,
}
// Invoke RunSQL with readOnly=false to force the DML execution path.
_, err = src.RunSQL(context.Background(),
"UPDATE users SET email='x' WHERE id=1", nil, false)
// We expect an error because the mock database cannot execute the query.
// If err is nil, it implies the logic skipped the execution block.
if err == nil {
t.Fatal("expected error from fake DB execution, but got nil; " +
"DML path may not have been executed")
}
}
func TestBuildGoOraConnString(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
user string
password string
connBase string
walletLocation string
want string
}{
{
name: "encodes credentials and wallet",
user: "user[client]",
password: "pa:ss@word",
connBase: "dbhost:1521/XEPDB1",
walletLocation: "/tmp/my wallet",
want: "oracle://user%5Bclient%5D:pa%3Ass%40word@dbhost:1521/XEPDB1?ssl=true&wallet=%2Ftmp%2Fmy+wallet",
},
{
name: "no wallet",
user: "scott",
password: "tiger",
connBase: "dbhost:1521/ORCL",
walletLocation: "",
want: "oracle://scott:tiger@dbhost:1521/ORCL",
},
{
name: "does not double encode percent encoded user",
user: "app_user%5BCLIENT_A%5D",
password: "secret",
connBase: "dbhost:1521/ORCL",
walletLocation: "",
want: "oracle://app_user%5BCLIENT_A%5D:secret@dbhost:1521/ORCL",
},
{
name: "uses trimmed wallet location",
user: "scott",
password: "tiger",
connBase: "dbhost:1521/ORCL",
walletLocation: " /tmp/wallet ",
want: "oracle://scott:tiger@dbhost:1521/ORCL?ssl=true&wallet=%2Ftmp%2Fwallet",
},
{
name: "preserves existing query without wallet",
user: "scott",
password: "tiger",
connBase: "dbhost:1521/ORCL?transport_connect_timeout=30",
walletLocation: "",
want: "oracle://scott:tiger@dbhost:1521/ORCL?transport_connect_timeout=30",
},
{
name: "merges existing query with wallet",
user: "scott",
password: "tiger",
connBase: "dbhost:1521/ORCL?foo=bar",
walletLocation: "/tmp/wallet",
want: "oracle://scott:tiger@dbhost:1521/ORCL?foo=bar&ssl=true&wallet=%2Ftmp%2Fwallet",
},
{
name: "preserves malformed existing query when appending wallet",
user: "scott",
password: "tiger",
connBase: "dbhost:1521/ORCL?already=%ZZ",
walletLocation: "/tmp/wallet",
want: "oracle://scott:tiger@dbhost:1521/ORCL?already=%ZZ&ssl=true&wallet=%2Ftmp%2Fwallet",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
config := Config{
User: tc.user,
Password: tc.password,
ConnectionString: tc.connBase,
WalletLocation: tc.walletLocation,
}
got, _ := buildGoOraConnString(config)
if got != tc.want {
t.Fatalf("buildGoOraConnString() = %q, want %q", got, tc.want)
}
})
}
}