Skip to content

Commit d8126da

Browse files
authored
Merge pull request #4995 from camilamacedo86/fix-test
🌱 refactor TestContext to allow use in unit tests with directory setup
2 parents 4b96727 + 5795f0c commit d8126da

File tree

4 files changed

+92
-62
lines changed

4 files changed

+92
-62
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ docs/book/src/docs
2828
/testdata/**/go.sum
2929
/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/bin
3030
/testdata/**legacy**
31-
/test/e2e/alphaupdate/e2e-**
31+
32+
## Skip testdata files that generate by tests using TestContext
33+
**/e2e-*/**

pkg/cli/alpha/internal/common/common_test.go

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,37 @@ package common
1919
import (
2020
"os"
2121
"path/filepath"
22-
"testing"
2322

2423
. "github.com/onsi/ginkgo/v2"
2524
. "github.com/onsi/gomega"
2625

2726
"sigs.k8s.io/kubebuilder/v4/pkg/config"
2827
"sigs.k8s.io/kubebuilder/v4/pkg/config/store/yaml"
2928
v3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
29+
"sigs.k8s.io/kubebuilder/v4/test/e2e/utils"
3030
)
3131

32-
func TestCommon(t *testing.T) {
33-
RegisterFailHandler(Fail)
34-
RunSpecs(t, "Common Pkg Suite")
35-
}
36-
3732
var _ = Describe("LoadProjectConfig", func() {
3833
var (
39-
tmpDir string
34+
kbc *utils.TestContext
4035
projectFile string
4136
)
4237

4338
BeforeEach(func() {
4439
var err error
45-
tmpDir, err = os.MkdirTemp("", "kubebuilder-common-test")
40+
kbc, err = utils.NewTestContext("kubebuilder", "GO111MODULE=on")
4641
Expect(err).NotTo(HaveOccurred())
47-
projectFile = filepath.Join(tmpDir, yaml.DefaultPath)
42+
Expect(kbc.Prepare()).To(Succeed())
43+
projectFile = filepath.Join(kbc.Dir, yaml.DefaultPath)
4844
})
4945

5046
AfterEach(func() {
51-
err := os.RemoveAll(tmpDir)
52-
Expect(err).NotTo(HaveOccurred())
47+
By("cleaning up test artifacts")
48+
kbc.Destroy()
5349
})
5450

5551
Context("when PROJECT file exists and is valid", func() {
5652
It("should load the project config successfully", func() {
57-
// Register version 3 config
5853
config.Register(config.Version{Number: 3}, func() config.Config {
5954
return &v3.Cfg{Version: config.Version{Number: 3}}
6055
})
@@ -63,26 +58,25 @@ var _ = Describe("LoadProjectConfig", func() {
6358
`
6459
Expect(os.WriteFile(projectFile, []byte(version), 0o644)).To(Succeed())
6560

66-
config, err := LoadProjectConfig(tmpDir)
61+
cfg, err := LoadProjectConfig(kbc.Dir)
6762
Expect(err).NotTo(HaveOccurred())
68-
Expect(config).NotTo(BeNil())
63+
Expect(cfg).NotTo(BeNil())
6964
})
7065
})
7166

7267
Context("when PROJECT file does not exist", func() {
7368
It("should return an error", func() {
74-
_, err := LoadProjectConfig(tmpDir)
69+
_, err := LoadProjectConfig(kbc.Dir)
7570
Expect(err).To(HaveOccurred())
7671
Expect(err.Error()).To(ContainSubstring("failed to load PROJECT file"))
7772
})
7873
})
7974

8075
Context("when PROJECT file is invalid", func() {
8176
It("should return an error", func() {
82-
// Write an invalid YAML content
8377
Expect(os.WriteFile(projectFile, []byte(":?!"), 0o644)).To(Succeed())
8478

85-
_, err := LoadProjectConfig(tmpDir)
79+
_, err := LoadProjectConfig(kbc.Dir)
8680
Expect(err).To(HaveOccurred())
8781
Expect(err.Error()).To(ContainSubstring("failed to load PROJECT file"))
8882
})
@@ -91,69 +85,64 @@ var _ = Describe("LoadProjectConfig", func() {
9185

9286
var _ = Describe("GetInputPath", func() {
9387
var (
94-
tmpDir string
88+
kbc *utils.TestContext
9589
projectFile string
9690
)
9791

9892
BeforeEach(func() {
9993
var err error
100-
tmpDir, err = os.MkdirTemp("", "kubebuilder-common-test")
94+
kbc, err = utils.NewTestContext("kubebuilder", "GO111MODULE=on")
10195
Expect(err).NotTo(HaveOccurred())
102-
projectFile = filepath.Join(tmpDir, yaml.DefaultPath)
96+
Expect(kbc.Prepare()).To(Succeed())
97+
projectFile = filepath.Join(kbc.Dir, yaml.DefaultPath)
10398
})
10499

105100
AfterEach(func() {
106-
err := os.RemoveAll(tmpDir)
107-
Expect(err).NotTo(HaveOccurred())
101+
By("cleaning up test artifacts")
102+
kbc.Destroy()
108103
})
109104

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())
105+
Context("when inputPath has trailing slash", func() {
106+
It("should handle trailing slash and find PROJECT file", func() {
107+
Expect(os.WriteFile(projectFile, []byte("test"), 0o644)).To(Succeed())
117108

118-
currWd, err := os.Getwd()
109+
inputPath, err := GetInputPath(kbc.Dir + "/")
119110
Expect(err).NotTo(HaveOccurred())
120-
121-
inputPath, err := GetInputPath("")
122-
Expect(err).NotTo(HaveOccurred())
123-
Expect(inputPath).To(Equal(currWd))
111+
Expect(inputPath).To(Equal(kbc.Dir + "/"))
124112
})
113+
})
125114

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-
115+
Context("when inputPath is empty", func() {
116+
It("should return error if PROJECT file does not exist in CWD", func() {
130117
inputPath, err := GetInputPath("")
131118
Expect(err).To(HaveOccurred())
132119
Expect(inputPath).To(Equal(""))
133120
Expect(err.Error()).To(ContainSubstring("does not exist"))
134121
})
135122
})
136123

137-
Context("when inputPath is provided", func() {
138-
It("should return inputPath if PROJECT file exists", func() {
124+
Context("when inputPath is valid and PROJECT file exists", func() {
125+
It("should return the inputPath", func() {
139126
Expect(os.WriteFile(projectFile, []byte("test"), 0o644)).To(Succeed())
140127

141-
inputPath, err := GetInputPath(tmpDir)
128+
inputPath, err := GetInputPath(kbc.Dir)
142129
Expect(err).NotTo(HaveOccurred())
143-
Expect(inputPath).To(Equal(tmpDir))
130+
Expect(inputPath).To(Equal(kbc.Dir))
144131
})
132+
})
145133

146-
It("should return error if PROJECT file does not exist at provided inputPath", func() {
147-
inputPath, err := GetInputPath(tmpDir)
134+
Context("when inputPath is valid but PROJECT file does not exist", func() {
135+
It("should return an error", func() {
136+
inputPath, err := GetInputPath(kbc.Dir)
148137
Expect(err).To(HaveOccurred())
149138
Expect(inputPath).To(Equal(""))
150139
Expect(err.Error()).To(ContainSubstring("does not exist"))
151140
})
152141
})
153142

154-
Context("when inputPath is invalid", func() {
155-
It("should return error if inputPath does not exist", func() {
156-
invalidPath := filepath.Join(tmpDir, "nonexistent")
143+
Context("when inputPath does not exist", func() {
144+
It("should return an error", func() {
145+
invalidPath := filepath.Join(kbc.Dir, "nonexistent")
157146
inputPath, err := GetInputPath(invalidPath)
158147
Expect(err).To(HaveOccurred())
159148
Expect(inputPath).To(Equal(""))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestCommon(t *testing.T) {
27+
RegisterFailHandler(Fail)
28+
RunSpecs(t, "Common Package Suite For Alpha Commands")
29+
}

test/e2e/utils/test_context.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,32 @@ func NewTestContext(binaryName string, env ...string) (*TestContext, error) {
7777
ServiceAccount: fmt.Sprintf("e2e-%s-controller-manager", testSuffix),
7878
CmdContext: cc,
7979
}
80+
81+
// For test outside of cluster we do not need to have kubectl
8082
var k8sVersion *KubernetesVersion
81-
v, err := kubectl.Version()
82-
if err != nil {
83+
fakeVersion := &KubernetesVersion{
84+
ClientVersion: VersionInfo{
85+
Major: "1",
86+
Minor: "0",
87+
GitVersion: "v1.0.0-fake",
88+
},
89+
ServerVersion: VersionInfo{
90+
Major: "1",
91+
Minor: "0",
92+
GitVersion: "v1.0.0-fake",
93+
},
94+
}
95+
96+
var v KubernetesVersion
97+
var lookupErr error
98+
99+
_, lookupErr = exec.LookPath("kubectl")
100+
if lookupErr != nil {
101+
_, _ = fmt.Fprintf(GinkgoWriter, "warning: kubectl not found in PATH; proceeding with fake version\n")
102+
k8sVersion = fakeVersion
103+
} else if v, err = kubectl.Version(); err != nil {
83104
_, _ = fmt.Fprintf(GinkgoWriter, "warning: failed to get kubernetes version: %v\n", err)
84-
k8sVersion = &KubernetesVersion{
85-
ClientVersion: VersionInfo{
86-
Major: "1",
87-
Minor: "0",
88-
GitVersion: "v1.0.0-fake",
89-
},
90-
ServerVersion: VersionInfo{
91-
Major: "1",
92-
Minor: "0",
93-
GitVersion: "v1.0.0-fake",
94-
},
95-
}
105+
k8sVersion = fakeVersion
96106
} else {
97107
k8sVersion = &v
98108
}

0 commit comments

Comments
 (0)