|
| 1 | +package examples |
| 2 | + |
| 3 | +import ( |
| 4 | + "embed" |
| 5 | + "io/fs" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "gopkg.in/yaml.v3" |
| 10 | +) |
| 11 | + |
| 12 | +//go:embed config-*.yaml |
| 13 | +var configFS embed.FS |
| 14 | + |
| 15 | +func TestConfigFiles(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + // Find all config-*.yaml files in the embedded filesystem |
| 19 | + matches, err := fs.Glob(configFS, "config-*.yaml") |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("Failed to find config files: %v", err) |
| 22 | + } |
| 23 | + |
| 24 | + if len(matches) == 0 { |
| 25 | + t.Fatal("No config-*.yaml files found") |
| 26 | + } |
| 27 | + |
| 28 | + for _, configPath := range matches { |
| 29 | + t.Run(filepath.Base(configPath), func(t *testing.T) { |
| 30 | + t.Parallel() |
| 31 | + |
| 32 | + // Read the file from embedded filesystem |
| 33 | + data, err := configFS.ReadFile(configPath) |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("Failed to read file: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + // Validate YAML syntax |
| 39 | + var config map[string]any |
| 40 | + if err := yaml.Unmarshal(data, &config); err != nil { |
| 41 | + t.Fatalf("Invalid YAML syntax: %v", err) |
| 42 | + } |
| 43 | + |
| 44 | + // Check required fields |
| 45 | + if _, ok := config["source"]; !ok { |
| 46 | + t.Fatal("Missing required field: source") |
| 47 | + } |
| 48 | + |
| 49 | + source, ok := config["source"].(map[string]any) |
| 50 | + if !ok { |
| 51 | + t.Fatal("source must be a map") |
| 52 | + } |
| 53 | + |
| 54 | + if _, ok := source["type"]; !ok { |
| 55 | + t.Fatal("Missing required field: source.type") |
| 56 | + } |
| 57 | + |
| 58 | + if _, ok := source["format"]; !ok { |
| 59 | + t.Fatal("Missing required field: source.format") |
| 60 | + } |
| 61 | + |
| 62 | + if _, ok := config["syncPolicy"]; !ok { |
| 63 | + t.Fatal("Missing required field: syncPolicy") |
| 64 | + } |
| 65 | + |
| 66 | + syncPolicy, ok := config["syncPolicy"].(map[string]any) |
| 67 | + if !ok { |
| 68 | + t.Fatal("syncPolicy must be a map") |
| 69 | + } |
| 70 | + |
| 71 | + if _, ok := syncPolicy["interval"]; !ok { |
| 72 | + t.Fatal("Missing required field: syncPolicy.interval") |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments