-
Notifications
You must be signed in to change notification settings - Fork 1.5k
test(source/cloud-sql-mysql): create MCP integration tests #2922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dishaprakash
wants to merge
31
commits into
main
Choose a base branch
from
mcp-cloud-sql-mysql-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
e9cc753
test(source/cloud-sql-mysql): create MCP integration tests
dishaprakash 7cdc345
gemini review + fix tests
dishaprakash d329ca4
fix tests
dishaprakash 1e1b12c
add mcpexec flag to run mysql tests with mcp
dishaprakash f1db911
revert mssql test deletion
dishaprakash 039658c
parity with api tests
dishaprakash 9b3d686
add json aware comparision
dishaprakash 6b9eae5
fix tetss
dishaprakash d0a64e2
fix
dishaprakash 4327892
changes
dishaprakash c261115
fix
dishaprakash 8621690
tests fix
dishaprakash 0b0fa11
fix tests
dishaprakash 4fd8144
fix tests
dishaprakash b9d1a30
fix cockroachdb
dishaprakash 4929422
fix tests
dishaprakash 8950c2f
fix tests
dishaprakash d4ab394
fix tests
dishaprakash 8588d48
code comments
dishaprakash cbbc657
apply rules
dishaprakash 77c4689
simplify options
dishaprakash 9ece0fd
fix imports
dishaprakash 7d807c6
Update option.go
dishaprakash 27f66d5
update repo name
dishaprakash 2f530c1
review changes
dishaprakash ce49676
fix tests
dishaprakash c9108fc
linter
dishaprakash 53b15f1
update null for tests
dishaprakash bc506e9
linter fix
dishaprakash 0910acf
rebase to main and copy helper from alloydbpg pr
dishaprakash cb30b82
linter
dishaprakash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
261 changes: 261 additions & 0 deletions
261
tests/cloudsqlmysql/cloud_sql_mysql_create_instance_mcp_integration_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| // 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 cloudsqlmysql_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "regexp" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/googleapis/mcp-toolbox/internal/testutils" | ||
| "github.com/googleapis/mcp-toolbox/tests" | ||
| "google.golang.org/api/sqladmin/v1" | ||
| ) | ||
|
|
||
| const createInstanceToolTypeMCP = "cloud-sql-mysql-create-instance" | ||
|
|
||
| type createInstanceTransportMCP struct { | ||
| transport http.RoundTripper | ||
| url *url.URL | ||
| } | ||
|
|
||
| func (t *createInstanceTransportMCP) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| if strings.HasPrefix(req.URL.String(), "https://sqladmin.googleapis.com") { | ||
| req.URL.Scheme = t.url.Scheme | ||
| req.URL.Host = t.url.Host | ||
| } | ||
| return t.transport.RoundTrip(req) | ||
| } | ||
|
|
||
| type masterHandlerMCP struct { | ||
| t *testing.T | ||
| } | ||
|
|
||
| func (h *masterHandlerMCP) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
| if !strings.Contains(r.UserAgent(), "genai-toolbox/") { | ||
| h.t.Errorf("User-Agent header not found") | ||
| } | ||
|
|
||
| var body sqladmin.DatabaseInstance | ||
| if err := json.NewDecoder(r.Body).Decode(&body); err != nil { | ||
| h.t.Fatalf("failed to decode request body: %v", err) | ||
| } | ||
|
|
||
| instanceName := body.Name | ||
| if instanceName == "" { | ||
| http.Error(w, "missing instance name", http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| var expectedBody sqladmin.DatabaseInstance | ||
| var response any | ||
| var statusCode int | ||
|
|
||
| switch instanceName { | ||
| case "instance1": | ||
| expectedBody = sqladmin.DatabaseInstance{ | ||
| Project: "p1", | ||
| Name: "instance1", | ||
| DatabaseVersion: "MYSQL_8_0", | ||
| RootPassword: "password123", | ||
| Settings: &sqladmin.Settings{ | ||
| AvailabilityType: "REGIONAL", | ||
| Edition: "ENTERPRISE_PLUS", | ||
| Tier: "db-perf-optimized-N-8", | ||
| DataDiskSizeGb: 250, | ||
| DataDiskType: "PD_SSD", | ||
| }, | ||
| } | ||
| response = map[string]any{"name": "op1", "status": "PENDING"} | ||
| statusCode = http.StatusOK | ||
| case "instance2": | ||
| expectedBody = sqladmin.DatabaseInstance{ | ||
| Project: "p2", | ||
| Name: "instance2", | ||
| DatabaseVersion: "MYSQL_8_4", | ||
| RootPassword: "password456", | ||
| Settings: &sqladmin.Settings{ | ||
| AvailabilityType: "ZONAL", | ||
| Edition: "ENTERPRISE_PLUS", | ||
| Tier: "db-perf-optimized-N-2", | ||
| DataDiskSizeGb: 100, | ||
| DataDiskType: "PD_SSD", | ||
| }, | ||
| } | ||
| response = map[string]any{"name": "op2", "status": "RUNNING"} | ||
| statusCode = http.StatusOK | ||
| default: | ||
| http.Error(w, fmt.Sprintf("unhandled instance name: %s", instanceName), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| if expectedBody.Project != body.Project { | ||
| h.t.Errorf("unexpected project: got %q, want %q", body.Project, expectedBody.Project) | ||
| } | ||
| if expectedBody.Name != body.Name { | ||
| h.t.Errorf("unexpected name: got %q, want %q", body.Name, expectedBody.Name) | ||
| } | ||
| if expectedBody.DatabaseVersion != body.DatabaseVersion { | ||
| h.t.Errorf("unexpected databaseVersion: got %q, want %q", body.DatabaseVersion, expectedBody.DatabaseVersion) | ||
| } | ||
| if expectedBody.RootPassword != body.RootPassword { | ||
| h.t.Errorf("unexpected rootPassword: got %q, want %q", body.RootPassword, expectedBody.RootPassword) | ||
| } | ||
| if diff := cmp.Diff(expectedBody.Settings, body.Settings); diff != "" { | ||
| h.t.Errorf("unexpected request body settings (-want +got):\n%s", diff) | ||
| } | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| w.WriteHeader(statusCode) | ||
| if err := json.NewEncoder(w).Encode(response); err != nil { | ||
| http.Error(w, err.Error(), http.StatusInternalServerError) | ||
| } | ||
| } | ||
|
|
||
| func TestCreateInstanceToolEndpointsMCP(t *testing.T) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), time.Minute) | ||
| defer cancel() | ||
|
|
||
| handler := &masterHandlerMCP{t: t} | ||
| server := httptest.NewServer(handler) | ||
| defer server.Close() | ||
|
|
||
| serverURL, err := url.Parse(server.URL) | ||
| if err != nil { | ||
| t.Fatalf("failed to parse server URL: %v", err) | ||
| } | ||
|
|
||
| originalTransport := http.DefaultClient.Transport | ||
| if originalTransport == nil { | ||
| originalTransport = http.DefaultTransport | ||
| } | ||
| http.DefaultClient.Transport = &createInstanceTransportMCP{ | ||
| transport: originalTransport, | ||
| url: serverURL, | ||
| } | ||
| t.Cleanup(func() { | ||
| http.DefaultClient.Transport = originalTransport | ||
| }) | ||
|
|
||
| toolsFile := getCreateInstanceToolsConfigMCP() | ||
| cmd, cleanup, err := tests.StartCmd(ctx, toolsFile) | ||
| if err != nil { | ||
| t.Fatalf("command initialization returned an error: %v", err) | ||
| } | ||
| defer cleanup() | ||
|
|
||
| waitCtx, cancelWait := context.WithTimeout(ctx, 10*time.Second) | ||
| defer cancelWait() | ||
| 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: %v", err) | ||
| } | ||
|
|
||
| tcs := []struct { | ||
| name string | ||
| toolName string | ||
| body string | ||
| want string | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "verify successful instance creation with production preset", | ||
| toolName: "create-instance-prod", | ||
| body: `{"project": "p1", "name": "instance1", "databaseVersion": "MYSQL_8_0", "rootPassword": "password123", "editionPreset": "Production"}`, | ||
| want: `{"name":"op1","status":"PENDING"}`, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "verify successful instance creation with development preset", | ||
| toolName: "create-instance-dev", | ||
| body: `{"project": "p2", "name": "instance2", "rootPassword": "password456", "editionPreset": "Development"}`, | ||
| want: `{"name":"op2","status":"RUNNING"}`, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "verify missing required parameter returns schema error", | ||
| toolName: "create-instance-prod", | ||
| body: `{"name": "instance1"}`, | ||
| want: `parameter "project" is required`, | ||
| expectError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tcs { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var args map[string]any | ||
| if err := json.Unmarshal([]byte(tc.body), &args); err != nil { | ||
| t.Fatalf("failed to unmarshal body: %v", err) | ||
| } | ||
|
|
||
| statusCode, mcpResp, err := tests.InvokeMCPTool(t, tc.toolName, args, nil) | ||
| if err != nil { | ||
| t.Fatalf("native error executing %s: %v", tc.toolName, err) | ||
| } | ||
|
|
||
| if statusCode != http.StatusOK { | ||
| t.Fatalf("expected status 200, got %d", statusCode) | ||
| } | ||
|
|
||
| if tc.expectError { | ||
| tests.AssertMCPError(t, mcpResp, tc.want) | ||
| } else { | ||
| if mcpResp.Result.IsError { | ||
| t.Fatalf("expected success, got error result: %v", mcpResp.Result) | ||
| } | ||
| gotStr := mcpResp.Result.Content[0].Text | ||
| var got, want map[string]any | ||
| if err := json.Unmarshal([]byte(gotStr), &got); err != nil { | ||
| t.Fatalf("failed to unmarshal result: %v", err) | ||
| } | ||
| if err := json.Unmarshal([]byte(tc.want), &want); err != nil { | ||
| t.Fatalf("failed to unmarshal want: %v", err) | ||
| } | ||
| if diff := cmp.Diff(want, got); diff != "" { | ||
| t.Errorf("unexpected result (-want +got):\n%s", diff) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func getCreateInstanceToolsConfigMCP() map[string]any { | ||
| return map[string]any{ | ||
| "sources": map[string]any{ | ||
| "my-cloud-sql-source": map[string]any{ | ||
| "type": "cloud-sql-admin", | ||
| }, | ||
| }, | ||
| "tools": map[string]any{ | ||
| "create-instance-prod": map[string]any{ | ||
| "type": createInstanceToolTypeMCP, | ||
| "source": "my-cloud-sql-source", | ||
| }, | ||
| "create-instance-dev": map[string]any{ | ||
| "type": createInstanceToolTypeMCP, | ||
| "source": "my-cloud-sql-source", | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.