|
| 1 | +package sdl |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "github.com/xeipuuv/gojsonschema" |
| 11 | + "gopkg.in/yaml.v3" |
| 12 | +) |
| 13 | + |
| 14 | +const fixturesRoot = "../../testdata/sdl" |
| 15 | +const schemasRoot = "../../specs/sdl" |
| 16 | + |
| 17 | +func validateAgainstSchema(t *testing.T, name string, data []byte, schemaPath string) { |
| 18 | + t.Helper() |
| 19 | + |
| 20 | + schemaBytes, err := os.ReadFile(schemaPath) |
| 21 | + if err != nil { |
| 22 | + t.Fatalf("Schema file %s not found: %v", schemaPath, err) |
| 23 | + } |
| 24 | + |
| 25 | + var schemaJSON map[string]any |
| 26 | + err = yaml.Unmarshal(schemaBytes, &schemaJSON) |
| 27 | + require.NoError(t, err, "Failed to parse YAML schema") |
| 28 | + |
| 29 | + jsonBytes, err := json.Marshal(schemaJSON) |
| 30 | + require.NoError(t, err, "Failed to convert schema to JSON") |
| 31 | + |
| 32 | + schemaLoader := gojsonschema.NewSchemaLoader() |
| 33 | + schema, err := schemaLoader.Compile(gojsonschema.NewBytesLoader(jsonBytes)) |
| 34 | + require.NoError(t, err, "Failed to compile schema") |
| 35 | + |
| 36 | + result, err := schema.Validate(gojsonschema.NewBytesLoader(data)) |
| 37 | + require.NoError(t, err, "Failed to validate against schema") |
| 38 | + |
| 39 | + if !result.Valid() { |
| 40 | + var errors []string |
| 41 | + for _, desc := range result.Errors() { |
| 42 | + errors = append(errors, desc.String()) |
| 43 | + } |
| 44 | + require.Failf(t, "%s validation failed", name, "Errors: %v", errors) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestParityV2_0(t *testing.T) { |
| 49 | + testParity(t, "v2.0") |
| 50 | +} |
| 51 | + |
| 52 | +func TestParityV2_1(t *testing.T) { |
| 53 | + testParity(t, "v2.1") |
| 54 | +} |
| 55 | + |
| 56 | +func testParity(t *testing.T, version string) { |
| 57 | + fixturesDir := filepath.Join(fixturesRoot, version) |
| 58 | + |
| 59 | + entries, err := os.ReadDir(fixturesDir) |
| 60 | + if os.IsNotExist(err) { |
| 61 | + t.Fatalf("Fixtures directory %s does not exist", fixturesDir) |
| 62 | + } |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + for _, entry := range entries { |
| 66 | + if !entry.IsDir() { |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + fixtureName := entry.Name() |
| 71 | + fixtureDir := filepath.Join(fixturesDir, fixtureName) |
| 72 | + inputPath := filepath.Join(fixtureDir, "input.yaml") |
| 73 | + manifestPath := filepath.Join(fixtureDir, "manifest.json") |
| 74 | + groupsPath := filepath.Join(fixtureDir, "groups.json") |
| 75 | + |
| 76 | + if _, err := os.Stat(manifestPath); os.IsNotExist(err) { |
| 77 | + t.Fatalf("manifest.json not generated for %s (run: make generate-sdl-fixtures)", fixtureName) |
| 78 | + } |
| 79 | + |
| 80 | + if _, err := os.Stat(groupsPath); os.IsNotExist(err) { |
| 81 | + t.Fatalf("groups.json not generated for %s (run: make generate-sdl-fixtures)", fixtureName) |
| 82 | + } |
| 83 | + |
| 84 | + t.Run(fixtureName, func(t *testing.T) { |
| 85 | + sdl, err := ReadFile(inputPath) |
| 86 | + require.NoError(t, err) |
| 87 | + |
| 88 | + expectedManifestBytes, err := os.ReadFile(manifestPath) |
| 89 | + require.NoError(t, err, "Failed to read expected manifest.json") |
| 90 | + |
| 91 | + expectedGroupsBytes, err := os.ReadFile(groupsPath) |
| 92 | + require.NoError(t, err, "Failed to read expected groups.json") |
| 93 | + |
| 94 | + manifest, err := sdl.Manifest() |
| 95 | + require.NoError(t, err) |
| 96 | + |
| 97 | + actualManifestBytes, err := json.Marshal(manifest) |
| 98 | + require.NoError(t, err) |
| 99 | + |
| 100 | + var expectedManifest, actualManifest any |
| 101 | + require.NoError(t, json.Unmarshal(expectedManifestBytes, &expectedManifest)) |
| 102 | + require.NoError(t, json.Unmarshal(actualManifestBytes, &actualManifest)) |
| 103 | + require.Equal(t, expectedManifest, actualManifest, "Manifest does not match expected output") |
| 104 | + |
| 105 | + validateAgainstSchema(t, "manifest", actualManifestBytes, schemasRoot+"/manifest.schema.yaml") |
| 106 | + |
| 107 | + groups, err := sdl.DeploymentGroups() |
| 108 | + require.NoError(t, err) |
| 109 | + |
| 110 | + actualGroupsBytes, err := json.Marshal(groups) |
| 111 | + require.NoError(t, err) |
| 112 | + |
| 113 | + var expectedGroups, actualGroups any |
| 114 | + require.NoError(t, json.Unmarshal(expectedGroupsBytes, &expectedGroups)) |
| 115 | + require.NoError(t, json.Unmarshal(actualGroupsBytes, &actualGroups)) |
| 116 | + require.Equal(t, expectedGroups, actualGroups, "Groups does not match expected output") |
| 117 | + |
| 118 | + validateAgainstSchema(t, "groups", actualGroupsBytes, schemasRoot+"/groups.schema.yaml") |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestInvalidSDLsRejected(t *testing.T) { |
| 124 | + invalidDir := filepath.Join(fixturesRoot, "invalid") |
| 125 | + |
| 126 | + entries, err := os.ReadDir(invalidDir) |
| 127 | + if os.IsNotExist(err) { |
| 128 | + t.Skip("Invalid fixtures directory does not exist yet") |
| 129 | + return |
| 130 | + } |
| 131 | + require.NoError(t, err) |
| 132 | + |
| 133 | + for _, entry := range entries { |
| 134 | + if entry.IsDir() { |
| 135 | + continue |
| 136 | + } |
| 137 | + |
| 138 | + fixturePath := filepath.Join(invalidDir, entry.Name()) |
| 139 | + t.Run(entry.Name(), func(t *testing.T) { |
| 140 | + _, err := ReadFile(fixturePath) |
| 141 | + require.Error(t, err) |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
0 commit comments