Skip to content

Commit 17dfc0c

Browse files
committed
Ginkgo testing for cmd package
Ginkgo testing for cmd package, validation package, render package, templating package, resourcecache package Ginkgo testing for cmd package, validation package, render package, templating package, resourcecache package
1 parent 9a73873 commit 17dfc0c

File tree

381 files changed

+194641
-105884
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

381 files changed

+194641
-105884
lines changed

cmd/helm2go-operator-sdk/new/chart.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,6 @@ func (hc *HelmChartClient) DoHelmGoConversion() (*resourcecache.ResourceCache, e
141141
func scaffoldOverwrite(outputDir, kind, apiVersion string, rcache *resourcecache.ResourceCache) error {
142142

143143
ok := templating.OverwriteController(outputDir, kind, apiVersion, rcache)
144-
if !ok {
145-
fmt.Println(ok)
146-
}
147144
// create templates for writing to file
148145
templates := templating.CacheTemplating(rcache, outputDir, kind, apiVersion)
149146
// templates to files; outputDir is the parent directory where the operator scaffolding lives

cmd/helm2go-operator-sdk/new/cmd_test.go

Lines changed: 138 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -2,122 +2,153 @@ package new
22

33
import (
44
"fmt"
5-
"io/ioutil"
6-
"log"
75
"os"
86
"path/filepath"
7+
98
"testing"
109

10+
. "github.com/onsi/ginkgo"
11+
. "github.com/onsi/gomega"
12+
1113
"github.com/redhat-nfvpe/helm2go-operator-sdk/internal/pathconfig"
1214
"github.com/spf13/cobra"
1315
)
1416

1517
var cwd, _ = os.Getwd()
1618
var parent = filepath.Dir(filepath.Dir(filepath.Dir(cwd)))
1719

18-
func silencelog() {
19-
log.SetOutput(ioutil.Discard)
20-
}
21-
func resetlog() {
22-
log.SetOutput(os.Stdout)
20+
func TestCommand(t *testing.T) {
21+
RegisterFailHandler(Fail)
22+
RunSpecs(t, "Command")
2323
}
2424

25-
func TestLoadChartGetsLocalChart(t *testing.T) {
26-
//g := gomega.NewGomegaWithT(t)
27-
silencelog()
28-
29-
var local = "/test/bitcoind"
30-
var testLocal = parent + local
31-
32-
// load the chart
33-
chartClient := NewChartClient()
34-
chartClient.HelmChartRef = testLocal
35-
err := chartClient.LoadChart()
36-
if err != nil {
37-
t.Fatal(err)
38-
}
39-
// verify that the chart loads the right thing
40-
if chartClient.Chart.GetMetadata().Name != "bitcoind" {
41-
resetlog()
42-
t.Fatalf("Unexpected Chart Name!")
43-
}
44-
}
25+
var _ = Describe("Load Chart", func() {
26+
It("Gets Local Chart", func() {
27+
var local = "/test/bitcoind"
28+
var testLocal = parent + local
29+
chartClient := NewChartClient()
30+
chartClient.HelmChartRef = testLocal
31+
err := chartClient.LoadChart()
32+
Expect(err).ToNot(HaveOccurred())
33+
Expect(chartClient.Chart.GetMetadata().Name).To(Equal("bitcoind"))
34+
})
35+
It("Gets External Charts From Valid Repos", func() {
36+
var client HelmChartClient
4537

46-
func TestCommandFlagValidation(t *testing.T) {
47-
// initiate flags for testing
48-
var err error
49-
helmChartRef = ""
50-
silencelog()
51-
if err = verifyFlags(); err == nil {
52-
resetlog()
53-
t.Logf("Error: %v", err)
54-
t.Fatal("Expected Flag Validation Error: --helm-chart-ref")
55-
silencelog()
56-
}
57-
helmChartRef = "./test/tomcat"
58-
kind = "Tomcat"
59-
apiVersion = ""
60-
if err = verifyFlags(); err == nil {
61-
resetlog()
62-
t.Logf("Error: %v", err)
63-
t.Fatal("Expected Flag Validation Error: --api-version")
64-
silencelog()
65-
}
66-
apiVersion = "app.example/v1alpha1"
67-
if err = verifyFlags(); err == nil {
68-
resetlog()
69-
t.Logf("Error: %v", err)
70-
t.Fatal("expected flag validation error; api version does not match naming convention")
71-
silencelog()
72-
}
73-
apiVersion = "app.example.com/v1alpha1"
74-
if err = verifyFlags(); err != nil {
75-
resetlog()
76-
t.Logf("Error: %v", err)
77-
t.Fatal("Unexpected Flag Validation Error")
78-
silencelog()
79-
}
80-
}
38+
gopathDir := os.Getenv("GOPATH")
39+
pathConfig := pathconfig.NewConfig(filepath.Join(gopathDir, "src/github.com/redhat-nfvpe/helm2go-operator-sdk"))
40+
client.PathConfig = pathConfig
8141

82-
func TestKindTypeValidation(t *testing.T) {
83-
var err error
84-
apiVersion = "app.example.com/v1alpha1"
85-
helmChartRef = "./test/tomcat"
86-
kind = ""
87-
silencelog()
88-
if err = verifyFlags(); err == nil {
89-
resetlog()
90-
t.Fatal("Expected Flag Validation Error: kind")
91-
silencelog()
92-
}
93-
kind = "tensorflow-notebook"
94-
if err = verifyFlags(); err == nil {
95-
resetlog()
96-
t.Fatalf("Expected Lowercase Flag Validation Error: --kind %v", err)
97-
silencelog()
98-
}
99-
kind = "tensorflowNotebook"
100-
if err = verifyFlags(); err == nil {
101-
resetlog()
102-
t.Fatal("Expected Lowercase Flag Validation Error: --kind")
103-
t.Fatal(err)
104-
silencelog()
105-
}
106-
kind = "Tensorflow-Notebook"
107-
if err = verifyFlags(); err == nil {
108-
resetlog()
109-
t.Fatal("Expected Hyphen Flag Validation Error: --kind")
110-
t.Fatal(err)
111-
silencelog()
112-
}
113-
kind = "TensorflowNotebook"
114-
if err = verifyFlags(); err != nil {
115-
resetlog()
116-
t.Fatal("Unexpected Flag Validation Error")
117-
t.Fatal(err)
118-
silencelog()
119-
}
120-
}
42+
client.HelmChartRef = "auto-deploy-app"
43+
client.HelmChartRepo = "https://charts.gitlab.io/"
44+
err := client.LoadChart()
45+
Expect(err).ToNot(HaveOccurred())
46+
47+
// cleanup auto-deploy-app
48+
49+
client.HelmChartRef = "not-a-chart"
50+
client.HelmChartRepo = ""
51+
err = client.LoadChart()
52+
Expect(err).To(HaveOccurred())
53+
54+
client.HelmChartRepo = "invalidrepo.io"
55+
err = client.LoadChart()
56+
Expect(err).To(HaveOccurred())
57+
})
58+
})
59+
60+
var _ = Describe("Flag Validation", func() {
61+
It("Verifies Command Flags", func() {
62+
var err error
63+
helmChartRef = ""
64+
err = verifyFlags()
65+
Expect(err).To(HaveOccurred())
66+
67+
helmChartRef = "./test/tomcat"
68+
kind = "Tomcat"
69+
apiVersion = ""
70+
err = verifyFlags()
71+
Expect(err).To(HaveOccurred())
72+
73+
apiVersion = "app.example/v1alpha1"
74+
err = verifyFlags()
75+
Expect(err).To(HaveOccurred())
76+
apiVersion = "app.example.com/v1alpha1"
77+
err = verifyFlags()
78+
Expect(err).ToNot(HaveOccurred())
79+
})
80+
It("Verifies Kind Flag", func() {
81+
var err error
82+
apiVersion = "app.example.com/v1alpha1"
83+
helmChartRef = "./test/tomcat"
84+
85+
kind = ""
86+
err = verifyFlags()
87+
Expect(err).To(HaveOccurred())
88+
89+
kind = "tensorflow-notebook"
90+
err = verifyFlags()
91+
Expect(err).To(HaveOccurred())
92+
93+
kind = "tensorflowNotebook"
94+
err = verifyFlags()
95+
Expect(err).To(HaveOccurred())
96+
97+
kind = "Tensorflow-Notebook"
98+
err = verifyFlags()
99+
Expect(err).To(HaveOccurred())
100+
101+
kind = "TensorflowNotebook"
102+
err = verifyFlags()
103+
Expect(err).ToNot(HaveOccurred())
104+
})
105+
It("Verifies API Version", func() {
106+
var err error
107+
helmChartRef = "./test/tomcat"
108+
kind = "TensorflowNotebook"
109+
110+
apiVersion = "app.example.com/v1alpha1"
111+
err = verifyFlags()
112+
Expect(err).ToNot(HaveOccurred())
113+
114+
apiVersion = "web.k8s.io/v1"
115+
err = verifyFlags()
116+
Expect(err).ToNot(HaveOccurred())
117+
118+
apiVersion = "k8s.io"
119+
err = verifyFlags()
120+
Expect(err).To(HaveOccurred())
121+
122+
apiVersion = "app,s.k8s.io/v1beta1"
123+
err = verifyFlags()
124+
Expect(err).To(HaveOccurred())
125+
126+
apiVersion = "apps.example/v1beta1"
127+
err = verifyFlags()
128+
Expect(err).To(HaveOccurred())
129+
130+
})
131+
})
132+
133+
var _ = Describe("Command Argument Validation", func() {
134+
It("Fails On Invalid Arguments", func() {
135+
var err error
136+
err = createInvalidCommand().Execute()
137+
Expect(err).To(HaveOccurred())
138+
139+
err = createInvalidOperatorName().Execute()
140+
Expect(err).To(HaveOccurred())
141+
142+
err = createInvalidKindName().Execute()
143+
Expect(err).To(HaveOccurred())
144+
})
145+
It("Passes On Valid Arguments", func() {
146+
var err error
147+
err = createValidCommand().Execute()
148+
Expect(err).ToNot(HaveOccurred())
149+
cleanupValidOperator()
150+
})
151+
})
121152

122153
//creates a command for use in the argument validation test
123154
func createValidCommand() *cobra.Command {
@@ -138,6 +169,12 @@ func createValidCommand() *cobra.Command {
138169
return cmd
139170
}
140171

172+
func cleanupValidOperator() {
173+
gopathDir := os.Getenv("GOPATH")
174+
operatorDir := filepath.Join(gopathDir, "src", "redhat-nfvpe", "helm2go-operator-sdk", "cmd", "helm2go-operator-sdk", "new", "test-operator")
175+
os.RemoveAll(operatorDir)
176+
}
177+
141178
// creates an invalid command for use in the argument validation test
142179
func createInvalidCommand() *cobra.Command {
143180
var cmd *cobra.Command
@@ -190,97 +227,3 @@ func createInvalidKindName() *cobra.Command {
190227
})
191228
return cmd
192229
}
193-
194-
func TestInvalidCommandArgumentValidation(t *testing.T) {
195-
var err error
196-
silencelog()
197-
if err = createInvalidCommand().Execute(); err == nil {
198-
resetlog()
199-
t.Logf("Error: %v", err)
200-
t.Fatal("Expected Error! Command Has No Operator Name Argument.")
201-
silencelog()
202-
}
203-
if err = createInvalidOperatorName().Execute(); err == nil {
204-
resetlog()
205-
t.Logf("Error: %v", err)
206-
t.Fatal("Expected Error! Command Has Invalid(empty) Operator Name Argument.")
207-
silencelog()
208-
}
209-
if err = createInvalidKindName().Execute(); err == nil {
210-
resetlog()
211-
t.Logf("Error: %v", err)
212-
t.Fatal("Expected Error! Command Has Invalid(Has Space) API Version Argument.")
213-
silencelog()
214-
}
215-
216-
}
217-
218-
func TestValidCommandArgument(t *testing.T) {
219-
defer os.RemoveAll("./test-operator")
220-
if err := createValidCommand().Execute(); err != nil {
221-
resetlog()
222-
t.Logf("Error: %v", err)
223-
os.RemoveAll("./test-operator")
224-
t.Fatal("Unexpected Error! Provided Correct Arguments and Flags.")
225-
silencelog()
226-
}
227-
228-
}
229-
230-
// clean up
231-
232-
func TestHelmChartDownload(t *testing.T) {
233-
var client HelmChartClient
234-
235-
gopathDir := os.Getenv("GOPATH")
236-
pathConfig := pathconfig.NewConfig(filepath.Join(gopathDir, "src/github.com/redhat-nfvpe/helm2go-operator-sdk"))
237-
client.PathConfig = pathConfig
238-
239-
// defer func() {
240-
// if r := recover(); r != nil {
241-
// fmt.Printf("Panic: %+v\n", r)
242-
// }
243-
// }()
244-
245-
client.HelmChartRef = "auto-deploy-app"
246-
client.HelmChartRepo = "https://charts.gitlab.io/"
247-
err := client.LoadChart()
248-
if err != nil {
249-
resetlog()
250-
t.Fatalf("Unexpected Error: %v\n", err)
251-
silencelog()
252-
}
253-
254-
client.HelmChartRef = "not-a-chart"
255-
client.HelmChartRepo = ""
256-
err = client.LoadChart()
257-
if err == nil {
258-
resetlog()
259-
t.Fatalf("Expected Error Chart Does Not Exist!")
260-
silencelog()
261-
}
262-
263-
client.HelmChartRepo = "invalidrepo.io"
264-
err = client.LoadChart()
265-
if err == nil {
266-
resetlog()
267-
t.Fatalf("Expected Error Invalid Repo!")
268-
silencelog()
269-
}
270-
}
271-
272-
func TestAPINamingConventionValidation(t *testing.T) {
273-
testCases := map[string]bool{
274-
"app.example.com/v1alpha1": true,
275-
"web.k8s.io/v1": true,
276-
"k8s.io": false,
277-
"app,s.k8s.io/v1beta1": false,
278-
"apps.example/v1beta1": false,
279-
}
280-
281-
for v, ok := range testCases {
282-
if m := apiVersionMatchesConvention(v); m != ok {
283-
t.Fatalf("error validating: %v; expected %v got %v", v, ok, m)
284-
}
285-
}
286-
}

cmd/helm2go-operator-sdk/new/cmd_util.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ func matchVersion(cmdOut *[]byte) error {
8484
pattern := regexp.MustCompile(`.*version\: +v(\d.\d.\d).*commit\: +(.*)`)
8585
matches := pattern.FindStringSubmatch(string(*cmdOut))
8686

87-
fmt.Println(matches)
88-
8987
if l := len(matches); l != 2+1 {
9088
return fmt.Errorf("expected three matches, received %d instead", l)
9189
}

0 commit comments

Comments
 (0)