|
| 1 | +// Copyright 2025 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package generate |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "path/filepath" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func TestParseCRDConfig(t *testing.T) { |
| 27 | + testYAML := ` |
| 28 | +apiVersion: apiextensions.k8s.io/v1 |
| 29 | +kind: CustomResourceDefinition |
| 30 | +metadata: |
| 31 | + name: clusters.atlas.generated.mongodb.com |
| 32 | + annotations: |
| 33 | + api-mappings: | |
| 34 | + properties: |
| 35 | + spec: |
| 36 | + properties: |
| 37 | + v20250312: |
| 38 | + x-atlas-sdk-version: go.mongodb.org/atlas-sdk/v20250312008/admin |
| 39 | +spec: |
| 40 | + group: atlas.generated.mongodb.com |
| 41 | + names: |
| 42 | + kind: Cluster |
| 43 | + plural: clusters |
| 44 | + versions: |
| 45 | + - name: v1 |
| 46 | +` |
| 47 | + |
| 48 | + tmpDir := t.TempDir() |
| 49 | + testFile := filepath.Join(tmpDir, "test.yaml") |
| 50 | + err := os.WriteFile(testFile, []byte(testYAML), 0644) |
| 51 | + require.NoError(t, err) |
| 52 | + |
| 53 | + t.Run("ParseValidCRD", func(t *testing.T) { |
| 54 | + config, err := ParseCRDConfig(testFile, "Cluster") |
| 55 | + require.NoError(t, err) |
| 56 | + assert.Equal(t, "Cluster", config.ResourceName) |
| 57 | + assert.Len(t, config.Mappings, 1) |
| 58 | + assert.Equal(t, "v20250312", config.Mappings[0].Version) |
| 59 | + assert.Equal(t, "go.mongodb.org/atlas-sdk/v20250312008/admin", config.Mappings[0].OpenAPIConfig.Package) |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("ParseNonExistentCRD", func(t *testing.T) { |
| 63 | + _, err := ParseCRDConfig(testFile, "NonExistent") |
| 64 | + assert.Error(t, err) |
| 65 | + assert.Contains(t, err.Error(), "not found") |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("ParseInvalidFile", func(t *testing.T) { |
| 69 | + _, err := ParseCRDConfig("/nonexistent/file.yaml", "Cluster") |
| 70 | + assert.Error(t, err) |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +func TestListCRDs(t *testing.T) { |
| 75 | + testYAML := ` |
| 76 | +apiVersion: apiextensions.k8s.io/v1 |
| 77 | +kind: CustomResourceDefinition |
| 78 | +metadata: |
| 79 | + name: clusters.atlas.generated.mongodb.com |
| 80 | +spec: |
| 81 | + group: atlas.generated.mongodb.com |
| 82 | + names: |
| 83 | + kind: Cluster |
| 84 | + plural: clusters |
| 85 | + categories: [atlas] |
| 86 | + versions: |
| 87 | + - name: v1 |
| 88 | +--- |
| 89 | +apiVersion: apiextensions.k8s.io/v1 |
| 90 | +kind: CustomResourceDefinition |
| 91 | +metadata: |
| 92 | + name: groups.atlas.generated.mongodb.com |
| 93 | +spec: |
| 94 | + group: atlas.generated.mongodb.com |
| 95 | + names: |
| 96 | + kind: Group |
| 97 | + plural: groups |
| 98 | + versions: |
| 99 | + - name: v1 |
| 100 | +` |
| 101 | + |
| 102 | + tmpDir := t.TempDir() |
| 103 | + testFile := filepath.Join(tmpDir, "test.yaml") |
| 104 | + err := os.WriteFile(testFile, []byte(testYAML), 0644) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + crds, err := ListCRDs(testFile) |
| 108 | + require.NoError(t, err) |
| 109 | + assert.Len(t, crds, 2) |
| 110 | + |
| 111 | + assert.Equal(t, "Cluster", crds[0].Kind) |
| 112 | + assert.Equal(t, "atlas.generated.mongodb.com", crds[0].Group) |
| 113 | + assert.Equal(t, "v1", crds[0].Version) |
| 114 | + assert.Contains(t, crds[0].Categories, "atlas") |
| 115 | + |
| 116 | + assert.Equal(t, "Group", crds[1].Kind) |
| 117 | +} |
0 commit comments