Skip to content

Commit

Permalink
Merge branch 'fix/OverrideValues'
Browse files Browse the repository at this point in the history
  • Loading branch information
quintush committed Jul 26, 2021
2 parents 1ae81ca + 7f27ba3 commit fd58c82
Show file tree
Hide file tree
Showing 33 changed files with 640 additions and 241 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
0.2.7 / 2021-07-26
==================
- Added samples for contains mapping (resolves #107)
- Improved errorhandling, show complete error on failure, when failed_template validator is not used (resolves #109)
- Fixed import-values (credits to: @rquinio1A, resolves #115)
- Added samples for subsubcharts and global values (resolves #114)
- Fix small documentation improvements and corrections (credits to: @jglick, @krichter722, @craig-mcmahon)
- Update packages to lates version

0.2.6 / 2021-03-31
==================
- Add support for list of templates on tests (credits to: @stevelipinski)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ DOCKER ?= "quintush/helm-unittest"

.PHONY: install
install: bootstrap build
mkdir $(HELM_PLUGIN_DIR)
mkdir -p $(HELM_PLUGIN_DIR)
cp -t $(HELM_PLUGIN_DIR) untt
cp -t $(HELM_PLUGIN_DIR) plugin.yaml

Expand Down
28 changes: 20 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,33 @@ require (
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
github.com/bradleyjkemp/cupaloy/v2 v2.6.0
github.com/cloudfoundry/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21
github.com/fatih/color v1.10.0
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
github.com/fatih/color v1.12.0
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.4.1
github.com/mitchellh/reflectwalk v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0
github.com/spf13/cobra v1.1.3
github.com/spf13/cobra v1.2.1
github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/testify v1.7.0
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
golang.org/x/oauth2 v0.0.0-20210313182246-cd4f82c27b84 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
google.golang.org/appengine v1.6.7 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/net v0.0.0-20210716203947-853a461950ff // indirect
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0
helm.sh/helm/v3 v3.5.1
helm.sh/helm/v3 v3.6.2
k8s.io/apiextensions-apiserver v0.21.3 // indirect
k8s.io/helm v2.17.0+incompatible
k8s.io/utils v0.0.0-20210305010621-2afb4311ab10 // indirect
k8s.io/klog/v2 v2.10.0 // indirect
k8s.io/utils v0.0.0-20210722164352-7f3ee0f31471 // indirect
)
490 changes: 337 additions & 153 deletions go.sum

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions pkg/unittest/.snapshots/TestV3RunJobWithToLongReleaseName
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(*results.TestJobResult)({
DisplayName: (string) (len=19) "to long releasename",
Index: (int) 0,
Passed: (bool) false,
ExecError: (*errors.fundamental)(invalid release name, must match regex ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$ and the length must not be longer than 53),
AssertsResult: ([]*results.AssertionResult) <nil>,
Duration: (time.Duration) 0s
})
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

### Chart [ basic ] ../../test/data/v2/basic
### Chart [ basic ] ../../test/data/v3/basic


PASS Configmap mulit line Test ../../test/data/v2/basic/tests/configmap_test.yaml
PASS Custom Resource Definition Test ../../test/data/v2/basic/tests/crd_test.yaml
PASS test deployment ../../test/data/v2/basic/tests/deployment_test.yaml
PASS test ingress ../../test/data/v2/basic/tests/ingress_test.yaml
PASS test notes ../../test/data/v2/basic/tests/notes_test.yaml
PASS test service ../../test/data/v2/basic/tests/service_test.yaml
PASS Configmap mulit line Test ../../test/data/v3/basic/tests/configmap_test.yaml
PASS Custom Resource Definition Test ../../test/data/v3/basic/tests/crd_test.yaml
PASS test deployment ../../test/data/v3/basic/tests/deployment_test.yaml
PASS test ingress ../../test/data/v3/basic/tests/ingress_test.yaml
PASS test notes ../../test/data/v3/basic/tests/notes_test.yaml
PASS test service ../../test/data/v3/basic/tests/service_test.yaml


Charts: 1 passed, 1 total
Expand Down
45 changes: 2 additions & 43 deletions pkg/unittest/.snapshots/TestV3RunnerOkWithFailedTests
Original file line number Diff line number Diff line change
Expand Up @@ -335,48 +335,7 @@
- containerPort: null
FAIL test deployment that would be fail ../../test/data/v3/basic/tests_failed/include_deployment_test.yaml
- should fail all kinds of assertion

- asserts[0] `equal` fail
Error: Invalid rendering: no template "basic/templates/configmap.yaml" associated with template "gotpl"

- asserts[1] `notEqual` fail
Error: Invalid rendering: no template "basic/templates/configmap.yaml" associated with template "gotpl"

- asserts[2] `matchRegex` fail
Error: Invalid rendering: no template "basic/templates/configmap.yaml" associated with template "gotpl"

- asserts[3] `notMatchRegex` fail
Error: Invalid rendering: no template "basic/templates/configmap.yaml" associated with template "gotpl"

- asserts[4] `contains` fail
Error: Invalid rendering: no template "basic/templates/configmap.yaml" associated with template "gotpl"

- asserts[5] `notContains` fail
Error: Invalid rendering: no template "basic/templates/configmap.yaml" associated with template "gotpl"

- asserts[6] `isNull` fail
Error: Invalid rendering: no template "basic/templates/configmap.yaml" associated with template "gotpl"

- asserts[7] `isNotNull` fail
Error: Invalid rendering: no template "basic/templates/configmap.yaml" associated with template "gotpl"

- asserts[8] `isEmpty` fail
Error: Invalid rendering: no template "basic/templates/configmap.yaml" associated with template "gotpl"

- asserts[9] `isNotEmpty` fail
Error: Invalid rendering: no template "basic/templates/configmap.yaml" associated with template "gotpl"

- asserts[10] `isKind` fail
Error: Invalid rendering: no template "basic/templates/configmap.yaml" associated with template "gotpl"

- asserts[11] `isAPIVersion` fail
Error: Invalid rendering: no template "basic/templates/configmap.yaml" associated with template "gotpl"

- asserts[12] `hasDocuments` fail
Error: Invalid rendering: no template "basic/templates/configmap.yaml" associated with template "gotpl"

- asserts[13] `matchSnapshot` fail
Error: Invalid rendering: no template "basic/templates/configmap.yaml" associated with template "gotpl"
Error: template: basic/templates/deployment.yaml:12:24: executing "basic/templates/deployment.yaml" at <include (print $.Template.BasePath "/configmap.yaml") .>: error calling include: template: no template "basic/templates/configmap.yaml" associated with template "gotpl"
FAIL test ingress that should fail ../../test/data/v3/basic/tests_failed/ingress_test.yaml
- should fail render nothing if not enabled

Expand Down Expand Up @@ -550,7 +509,7 @@

Charts: 1 failed, 0 passed, 1 total
Test Suites: 8 failed, 0 passed, 8 total
Tests: 16 failed, 0 passed, 16 total
Tests: 16 failed, 1 errored, 0 passed, 16 total
Snapshot: 2 passed, 2 total
Time: XX.XXXms

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

### Chart [ basic ] ../../test/data/v2/basic
### Chart [ basic ] ../../test/data/v3/basic


PASS Configmap mulit line Test ../../test/data/v2/basic/tests/configmap_test.yaml
PASS Custom Resource Definition Test ../../test/data/v2/basic/tests/crd_test.yaml
PASS test deployment ../../test/data/v2/basic/tests/deployment_test.yaml
PASS test ingress ../../test/data/v2/basic/tests/ingress_test.yaml
PASS test notes ../../test/data/v2/basic/tests/notes_test.yaml
PASS test service ../../test/data/v2/basic/tests/service_test.yaml
PASS Configmap mulit line Test ../../test/data/v3/basic/tests/configmap_test.yaml
PASS Custom Resource Definition Test ../../test/data/v3/basic/tests/crd_test.yaml
PASS test deployment ../../test/data/v3/basic/tests/deployment_test.yaml
PASS test ingress ../../test/data/v3/basic/tests/ingress_test.yaml
PASS test notes ../../test/data/v3/basic/tests/notes_test.yaml
PASS test service ../../test/data/v3/basic/tests/service_test.yaml


Charts: 1 passed, 1 total
Expand Down
14 changes: 14 additions & 0 deletions pkg/unittest/.snapshots/TestV3RunnerOkWithSubSubChartsPassedTests
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

### Chart [ with-subsubcharts ] ../../test/data/v3/with-subsubcharts


PASS ../../test/data/v3/with-subsubcharts/tests/deployment_test.yaml


Charts: 1 passed, 1 total
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshot: 0 passed, 0 total
Time: XX.XXXms


21 changes: 20 additions & 1 deletion pkg/unittest/test_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ type TestJob struct {
definitionFile string
// list of templates assertion should assert if not specified
defaultTemplatesToAssert []string
// requireSuccess
requireRenderSuccess bool
}

// RunV2 render the chart and validate it with assertions in TestJob.
Expand Down Expand Up @@ -424,6 +426,14 @@ func (t *TestJob) renderV3Chart(targetChart *v3chart.Chart, userValues []byte) (
}
options := *t.releaseV3Option()

//Check Release Name length
if t.Release.Name != "" {
err = v3util.ValidateReleaseName(t.Release.Name)
if err != nil {
return nil, false, err
}
}

// Override the chart version when version is setup in test.
if t.Chart.Version != "" {
targetChart.Metadata.Version = t.Chart.Version
Expand All @@ -448,9 +458,15 @@ func (t *TestJob) renderV3Chart(targetChart *v3chart.Chart, userValues []byte) (
filteredChart := t.filterV3Chart(targetChart)

outputOfFiles, err := v3engine.Render(filteredChart, vals)

// When rendering failed, due to fail or required,
// make sure to translate the error to outputOfFiles.
if err != nil {
// When no failed assertion is set, the error can be send directly as a failure.
if t.requireRenderSuccess {
return nil, false, err
}

renderSucceed = false
// Parse the error and create an outputFile
filePath, content := parseV3RenderError(err.Error())
Expand All @@ -459,7 +475,7 @@ func (t *TestJob) renderV3Chart(targetChart *v3chart.Chart, userValues []byte) (
return nil, renderSucceed, err
}

// If error validate if template error occurred
// If error, validate if template error occurred
if content == noValueContent {
for _, fileName := range t.defaultTemplatesToAssert {
selectedTemplateName := filepath.ToSlash(filepath.Join(t.chartRoute, getTemplateFileName(fileName)))
Expand Down Expand Up @@ -654,7 +670,10 @@ func (t *TestJob) polishAssertionsTemplate(targetChartName string) {
t.chartRoute = targetChartName
}

t.requireRenderSuccess = true

for _, assertion := range t.Assertions {
t.requireRenderSuccess = t.requireRenderSuccess && assertion.requireRenderSuccess
templatesToAssert := make([]string, 0)

if t.DocumentIndex != nil {
Expand Down
23 changes: 23 additions & 0 deletions pkg/unittest/test_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,29 @@ asserts:
a.Equal(1, len(testResult.AssertsResult))
}

func TestV3RunJobWithToLongReleaseName(t *testing.T) {
c, _ := loader.Load(testV3BasicChart)
manifest := `
it: to long releasename
release:
name: my-very-very-very-very-very-very-very-very-very-very-very-very-release
asserts:
- hasDocuments:
count: 1
template: crd_backup.yaml
`
var tj TestJob
yaml.Unmarshal([]byte(manifest), &tj)

testResult := tj.RunV3(c, &snapshot.Cache{}, true, &results.TestJobResult{})

a := assert.New(t)
cupaloy.SnapshotT(t, makeTestJobResultSnapshotable(testResult))

a.NotNil(testResult.ExecError)
a.False(testResult.Passed)
}

func TestV3RunJobWithCapabilitySettings(t *testing.T) {
c, _ := loader.Load(testV3BasicChart)
manifest := `
Expand Down
15 changes: 13 additions & 2 deletions pkg/unittest/test_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,25 @@ func TestV3RunnerOkWithPassedTests(t *testing.T) {
cupaloy.SnapshotT(t, makeOutputSnapshotable(buffer.String())...)
}

func TestV3RunnerOkWithSubSubChartsPassedTests(t *testing.T) {
buffer := new(bytes.Buffer)
runner := TestRunner{
Printer: printer.NewPrinter(buffer, nil),
TestFiles: []string{testTestFiles},
}
passed := runner.RunV3([]string{testV3WithSubSubFolderChart})
assert.True(t, passed, buffer.String())
cupaloy.SnapshotT(t, makeOutputSnapshotable(buffer.String())...)
}

func TestV3RunnerOkWithOverrideValuesPassedTests(t *testing.T) {
buffer := new(bytes.Buffer)
runner := TestRunner{
Printer: printer.NewPrinter(buffer, nil),
TestFiles: []string{testTestFiles},
ValuesFiles: []string{testValuesFiles},
}
passed := runner.RunV3([]string{testV2BasicChart})
passed := runner.RunV3([]string{testV3BasicChart})
assert.True(t, passed, buffer.String())
cupaloy.SnapshotT(t, makeOutputSnapshotable(buffer.String())...)
}
Expand All @@ -217,7 +228,7 @@ func TestV3RunnerOkWithAbsoluteOverrideValuesPassedTests(t *testing.T) {
TestFiles: []string{testTestFiles},
ValuesFiles: []string{fullPath},
}
passed := runner.RunV3([]string{testV2BasicChart})
passed := runner.RunV3([]string{testV3BasicChart})
assert.True(t, passed, buffer.String())
cupaloy.SnapshotT(t, makeOutputSnapshotable(buffer.String())...)
}
Expand Down
28 changes: 24 additions & 4 deletions pkg/unittest/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type TestSuite struct {
// where the test suite file located
definitionFile string
// route indicate which chart in the dependency hierarchy
// like "parant-chart", "parent-charts/charts/child-chart"
// like "parent-chart", "parent-charts/charts/child-chart"
chartRoute string
}

Expand Down Expand Up @@ -222,7 +222,7 @@ func (s *TestSuite) runV3TestJobs(
jobResults := make([]*results.TestJobResult, len(s.Tests))
metadataDependenciesBackup := cloneDependencies(chart.Metadata.Dependencies)
dependenciesBackup := chart.Dependencies()
valuesBackup := chart.Values
valuesBackup := cloneValues(chart.Values)

for idx, testJob := range s.Tests {
jobResult := testJob.RunV3(chart, cache, failfast, &results.TestJobResult{Index: idx})
Expand All @@ -233,7 +233,9 @@ func (s *TestSuite) runV3TestJobs(
}

chart.SetDependencies(dependenciesBackup...)
chart.Values = valuesBackup
chart.Values = nil
chart.Values = cloneValues(valuesBackup)
chart.Metadata.Dependencies = nil
chart.Metadata.Dependencies = cloneDependencies(metadataDependenciesBackup)

if !suitePass && failfast {
Expand All @@ -254,7 +256,7 @@ func cloneDependencies(metadataDependencies []*v3chart.Dependency) []*v3chart.De
Condition: metadataDependency.Condition,
Tags: metadataDependency.Tags,
Enabled: metadataDependency.Enabled,
ImportValues: metadataDependency.ImportValues,
ImportValues: cloneImportValues(metadataDependency.ImportValues),
Alias: metadataDependency.Alias,
}

Expand All @@ -263,3 +265,21 @@ func cloneDependencies(metadataDependencies []*v3chart.Dependency) []*v3chart.De

return clonedDependencies
}

func cloneValues(values map[string]interface{}) map[string]interface{} {
clonedValues := make(map[string]interface{})

for key, value := range values {
clonedValues[key] = value
}

return clonedValues
}

func cloneImportValues(importValues []interface{}) []interface{} {
clonedImportValues := make([]interface{}, 0)

clonedImportValues = append(clonedImportValues, importValues...)

return clonedImportValues
}
1 change: 1 addition & 0 deletions pkg/unittest/test_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const testV3InvalidBasicChart string = "../../test/data/v3/invalidbasic"
const testV3BasicChart string = "../../test/data/v3/basic"
const testV3WithSubChart string = "../../test/data/v3/with-subchart"
const testV3WithSubFolderChart string = "../../test/data/v3/with-subfolder"
const testV3WithSubSubFolderChart string = "../../test/data/v3/with-subsubcharts"

var tmpdir, _ = ioutil.TempDir("", testSuiteTests)

Expand Down
Loading

0 comments on commit fd58c82

Please sign in to comment.