Skip to content

Commit

Permalink
Merge branch 'pr/517' into chore/UpdateTestsAndDependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
quintush committed Dec 29, 2024
2 parents af69950 + 11c0357 commit 4f9ae32
Show file tree
Hide file tree
Showing 25 changed files with 1,388 additions and 291 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ _dist

.idea

.direnv
.direnv
cover.out
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ help:

.PHONY: plugin-dir
plugin-dir:
HELM_3_PLUGINS := $(shell bash -c 'eval $$(helm env); echo $$HELM_PLUGINS')
HELM_PLUGIN_DIR := $(HELM_3_PLUGINS)/helm-unittest
HELM_3_PLUGINS := $(shell bash -c 'eval $$(helm env); echo $$HELM_PLUGINS')
HELM_PLUGIN_DIR := $(HELM_3_PLUGINS)/helm-unittest

.PHONY: install
install: bootstrap build plugin-dir
Expand All @@ -50,6 +50,11 @@ hookInstall: bootstrap build
unittest: ## Run unit tests
go test ./... -v -cover

.PHONY: test-coverage
test-coverage: ## Test coverage with open report in default browser
@go test -cover -coverprofile=cover.out -v ./...
@go tool cover -html=cover.out

.PHONY: build-debug
build-debug: ## Compile packages and dependencies with debug flag
go build -o untt-dbg -gcflags "all=-N -l" ./cmd/helm-unittest
Expand Down Expand Up @@ -89,7 +94,7 @@ dependency: ## Dependency maintanance
go mod tidy

.PHONY: dockerimage
dockerimage: build
dockerimage: build ## Build docker image
docker build --no-cache --build-arg HELM_VERSION=$(HELM_VERSION) -t $(DOCKER):$(VERSION) -f AlpineTest.Dockerfile .

.PHONY: test-docker
Expand Down
61 changes: 58 additions & 3 deletions internal/common/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,39 @@ package common
import (
"bytes"
"strings"
"testing"

yaml "gopkg.in/yaml.v3"
"io"

"github.com/stretchr/testify/assert"
yamlv3 "gopkg.in/yaml.v3"
yaml "sigs.k8s.io/yaml"
)

type YamlNode struct {
Node yamlv3.Node
}

func NewYamlNode() YamlNode {
return YamlNode{
Node: yamlv3.Node{},
}
}

// YamlNewDecoder returns a new decoder that reads from r.
func YamlNewDecoder(r io.Reader) *yamlv3.Decoder {
return yamlv3.NewDecoder(r)
}

// YamlNewEncoder returns a new encoder that writes to w.
func YamlNewEncoder(w io.Writer) *yamlv3.Encoder {
return yamlv3.NewEncoder(w)
}

// TrustedMarshalYAML marshal yaml without error returned, if an error happens it panics
func TrustedMarshalYAML(d interface{}) string {
byteBuffer := new(bytes.Buffer)
yamlEncoder := yaml.NewEncoder(byteBuffer)
yamlEncoder := yamlv3.NewEncoder(byteBuffer)
yamlEncoder.SetIndent(YAMLINDENTION)
defer yamlEncoder.Close()
if err := yamlEncoder.Encode(d); err != nil {
Expand All @@ -22,9 +47,39 @@ func TrustedMarshalYAML(d interface{}) string {
// TrustedUnmarshalYAML unmarshal yaml without error returned, if an error happens it panics
func TrustedUnmarshalYAML(d string) map[string]interface{} {
parsedYaml := K8sManifest{}
yamlDecoder := yaml.NewDecoder(strings.NewReader(d))
yamlDecoder := yamlv3.NewDecoder(strings.NewReader(d))
if err := yamlDecoder.Decode(&parsedYaml); err != nil {
panic(err)
}
return parsedYaml
}

func YamlToJson(in string) ([]byte, error) {
return yaml.YAMLToJSON([]byte(in))
}

func YmlUnmarshal(in string, out interface{}) error {
err := yamlv3.Unmarshal([]byte(in), out)
return err
}

func YmlUnmarshalTestHelper(input string, out any, t *testing.T) {
t.Helper()
err := YmlUnmarshal(input, out)
assert.NoError(t, err)
}

func YmlMarshall(in interface{}) (string, error) {
out, err := yaml.Marshal(in)
if err != nil {
return "", err
}
return string(out), nil
}

func YmlMarshallTestHelper(in interface{}, t *testing.T) string {
t.Helper()
out, err := yaml.Marshal(in)
assert.NoError(t, err)
return string(out)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
PASS test ingress ../../test/data/v3/basic/tests/ingress_test.yaml
PASS test notes ../../test/data/v3/basic/tests/notes_test.yaml
PASS test override names and fullNames in Kubernetes resources ../../test/data/v3/basic/tests/namesOverride_test.yaml
PASS test pod disruption budget ../../test/data/v3/basic/tests/pdp_test.yaml
PASS test service ../../test/data/v3/basic/tests/service_test.yaml
PASS test service account ../../test/data/v3/basic/tests/serviceaccount_test.yaml


Charts: 1 passed, 1 total
Test Suites: 12 passed, 12 total
Tests: 34 passed, 34 total
Test Suites: 13 passed, 13 total
Tests: 40 passed, 40 total
Snapshot: 4 passed, 4 total
Time: XX.XXXms

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
PASS test ingress ../../test/data/v3/basic/tests/ingress_test.yaml
PASS test notes ../../test/data/v3/basic/tests/notes_test.yaml
PASS test override names and fullNames in Kubernetes resources ../../test/data/v3/basic/tests/namesOverride_test.yaml
PASS test pod disruption budget ../../test/data/v3/basic/tests/pdp_test.yaml
PASS test service ../../test/data/v3/basic/tests/service_test.yaml
PASS test service account ../../test/data/v3/basic/tests/serviceaccount_test.yaml


Charts: 1 passed, 1 total
Test Suites: 12 passed, 12 total
Tests: 34 passed, 34 total
Test Suites: 13 passed, 13 total
Tests: 40 passed, 40 total
Snapshot: 4 passed, 4 total
Time: XX.XXXms

Expand Down
5 changes: 3 additions & 2 deletions pkg/unittest/.snapshots/TestV3RunnerOkWithPassedTests
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
PASS test ingress ../../test/data/v3/basic/tests/ingress_test.yaml
PASS test notes ../../test/data/v3/basic/tests/notes_test.yaml
PASS test override names and fullNames in Kubernetes resources ../../test/data/v3/basic/tests/namesOverride_test.yaml
PASS test pod disruption budget ../../test/data/v3/basic/tests/pdp_test.yaml
PASS test service ../../test/data/v3/basic/tests/service_test.yaml
PASS test service account ../../test/data/v3/basic/tests/serviceaccount_test.yaml


Charts: 1 passed, 1 total
Test Suites: 12 passed, 12 total
Tests: 34 passed, 34 total
Test Suites: 13 passed, 13 total
Tests: 40 passed, 40 total
Snapshot: 4 passed, 4 total
Time: XX.XXXms

Expand Down
28 changes: 10 additions & 18 deletions pkg/unittest/assertion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/helm-unittest/helm-unittest/pkg/unittest/results"
"github.com/helm-unittest/helm-unittest/pkg/unittest/snapshot"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)

func validateSucceededTestAssertions(
Expand All @@ -18,10 +17,9 @@ func validateSucceededTestAssertions(
renderedMap map[string][]common.K8sManifest) {

assertions := make([]Assertion, assertionCount)
err := yaml.Unmarshal([]byte(assertionsYAML), &assertions)
common.YmlUnmarshalTestHelper(assertionsYAML, &assertions, t)

a := assert.New(t)
a.Nil(err)

for idx, assertion := range assertions {
result := assertion.Assert(renderedMap, fakeSnapshotComparer(true), true, nil, &results.AssertionResult{Index: idx}, false)
Expand All @@ -36,7 +34,7 @@ func validateSucceededTestAssertions(
}
}

func TestAssertionUnmarshaledFromYAML(t *testing.T) {
func TestAssertionUnmarshalFromYAML(t *testing.T) {
assertionsYAML := `
- equal:
- notEqual:
Expand Down Expand Up @@ -75,11 +73,10 @@ func TestAssertionUnmarshaledFromYAML(t *testing.T) {

a := assert.New(t)
assertionsAsMap := make([]map[string]interface{}, 33)
mapErr := yaml.Unmarshal([]byte(assertionsYAML), &assertionsAsMap)
a.Nil(mapErr)
common.YmlUnmarshalTestHelper(assertionsYAML, &assertionsAsMap, t)

assertions := make([]Assertion, 33)
assErr := yaml.Unmarshal([]byte(assertionsYAML), &assertions)
a.Nil(assErr)
common.YmlUnmarshalTestHelper(assertionsYAML, &assertions, t)

for idx, assertion := range assertions {
_, ok := assertionsAsMap[idx][assertion.AssertType]
Expand Down Expand Up @@ -152,8 +149,7 @@ func TestAssertionUnmarshaledFromYAMLWithNotTrue(t *testing.T) {
a := assert.New(t)

assertions := make([]Assertion, 29)
err := yaml.Unmarshal([]byte(assertionsYAML), &assertions)
a.Nil(err)
common.YmlUnmarshalTestHelper(assertionsYAML, &assertions, t)

for _, assertion := range assertions {
a.True(assertion.Not)
Expand Down Expand Up @@ -208,8 +204,7 @@ func TestReverseAssertionTheSameAsOriginalOneWithNotTrue(t *testing.T) {
a := assert.New(t)

assertions := make([]Assertion, 28)
err := yaml.Unmarshal([]byte(assertionsYAML), &assertions)
a.Nil(err)
common.YmlUnmarshalTestHelper(assertionsYAML, &assertions, t)

for idx := 0; idx < len(assertions); idx += 2 {
a.Equal(assertions[idx].Not, !assertions[idx+1].Not)
Expand Down Expand Up @@ -335,10 +330,9 @@ template: not-existed.yaml
equal:
`
assertion := new(Assertion)
err := yaml.Unmarshal([]byte(assertionYAML), &assertion)
common.YmlUnmarshalTestHelper(assertionYAML, &assertion, t)

a := assert.New(t)
a.Nil(err)

result := assertion.Assert(renderedMap, fakeSnapshotComparer(true), true, nil, &results.AssertionResult{Index: 0}, false)
a.Equal(&results.AssertionResult{
Expand All @@ -358,10 +352,9 @@ func TestAssertionAssertWhenTemplateNotSpecifiedAndNoDefault(t *testing.T) {
}
assertionYAML := "equal:"
assertion := new(Assertion)
err := yaml.Unmarshal([]byte(assertionYAML), &assertion)
common.YmlUnmarshalTestHelper(assertionYAML, &assertion, t)

a := assert.New(t)
a.Nil(err)
result := assertion.Assert(renderedMap, fakeSnapshotComparer(true), true, nil, &results.AssertionResult{Index: 0}, false)
a.Equal(&results.AssertionResult{
Index: 0,
Expand All @@ -384,10 +377,9 @@ documentIndex: 1
equal:
`
assertion := new(Assertion)
err := yaml.Unmarshal([]byte(assertionYAML), &assertion)
common.YmlUnmarshalTestHelper(assertionYAML, &assertion, t)

a := assert.New(t)
a.Nil(err)

result := assertion.Assert(renderedMap, fakeSnapshotComparer(true), true, nil, &results.AssertionResult{Index: 0}, false)
a.Equal(&results.AssertionResult{
Expand Down
Loading

0 comments on commit 4f9ae32

Please sign in to comment.