1
0
Fork 0
mirror of https://github.com/dragonflydb/dragonfly.git synced 2024-12-14 11:58:02 +00:00
dragonflydb-dragonfly/contrib/charts/dragonfly/golden_test.go
Tarun Pothulapati 2d3496dd40
test(helm): Add command to update golden files (#890)
While we have docs in `Contributing.md`, It seems better to
have that command emitted in the error message. It is much
more straightforward this way.
2023-03-01 12:19:32 +05:30

60 lines
1.3 KiB
Go

package golden
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/gruntwork-io/terratest/modules/helm"
)
var update = flag.Bool("update", false, "update golden test output files")
func TestHelmRender(t *testing.T) {
files, err := os.ReadDir("./ci")
if err != nil {
t.Fatal(err)
}
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".yaml") && !strings.HasSuffix(f.Name(), ".golden.yaml") {
// Render this values.yaml file
output := helm.RenderTemplate(t,
&helm.Options{
ValuesFiles: []string{"ci/" + f.Name()},
},
"../dragonfly",
"test",
nil,
)
goldenFile := "ci/" + strings.TrimSuffix(f.Name(), filepath.Ext(".yaml")) + ".golden.yaml"
regex := regexp.MustCompile(`\s+helm.sh/chart:\s+.*`)
bytes := regex.ReplaceAll([]byte(output), []byte(""))
output = fmt.Sprintf("%s\n", string(bytes))
if *update {
err := ioutil.WriteFile(goldenFile, []byte(output), 0644)
if err != nil {
t.Fatal(err)
}
}
expected, err := ioutil.ReadFile(goldenFile)
if err != nil {
t.Fatal(err)
}
if string(expected) != output {
t.Fatalf("Expected %s, but got %s\n. Update golden files by running `go test -v ./... -update`", string(expected), output)
}
}
}
}