Skip to content

Commit 464a909

Browse files
authored
Merge pull request #5169 from camilamacedo86/increase-coverage
🌱 Increase unit test coverage
2 parents 7abde80 + b3919c7 commit 464a909

File tree

3 files changed

+391
-0
lines changed

3 files changed

+391
-0
lines changed

pkg/cli/cmd_helpers_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cli
18+
19+
import (
20+
"errors"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
var _ = Describe("cmd_helpers", func() {
28+
Context("error types", func() {
29+
It("noResolvedPluginError should return correct message", func() {
30+
err := noResolvedPluginError{}
31+
Expect(err.Error()).To(ContainSubstring("no resolved plugin"))
32+
Expect(err.Error()).To(ContainSubstring("verify the project version and plugins"))
33+
})
34+
35+
It("noAvailablePluginError should return correct message with subcommand", func() {
36+
err := noAvailablePluginError{subcommand: "init"}
37+
Expect(err.Error()).To(ContainSubstring("init"))
38+
Expect(err.Error()).To(ContainSubstring("do not provide any"))
39+
})
40+
})
41+
42+
Context("cmdErr", func() {
43+
It("should update command with error information", func() {
44+
cmd := &cobra.Command{
45+
Long: "Original description",
46+
RunE: func(*cobra.Command, []string) error {
47+
return nil
48+
},
49+
}
50+
testError := errors.New("test error")
51+
52+
cmdErr(cmd, testError)
53+
54+
Expect(cmd.Long).To(ContainSubstring("Original description"))
55+
Expect(cmd.Long).To(ContainSubstring("test error"))
56+
Expect(cmd.RunE).NotTo(BeNil())
57+
58+
// Execute the modified RunE to verify it returns the error
59+
err := cmd.RunE(cmd, []string{})
60+
Expect(err).To(Equal(testError))
61+
})
62+
})
63+
64+
Context("errCmdFunc", func() {
65+
It("should return a function that returns the provided error", func() {
66+
testError := errors.New("test error")
67+
runE := errCmdFunc(testError)
68+
69+
err := runE(nil, nil)
70+
Expect(err).To(Equal(testError))
71+
})
72+
})
73+
})

pkg/machinery/mixins_delim_test.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package machinery
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
23+
"sigs.k8s.io/kubebuilder/v4/pkg/model/resource"
24+
)
25+
26+
var _ = Describe("TemplateMixin Delimiters", func() {
27+
var tmp TemplateMixin
28+
29+
BeforeEach(func() {
30+
tmp = TemplateMixin{}
31+
})
32+
33+
Context("SetDelim and GetDelim", func() {
34+
It("should set and get custom delimiters", func() {
35+
tmp.SetDelim("[[", "]]")
36+
left, right := tmp.GetDelim()
37+
Expect(left).To(Equal("[["))
38+
Expect(right).To(Equal("]]"))
39+
})
40+
41+
It("should return empty strings when delimiters are not set", func() {
42+
left, right := tmp.GetDelim()
43+
Expect(left).To(Equal(""))
44+
Expect(right).To(Equal(""))
45+
})
46+
47+
It("should allow setting delimiters multiple times", func() {
48+
tmp.SetDelim("[[", "]]")
49+
left, right := tmp.GetDelim()
50+
Expect(left).To(Equal("[["))
51+
Expect(right).To(Equal("]]"))
52+
53+
tmp.SetDelim("<%", "%>")
54+
left, right = tmp.GetDelim()
55+
Expect(left).To(Equal("<%"))
56+
Expect(right).To(Equal("%>"))
57+
})
58+
})
59+
})
60+
61+
var _ = Describe("Mixins injection behaviors", func() {
62+
Context("DomainMixin", func() {
63+
It("should not overwrite existing domain", func() {
64+
tmp := DomainMixin{Domain: "existing.domain"}
65+
tmp.InjectDomain("new.domain")
66+
Expect(tmp.Domain).To(Equal("existing.domain"))
67+
})
68+
69+
It("should inject domain when empty", func() {
70+
tmp := DomainMixin{}
71+
tmp.InjectDomain("new.domain")
72+
Expect(tmp.Domain).To(Equal("new.domain"))
73+
})
74+
})
75+
76+
Context("RepositoryMixin", func() {
77+
It("should not overwrite existing repository", func() {
78+
tmp := RepositoryMixin{Repo: "existing.repo"}
79+
tmp.InjectRepository("new.repo")
80+
Expect(tmp.Repo).To(Equal("existing.repo"))
81+
})
82+
83+
It("should inject repository when empty", func() {
84+
tmp := RepositoryMixin{}
85+
tmp.InjectRepository("new.repo")
86+
Expect(tmp.Repo).To(Equal("new.repo"))
87+
})
88+
})
89+
90+
Context("ProjectNameMixin", func() {
91+
It("should not overwrite existing project name", func() {
92+
tmp := ProjectNameMixin{ProjectName: "existing"}
93+
tmp.InjectProjectName("new")
94+
Expect(tmp.ProjectName).To(Equal("existing"))
95+
})
96+
97+
It("should inject project name when empty", func() {
98+
tmp := ProjectNameMixin{}
99+
tmp.InjectProjectName("new")
100+
Expect(tmp.ProjectName).To(Equal("new"))
101+
})
102+
})
103+
104+
Context("BoilerplateMixin", func() {
105+
It("should not overwrite existing boilerplate", func() {
106+
tmp := BoilerplateMixin{Boilerplate: "existing"}
107+
tmp.InjectBoilerplate("new")
108+
Expect(tmp.Boilerplate).To(Equal("existing"))
109+
})
110+
111+
It("should inject boilerplate when empty", func() {
112+
tmp := BoilerplateMixin{}
113+
tmp.InjectBoilerplate("new")
114+
Expect(tmp.Boilerplate).To(Equal("new"))
115+
})
116+
})
117+
118+
Context("ResourceMixin", func() {
119+
It("should not overwrite existing resource", func() {
120+
existing := &resource.Resource{GVK: resource.GVK{Group: "existing"}}
121+
tmp := ResourceMixin{Resource: existing}
122+
tmp.InjectResource(&resource.Resource{GVK: resource.GVK{Group: "new"}})
123+
Expect(tmp.Resource.Group).To(Equal("existing"))
124+
})
125+
126+
It("should inject resource when nil", func() {
127+
tmp := ResourceMixin{}
128+
res := &resource.Resource{GVK: resource.GVK{Group: "new"}}
129+
tmp.InjectResource(res)
130+
Expect(tmp.Resource.Group).To(Equal("new"))
131+
})
132+
})
133+
})
134+
135+
var _ = Describe("IfNotExistsActionMixin", func() {
136+
Context("GetIfNotExistsAction", func() {
137+
It("should return the configured action", func() {
138+
tmp := IfNotExistsActionMixin{IfNotExistsAction: IgnoreFile}
139+
Expect(tmp.GetIfNotExistsAction()).To(Equal(IgnoreFile))
140+
})
141+
142+
It("should return zero value when not set", func() {
143+
tmp := IfNotExistsActionMixin{}
144+
Expect(tmp.GetIfNotExistsAction()).To(Equal(IfNotExistsAction(0)))
145+
})
146+
})
147+
})
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package resource
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
var _ = Describe("Webhooks Copy and AddSpoke", func() {
25+
Context("Copy", func() {
26+
It("should create a deep copy of Webhooks", func() {
27+
original := Webhooks{
28+
WebhookVersion: v1,
29+
Defaulting: true,
30+
Validation: true,
31+
Conversion: false,
32+
Spoke: []string{"v1", "v2"},
33+
}
34+
35+
copied := original.Copy()
36+
37+
Expect(copied.WebhookVersion).To(Equal(original.WebhookVersion))
38+
Expect(copied.Defaulting).To(Equal(original.Defaulting))
39+
Expect(copied.Validation).To(Equal(original.Validation))
40+
Expect(copied.Conversion).To(Equal(original.Conversion))
41+
Expect(copied.Spoke).To(Equal(original.Spoke))
42+
})
43+
44+
It("should not affect original when modifying the copy", func() {
45+
original := Webhooks{
46+
WebhookVersion: v1,
47+
Defaulting: true,
48+
Spoke: []string{"v1"},
49+
}
50+
51+
copied := original.Copy()
52+
copied.Defaulting = false
53+
copied.Spoke = append(copied.Spoke, "v2")
54+
55+
Expect(original.Defaulting).To(BeTrue())
56+
Expect(original.Spoke).To(Equal([]string{"v1"}))
57+
Expect(copied.Defaulting).To(BeFalse())
58+
Expect(copied.Spoke).To(Equal([]string{"v1", "v2"}))
59+
})
60+
61+
It("should handle empty Spoke slice", func() {
62+
original := Webhooks{
63+
WebhookVersion: v1,
64+
Spoke: []string{},
65+
}
66+
67+
copied := original.Copy()
68+
Expect(copied.Spoke).To(BeNil())
69+
})
70+
71+
It("should handle nil Spoke slice", func() {
72+
original := Webhooks{
73+
WebhookVersion: v1,
74+
Spoke: nil,
75+
}
76+
77+
copied := original.Copy()
78+
Expect(copied.Spoke).To(BeNil())
79+
})
80+
81+
It("should create independent Spoke slices", func() {
82+
original := Webhooks{
83+
Spoke: []string{"v1"},
84+
}
85+
86+
copied := original.Copy()
87+
copied.Spoke[0] = "v2"
88+
89+
Expect(original.Spoke[0]).To(Equal("v1"))
90+
Expect(copied.Spoke[0]).To(Equal("v2"))
91+
})
92+
})
93+
94+
Context("AddSpoke", func() {
95+
It("should add a new spoke version", func() {
96+
webhook := &Webhooks{}
97+
webhook.AddSpoke("v1")
98+
99+
Expect(webhook.Spoke).To(HaveLen(1))
100+
Expect(webhook.Spoke).To(ContainElement("v1"))
101+
})
102+
103+
It("should not add duplicate spoke versions", func() {
104+
webhook := &Webhooks{
105+
Spoke: []string{"v1"},
106+
}
107+
webhook.AddSpoke("v1")
108+
109+
Expect(webhook.Spoke).To(HaveLen(1))
110+
Expect(webhook.Spoke).To(Equal([]string{"v1"}))
111+
})
112+
113+
It("should add multiple different spoke versions", func() {
114+
webhook := &Webhooks{}
115+
webhook.AddSpoke("v1")
116+
webhook.AddSpoke("v2")
117+
webhook.AddSpoke("v3")
118+
119+
Expect(webhook.Spoke).To(HaveLen(3))
120+
Expect(webhook.Spoke).To(ContainElements("v1", "v2", "v3"))
121+
})
122+
123+
It("should handle adding existing version in the middle", func() {
124+
webhook := &Webhooks{
125+
Spoke: []string{"v1", "v2", "v3"},
126+
}
127+
webhook.AddSpoke("v2")
128+
129+
Expect(webhook.Spoke).To(HaveLen(3))
130+
Expect(webhook.Spoke).To(Equal([]string{"v1", "v2", "v3"}))
131+
})
132+
})
133+
134+
Context("Validate with duplicate Spoke versions", func() {
135+
It("should fail validation with duplicate spoke versions", func() {
136+
webhook := Webhooks{
137+
WebhookVersion: v1,
138+
Spoke: []string{"v1", "v1"},
139+
}
140+
141+
Expect(webhook.Validate()).NotTo(Succeed())
142+
})
143+
144+
It("should succeed validation with unique spoke versions", func() {
145+
webhook := Webhooks{
146+
WebhookVersion: v1,
147+
Spoke: []string{"v1", "v2", "v3"},
148+
}
149+
150+
Expect(webhook.Validate()).To(Succeed())
151+
})
152+
})
153+
154+
Context("IsEmpty with Spoke", func() {
155+
It("should return false when only Spoke is set", func() {
156+
webhook := Webhooks{
157+
Spoke: []string{"v1"},
158+
}
159+
160+
Expect(webhook.IsEmpty()).To(BeFalse())
161+
})
162+
163+
It("should return true when Spoke is empty array", func() {
164+
webhook := Webhooks{
165+
Spoke: []string{},
166+
}
167+
168+
Expect(webhook.IsEmpty()).To(BeTrue())
169+
})
170+
})
171+
})

0 commit comments

Comments
 (0)