|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/tar" |
| 5 | + "compress/gzip" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/bazelbuild/rules_go/go/runfiles" |
| 12 | + "gopkg.in/yaml.v3" |
| 13 | +) |
| 14 | + |
| 15 | +type HelmChartDependency struct { |
| 16 | + Name string |
| 17 | + Repository string |
| 18 | + Version string |
| 19 | +} |
| 20 | + |
| 21 | +type HelmChart struct { |
| 22 | + Dependencies []HelmChartDependency |
| 23 | +} |
| 24 | + |
| 25 | +func loadChart(content string) (HelmChart, error) { |
| 26 | + var chart HelmChart |
| 27 | + err := yaml.Unmarshal([]byte(content), &chart) |
| 28 | + if err != nil { |
| 29 | + return chart, fmt.Errorf("Error unmarshalling chart content: %w", err) |
| 30 | + } |
| 31 | + |
| 32 | + return chart, nil |
| 33 | +} |
| 34 | + |
| 35 | +func TestWithCrdsTest(t *testing.T) { |
| 36 | + // Retrieve the Helm chart location from the environment variable |
| 37 | + helmChartPath := os.Getenv("HELM_CHART") |
| 38 | + if helmChartPath == "" { |
| 39 | + t.Fatal("HELM_CHART environment variable is not set") |
| 40 | + } |
| 41 | + |
| 42 | + // Locate the runfile |
| 43 | + path, err := runfiles.Rlocation(helmChartPath) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("Failed to find runfile with: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + // Open the .tgz file |
| 49 | + file, err := os.Open(path) |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("Failed to open the Helm chart file: %v", err) |
| 52 | + } |
| 53 | + defer file.Close() |
| 54 | + |
| 55 | + // Wrap the file in a Gzip reader |
| 56 | + gzr, err := gzip.NewReader(file) |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("Failed to create Gzip reader: %v", err) |
| 59 | + } |
| 60 | + defer gzr.Close() |
| 61 | + |
| 62 | + // Create a tar reader from the Gzip reader |
| 63 | + tarReader := tar.NewReader(gzr) |
| 64 | + |
| 65 | + // Initialize flags to check for the two files |
| 66 | + var crdsFound bool |
| 67 | + |
| 68 | + // Iterate through the tar archive |
| 69 | + for { |
| 70 | + header, err := tarReader.Next() |
| 71 | + if err == io.EOF { |
| 72 | + break // End of archive |
| 73 | + } |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("Error reading tar archive: %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + if header.Name == "with-crds/crds/test.crd.yaml" { |
| 79 | + crdsFound = true |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("Failed to read with-crds/crds/test.crd.yaml: %v", err) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if !crdsFound { |
| 87 | + t.Error("crds/test.crd.yaml was not found in the Helm chart") |
| 88 | + } |
| 89 | +} |
0 commit comments