Skip to content

Commit

Permalink
Refactoring for test
Browse files Browse the repository at this point in the history
  • Loading branch information
jlandowner committed Apr 29, 2024
1 parent b9d8314 commit ee81b84
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 17 deletions.
18 changes: 18 additions & 0 deletions __snapshots__/main_test.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
['rootCmd fail including dynamic outputs should fail 1']
SnapShot = 'snapshot does not match chart=example/app1 values='

['rootCmd fail invalid flag should fail 1']
SnapShot = 'unknown flag: --invalid'

['rootCmd fail required flag is not set should fail 1']
SnapShot = 'required flag(s) "chart" not set'

['rootCmd fail snapshot is different should fail 1']
SnapShot = 'snapshot does not match chart=example/app1 values=example/app1/testfail/test_ingress_enabled.yaml'

['rootCmd fail values directory contains not matched snapshots should fail 1']
SnapShot = 'snapshot does not match chart=example/app1 values=example/app1/testfail/test_ingress_enabled.yaml'

['rootCmd fail values file not found should fail 1']
SnapShot = """
values file 'example/app1/test/notfound.yaml' not found"""
31 changes: 14 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ var (
version = "snapshot"
commit = "snapshot"
date = "snapshot"
o = &option{}
log *slog.Logger
values []string
mutex = &sync.Mutex{}
o *option
rootCmd *cobra.Command
log *slog.Logger
)

type option struct {
Expand Down Expand Up @@ -89,8 +89,13 @@ func (o *option) snapshotVersion() string {
}
}

func main() {
rootCmd := &cobra.Command{
func init() {
initRootCmd()
}

func initRootCmd() {
o = &option{}
rootCmd = &cobra.Command{
Use: "chartsnap -c CHART",
Short: "Snapshot testing tool for Helm charts",
Long: `
Expand Down Expand Up @@ -156,7 +161,6 @@ MIT 2023 jlandowner/helm-chartsnap
NO_COLOR=1 chartsnap -c YOUR_CHART`,
Version: fmt.Sprintf("version=%s commit=%s date=%s", version, commit, date),
RunE: run,
PreRunE: prerun,
}
rootCmd.SilenceUsage = true
rootCmd.SilenceErrors = true
Expand Down Expand Up @@ -185,7 +189,9 @@ MIT 2023 jlandowner/helm-chartsnap
}
rootCmd.PersistentFlags().BoolVar(&o.LegacySnapshot, "legacy-snapshot", false, "use toml-based legacy snapshot format")
rootCmd.PersistentFlags().StringVar(&o.SnapshotVersion, "snapshot-version", "", "use a specific snapshot format version. v1, v2, v3 are supported. (default: latest)")
}

func main() {
if err := rootCmd.Execute(); err != nil {
slog.New(slogHandler()).Error(err.Error())
os.Exit(1)
Expand All @@ -203,14 +209,6 @@ func slogHandler() slog.Handler {
})
}

func prerun(cmd *cobra.Command, args []string) error {
if o.Chart == "" {
// show help message when executed without any args (meaning required --chart flag is not set)
return cmd.Help()
}
return nil
}

func loadSnapshotConfig(file string, cfg *v1alpha1.SnapshotConfig) error {
err := v1alpha1.FromFile(file, cfg)
if err != nil && !os.IsNotExist(err) {
Expand Down Expand Up @@ -244,9 +242,8 @@ func run(cmd *cobra.Command, args []string) error {
}
}

if o.ValuesFile == "" {
values = []string{""}
} else {
values := []string{""}
if o.ValuesFile != "" {
stat, err := os.Stat(o.ValuesFile)
if err != nil {
if os.IsNotExist(err) {
Expand Down
144 changes: 144 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package main

import (
"os"
"testing"

. "github.com/jlandowner/helm-chartsnap/pkg/snap/gomega"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestMain(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Main Suite")
}

var _ = Describe("rootCmd", func() {
BeforeEach(func() {
initRootCmd()
})
Context("success", func() {
Context("snapshot local chart with single values file", func() {
It("should pass", func() {
rootCmd.SetArgs([]string{"--chart", "example/app1", "-f", "example/app1/test/test_ingress_enabled.yaml", "--namespace", "default"})
err := rootCmd.Execute()
Expect(err).ShouldNot(HaveOccurred())
})
})

Context("snapshot local chart with values directory", func() {
It("should pass", func() {
rootCmd.SetArgs([]string{"--chart", "example/app1", "-f", "example/app1/test/", "--namespace", "default"})
err := rootCmd.Execute()
Expect(err).ShouldNot(HaveOccurred())
})
})

Context("snapshot OCI chart", func() {
It("should pass", func() {
rootCmd.SetArgs([]string{"--chart", "oci://ghcr.io/nginxinc/charts/nginx-gateway-fabric", "-f", "example/remote/nginx-gateway-fabric.values.yaml", "--", "--namespace", "nginx-gateway"})
err := rootCmd.Execute()
Expect(err).ShouldNot(HaveOccurred())
})
})

Context("snapshot remote chart 1", func() {
It("should pass", func() {
rootCmd.SetArgs([]string{"--chart", "cilium", "-f", "example/remote/cilium.values.yaml", "--", "--namespace", "kube-system", "--repo", "https://helm.cilium.io"})
err := rootCmd.Execute()
Expect(err).ShouldNot(HaveOccurred())
})
})

Context("snapshot remote chart 2", func() {
It("should pass", func() {
rootCmd.SetArgs([]string{"--chart", "ingress-nginx", "-f", "example/remote/ingress-nginx.values.yaml", "--", "--namespace", "ingress-nginx", "--repo", "https://kubernetes.github.io/ingress-nginx", "--skip-tests"})
err := rootCmd.Execute()
Expect(err).ShouldNot(HaveOccurred())
})
})

Context("snapshot empty chart", func() {
It("should pass", func() {
rootCmd.SetArgs([]string{"--chart", "example/app2", "--namespace", "default"})
err := rootCmd.Execute()
Expect(err).ShouldNot(HaveOccurred())
})
})

Context("snapshot empty chart with no config file", func() {
It("should pass", func() {
rootCmd.SetArgs([]string{"--chart", "example/app2", "--namespace", "default", "--config-file", "notfound"})
err := rootCmd.Execute()
Expect(err).ShouldNot(HaveOccurred())
})
})

Context("snapshot empty chart with debug mode", func() {
It("should pass", func() {
os.Setenv("HELM_DEBUG", "true")
defer os.Unsetenv("HELM_DEBUG")
rootCmd.SetArgs([]string{"--chart", "example/app2", "--namespace", "default"})
err := rootCmd.Execute()
Expect(err).ShouldNot(HaveOccurred())
})
})
})

Context("fail", func() {
Context("including dynamic outputs", func() {
It("should fail", func() {
rootCmd.SetArgs([]string{"--chart", "example/app1", "--namespace", "default"})
err := rootCmd.Execute()
Expect(err).To(HaveOccurred())
Ω(err.Error()).To(MatchSnapShot())
})
})

Context("snapshot is different", func() {
It("should fail", func() {
rootCmd.SetArgs([]string{"--chart", "example/app1", "--namespace", "default", "-f", "example/app1/testfail/test_ingress_enabled.yaml"})
err := rootCmd.Execute()
Expect(err).To(HaveOccurred())
Ω(err.Error()).To(MatchSnapShot())
})
})

Context("values directory contains not matched snapshots", func() {
It("should fail", func() {
rootCmd.SetArgs([]string{"--chart", "example/app1", "--namespace", "default", "-f", "example/app1/testfail"})
err := rootCmd.Execute()
Expect(err).To(HaveOccurred())
Ω(err.Error()).To(MatchSnapShot())
})
})

Context("values file not found", func() {
It("should fail", func() {
rootCmd.SetArgs([]string{"--chart", "example/app1", "-f", "example/app1/test/notfound.yaml", "--namespace", "default"})
err := rootCmd.Execute()
Expect(err).To(HaveOccurred())
Ω(err.Error()).To(MatchSnapShot())
})
})

Context("required flag is not set", func() {
It("should fail", func() {
rootCmd.SetArgs([]string{})
err := rootCmd.Execute()
Expect(err).To(HaveOccurred())
Ω(err.Error()).To(MatchSnapShot())
})
})

Context("invalid flag", func() {
It("should fail", func() {
rootCmd.SetArgs([]string{"--chart", "example/app1", "-f", "example/app1/test/test_ingress_enabled.yaml", "--namespace", "default", "--invalid"})
err := rootCmd.Execute()
Expect(err).To(HaveOccurred())
Ω(err.Error()).To(MatchSnapShot())
})
})
})
})
4 changes: 4 additions & 0 deletions pkg/api/v1alpha1/__snapshots__/testspec_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ SnapShot = """
SnapShot = """
failed to decode file 'testdata/testspec_values_invalid.yaml': yaml: line 10: could not find expected ':'"""

['TestSpec FromFile when loading not found should not load config 1']
SnapShot = """
failed to open file 'testdata/notfound.yaml': open testdata/notfound.yaml: no such file or directory"""

['TestSpec FromFile when values.yaml has testSpec should load config 1']
SnapShot = """
{
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/v1alpha1/testspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ var _ = Describe("TestSpec", func() {
Expect(err.Error()).To(MatchSnapShot())
})
})

Context("when loading not found", func() {
It("should not load config", func() {
var v SnapshotValues
err := FromFile("testdata/notfound.yaml", &v)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(MatchSnapShot())
})
})
})

var _ = Describe("Merge", func() {
Expand Down

0 comments on commit ee81b84

Please sign in to comment.