-
Notifications
You must be signed in to change notification settings - Fork 213
vmcp: allow embedders to inject custom session DataStorage #4933
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
JAORMX
wants to merge
2
commits into
main
Choose a base branch
from
worktree-vmcp-custom-datastorage-4928
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
2 commits
Select commit
Hold shift + click to select a range
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
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,162 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package server_test | ||
|
|
||
| import ( | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/mock/gomock" | ||
|
|
||
| transportsession "github.com/stacklok/toolhive/pkg/transport/session" | ||
| "github.com/stacklok/toolhive/pkg/vmcp" | ||
| vmcpconfig "github.com/stacklok/toolhive/pkg/vmcp/config" | ||
| discoveryMocks "github.com/stacklok/toolhive/pkg/vmcp/discovery/mocks" | ||
| "github.com/stacklok/toolhive/pkg/vmcp/mocks" | ||
| routerMocks "github.com/stacklok/toolhive/pkg/vmcp/router/mocks" | ||
| "github.com/stacklok/toolhive/pkg/vmcp/server" | ||
| ) | ||
|
|
||
| // countingDataStorage wraps a real LocalSessionDataStorage and counts how | ||
| // many times Close has been invoked. Used to assert that Server.Stop does | ||
| // not close a caller-supplied DataStorage. | ||
| type countingDataStorage struct { | ||
| transportsession.DataStorage | ||
| closeCalls atomic.Int32 | ||
| } | ||
|
|
||
| func (c *countingDataStorage) Close() error { | ||
| c.closeCalls.Add(1) | ||
| return c.DataStorage.Close() | ||
| } | ||
|
|
||
| func newCountingDataStorage(t *testing.T) *countingDataStorage { | ||
| t.Helper() | ||
| inner, err := transportsession.NewLocalSessionDataStorage(5 * time.Minute) | ||
| require.NoError(t, err) | ||
| return &countingDataStorage{DataStorage: inner} | ||
| } | ||
|
|
||
| func TestNew_CallerOwnedDataStorageNotClosedOnStop(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| spy := newCountingDataStorage(t) | ||
| // The spy is caller-owned; close the inner LocalSessionDataStorage | ||
| // directly at the end of the test so the counter is not ticked by | ||
| // cleanup — the post-Stop assertion below must reflect only the server's | ||
| // behaviour. Err ignored: closing an already-closed local store is a | ||
| // no-op in this implementation. | ||
| t.Cleanup(func() { | ||
| _ = spy.DataStorage.Close() | ||
| }) | ||
|
|
||
| ctrl := gomock.NewController(t) | ||
| t.Cleanup(ctrl.Finish) | ||
|
|
||
| mockRouter := routerMocks.NewMockRouter(ctrl) | ||
| mockBackendClient := mocks.NewMockBackendClient(ctrl) | ||
| mockDiscoveryMgr := discoveryMocks.NewMockManager(ctrl) | ||
| mockDiscoveryMgr.EXPECT().Stop().Times(1) | ||
|
|
||
| srv, err := server.New( | ||
| t.Context(), | ||
| &server.Config{ | ||
| Host: "127.0.0.1", | ||
| Port: 0, | ||
| SessionFactory: newNoopMockFactory(t), | ||
| DataStorage: spy, | ||
| }, | ||
| mockRouter, | ||
| mockBackendClient, | ||
| mockDiscoveryMgr, | ||
| vmcp.NewImmutableRegistry([]vmcp.Backend{}), | ||
| nil, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| err = srv.Stop(t.Context()) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, int32(0), spy.closeCalls.Load(), | ||
| "server must not close a caller-supplied DataStorage") | ||
| } | ||
|
|
||
| func TestNew_BothSessionStorageAndDataStorageErrors(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| spy := newCountingDataStorage(t) | ||
| // Err ignored: closing an already-closed local store is a no-op. | ||
| t.Cleanup(func() { | ||
| _ = spy.DataStorage.Close() | ||
| }) | ||
|
|
||
| ctrl := gomock.NewController(t) | ||
| t.Cleanup(ctrl.Finish) | ||
|
|
||
| mockRouter := routerMocks.NewMockRouter(ctrl) | ||
| mockBackendClient := mocks.NewMockBackendClient(ctrl) | ||
| mockDiscoveryMgr := discoveryMocks.NewMockManager(ctrl) | ||
|
|
||
| _, err := server.New( | ||
| t.Context(), | ||
| &server.Config{ | ||
| Host: "127.0.0.1", | ||
| Port: 0, | ||
| SessionFactory: newNoopMockFactory(t), | ||
| SessionStorage: &vmcpconfig.SessionStorageConfig{ | ||
| Provider: "redis", | ||
| Address: "127.0.0.1:6379", | ||
| }, | ||
| DataStorage: spy, | ||
| }, | ||
| mockRouter, | ||
| mockBackendClient, | ||
| mockDiscoveryMgr, | ||
| vmcp.NewImmutableRegistry([]vmcp.Backend{}), | ||
| nil, | ||
| ) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "DataStorage") | ||
| assert.Contains(t, err.Error(), "SessionStorage") | ||
| assert.Equal(t, int32(0), spy.closeCalls.Load(), | ||
| "server must not close a caller-supplied DataStorage on misconfiguration") | ||
| } | ||
|
|
||
| func TestNew_ServerBuiltDataStorageStopSucceeds(t *testing.T) { | ||
| // Guards against accidental regression of the server-owned close path | ||
| // when Close moved from an inline Stop() block onto sessionDataStorageCloser. | ||
| // Stop() must still complete without error when the server built the store. | ||
| // This is a smoke test — it cannot observe Close on the internal | ||
| // LocalSessionDataStorage because that type is constructed inside New(). | ||
| t.Parallel() | ||
|
|
||
| ctrl := gomock.NewController(t) | ||
| t.Cleanup(ctrl.Finish) | ||
|
|
||
| mockRouter := routerMocks.NewMockRouter(ctrl) | ||
| mockBackendClient := mocks.NewMockBackendClient(ctrl) | ||
| mockDiscoveryMgr := discoveryMocks.NewMockManager(ctrl) | ||
| mockDiscoveryMgr.EXPECT().Stop().Times(1) | ||
|
|
||
| srv, err := server.New( | ||
| t.Context(), | ||
| &server.Config{ | ||
| Host: "127.0.0.1", | ||
| Port: 0, | ||
| SessionFactory: newNoopMockFactory(t), | ||
| SessionStorage: &vmcpconfig.SessionStorageConfig{Provider: "memory"}, | ||
| }, | ||
| mockRouter, | ||
| mockBackendClient, | ||
| mockDiscoveryMgr, | ||
| vmcp.NewImmutableRegistry([]vmcp.Backend{}), | ||
| nil, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| require.NoError(t, srv.Stop(t.Context())) | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit / doc inconsistency (non-blocking): The comment says "setting both is rejected" but the actual check in
buildSessionDataStorageonly rejects whenSessionStorage.Provider != "". An embedder who passes&vmcpconfig.SessionStorageConfig{}(struct with emptyProvider) alongsideDataStoragewill not get an error — the server silently usesDataStorageand ignores theSessionStoragestruct entirely.This contradicts the
DataStoragefield's own comment a few lines down, which is more precise: "Setting both DataStorage and a non-empty SessionStorage.Provider is rejected".Suggested fix:
Evidence:
buildSessionDataStoragecondition atpkg/vmcp/server/server.goline ~302: