forked from rancher/helm-unittest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (95 loc) · 2.82 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"github.com/bdun1013/helm-snapshot/pkg/printer"
"github.com/bdun1013/helm-snapshot/pkg/runner"
"github.com/spf13/cobra"
)
var version string
func main() {
Execute(version)
}
var testConfig = runner.TestConfig{}
var cmd = &cobra.Command{
Use: "snapshot [flags] CHART [...]",
Short: "snapshot for helm charts",
Long: `Running chart snapshot written in YAML.
This renders your charts locally (without tiller) and
validates the rendered output with the tests defined in
test suite files. Simplest test suite file looks like
below:
---
# CHART_PATH/tests/deployment_test.yaml
suite: test my deployment
templates:
- deployment.yaml
tests:
- it: should be a Deployment
asserts:
- isKind:
of: Deployment
---
Put the test files in "tests" directory under your chart
with suffix "_test.yaml", and run:
$ helm snapshot my-chart
Or specify the suite files glob path pattern:
$ helm snapshot -f 'my-tests/*.yaml' my-chart
Check https://github.com/bdun1013/helm-snapshot for more
details about how to write tests.
`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, chartPaths []string) {
var colored *bool
if cmd.PersistentFlags().Changed("color") {
colored = &testConfig.Colored
}
printer := printer.NewPrinter(os.Stdout, colored)
runner := runner.TestRunner{Printer: printer, Config: testConfig}
passed := runner.Run(chartPaths)
if !passed {
os.Exit(1)
}
},
}
// Execute execute snapshot command
func Execute(version string) {
cmd.AddCommand(newVersionCommand(os.Stdout, version))
if err := cmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func newVersionCommand(out io.Writer, version string) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Prints the version number of helm snapshot",
Long: "Prints the version number of helm snapshot",
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintln(out, "helm snapshot plugin version", version, "built with go version", runtime.Version())
},
}
return cmd
}
func init() {
cmd.PersistentFlags().BoolVar(
&testConfig.Colored, "color", false,
"enforce printing colored output even stdout is not a tty. Set to false to disable color",
)
defaultFilePattern := filepath.Join("templates/tests/snapshot", "*_test.yaml")
cmd.PersistentFlags().StringArrayVarP(
&testConfig.TestFiles, "file", "f", []string{defaultFilePattern},
"glob paths of test files location, default to "+defaultFilePattern,
)
cmd.PersistentFlags().BoolVarP(
&testConfig.UpdateSnapshot, "update-snapshot", "u", false,
"update the snapshot cached if needed, make sure you review the change before update",
)
cmd.PersistentFlags().BoolVarP(
&testConfig.WithSubChart, "with-subchart", "s", true,
"include tests of the subcharts within `charts` folder",
)
}