Skip to content

Commit 5536168

Browse files
Added tests for init.go
1 parent 88afbd6 commit 5536168

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

pkg/quarkus/v1alpha/init_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2021 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package v1
16+
17+
import (
18+
"os"
19+
"path/filepath"
20+
"strings"
21+
22+
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/gomega"
24+
"github.com/spf13/pflag"
25+
26+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
27+
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
28+
)
29+
30+
var _ = Describe("v1", func() {
31+
var (
32+
successInitSubcommand initSubcommand
33+
failureInitSubcommand initSubcommand
34+
)
35+
36+
BeforeEach(func() {
37+
successInitSubcommand = initSubcommand{
38+
domain: "testDomain",
39+
}
40+
41+
failureInitSubcommand = initSubcommand{
42+
domain: "testDomain",
43+
projectName: "?&fail&?",
44+
commandName: "failureTest",
45+
}
46+
})
47+
48+
Describe("UpdateMetadata", func() {
49+
It("Check that function call sets data correctly", func() {
50+
testCliMetadata := plugin.CLIMetadata{CommandName: "TestCommand"}
51+
testSubcommandMetadata := plugin.SubcommandMetadata{}
52+
Expect(failureInitSubcommand.commandName).NotTo(Equal(testCliMetadata.CommandName))
53+
54+
successInitSubcommand.UpdateMetadata(testCliMetadata, &testSubcommandMetadata)
55+
Expect(successInitSubcommand.commandName).To(Equal(testCliMetadata.CommandName))
56+
})
57+
})
58+
59+
Describe("BindFlags", func() {
60+
It("verify all fields were set correctly", func() {
61+
flagTest := pflag.NewFlagSet("testFlag", -1)
62+
successInitSubcommand.BindFlags(flagTest)
63+
Expect(flagTest.SortFlags).To(BeFalse())
64+
Expect(successInitSubcommand.domain).To(Equal("my.domain"))
65+
Expect(successInitSubcommand.projectName).To(Equal(""))
66+
Expect(successInitSubcommand.group).To(Equal(""))
67+
Expect(successInitSubcommand.version).To(Equal(""))
68+
Expect(successInitSubcommand.kind).To(Equal(""))
69+
})
70+
})
71+
72+
Describe("InjectConfig", func() {
73+
It("verify all fields were set correctly", func() {
74+
testConfig, _ := config.New(config.Version{Number: 3})
75+
dir, _ := os.Getwd()
76+
Expect(failureInitSubcommand.InjectConfig(testConfig)).To(HaveOccurred())
77+
78+
successInitSubcommand.InjectConfig(testConfig)
79+
Expect(successInitSubcommand.config, testConfig)
80+
Expect(successInitSubcommand.domain, testConfig.GetDomain())
81+
Expect(successInitSubcommand.projectName, strings.ToLower(filepath.Base(dir)))
82+
Expect(successInitSubcommand.projectName, testConfig.GetProjectName())
83+
Expect(successInitSubcommand.InjectConfig(testConfig)).To(BeNil())
84+
})
85+
})
86+
87+
Describe("Validate", func() {
88+
It("should return nil", func() {
89+
Expect(successInitSubcommand.Validate()).To(BeNil())
90+
})
91+
})
92+
93+
Describe("PostScaffold", func() {
94+
It("should return nil", func() {
95+
Expect(successInitSubcommand.PostScaffold()).To(BeNil())
96+
})
97+
})
98+
})

pkg/quarkus/v1alpha/v1_suite_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2021 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package v1
16+
17+
import (
18+
"testing"
19+
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
func TestUtil(t *testing.T) {
25+
RegisterFailHandler(Fail)
26+
RunSpecs(t, "v1")
27+
}

0 commit comments

Comments
 (0)