Skip to content

Commit

Permalink
Merge pull request helm#11923 from Bhargav-InfraCloud/helm-create-dep…
Browse files Browse the repository at this point in the history
…rication-tests

test(create): Add test to check deprecated `apiVersion`s in resource templates created by `helm create`
  • Loading branch information
mattfarina authored Sep 27, 2024
2 parents 983d5c2 + 9c0b4c8 commit d45439f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ test-unit:
@echo
@echo "==> Running unit tests <=="
GO111MODULE=on go test $(GOFLAGS) -run $(TESTS) $(PKG) $(TESTFLAGS)
@echo
@echo "==> Running unit test(s) with ldflags <=="
# Test to check the deprecation warnings on Kubernetes templates created by `helm create` against the current Kubernetes
# version. Note: The version details are set in var LDFLAGS. To avoid the ldflags impact on other unit tests that are
# based on older versions, this is run separately. When run without the ldflags in the unit test (above) or coverage
# test, it still passes with a false-positive result as the resources shouldn’t be deprecated in the older Kubernetes
# version if it only starts failing with the latest.
GO111MODULE=on go test $(GOFLAGS) -run ^TestHelmCreateChart_CheckDeprecatedWarnings$$ ./pkg/lint/ $(TESTFLAGS) -ldflags '$(LDFLAGS)'


.PHONY: test-coverage
test-coverage:
Expand Down
4 changes: 4 additions & 0 deletions pkg/chartutil/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,10 @@ func Create(name, dir string) (string, error) {
return cdir, errors.Errorf("file %s already exists and is not a directory", cdir)
}

// Note: If adding a new template below (i.e., to `helm create`) which is disabled by default (similar to hpa and
// ingress below); or making an existing template disabled by default, add the enabling condition in
// `TestHelmCreateChart_CheckDeprecatedWarnings` in `pkg/lint/lint_test.go` to make it run through deprecation checks
// with latest Kubernetes version.
files := []struct {
path string
content []byte
Expand Down
47 changes: 47 additions & 0 deletions pkg/lint/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,53 @@ func TestHelmCreateChart(t *testing.T) {
}
}

// TestHelmCreateChart_CheckDeprecatedWarnings checks if any default template created by `helm create` throws
// deprecated warnings in the linter check against the current Kubernetes version (provided using ldflags).
//
// See https://github.com/helm/helm/issues/11495
//
// Resources like hpa and ingress, which are disabled by default in values.yaml are enabled here using the equivalent
// of the `--set` flag.
//
// Note: This test requires the following ldflags to be set per the current Kubernetes version to avoid false-positive
// results.
// 1. -X helm.sh/helm/v3/pkg/lint/rules.k8sVersionMajor=<k8s-major-version>
// 2. -X helm.sh/helm/v3/pkg/lint/rules.k8sVersionMinor=<k8s-minor-version>
// or directly use '$(LDFLAGS)' in Makefile.
//
// When run without ldflags, the test passes giving a false-positive result. This is because the variables
// `k8sVersionMajor` and `k8sVersionMinor` by default are set to an older version of Kubernetes, with which, there
// might not be the deprecation warning.
func TestHelmCreateChart_CheckDeprecatedWarnings(t *testing.T) {
createdChart, err := chartutil.Create("checkdeprecatedwarnings", t.TempDir())
if err != nil {
t.Error(err)
return
}

// Add values to enable hpa, and ingress which are disabled by default.
// This is the equivalent of:
// helm lint checkdeprecatedwarnings --set 'autoscaling.enabled=true,ingress.enabled=true'
updatedValues := map[string]interface{}{
"autoscaling": map[string]interface{}{
"enabled": true,
},
"ingress": map[string]interface{}{
"enabled": true,
},
}

linterRunDetails := All(createdChart, updatedValues, namespace, true)
for _, msg := range linterRunDetails.Messages {
if strings.HasPrefix(msg.Error(), "[WARNING]") &&
strings.Contains(msg.Error(), "deprecated") {
// When there is a deprecation warning for an object created
// by `helm create` for the current Kubernetes version, fail.
t.Errorf("Unexpected deprecation warning for %q: %s", msg.Path, msg.Error())
}
}
}

// lint ignores import-values
// See https://github.com/helm/helm/issues/9658
func TestSubChartValuesChart(t *testing.T) {
Expand Down

0 comments on commit d45439f

Please sign in to comment.