mirror of
https://github.com/googleapis/genai-toolbox.git
synced 2026-05-02 03:00:36 -04:00
Add support for CockroachDB v25.4.0+ using the official cockroach-go/v2 library for automatic transaction retry. - Add CockroachDB source with ExecuteTxWithRetry using crdbpgx.ExecuteTx - Implement 4 tools: execute-sql, sql, list-tables, list-schemas - Use UUID primary keys (CockroachDB best practice) - Add unit tests for source and all tools - Add integration tests with retry verification - Update Cloud Build configuration for CI Fixes #2005 ## Description > Should include a concise description of the changes (bug or feature), it's > impact, along with a summary of the solution ## PR Checklist > Thank you for opening a Pull Request! Before submitting your PR, there are a > few things you can do to make sure it goes smoothly: - [ ] Make sure you reviewed [CONTRIBUTING.md](https://github.com/googleapis/genai-toolbox/blob/main/CONTRIBUTING.md) - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/genai-toolbox/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) - [ ] Make sure to add `!` if this involve a breaking change 🛠️ Fixes #<issue_number_goes_here> --------- Co-authored-by: duwenxin99 <duwenxin@google.com> Co-authored-by: Wenxin Du <117315983+duwenxin99@users.noreply.github.com>
225 lines
4.7 KiB
Go
225 lines
4.7 KiB
Go
// Copyright 2026 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package cockroachdb
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/goccy/go-yaml"
|
|
)
|
|
|
|
func TestCockroachDBSourceConfig(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
yaml string
|
|
}{
|
|
{
|
|
name: "valid config",
|
|
yaml: `
|
|
name: test-cockroachdb
|
|
type: cockroachdb
|
|
host: localhost
|
|
port: "26257"
|
|
user: root
|
|
password: ""
|
|
database: defaultdb
|
|
maxRetries: 5
|
|
retryBaseDelay: 500ms
|
|
queryParams:
|
|
sslmode: disable
|
|
`,
|
|
},
|
|
{
|
|
name: "with optional queryParams",
|
|
yaml: `
|
|
name: test-cockroachdb
|
|
type: cockroachdb
|
|
host: localhost
|
|
port: "26257"
|
|
user: root
|
|
password: testpass
|
|
database: testdb
|
|
queryParams:
|
|
sslmode: require
|
|
sslcert: /path/to/cert
|
|
`,
|
|
},
|
|
{
|
|
name: "with custom retry settings",
|
|
yaml: `
|
|
name: test-cockroachdb
|
|
type: cockroachdb
|
|
host: localhost
|
|
port: "26257"
|
|
user: root
|
|
password: ""
|
|
database: defaultdb
|
|
maxRetries: 10
|
|
retryBaseDelay: 1s
|
|
`,
|
|
},
|
|
{
|
|
name: "without password (insecure mode)",
|
|
yaml: `
|
|
name: test-cockroachdb
|
|
type: cockroachdb
|
|
host: localhost
|
|
port: "26257"
|
|
user: root
|
|
database: defaultdb
|
|
`,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
decoder := yaml.NewDecoder(strings.NewReader(tt.yaml))
|
|
cfg, err := newConfig(context.Background(), "test", decoder)
|
|
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
if cfg == nil {
|
|
t.Fatal("expected config but got nil")
|
|
}
|
|
|
|
// Verify it's the right type
|
|
cockroachCfg, ok := cfg.(Config)
|
|
if !ok {
|
|
t.Fatalf("expected Config type, got %T", cfg)
|
|
}
|
|
|
|
// Verify SourceConfigType
|
|
if cockroachCfg.SourceConfigType() != SourceType {
|
|
t.Errorf("expected SourceConfigType %q, got %q", SourceType, cockroachCfg.SourceConfigType())
|
|
}
|
|
|
|
t.Logf("✅ Config parsed successfully: %+v", cockroachCfg)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCockroachDBSourceType(t *testing.T) {
|
|
yamlContent := `
|
|
name: test-cockroachdb
|
|
type: cockroachdb
|
|
host: localhost
|
|
port: "26257"
|
|
user: root
|
|
password: ""
|
|
database: defaultdb
|
|
`
|
|
decoder := yaml.NewDecoder(strings.NewReader(yamlContent))
|
|
cfg, err := newConfig(context.Background(), "test", decoder)
|
|
if err != nil {
|
|
t.Fatalf("failed to create config: %v", err)
|
|
}
|
|
|
|
if cfg.SourceConfigType() != "cockroachdb" {
|
|
t.Errorf("expected SourceConfigType 'cockroachdb', got %q", cfg.SourceConfigType())
|
|
}
|
|
}
|
|
|
|
func TestCockroachDBDefaultValues(t *testing.T) {
|
|
yamlContent := `
|
|
name: test-cockroachdb
|
|
type: cockroachdb
|
|
host: localhost
|
|
port: "26257"
|
|
user: root
|
|
password: ""
|
|
database: defaultdb
|
|
`
|
|
decoder := yaml.NewDecoder(strings.NewReader(yamlContent))
|
|
cfg, err := newConfig(context.Background(), "test", decoder)
|
|
if err != nil {
|
|
t.Fatalf("failed to create config: %v", err)
|
|
}
|
|
|
|
cockroachCfg, ok := cfg.(Config)
|
|
if !ok {
|
|
t.Fatalf("expected Config type")
|
|
}
|
|
|
|
// Check default values
|
|
if cockroachCfg.MaxRetries != 5 {
|
|
t.Errorf("expected default MaxRetries 5, got %d", cockroachCfg.MaxRetries)
|
|
}
|
|
|
|
if cockroachCfg.RetryBaseDelay != "500ms" {
|
|
t.Errorf("expected default RetryBaseDelay '500ms', got %q", cockroachCfg.RetryBaseDelay)
|
|
}
|
|
|
|
t.Logf("✅ Default values set correctly")
|
|
}
|
|
|
|
func TestConvertParamMapToRawQuery(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
params map[string]string
|
|
want []string // Expected substrings in any order
|
|
}{
|
|
{
|
|
name: "empty params",
|
|
params: map[string]string{},
|
|
want: []string{},
|
|
},
|
|
{
|
|
name: "single param",
|
|
params: map[string]string{
|
|
"sslmode": "disable",
|
|
},
|
|
want: []string{"sslmode=disable"},
|
|
},
|
|
{
|
|
name: "multiple params",
|
|
params: map[string]string{
|
|
"sslmode": "require",
|
|
"application_name": "test-app",
|
|
},
|
|
want: []string{"sslmode=require", "application_name=test-app"},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := ConvertParamMapToRawQuery(tt.params)
|
|
|
|
if len(tt.want) == 0 {
|
|
if result != "" {
|
|
t.Errorf("expected empty string, got %q", result)
|
|
}
|
|
return
|
|
}
|
|
|
|
// Check that all expected substrings are in the result
|
|
for _, want := range tt.want {
|
|
if !contains(result, want) {
|
|
t.Errorf("expected result to contain %q, got %q", want, result)
|
|
}
|
|
}
|
|
|
|
t.Logf("✅ Query string: %s", result)
|
|
})
|
|
}
|
|
}
|
|
|
|
func contains(s, substr string) bool {
|
|
return strings.Contains(s, substr)
|
|
}
|