|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + v1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1" |
| 5 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 6 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 7 | + "k8s.io/client-go/dynamic" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestResourcesCreateOrUpdate(t *testing.T) { |
| 12 | + testCase(t, func(c *mcpContext) { |
| 13 | + c.withEnvTest() |
| 14 | + t.Run("resources_create_or_update with nil resource returns error", func(t *testing.T) { |
| 15 | + toolResult, _ := c.callTool("resources_create_or_update", map[string]interface{}{}) |
| 16 | + if toolResult.IsError != true { |
| 17 | + t.Fatalf("call tool should fail") |
| 18 | + return |
| 19 | + } |
| 20 | + if toolResult.Content[0].(map[string]interface{})["text"].(string) != "failed to create or update resources, missing argument resource" { |
| 21 | + t.Fatalf("invalid error message, got %v", toolResult.Content[0].(map[string]interface{})["text"].(string)) |
| 22 | + return |
| 23 | + } |
| 24 | + }) |
| 25 | + t.Run("resources_create_or_update with empty resource returns error", func(t *testing.T) { |
| 26 | + toolResult, _ := c.callTool("resources_create_or_update", map[string]interface{}{"resource": ""}) |
| 27 | + if toolResult.IsError != true { |
| 28 | + t.Fatalf("call tool should fail") |
| 29 | + return |
| 30 | + } |
| 31 | + if toolResult.Content[0].(map[string]interface{})["text"].(string) != "failed to create or update resources, missing argument resource" { |
| 32 | + t.Fatalf("invalid error message, got %v", toolResult.Content[0].(map[string]interface{})["text"].(string)) |
| 33 | + return |
| 34 | + } |
| 35 | + }) |
| 36 | + client := c.newKubernetesClient() |
| 37 | + configMapYaml := "apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: a-cm-created-or-updated\n namespace: default\n" |
| 38 | + resourcesCreateOrUpdateCm1, err := c.callTool("resources_create_or_update", map[string]interface{}{"resource": configMapYaml}) |
| 39 | + t.Run("resources_create_or_update with valid namespaced yaml resource returns success", func(t *testing.T) { |
| 40 | + if err != nil { |
| 41 | + t.Fatalf("call tool failed %v", err) |
| 42 | + return |
| 43 | + } |
| 44 | + if resourcesCreateOrUpdateCm1.IsError { |
| 45 | + t.Fatalf("call tool failed") |
| 46 | + return |
| 47 | + } |
| 48 | + }) |
| 49 | + t.Run("resources_create_or_update with valid namespaced yaml resource creates ConfigMap", func(t *testing.T) { |
| 50 | + cm, _ := client.CoreV1().ConfigMaps("default").Get(c.ctx, "a-cm-created-or-updated", metav1.GetOptions{}) |
| 51 | + if cm == nil { |
| 52 | + t.Fatalf("ConfigMap not found") |
| 53 | + return |
| 54 | + } |
| 55 | + }) |
| 56 | + configMapJson := "{\"apiVersion\": \"v1\", \"kind\": \"ConfigMap\", \"metadata\": {\"name\": \"a-cm-created-or-updated-2\", \"namespace\": \"default\"}}" |
| 57 | + resourcesCreateOrUpdateCm2, err := c.callTool("resources_create_or_update", map[string]interface{}{"resource": configMapJson}) |
| 58 | + t.Run("resources_create_or_update with valid namespaced json resource returns success", func(t *testing.T) { |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("call tool failed %v", err) |
| 61 | + return |
| 62 | + } |
| 63 | + if resourcesCreateOrUpdateCm2.IsError { |
| 64 | + t.Fatalf("call tool failed") |
| 65 | + return |
| 66 | + } |
| 67 | + }) |
| 68 | + t.Run("resources_create_or_update with valid namespaced json resource creates config map", func(t *testing.T) { |
| 69 | + cm, _ := client.CoreV1().ConfigMaps("default").Get(c.ctx, "a-cm-created-or-updated-2", metav1.GetOptions{}) |
| 70 | + if cm == nil { |
| 71 | + t.Fatalf("ConfigMap not found") |
| 72 | + return |
| 73 | + } |
| 74 | + }) |
| 75 | + customResourceDefinitionJson := ` |
| 76 | + { |
| 77 | + "apiVersion": "apiextensions.k8s.io/v1", |
| 78 | + "kind": "CustomResourceDefinition", |
| 79 | + "metadata": {"name": "customs.example.com"}, |
| 80 | + "spec": { |
| 81 | + "group": "example.com", |
| 82 | + "versions": [{ |
| 83 | + "name": "v1","served": true,"storage": true, |
| 84 | + "schema": {"openAPIV3Schema": {"type": "object"}} |
| 85 | + }], |
| 86 | + "scope": "Namespaced", |
| 87 | + "names": {"plural": "customs","singular": "custom","kind": "Custom"} |
| 88 | + } |
| 89 | + }` |
| 90 | + resourcesCreateOrUpdateCrd, err := c.callTool("resources_create_or_update", map[string]interface{}{"resource": customResourceDefinitionJson}) |
| 91 | + t.Run("resources_create_or_update with valid cluster-scoped json resource returns success", func(t *testing.T) { |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("call tool failed %v", err) |
| 94 | + return |
| 95 | + } |
| 96 | + if resourcesCreateOrUpdateCrd.IsError { |
| 97 | + t.Fatalf("call tool failed") |
| 98 | + return |
| 99 | + } |
| 100 | + }) |
| 101 | + t.Run("resources_create_or_update with valid cluster-scoped json resource creates custom resource definition", func(t *testing.T) { |
| 102 | + apiExtensionsV1Client := v1.NewForConfigOrDie(envTestRestConfig) |
| 103 | + _, err = apiExtensionsV1Client.CustomResourceDefinitions().Get(c.ctx, "customs.example.com", metav1.GetOptions{}) |
| 104 | + if err != nil { |
| 105 | + t.Fatalf("custom resource definition not found") |
| 106 | + return |
| 107 | + } |
| 108 | + }) |
| 109 | + customJson := "{\"apiVersion\": \"example.com/v1\", \"kind\": \"Custom\", \"metadata\": {\"name\": \"a-custom-resource\"}}" |
| 110 | + resourcesCreateOrUpdateCustom, err := c.callTool("resources_create_or_update", map[string]interface{}{"resource": customJson}) |
| 111 | + t.Run("resources_create_or_update with valid namespaced json resource returns success", func(t *testing.T) { |
| 112 | + if err != nil { |
| 113 | + t.Fatalf("call tool failed %v", err) |
| 114 | + return |
| 115 | + } |
| 116 | + if resourcesCreateOrUpdateCustom.IsError { |
| 117 | + t.Fatalf("call tool failed") |
| 118 | + return |
| 119 | + } |
| 120 | + }) |
| 121 | + t.Run("resources_create_or_update with valid namespaced json resource creates custom resource", func(t *testing.T) { |
| 122 | + dynamicClient := dynamic.NewForConfigOrDie(envTestRestConfig) |
| 123 | + _, err = dynamicClient. |
| 124 | + Resource(schema.GroupVersionResource{Group: "example.com", Version: "v1", Resource: "customs"}). |
| 125 | + Namespace("default"). |
| 126 | + Get(c.ctx, "a-custom-resource", metav1.GetOptions{}) |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("custom resource not found") |
| 129 | + return |
| 130 | + } |
| 131 | + }) |
| 132 | + customJsonUpdated := "{\"apiVersion\": \"example.com/v1\", \"kind\": \"Custom\", \"metadata\": {\"name\": \"a-custom-resource\",\"annotations\": {\"updated\": \"true\"}}}" |
| 133 | + resourcesCreateOrUpdateCustomUpdated, err := c.callTool("resources_create_or_update", map[string]interface{}{"resource": customJsonUpdated}) |
| 134 | + t.Run("resources_create_or_update with valid namespaced json resource updates custom resource", func(t *testing.T) { |
| 135 | + if err != nil { |
| 136 | + t.Fatalf("call tool failed %v", err) |
| 137 | + return |
| 138 | + } |
| 139 | + if resourcesCreateOrUpdateCustomUpdated.IsError { |
| 140 | + t.Fatalf("call tool failed") |
| 141 | + return |
| 142 | + } |
| 143 | + }) |
| 144 | + t.Run("resources_create_or_update with valid namespaced json resource updates custom resource", func(t *testing.T) { |
| 145 | + dynamicClient := dynamic.NewForConfigOrDie(envTestRestConfig) |
| 146 | + customResource, _ := dynamicClient. |
| 147 | + Resource(schema.GroupVersionResource{Group: "example.com", Version: "v1", Resource: "customs"}). |
| 148 | + Namespace("default"). |
| 149 | + Get(c.ctx, "a-custom-resource", metav1.GetOptions{}) |
| 150 | + if customResource == nil { |
| 151 | + t.Fatalf("custom resource not found") |
| 152 | + return |
| 153 | + } |
| 154 | + annotations := customResource.GetAnnotations() |
| 155 | + if annotations == nil || annotations["updated"] != "true" { |
| 156 | + t.Fatalf("custom resource not updated") |
| 157 | + return |
| 158 | + } |
| 159 | + }) |
| 160 | + }) |
| 161 | +} |
0 commit comments