-
Notifications
You must be signed in to change notification settings - Fork 2
feat: deploy RAG server container #333
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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
256 changes: 256 additions & 0 deletions
256
server/internal/orchestrator/swarm/rag_instance_resources_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,256 @@ | ||
| package swarm | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/pgEdge/control-plane/server/internal/config" | ||
| "github.com/pgEdge/control-plane/server/internal/database" | ||
| "github.com/pgEdge/control-plane/server/internal/ds" | ||
| "github.com/pgEdge/control-plane/server/internal/filesystem" | ||
| "github.com/pgEdge/control-plane/server/internal/resource" | ||
| ) | ||
|
|
||
| // newTestOrchestrator returns an Orchestrator with serviceVersions initialised | ||
| // from a minimal config, suitable for unit tests that call generateRAGInstanceResources. | ||
| func newTestOrchestrator() *Orchestrator { | ||
| return &Orchestrator{ | ||
| serviceVersions: NewServiceVersions(config.Config{}), | ||
| } | ||
| } | ||
|
|
||
| // minimalRAGConfig returns a minimal valid RAG service config suitable for unit tests. | ||
| func minimalRAGConfig() map[string]any { | ||
| return map[string]any{ | ||
| "pipelines": []any{ | ||
| map[string]any{ | ||
| "name": "default", | ||
| "tables": []any{ | ||
| map[string]any{ | ||
| "table": "docs", | ||
| "text_column": "content", | ||
| "vector_column": "embedding", | ||
| }, | ||
| }, | ||
| "embedding_llm": map[string]any{ | ||
| "provider": "openai", | ||
| "model": "text-embedding-3-small", | ||
| "api_key": "sk-embed", | ||
| }, | ||
| "rag_llm": map[string]any{ | ||
| "provider": "anthropic", | ||
| "model": "claude-sonnet-4-5", | ||
| "api_key": "sk-ant", | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func TestGenerateRAGInstanceResources_ResourceList(t *testing.T) { | ||
| o := newTestOrchestrator() | ||
| spec := &database.ServiceInstanceSpec{ | ||
| ServiceInstanceID: "storefront-rag-host1", | ||
| ServiceSpec: &database.ServiceSpec{ | ||
| ServiceID: "rag", | ||
| ServiceType: "rag", | ||
| Version: "latest", | ||
| Config: minimalRAGConfig(), | ||
| }, | ||
| DatabaseID: "storefront", | ||
| DatabaseName: "storefront", | ||
| HostID: "host-1", | ||
| NodeName: "n1", | ||
| } | ||
|
|
||
| result, err := o.generateRAGInstanceResources(spec) | ||
| require.NoError(t, err) | ||
|
|
||
| require.NotNil(t, result.ServiceInstance) | ||
| assert.Equal(t, spec.ServiceInstanceID, result.ServiceInstance.ServiceInstanceID) | ||
| assert.Equal(t, spec.HostID, result.ServiceInstance.HostID) | ||
| assert.Equal(t, database.ServiceInstanceStateCreating, result.ServiceInstance.State) | ||
|
|
||
| // Single node: Network + canonical RO + DirResource + Keys + Config + InstanceSpec + ServiceInstance = 7. | ||
| require.Len(t, result.Resources, 7) | ||
| assert.Equal(t, ResourceTypeNetwork, result.Resources[0].Identifier.Type) | ||
| assert.Equal(t, ResourceTypeServiceUserRole, result.Resources[1].Identifier.Type) | ||
| assert.Equal(t, ServiceUserRoleIdentifier("rag", ServiceUserRoleRO), result.Resources[1].Identifier) | ||
| assert.Equal(t, filesystem.ResourceTypeDir, result.Resources[2].Identifier.Type) | ||
| assert.Equal(t, ResourceTypeRAGServiceKeys, result.Resources[3].Identifier.Type) | ||
| assert.Equal(t, ResourceTypeRAGConfig, result.Resources[4].Identifier.Type) | ||
| assert.Equal(t, ResourceTypeServiceInstanceSpec, result.Resources[5].Identifier.Type) | ||
| assert.Equal(t, ResourceTypeServiceInstance, result.Resources[6].Identifier.Type) | ||
| } | ||
|
|
||
| func TestGenerateRAGInstanceResources_MultiNode(t *testing.T) { | ||
| o := newTestOrchestrator() | ||
| spec := &database.ServiceInstanceSpec{ | ||
| ServiceInstanceID: "storefront-rag-host1", | ||
| ServiceSpec: &database.ServiceSpec{ | ||
| ServiceID: "rag", | ||
| ServiceType: "rag", | ||
| Version: "latest", | ||
| Config: minimalRAGConfig(), | ||
| }, | ||
| DatabaseID: "storefront", | ||
| DatabaseName: "storefront", | ||
| HostID: "host-1", | ||
| NodeName: "n1", | ||
| DatabaseNodes: []*database.NodeInstances{ | ||
| {NodeName: "n1"}, | ||
| {NodeName: "n2"}, | ||
| {NodeName: "n3"}, | ||
| }, | ||
| } | ||
|
|
||
| result, err := o.generateRAGInstanceResources(spec) | ||
| require.NoError(t, err) | ||
|
|
||
| // 3 nodes → Network + canonical(n1) + per-node(n2) + per-node(n3) + dir + keys + config + spec + instance = 9. | ||
| require.Len(t, result.Resources, 9) | ||
|
|
||
| // Resources[0] is Network; Resources[1..3] are ServiceUserRole resources. | ||
| for i := 1; i < 4; i++ { | ||
| assert.Equal(t, ResourceTypeServiceUserRole, result.Resources[i].Identifier.Type) | ||
| } | ||
|
|
||
| // Canonical is index 1 and has no CredentialSource. | ||
| canonical, err := resource.ToResource[*ServiceUserRole](result.Resources[1]) | ||
| require.NoError(t, err) | ||
| assert.Nil(t, canonical.CredentialSource) | ||
| assert.Equal(t, ServiceUserRoleRO, canonical.Mode) | ||
|
|
||
| // Per-node resources point back to canonical. | ||
| canonicalID := ServiceUserRoleIdentifier("rag", ServiceUserRoleRO) | ||
| for i, rd := range result.Resources[2:4] { | ||
| perNode, err := resource.ToResource[*ServiceUserRole](rd) | ||
| require.NoErrorf(t, err, "ToResource per-node[%d]", i) | ||
| assert.Equalf(t, &canonicalID, perNode.CredentialSource, "per-node[%d].CredentialSource", i) | ||
| assert.Equalf(t, ServiceUserRoleRO, perNode.Mode, "per-node[%d].Mode", i) | ||
| } | ||
|
|
||
| assert.Equal(t, filesystem.ResourceTypeDir, result.Resources[4].Identifier.Type) | ||
| assert.Equal(t, ResourceTypeRAGServiceKeys, result.Resources[5].Identifier.Type) | ||
| assert.Equal(t, ResourceTypeRAGConfig, result.Resources[6].Identifier.Type) | ||
| assert.Equal(t, ResourceTypeServiceInstanceSpec, result.Resources[7].Identifier.Type) | ||
| assert.Equal(t, ResourceTypeServiceInstance, result.Resources[8].Identifier.Type) | ||
| } | ||
|
|
||
| func TestGenerateRAGInstanceResources_MultiNode_CanonicalNotFirst(t *testing.T) { | ||
| o := newTestOrchestrator() | ||
| spec := &database.ServiceInstanceSpec{ | ||
| ServiceInstanceID: "storefront-rag-host2", | ||
| ServiceSpec: &database.ServiceSpec{ | ||
| ServiceID: "rag", | ||
| ServiceType: "rag", | ||
| Version: "latest", | ||
| Config: minimalRAGConfig(), | ||
| }, | ||
| DatabaseID: "storefront", | ||
| DatabaseName: "storefront", | ||
| HostID: "host-2", | ||
| NodeName: "n2", // canonical is n2, not at index 0 | ||
| DatabaseNodes: []*database.NodeInstances{ | ||
| {NodeName: "n1"}, | ||
| {NodeName: "n2"}, | ||
| {NodeName: "n3"}, | ||
| }, | ||
| } | ||
|
|
||
| result, err := o.generateRAGInstanceResources(spec) | ||
| require.NoError(t, err) | ||
|
|
||
| // 3 nodes → Network + canonical(n2) + per-node(n1) + per-node(n3) + dir + keys + config + spec + instance = 9. | ||
| require.Len(t, result.Resources, 9) | ||
|
|
||
| // Canonical (index 1, after Network) must be n2 with no CredentialSource. | ||
| canonical, err := resource.ToResource[*ServiceUserRole](result.Resources[1]) | ||
| require.NoError(t, err) | ||
| assert.Nil(t, canonical.CredentialSource) | ||
| assert.Equal(t, "n2", canonical.NodeName) | ||
|
|
||
| // Per-node resources must cover n1 and n3, not n2. | ||
| canonicalID := ServiceUserRoleIdentifier("rag", ServiceUserRoleRO) | ||
| perNodeNames := make(map[string]bool) | ||
| for i, rd := range result.Resources[2:4] { | ||
| perNode, err := resource.ToResource[*ServiceUserRole](rd) | ||
| require.NoErrorf(t, err, "ToResource per-node[%d]", i) | ||
| assert.Equalf(t, &canonicalID, perNode.CredentialSource, "per-node[%d].CredentialSource", i) | ||
| perNodeNames[perNode.NodeName] = true | ||
| } | ||
| assert.False(t, perNodeNames["n2"], "canonical node n2 must not appear in per-node resources") | ||
| assert.True(t, perNodeNames["n1"], "n1 must be a per-node resource") | ||
| assert.True(t, perNodeNames["n3"], "n3 must be a per-node resource") | ||
| } | ||
|
|
||
| func TestGenerateServiceInstanceResources_RAGDispatch(t *testing.T) { | ||
| o := newTestOrchestrator() | ||
| spec := &database.ServiceInstanceSpec{ | ||
| ServiceInstanceID: "db1-rag-host1", | ||
| ServiceSpec: &database.ServiceSpec{ | ||
| ServiceID: "rag", | ||
| ServiceType: "rag", | ||
| Version: "latest", | ||
| Config: minimalRAGConfig(), | ||
| }, | ||
| DatabaseID: "db1", | ||
| DatabaseName: "db1", | ||
| HostID: "host-1", | ||
| NodeName: "n1", | ||
| } | ||
|
|
||
| result, err := o.GenerateServiceInstanceResources(spec) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, result) | ||
| } | ||
|
|
||
| func TestGenerateServiceInstanceResources_UnknownTypeReturnsError(t *testing.T) { | ||
| o := newTestOrchestrator() | ||
| spec := &database.ServiceInstanceSpec{ | ||
| ServiceInstanceID: "db1-unknown-host1", | ||
| ServiceSpec: &database.ServiceSpec{ | ||
| ServiceID: "unknown", | ||
| ServiceType: "unknown", | ||
| Version: "latest", | ||
| }, | ||
| DatabaseID: "db1", | ||
| DatabaseName: "db1", | ||
| HostID: "host-1", | ||
| NodeName: "n1", | ||
| } | ||
|
|
||
| _, err := o.GenerateServiceInstanceResources(spec) | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func TestGenerateRAGInstanceResources_IncompatibleVersion(t *testing.T) { | ||
| o := newTestOrchestrator() | ||
| // Override the "rag/latest" image with a constraint requiring PG >= 18. | ||
| o.serviceVersions.addServiceImage("rag", "latest", &ServiceImage{ | ||
| Tag: "rag-server:latest", | ||
| PostgresConstraint: &ds.VersionConstraint{ | ||
| Min: ds.MustParseVersion("18"), | ||
| }, | ||
| }) | ||
|
|
||
| spec := &database.ServiceInstanceSpec{ | ||
| ServiceInstanceID: "db1-rag-host1", | ||
| ServiceSpec: &database.ServiceSpec{ | ||
| ServiceID: "rag", | ||
| ServiceType: "rag", | ||
| Version: "latest", | ||
| Config: minimalRAGConfig(), | ||
| }, | ||
| DatabaseID: "db1", | ||
| DatabaseName: "db1", | ||
| HostID: "host-1", | ||
| NodeName: "n1", | ||
| PgEdgeVersion: ds.MustPgEdgeVersion("17", "5.0.0"), | ||
| } | ||
|
|
||
| _, err := o.generateRAGInstanceResources(spec) | ||
| require.ErrorContains(t, err, "not compatible") | ||
| } |
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.