Skip to content

Commit ce89e63

Browse files
committed
add ut for common utils
1 parent 5ed6496 commit ce89e63

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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 common
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
"testing"
23+
24+
. "github.com/onsi/ginkgo/v2"
25+
. "github.com/onsi/gomega"
26+
27+
"sigs.k8s.io/kubebuilder/v4/pkg/config"
28+
"sigs.k8s.io/kubebuilder/v4/pkg/config/store/yaml"
29+
v3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
30+
)
31+
32+
func TestCommon(t *testing.T) {
33+
RegisterFailHandler(Fail)
34+
RunSpecs(t, "Common Pkg Suite")
35+
}
36+
37+
var _ = Describe("LoadProjectConfig", func() {
38+
var (
39+
tmpDir string
40+
projectFile string
41+
)
42+
43+
BeforeEach(func() {
44+
var err error
45+
tmpDir, err = os.MkdirTemp("", "kubebuilder-common-test")
46+
Expect(err).NotTo(HaveOccurred())
47+
projectFile = filepath.Join(tmpDir, yaml.DefaultPath)
48+
})
49+
50+
AfterEach(func() {
51+
err := os.RemoveAll(tmpDir)
52+
Expect(err).NotTo(HaveOccurred())
53+
})
54+
55+
Context("when PROJECT file exists and is valid", func() {
56+
It("should load the project config successfully", func() {
57+
// Register version 3 config
58+
config.Register(config.Version{Number: 3}, func() config.Config {
59+
return &v3.Cfg{Version: config.Version{Number: 3}}
60+
})
61+
62+
const version = `version: "3"
63+
`
64+
Expect(os.WriteFile(projectFile, []byte(version), 0o644)).To(Succeed())
65+
66+
config, err := LoadProjectConfig(tmpDir)
67+
Expect(err).NotTo(HaveOccurred())
68+
Expect(config).NotTo(BeNil())
69+
})
70+
})
71+
72+
Context("when PROJECT file does not exist", func() {
73+
It("should return an error", func() {
74+
_, err := LoadProjectConfig(tmpDir)
75+
Expect(err).To(HaveOccurred())
76+
Expect(err.Error()).To(ContainSubstring("failed to load PROJECT file"))
77+
})
78+
})
79+
80+
Context("when PROJECT file is invalid", func() {
81+
It("should return an error", func() {
82+
// Write an invalid YAML content
83+
Expect(os.WriteFile(projectFile, []byte(":?!"), 0o644)).To(Succeed())
84+
85+
_, err := LoadProjectConfig(tmpDir)
86+
Expect(err).To(HaveOccurred())
87+
Expect(err.Error()).To(ContainSubstring("failed to load PROJECT file"))
88+
})
89+
})
90+
})
91+
92+
var _ = Describe("GetInputPath", func() {
93+
var (
94+
tmpDir string
95+
projectFile string
96+
)
97+
98+
BeforeEach(func() {
99+
var err error
100+
tmpDir, err = os.MkdirTemp("", "kubebuilder-common-test")
101+
Expect(err).NotTo(HaveOccurred())
102+
projectFile = filepath.Join(tmpDir, yaml.DefaultPath)
103+
})
104+
105+
AfterEach(func() {
106+
err := os.RemoveAll(tmpDir)
107+
Expect(err).NotTo(HaveOccurred())
108+
})
109+
110+
Context("when inputPath is empty", func() {
111+
It("should return current working directory if PROJECT file exists", func() {
112+
// Create PROJECT file in tmpDir
113+
Expect(os.WriteFile(projectFile, []byte("test-data"), 0o644)).To(Succeed())
114+
115+
// Change working directory to tmpDir
116+
Expect(os.Chdir(tmpDir)).To(Succeed())
117+
118+
currWd, err := os.Getwd()
119+
Expect(err).NotTo(HaveOccurred())
120+
121+
inputPath, err := GetInputPath("")
122+
Expect(err).NotTo(HaveOccurred())
123+
Expect(inputPath).To(Equal(currWd))
124+
})
125+
126+
It("should return error if PROJECT file does not exist in cwd", func() {
127+
// Change working directory to tmpDir (no PROJECT file)
128+
Expect(os.Chdir(tmpDir)).To(Succeed())
129+
130+
inputPath, err := GetInputPath("")
131+
Expect(err).To(HaveOccurred())
132+
Expect(inputPath).To(Equal(""))
133+
Expect(err.Error()).To(ContainSubstring("does not exist"))
134+
})
135+
})
136+
137+
Context("when inputPath is provided", func() {
138+
It("should return inputPath if PROJECT file exists", func() {
139+
Expect(os.WriteFile(projectFile, []byte("test"), 0o644)).To(Succeed())
140+
141+
inputPath, err := GetInputPath(tmpDir)
142+
Expect(err).NotTo(HaveOccurred())
143+
Expect(inputPath).To(Equal(tmpDir))
144+
})
145+
146+
It("should return error if PROJECT file does not exist at provided inputPath", func() {
147+
inputPath, err := GetInputPath(tmpDir)
148+
Expect(err).To(HaveOccurred())
149+
Expect(inputPath).To(Equal(""))
150+
Expect(err.Error()).To(ContainSubstring("does not exist"))
151+
})
152+
})
153+
154+
Context("when inputPath is invalid", func() {
155+
It("should return error if inputPath does not exist", func() {
156+
invalidPath := filepath.Join(tmpDir, "nonexistent")
157+
inputPath, err := GetInputPath(invalidPath)
158+
Expect(err).To(HaveOccurred())
159+
Expect(inputPath).To(Equal(""))
160+
Expect(err.Error()).To(ContainSubstring("does not exist"))
161+
})
162+
})
163+
})

0 commit comments

Comments
 (0)