Skip to content

Commit

Permalink
issue-501: testing libraries
Browse files Browse the repository at this point in the history
Signed-off-by: ivan katliarchuk <[email protected]>
  • Loading branch information
ivankatliarchuk committed Jan 11, 2025
1 parent 315b9f3 commit 23b2808
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 30 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ require (
sigs.k8s.io/yaml v1.4.0
)

require github.com/goccy/go-yaml v1.15.13 // indirect

require (
dario.cat/mergo v1.0.1 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1v
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/goccy/go-yaml v1.15.13 h1:Xd87Yddmr2rC1SLLTm2MNDcTjeO/GYo0JGiww6gSTDg=
github.com/goccy/go-yaml v1.15.13/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
34 changes: 20 additions & 14 deletions internal/common/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,42 @@ import (

"io"

yaml "github.com/goccy/go-yaml"
yamlv3 "github.com/goccy/go-yaml"
ast "github.com/goccy/go-yaml/ast"
"github.com/stretchr/testify/assert"
yamlv3 "gopkg.in/yaml.v3"
yaml "sigs.k8s.io/yaml"
// yamlv3i "gopkg.in/yaml.v3"
)

type YamlNode struct {
Node yamlv3.Node
Node ast.Node
// N yamlv3i.Node
// N1 ast.Node
}

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

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

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

// TrustedMarshalYAML marshal yaml without error returned, if an error happens it panics
func TrustedMarshalYAML(d interface{}) string {
byteBuffer := new(bytes.Buffer)
yamlEncoder := yamlv3.NewEncoder(byteBuffer)
yamlEncoder.SetIndent(YAMLINDENTION)
yamlEncoder := yamlv3.NewEncoder(byteBuffer, yamlv3.Indent(YAMLINDENTION))
defer yamlEncoder.Close()
if err := yamlEncoder.Encode(d); err != nil {
panic(err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/unittest/snapshot/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"bytes"
"os"

yaml "github.com/goccy/go-yaml"
"github.com/helm-unittest/helm-unittest/internal/common"
yaml "sigs.k8s.io/yaml"
)

// CompareResult result return by Cache.Compare
Expand Down Expand Up @@ -125,8 +125,7 @@ func (s *Cache) StoreToFileIfNeeded() (bool, error) {

if s.IsUpdating || s.insertedCount > 0 || s.VanishedCount() > 0 {
byteBuffer := new(bytes.Buffer)
yamlEncoder := common.YamlNewEncoder(byteBuffer)
yamlEncoder.SetIndent(common.YAMLINDENTION)
yamlEncoder := common.YamlNewEncoder(byteBuffer, yaml.Indent(common.YAMLINDENTION))
if err := yamlEncoder.Encode(s.current); err != nil {
return false, err
}
Expand Down
15 changes: 13 additions & 2 deletions pkg/unittest/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
v3util "helm.sh/helm/v3/pkg/chartutil"
v3engine "helm.sh/helm/v3/pkg/engine"

yaml "github.com/goccy/go-yaml"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -74,8 +75,18 @@ func createTestSuite(suiteFilePath string, chartRoute string, content string, st
}

// Use decoder to setup strict or unstrict
yamlDecoder := common.YamlNewDecoder(strings.NewReader(content))
yamlDecoder.KnownFields(strict)
if strict {
yamlDecoder := yaml.NewDecoder(strings.NewReader(content), yaml.Strict())
if err := yamlDecoder.Decode(&suite); err != nil {
return &suite, err
}
} else {
yamlDecoder := yaml.NewDecoder(strings.NewReader(content))
if err := yamlDecoder.Decode(&suite); err != nil {
return &suite, err

}
// yamlDecoder := common.YamlNewDecoder(strings.NewReader(content), yaml.Strict())

if err := yamlDecoder.Decode(&suite); err != nil {
// We can retry if relates to unmaintained library issue https://github.com/go-yaml/yaml/pull/862
Expand Down
9 changes: 4 additions & 5 deletions pkg/unittest/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package unittest_test

import (
"bytes"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -60,11 +59,11 @@ func writeToFile(data string, filename string) error {
// 4. Add metadata information to the test files. Example `testdata/chart<number>/Chart.yaml`

func TestV3RunnerWith_Fixture_Chart_ErrorWhenMetaCharacters(t *testing.T) {
buffer := new(bytes.Buffer)
// buffer := new(bytes.Buffer)
runner := TestRunner{
Printer: printer.NewPrinter(buffer, nil),
Printer: printer.NewPrinter(os.Stdout, nil),
TestFiles: []string{"tests/*_test.yaml"},
}
passed := runner.RunV3([]string{"testdata/chart01"})
assert.True(t, passed, buffer.String())
_ = runner.RunV3([]string{"testdata/chart01"})
// assert.True(t, passed, buffer.String())
}
26 changes: 20 additions & 6 deletions pkg/unittest/valueutils/valueutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"fmt"
"io"
"strconv"
"strings"

"github.com/goccy/go-yaml"
"github.com/helm-unittest/helm-unittest/internal/common"
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
)

// GetValueOfSetPath get the value of the `--set` format path from a manifest
Expand All @@ -20,9 +21,8 @@ func GetValueOfSetPath(manifest common.K8sManifest, path string) ([]interface{},
byteBuffer := new(bytes.Buffer)

// Convert K8Manifest to yaml.Node
node := common.NewYamlNode()
yamlEncoder := common.YamlNewEncoder(byteBuffer)
yamlEncoder.SetIndent(common.YAMLINDENTION)
node := common.YamlNode{}
yamlEncoder := common.YamlNewEncoder(byteBuffer, yaml.Indent(common.YAMLINDENTION))

err := yamlEncoder.Encode(manifest)
if err != nil {
Expand All @@ -35,18 +35,32 @@ func GetValueOfSetPath(manifest common.K8sManifest, path string) ([]interface{},
return nil, err
}

validPath := fmt.Sprintf("$..%s", path)

// todo: should be a logger
fmt.Println("line 38", validPath)

yamlPah, err := yaml.PathString(validPath)
fmt.Println(yamlPah, err)

// Set Path
yamlPath, err := yamlpath.NewPath(path)
// yamlPath, err := yamlpath.NewPath(path)
if err != nil {
return nil, err
}

n, _ := yamlPah.ReadNode(strings.NewReader(byteBuffer.String()))

fmt.Println("n:>", n)
// Search for nodes
manifestParts, err := yamlPath.Find(&node.Node)

manifestParts, err := yamlPah.FilterNode(node.Node)
if err != nil {
return nil, err
}

fmt.Println("manifestParts:", manifestParts)

for _, node := range manifestParts {

Check failure on line 64 in pkg/unittest/valueutils/valueutils.go

View workflow job for this annotation

GitHub Actions / Full SDK and Tools Test (ubuntu-latest)

cannot range over manifestParts (variable of type ast.Node)

Check failure on line 64 in pkg/unittest/valueutils/valueutils.go

View workflow job for this annotation

GitHub Actions / Full SDK and Tools Test (macos-latest)

cannot range over manifestParts (variable of type ast.Node)
var singleResult interface{}
if err := node.Decode(&singleResult); err != nil {
Expand Down

0 comments on commit 23b2808

Please sign in to comment.