|
| 1 | +package resolver_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/openmfp/kubernetes-graphql-gateway/gateway/resolver" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestKubernetesToGraphQL(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + input any |
| 15 | + expected any |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "complete_kubernetes_object", |
| 19 | + input: map[string]any{ |
| 20 | + "apiVersion": "apps/v1", |
| 21 | + "kind": "Deployment", |
| 22 | + "metadata": map[string]any{ |
| 23 | + "name": "my-app", |
| 24 | + "namespace": "default", |
| 25 | + "labels": map[string]any{ |
| 26 | + "app.kubernetes.io/name": "my-app", |
| 27 | + "app.kubernetes.io/version": "1.0.0", |
| 28 | + }, |
| 29 | + "annotations": map[string]any{ |
| 30 | + "deployment.kubernetes.io/revision": "1", |
| 31 | + }, |
| 32 | + }, |
| 33 | + "spec": map[string]any{ |
| 34 | + "replicas": 3, |
| 35 | + "nodeSelector": map[string]any{ |
| 36 | + "kubernetes.io/arch": "amd64", |
| 37 | + "node.kubernetes.io/instance-type": "m5.large", |
| 38 | + }, |
| 39 | + "selector": map[string]any{ |
| 40 | + "matchLabels": map[string]any{ |
| 41 | + "app.kubernetes.io/name": "my-app", |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + expected: map[string]any{ |
| 47 | + "apiVersion": "apps/v1", |
| 48 | + "kind": "Deployment", |
| 49 | + "metadata": map[string]any{ |
| 50 | + "name": "my-app", |
| 51 | + "namespace": "default", |
| 52 | + "labels": []map[string]any{ |
| 53 | + {"key": "app.kubernetes.io/name", "value": "my-app"}, |
| 54 | + {"key": "app.kubernetes.io/version", "value": "1.0.0"}, |
| 55 | + }, |
| 56 | + "annotations": []map[string]any{ |
| 57 | + {"key": "deployment.kubernetes.io/revision", "value": "1"}, |
| 58 | + }, |
| 59 | + }, |
| 60 | + "spec": map[string]any{ |
| 61 | + "replicas": 3, |
| 62 | + "nodeSelector": []map[string]any{ |
| 63 | + {"key": "kubernetes.io/arch", "value": "amd64"}, |
| 64 | + {"key": "node.kubernetes.io/instance-type", "value": "m5.large"}, |
| 65 | + }, |
| 66 | + "selector": map[string]any{ |
| 67 | + "matchLabels": []map[string]any{ |
| 68 | + {"key": "app.kubernetes.io/name", "value": "my-app"}, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "object_without_metadata_or_spec", |
| 76 | + input: map[string]any{ |
| 77 | + "apiVersion": "v1", |
| 78 | + "kind": "ConfigMap", |
| 79 | + "data": map[string]any{ |
| 80 | + "config.yaml": "key: value", |
| 81 | + }, |
| 82 | + }, |
| 83 | + expected: map[string]any{ |
| 84 | + "apiVersion": "v1", |
| 85 | + "kind": "ConfigMap", |
| 86 | + "data": map[string]any{ |
| 87 | + "config.yaml": "key: value", |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "nil_input", |
| 93 | + input: nil, |
| 94 | + expected: nil, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "invalid_type", |
| 98 | + input: "not-a-map", |
| 99 | + expected: "not-a-map", |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + for _, tt := range tests { |
| 104 | + t.Run(tt.name, func(t *testing.T) { |
| 105 | + result := resolver.KubernetesToGraphQL(tt.input) |
| 106 | + |
| 107 | + if tt.expected == nil { |
| 108 | + assert.Nil(t, result) |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + // For complex nested objects, we need custom comparison logic |
| 113 | + if expectedMap, ok := tt.expected.(map[string]any); ok { |
| 114 | + resultMap, ok := result.(map[string]any) |
| 115 | + require.True(t, ok, "Expected result to be a map") |
| 116 | + |
| 117 | + // Compare basic fields |
| 118 | + for key, expectedVal := range expectedMap { |
| 119 | + resultVal := resultMap[key] |
| 120 | + |
| 121 | + switch key { |
| 122 | + case "metadata": |
| 123 | + compareMetadata(t, expectedVal, resultVal) |
| 124 | + case "spec": |
| 125 | + compareSpec(t, expectedVal, resultVal) |
| 126 | + default: |
| 127 | + assert.Equal(t, expectedVal, resultVal) |
| 128 | + } |
| 129 | + } |
| 130 | + } else { |
| 131 | + assert.Equal(t, tt.expected, result) |
| 132 | + } |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestGraphQLToKubernetes(t *testing.T) { |
| 138 | + tests := []struct { |
| 139 | + name string |
| 140 | + input any |
| 141 | + expected any |
| 142 | + }{ |
| 143 | + { |
| 144 | + name: "graphql_input_with_label_arrays", |
| 145 | + input: map[string]any{ |
| 146 | + "metadata": map[string]any{ |
| 147 | + "name": "my-app", |
| 148 | + "labels": []any{ |
| 149 | + map[string]any{"key": "app.kubernetes.io/name", "value": "my-app"}, |
| 150 | + map[string]any{"key": "environment", "value": "production"}, |
| 151 | + }, |
| 152 | + }, |
| 153 | + "spec": map[string]any{ |
| 154 | + "nodeSelector": []any{ |
| 155 | + map[string]any{"key": "kubernetes.io/arch", "value": "amd64"}, |
| 156 | + }, |
| 157 | + "selector": map[string]any{ |
| 158 | + "matchLabels": []any{ |
| 159 | + map[string]any{"key": "app.kubernetes.io/name", "value": "my-app"}, |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + expected: map[string]any{ |
| 165 | + "metadata": map[string]any{ |
| 166 | + "name": "my-app", |
| 167 | + "labels": map[string]string{ |
| 168 | + "app.kubernetes.io/name": "my-app", |
| 169 | + "environment": "production", |
| 170 | + }, |
| 171 | + }, |
| 172 | + "spec": map[string]any{ |
| 173 | + "nodeSelector": map[string]string{ |
| 174 | + "kubernetes.io/arch": "amd64", |
| 175 | + }, |
| 176 | + "selector": map[string]any{ |
| 177 | + "matchLabels": map[string]string{ |
| 178 | + "app.kubernetes.io/name": "my-app", |
| 179 | + }, |
| 180 | + }, |
| 181 | + }, |
| 182 | + }, |
| 183 | + }, |
| 184 | + { |
| 185 | + name: "nil_input", |
| 186 | + input: nil, |
| 187 | + expected: nil, |
| 188 | + }, |
| 189 | + { |
| 190 | + name: "invalid_type", |
| 191 | + input: "not-a-map", |
| 192 | + expected: "not-a-map", |
| 193 | + }, |
| 194 | + } |
| 195 | + |
| 196 | + for _, tt := range tests { |
| 197 | + t.Run(tt.name, func(t *testing.T) { |
| 198 | + result := resolver.GraphqlToKubernetes(tt.input) |
| 199 | + assert.Equal(t, tt.expected, result) |
| 200 | + }) |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +// Helper functions for complex comparisons |
| 205 | +func compareMetadata(t *testing.T, expected, result any) { |
| 206 | + expectedMeta := expected.(map[string]any) |
| 207 | + resultMeta, ok := result.(map[string]any) |
| 208 | + require.True(t, ok, "Expected metadata to be a map") |
| 209 | + |
| 210 | + for key, expectedVal := range expectedMeta { |
| 211 | + resultVal := resultMeta[key] |
| 212 | + |
| 213 | + if key == "labels" || key == "annotations" { |
| 214 | + if expectedVal == nil { |
| 215 | + assert.Nil(t, resultVal) |
| 216 | + } else { |
| 217 | + expectedArray := expectedVal.([]map[string]any) |
| 218 | + resultArray, ok := resultVal.([]map[string]any) |
| 219 | + require.True(t, ok, "Expected %s to be an array", key) |
| 220 | + assert.ElementsMatch(t, expectedArray, resultArray) |
| 221 | + } |
| 222 | + } else { |
| 223 | + assert.Equal(t, expectedVal, resultVal) |
| 224 | + } |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +func compareSpec(t *testing.T, expected, result any) { |
| 229 | + expectedSpec := expected.(map[string]any) |
| 230 | + resultSpec, ok := result.(map[string]any) |
| 231 | + require.True(t, ok, "Expected spec to be a map") |
| 232 | + |
| 233 | + for key, expectedVal := range expectedSpec { |
| 234 | + resultVal := resultSpec[key] |
| 235 | + |
| 236 | + switch key { |
| 237 | + case "nodeSelector": |
| 238 | + if expectedVal == nil { |
| 239 | + assert.Nil(t, resultVal) |
| 240 | + } else { |
| 241 | + expectedArray := expectedVal.([]map[string]any) |
| 242 | + resultArray, ok := resultVal.([]map[string]any) |
| 243 | + require.True(t, ok, "Expected nodeSelector to be an array") |
| 244 | + assert.ElementsMatch(t, expectedArray, resultArray) |
| 245 | + } |
| 246 | + case "selector": |
| 247 | + expectedSelector := expectedVal.(map[string]any) |
| 248 | + resultSelector, ok := resultVal.(map[string]any) |
| 249 | + require.True(t, ok, "Expected selector to be a map") |
| 250 | + |
| 251 | + if expectedMatchLabels, ok := expectedSelector["matchLabels"]; ok { |
| 252 | + resultMatchLabels := resultSelector["matchLabels"] |
| 253 | + expectedArray := expectedMatchLabels.([]map[string]any) |
| 254 | + resultArray, ok := resultMatchLabels.([]map[string]any) |
| 255 | + require.True(t, ok, "Expected matchLabels to be an array") |
| 256 | + assert.ElementsMatch(t, expectedArray, resultArray) |
| 257 | + } |
| 258 | + default: |
| 259 | + assert.Equal(t, expectedVal, resultVal) |
| 260 | + } |
| 261 | + } |
| 262 | +} |
0 commit comments