mirror of
https://github.com/openshift/openshift-mcp-server.git
synced 2025-10-17 14:27:48 +03:00
test(pods): pods_list_in_namespace test suite
This commit is contained in:
@@ -23,13 +23,55 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// envTest has an expensive setup, so we only want to do it once per entire test run.
|
||||
var envTest *envtest.Environment
|
||||
var envTestRestConfig *rest.Config
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// Set up
|
||||
envTestDir, err := store.DefaultStoreDir()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
envTestEnv := &env.Env{
|
||||
FS: afero.Afero{Fs: afero.NewOsFs()},
|
||||
Out: os.Stdout,
|
||||
Client: &remote.HTTPClient{
|
||||
IndexURL: remote.DefaultIndexURL,
|
||||
},
|
||||
Platform: versions.PlatformItem{
|
||||
Platform: versions.Platform{
|
||||
OS: runtime.GOOS,
|
||||
Arch: runtime.GOARCH,
|
||||
},
|
||||
},
|
||||
Version: versions.AnyVersion,
|
||||
Store: store.NewAt(envTestDir),
|
||||
}
|
||||
envTestEnv.CheckCoherence()
|
||||
workflows.Use{}.Do(envTestEnv)
|
||||
versionDir := envTestEnv.Platform.Platform.BaseName(*envTestEnv.Version.AsConcrete())
|
||||
envTest = &envtest.Environment{
|
||||
BinaryAssetsDirectory: filepath.Join(envTestDir, "k8s", versionDir),
|
||||
}
|
||||
envTestRestConfig, _ = envTest.Start()
|
||||
|
||||
// Test!
|
||||
code := m.Run()
|
||||
|
||||
// Tear down
|
||||
if envTest != nil {
|
||||
_ = envTest.Stop()
|
||||
}
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
type mcpContext struct {
|
||||
ctx context.Context
|
||||
tempDir string
|
||||
testServer *httptest.Server
|
||||
cancel context.CancelFunc
|
||||
mcpClient *client.SSEMCPClient
|
||||
envTest *envtest.Environment
|
||||
}
|
||||
|
||||
func (c *mcpContext) beforeEach(t *testing.T) {
|
||||
@@ -57,9 +99,6 @@ func (c *mcpContext) beforeEach(t *testing.T) {
|
||||
}
|
||||
|
||||
func (c *mcpContext) afterEach() {
|
||||
if c.envTest != nil {
|
||||
_ = c.envTest.Stop()
|
||||
}
|
||||
c.cancel()
|
||||
_ = c.mcpClient.Close()
|
||||
c.testServer.Close()
|
||||
@@ -94,36 +133,7 @@ func (c *mcpContext) withKubeConfig(rc *rest.Config) *api.Config {
|
||||
}
|
||||
|
||||
func (c *mcpContext) withEnvTest() {
|
||||
if c.envTest != nil {
|
||||
return
|
||||
}
|
||||
envTestDir, err := store.DefaultStoreDir()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
envTest := &env.Env{
|
||||
FS: afero.Afero{Fs: afero.NewOsFs()},
|
||||
Out: os.Stdout,
|
||||
Client: &remote.HTTPClient{
|
||||
IndexURL: remote.DefaultIndexURL,
|
||||
},
|
||||
Platform: versions.PlatformItem{
|
||||
Platform: versions.Platform{
|
||||
OS: runtime.GOOS,
|
||||
Arch: runtime.GOARCH,
|
||||
},
|
||||
},
|
||||
Version: versions.AnyVersion,
|
||||
Store: store.NewAt(envTestDir),
|
||||
}
|
||||
envTest.CheckCoherence()
|
||||
workflows.Use{}.Do(envTest)
|
||||
versionDir := envTest.Platform.Platform.BaseName(*envTest.Version.AsConcrete())
|
||||
c.envTest = &envtest.Environment{
|
||||
BinaryAssetsDirectory: filepath.Join(envTestDir, "k8s", versionDir),
|
||||
}
|
||||
restConfig, _ := c.envTest.Start()
|
||||
c.withKubeConfig(restConfig)
|
||||
c.withKubeConfig(envTestRestConfig)
|
||||
}
|
||||
|
||||
func (c *mcpContext) newKubernetesClient() *kubernetes.Clientset {
|
||||
@@ -136,3 +146,10 @@ func (c *mcpContext) newKubernetesClient() *kubernetes.Clientset {
|
||||
}
|
||||
return kubernetesClient
|
||||
}
|
||||
|
||||
func (c *mcpContext) callTool(name string, args map[string]interface{}) (*mcp.CallToolResult, error) {
|
||||
callToolRequest := mcp.CallToolRequest{}
|
||||
callToolRequest.Params.Name = name
|
||||
callToolRequest.Params.Arguments = args
|
||||
return c.mcpClient.CallTool(c.ctx, callToolRequest)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
v1 "k8s.io/client-go/tools/clientcmd/api/v1"
|
||||
"sigs.k8s.io/yaml"
|
||||
"testing"
|
||||
@@ -9,10 +8,7 @@ import (
|
||||
|
||||
func TestConfigurationView(t *testing.T) {
|
||||
testCase(t, func(c *mcpContext) {
|
||||
configurationGet := mcp.CallToolRequest{}
|
||||
configurationGet.Params.Name = "configuration_view"
|
||||
configurationGet.Params.Arguments = map[string]interface{}{}
|
||||
toolResult, err := c.mcpClient.CallTool(c.ctx, configurationGet)
|
||||
toolResult, err := c.callTool("configuration_view", map[string]interface{}{})
|
||||
t.Run("configuration_view returns configuration", func(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("call tool failed %v", err)
|
||||
|
||||
@@ -2,7 +2,6 @@ package mcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
@@ -13,11 +12,9 @@ import (
|
||||
|
||||
func TestPodsListInAllNamespaces(t *testing.T) {
|
||||
testCase(t, func(c *mcpContext) {
|
||||
c.withEnvTest()
|
||||
createTestData(c.ctx, c.newKubernetesClient())
|
||||
configurationGet := mcp.CallToolRequest{}
|
||||
configurationGet.Params.Name = "pods_list"
|
||||
configurationGet.Params.Arguments = map[string]interface{}{}
|
||||
toolResult, err := c.mcpClient.CallTool(c.ctx, configurationGet)
|
||||
toolResult, err := c.callTool("pods_list", map[string]interface{}{})
|
||||
t.Run("pods_list returns pods list", func(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("call tool failed %v", err)
|
||||
@@ -67,6 +64,68 @@ func TestPodsListInAllNamespaces(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestPodsListInNamespace(t *testing.T) {
|
||||
testCase(t, func(c *mcpContext) {
|
||||
c.withEnvTest()
|
||||
t.Run("pods_list_in_namespace with nil namespace returns pods list", func(t *testing.T) {
|
||||
toolResult, _ := c.callTool("pods_list_in_namespace", map[string]interface{}{})
|
||||
if toolResult.IsError != true {
|
||||
t.Fatalf("call tool should fail")
|
||||
return
|
||||
}
|
||||
if toolResult.Content[0].(map[string]interface{})["text"].(string) != "failed to list pods in namespace, missing argument namespace" {
|
||||
t.Fatalf("invalid error message, got %v", toolResult.Content[0].(map[string]interface{})["text"].(string))
|
||||
return
|
||||
}
|
||||
})
|
||||
createTestData(c.ctx, c.newKubernetesClient())
|
||||
toolResult, err := c.callTool("pods_list_in_namespace", map[string]interface{}{
|
||||
"namespace": "ns-1",
|
||||
})
|
||||
t.Run("pods_list_in_namespace returns pods list", func(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("call tool failed %v", err)
|
||||
return
|
||||
}
|
||||
if toolResult.IsError {
|
||||
t.Fatalf("call tool failed")
|
||||
return
|
||||
}
|
||||
})
|
||||
var decoded []unstructured.Unstructured
|
||||
err = yaml.Unmarshal([]byte(toolResult.Content[0].(map[string]interface{})["text"].(string)), &decoded)
|
||||
t.Run("pods_list_in_namespace has yaml content", func(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("invalid tool result content %v", err)
|
||||
return
|
||||
}
|
||||
})
|
||||
t.Run("pods_list_in_namespace returns 1 items", func(t *testing.T) {
|
||||
if len(decoded) != 1 {
|
||||
t.Fatalf("invalid pods count, expected 1, got %v", len(decoded))
|
||||
return
|
||||
}
|
||||
})
|
||||
t.Run("pods_list_in_namespace returns pod in ns-1", func(t *testing.T) {
|
||||
if decoded[0].GetName() != "a-pod-in-ns-1" {
|
||||
t.Fatalf("invalid pod name, expected a-pod-in-ns-1, got %v", decoded[0].GetName())
|
||||
return
|
||||
}
|
||||
if decoded[0].GetNamespace() != "ns-1" {
|
||||
t.Fatalf("invalid pod namespace, expected ns-1, got %v", decoded[0].GetNamespace())
|
||||
return
|
||||
}
|
||||
})
|
||||
t.Run("pods_list_in_namespace omits managed fields", func(t *testing.T) {
|
||||
if decoded[0].GetManagedFields() != nil {
|
||||
t.Fatalf("managed fields should be omitted, got %v", decoded[0].GetManagedFields())
|
||||
return
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func createTestData(ctx context.Context, kc *kubernetes.Clientset) {
|
||||
_, _ = kc.CoreV1().Namespaces().
|
||||
Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "ns-1"}}, metav1.CreateOptions{})
|
||||
|
||||
Reference in New Issue
Block a user