mirror of
https://github.com/googleapis/genai-toolbox.git
synced 2026-01-09 07:28:05 -05:00
Adds the following tools for Postgres: (1) list_stored_procedure: Retrieves stored procedure metadata returning schema name, procedure name, procedure owner, language, definition, and description, filtered by optional role name (procedure owner), schema name, and limit (default 20). <img width="3808" height="1181" alt="image" src="https://github.com/user-attachments/assets/43513a04-95ce-478f-a59f-3e5dafdb6b23" /> <img width="2654" height="1288" alt="image" src="https://github.com/user-attachments/assets/84aca162-3779-4daa-ae2f-61620560589f" /> > 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: - [x] Make sure you reviewed [CONTRIBUTING.md](https://github.com/googleapis/genai-toolbox/blob/main/CONTRIBUTING.md) - [x] 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 - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) - [x] Make sure to add `!` if this involve a breaking change 🛠️ Fixes #1738
169 lines
6.2 KiB
Go
169 lines
6.2 KiB
Go
// Copyright 2024 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 postgres
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/googleapis/genai-toolbox/internal/testutils"
|
|
"github.com/googleapis/genai-toolbox/tests"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
var (
|
|
PostgresSourceKind = "postgres"
|
|
PostgresToolKind = "postgres-sql"
|
|
PostgresDatabase = os.Getenv("POSTGRES_DATABASE")
|
|
PostgresHost = os.Getenv("POSTGRES_HOST")
|
|
PostgresPort = os.Getenv("POSTGRES_PORT")
|
|
PostgresUser = os.Getenv("POSTGRES_USER")
|
|
PostgresPass = os.Getenv("POSTGRES_PASS")
|
|
)
|
|
|
|
func getPostgresVars(t *testing.T) map[string]any {
|
|
switch "" {
|
|
case PostgresDatabase:
|
|
t.Fatal("'POSTGRES_DATABASE' not set")
|
|
case PostgresHost:
|
|
t.Fatal("'POSTGRES_HOST' not set")
|
|
case PostgresPort:
|
|
t.Fatal("'POSTGRES_PORT' not set")
|
|
case PostgresUser:
|
|
t.Fatal("'POSTGRES_USER' not set")
|
|
case PostgresPass:
|
|
t.Fatal("'POSTGRES_PASS' not set")
|
|
}
|
|
|
|
return map[string]any{
|
|
"kind": PostgresSourceKind,
|
|
"host": PostgresHost,
|
|
"port": PostgresPort,
|
|
"database": PostgresDatabase,
|
|
"user": PostgresUser,
|
|
"password": PostgresPass,
|
|
}
|
|
}
|
|
|
|
// Copied over from postgres.go
|
|
func initPostgresConnectionPool(host, port, user, pass, dbname string) (*pgxpool.Pool, error) {
|
|
// urlExample := "postgres:dd//username:password@localhost:5432/database_name"
|
|
url := &url.URL{
|
|
Scheme: "postgres",
|
|
User: url.UserPassword(user, pass),
|
|
Host: fmt.Sprintf("%s:%s", host, port),
|
|
Path: dbname,
|
|
}
|
|
pool, err := pgxpool.New(context.Background(), url.String())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Unable to create connection pool: %w", err)
|
|
}
|
|
|
|
return pool, nil
|
|
}
|
|
|
|
func TestPostgres(t *testing.T) {
|
|
sourceConfig := getPostgresVars(t)
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
|
|
var args []string
|
|
|
|
pool, err := initPostgresConnectionPool(PostgresHost, PostgresPort, PostgresUser, PostgresPass, PostgresDatabase)
|
|
if err != nil {
|
|
t.Fatalf("unable to create postgres connection pool: %s", err)
|
|
}
|
|
|
|
// cleanup test environment
|
|
tests.CleanupPostgresTables(t, ctx, pool)
|
|
|
|
// create table name with UUID
|
|
tableNameParam := "param_table_" + strings.ReplaceAll(uuid.New().String(), "-", "")
|
|
tableNameAuth := "auth_table_" + strings.ReplaceAll(uuid.New().String(), "-", "")
|
|
tableNameTemplateParam := "template_param_table_" + strings.ReplaceAll(uuid.New().String(), "-", "")
|
|
|
|
// set up data for param tool
|
|
createParamTableStmt, insertParamTableStmt, paramToolStmt, idParamToolStmt, nameParamToolStmt, arrayToolStmt, paramTestParams := tests.GetPostgresSQLParamToolInfo(tableNameParam)
|
|
teardownTable1 := tests.SetupPostgresSQLTable(t, ctx, pool, createParamTableStmt, insertParamTableStmt, tableNameParam, paramTestParams)
|
|
defer teardownTable1(t)
|
|
|
|
// set up data for auth tool
|
|
createAuthTableStmt, insertAuthTableStmt, authToolStmt, authTestParams := tests.GetPostgresSQLAuthToolInfo(tableNameAuth)
|
|
teardownTable2 := tests.SetupPostgresSQLTable(t, ctx, pool, createAuthTableStmt, insertAuthTableStmt, tableNameAuth, authTestParams)
|
|
defer teardownTable2(t)
|
|
|
|
// Write config into a file and pass it to command
|
|
toolsFile := tests.GetToolsConfig(sourceConfig, PostgresToolKind, paramToolStmt, idParamToolStmt, nameParamToolStmt, arrayToolStmt, authToolStmt)
|
|
toolsFile = tests.AddExecuteSqlConfig(t, toolsFile, "postgres-execute-sql")
|
|
tmplSelectCombined, tmplSelectFilterCombined := tests.GetPostgresSQLTmplToolStatement()
|
|
toolsFile = tests.AddTemplateParamConfig(t, toolsFile, PostgresToolKind, tmplSelectCombined, tmplSelectFilterCombined, "")
|
|
toolsFile = tests.AddPostgresPrebuiltConfig(t, toolsFile)
|
|
|
|
cmd, cleanup, err := tests.StartCmd(ctx, toolsFile, args...)
|
|
if err != nil {
|
|
t.Fatalf("command initialization returned an error: %s", err)
|
|
}
|
|
defer cleanup()
|
|
|
|
waitCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
out, err := testutils.WaitForString(waitCtx, regexp.MustCompile(`Server ready to serve`), cmd.Out)
|
|
if err != nil {
|
|
t.Logf("toolbox command logs: \n%s", out)
|
|
t.Fatalf("toolbox didn't start successfully: %s", err)
|
|
}
|
|
|
|
// Get configs for tests
|
|
select1Want, mcpMyFailToolWant, createTableStatement, mcpSelect1Want := tests.GetPostgresWants()
|
|
|
|
// Run tests
|
|
tests.RunToolGetTest(t)
|
|
tests.RunToolInvokeTest(t, select1Want)
|
|
tests.RunMCPToolCallMethod(t, mcpMyFailToolWant, mcpSelect1Want)
|
|
tests.RunExecuteSqlToolInvokeTest(t, createTableStatement, select1Want)
|
|
tests.RunToolInvokeWithTemplateParameters(t, tableNameTemplateParam)
|
|
|
|
// Run Postgres prebuilt tool tests
|
|
tests.RunPostgresListTablesTest(t, tableNameParam, tableNameAuth, PostgresUser)
|
|
tests.RunPostgresListViewsTest(t, ctx, pool)
|
|
tests.RunPostgresListSchemasTest(t, ctx, pool)
|
|
tests.RunPostgresListActiveQueriesTest(t, ctx, pool)
|
|
tests.RunPostgresListAvailableExtensionsTest(t)
|
|
tests.RunPostgresListInstalledExtensionsTest(t)
|
|
tests.RunPostgresDatabaseOverviewTest(t, ctx, pool)
|
|
tests.RunPostgresListTriggersTest(t, ctx, pool)
|
|
tests.RunPostgresListIndexesTest(t, ctx, pool)
|
|
tests.RunPostgresListSequencesTest(t, ctx, pool)
|
|
tests.RunPostgresLongRunningTransactionsTest(t, ctx, pool)
|
|
tests.RunPostgresListLocksTest(t, ctx, pool)
|
|
tests.RunPostgresReplicationStatsTest(t, ctx, pool)
|
|
tests.RunPostgresListQueryStatsTest(t, ctx, pool)
|
|
tests.RunPostgresGetColumnCardinalityTest(t, ctx, pool)
|
|
tests.RunPostgresListTableStatsTest(t, ctx, pool)
|
|
tests.RunPostgresListPublicationTablesTest(t, ctx, pool)
|
|
tests.RunPostgresListTableSpacesTest(t)
|
|
tests.RunPostgresListPgSettingsTest(t, ctx, pool)
|
|
tests.RunPostgresListDatabaseStatsTest(t, ctx, pool)
|
|
tests.RunPostgresListRolesTest(t, ctx, pool)
|
|
tests.RunPostgresListStoredProcedureTest(t, ctx, pool)
|
|
}
|