Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved test for helm chart deps #97

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ use_repo(
"helm_windows_amd64_toolchain",
)
use_repo(helm, "go_yaml_yaml")
use_repo(helm, "helm_test_deps__with_chart_deps", "rules_helm_test_container_base")
use_repo(
helm,
"helm_test_deps__with_chart_deps_postgresql",
"helm_test_deps__with_chart_deps_redis",
"rules_helm_test_container_base",
)

register_toolchains(
"@helm_darwin_amd64_toolchain//:toolchain",
Expand Down
43 changes: 28 additions & 15 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion tests/test_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ load("//helm:defs.bzl", "helm_import_repository")

def helm_test_deps():
helm_import_repository(
name = "helm_test_deps__with_chart_deps",
name = "helm_test_deps__with_chart_deps_redis",
repository = "https://charts.bitnami.com/bitnami",
url = "https://charts.bitnami.com/bitnami/redis-14.4.0.tgz",
version = "14.4.0",
sha256 = "43374837646a67539eb2999cd8973dc54e8fcdc14896761e594b9d616734edf2",
chart_name = "redis",
)

helm_import_repository(
name = "helm_test_deps__with_chart_deps_postgresql",
repository = "https://charts.bitnami.com/bitnami",
url = "https://charts.bitnami.com/bitnami/postgresql-14.0.5.tgz",
version = "14.0.5",
sha256 = "38d9b6657aa3b0cc16d190570dbaf96796e997d03a1665264dac9966343e4d1b",
chart_name = "postgresql",
)

oci_pull(
name = "rules_helm_test_container_base",
digest = "sha256:2042a492bcdd847a01cd7f119cd48caa180da696ed2aedd085001a78664407d6",
Expand Down
16 changes: 14 additions & 2 deletions tests/with_chart_deps/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_test")
load("//helm:defs.bzl", "helm_chart", "helm_lint_test")

exports_files(["Chart.lock"])
Expand All @@ -6,10 +7,21 @@ helm_chart(
name = "with_chart_deps",
chart = "Chart.yaml",
values = "values.yaml",
deps = ["@helm_test_deps__with_chart_deps//:redis"],
deps = [
"@helm_test_deps__with_chart_deps_postgresql//:postgresql",
"@helm_test_deps__with_chart_deps_redis//:redis",
],
)

helm_lint_test(
name = "with_chart_deps_test",
name = "with_chart_deps_lint_test",
chart = ":with_chart_deps",
)

go_test(
name = "with_chart_deps_test",
srcs = ["with_chart_deps_test.go"],
data = [":with_chart_deps"],
env = {"HELM_CHART": "$(rlocationpath :with_chart_deps)"},
deps = ["@io_bazel_rules_go//go/runfiles"],
)
3 changes: 3 additions & 0 deletions tests/with_chart_deps/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ dependencies:
- name: redis
repository: https://charts.bitnami.com/bitnami
version: 14.4.0
- name: postgresql
repository: https://charts.bitnami.com/bitnami
version: 14.0.5
93 changes: 93 additions & 0 deletions tests/with_chart_deps/with_chart_deps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"archive/tar"
"compress/gzip"
"io"
"os"
"strings"
"testing"

"github.com/bazelbuild/rules_go/go/runfiles"
)

func WithChartDepsTest(t *testing.T) {
// Retrieve the Helm chart location from the environment variable
helmChartPath := os.Getenv("HELM_CHART")
if helmChartPath == "" {
t.Fatal("HELM_CHART environment variable is not set")
}

// Locate the runfile
path, err := runfiles.Rlocation(helmChartPath)
if err != nil {
t.Fatalf("Failed to find runfile with: %v", err)
}

// Open the .tgz file
file, err := os.Open(path)
if err != nil {
t.Fatalf("Failed to open the Helm chart file: %v", err)
}
defer file.Close()

// Wrap the file in a Gzip reader
gzr, err := gzip.NewReader(file)
if err != nil {
t.Fatalf("Failed to create Gzip reader: %v", err)
}
defer gzr.Close()

// Create a tar reader from the Gzip reader
tarReader := tar.NewReader(gzr)

// Initialize flags to check for the two files
var redisChartFound, postgresChartFound bool
var redisChartContent, postgresChartContent string

// Iterate through the tar archive
for {
header, err := tarReader.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
t.Fatalf("Error reading tar archive: %v", err)
}

// Check for the two specific files
if header.Name == "charts/redis/Chart.yaml" {
redisChartFound = true
content, err := io.ReadAll(tarReader)
if err != nil {
t.Fatalf("Failed to read redis Chart.yaml: %v", err)
}
redisChartContent = string(content)
}

if header.Name == "charts/postgresql/Chart.yaml" {
postgresChartFound = true
content, err := io.ReadAll(tarReader)
if err != nil {
t.Fatalf("Failed to read postgresql Chart.yaml: %v", err)
}
postgresChartContent = string(content)
}
}

// Assert that both files were found
if !redisChartFound {
t.Error("charts/redis/Chart.yaml was not found in the Helm chart")
}
if !postgresChartFound {
t.Error("charts/postgresql/Chart.yaml was not found in the Helm chart")
}

// Assert that the content of both files contains the expected strings
if !strings.Contains(redisChartContent, "redis") {
t.Error("charts/redis/Chart.yaml does not contain the expected string 'redis'")
}
if !strings.Contains(postgresChartContent, "postgres") {
t.Error("charts/postgresql/Chart.yaml does not contain the expected string 'postgres'")
}
}
Loading