Skip to content

Commit 50e68c5

Browse files
committed
Move test project to testdata
Go tooling ignores testdata, so the Go module manipulation commands (pretty much everything that Go does) will ignore the test projects. The v1 scaffolding needs to be in its own gopath, otherwise the tooling will ignore the vendor directory.
1 parent 6bf7647 commit 50e68c5

File tree

6 files changed

+44
-28
lines changed

6 files changed

+44
-28
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ docs/book/node_modules/
1212

1313
# skip bin
1414
bin/*
15+
16+
# skip testdata gopath non-src
17+
/testdata/gopath/bin
18+
/testdata/gopath/pkg

common.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ function setup_envs {
161161

162162
function restore_go_deps {
163163
header_text "restoring Go dependencies"
164-
tar -zxf ${go_workspace}/src/sigs.k8s.io/kubebuilder/test/vendor.v1.tgz
164+
tar -zxf ${go_workspace}/src/sigs.k8s.io/kubebuilder/testdata/vendor.v1.tgz
165165
}
166166

167167
function cache_project {

generated_golden.sh

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,20 @@ build_kb() {
2929
scaffold_test_project() {
3030
project=$1
3131
version=$2
32-
mkdir -p ./test/$project
33-
rm -rf ./test/$project/*
32+
testdata_dir=$(pwd)/testdata
33+
mkdir -p ./testdata/$project
34+
rm -rf ./testdata/$project/*
3435
pushd .
35-
cd test/$project
36+
cd testdata/$project
3637
# untar Gopkg.lock and vendor directory for appropriate project version
3738
tar -zxf ../vendor.v$version.tgz
3839

39-
kb=../../bin/kubebuilder
40+
kb=$testdata_dir/../bin/kubebuilder
4041

41-
$kb init --project-version $version --domain testproject.org --license apache2 --owner "The Kubernetes authors" --dep=false
42+
oldgopath=$GOPATH
4243
if [ $version == "1" ]; then
44+
export GOPATH=$(pwd)/../.. # go ignores vendor under testdata, so fake out a gopath
45+
$kb init --project-version $version --domain testproject.org --license apache2 --owner "The Kubernetes authors" --dep=false
4346
$kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
4447
$kb alpha webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=create,update --make=false
4548
$kb alpha webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=delete --make=false
@@ -51,6 +54,7 @@ scaffold_test_project() {
5154
$kb alpha webhook --group core --version v1 --kind Namespace --type=mutating --operations=update --make=false
5255
$kb create api --group policy --version v1beta1 --kind HealthCheckPolicy --example=false --controller=true --resource=true --namespaced=false --make=false
5356
elif [ $version == "2" ]; then
57+
$kb init --project-version $version --domain testproject.org --license apache2 --owner "The Kubernetes authors"
5458
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false
5559
$kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
5660
$kb alpha webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=create,update --make=false
@@ -66,9 +70,11 @@ scaffold_test_project() {
6670
rm -f Gopkg.lock
6771
rm -rf ./vendor
6872
rm -rf ./bin
73+
export GOPATH=$oldgopath
6974
popd
7075
}
7176

72-
build_kb && \
73-
scaffold_test_project project 1 && \
77+
set -e
78+
build_kb
79+
scaffold_test_project gopath/src/project 1
7480
scaffold_test_project project_v2 2

pkg/scaffold/project/project_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ Copyright %s Example Owners.
180180
Context("with defaults ", func() {
181181
It("should match the golden file", func() {
182182
instance := &project.Makefile{Image: "controller:latest"}
183-
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
183+
instance.Repo = "project"
184184
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
185185

186186
// Verify the contents matches the golden file.
@@ -197,7 +197,7 @@ Copyright %s Example Owners.
197197
Context("with defaults ", func() {
198198
It("should match the golden file", func() {
199199
instance := &project.Kustomize{Prefix: "project"}
200-
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
200+
instance.Repo = "project"
201201
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
202202

203203
// Verify the contents matches the golden file.
@@ -214,7 +214,7 @@ Copyright %s Example Owners.
214214
Context("with rbac", func() {
215215
It("should match the golden file", func() {
216216
instance := &project.KustomizeRBAC{}
217-
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
217+
instance.Repo = "project"
218218
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
219219

220220
// Verify the contents matches the golden file.
@@ -231,7 +231,7 @@ Copyright %s Example Owners.
231231
Context("with manager", func() {
232232
It("should match the golden file", func() {
233233
instance := &project.KustomizeManager{}
234-
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
234+
instance.Repo = "project"
235235
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
236236

237237
// Verify the contents matches the golden file.
@@ -248,7 +248,7 @@ Copyright %s Example Owners.
248248
Context("with defaults ", func() {
249249
It("should match the golden file", func() {
250250
instance := &project.KustomizeImagePatch{}
251-
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
251+
instance.Repo = "project"
252252
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
253253

254254
// Verify the contents matches the golden file.
@@ -265,7 +265,7 @@ Copyright %s Example Owners.
265265
Context("with defaults ", func() {
266266
It("should match the golden file", func() {
267267
instance := &project.KustomizePrometheusMetricsPatch{}
268-
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
268+
instance.Repo = "project"
269269
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
270270

271271
// Verify the contents matches the golden file.
@@ -300,7 +300,7 @@ Copyright %s Example Owners.
300300
instance := &project.Project{}
301301
instance.Version = "1"
302302
instance.Domain = "testproject.org"
303-
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
303+
instance.Repo = "project"
304304
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
305305

306306
// Verify the contents matches the golden file.
@@ -360,7 +360,7 @@ Copyright %s Example Owners.
360360
i, err := instance.GetInput()
361361
Expect(err).NotTo(HaveOccurred())
362362
Expect(i.Path).To(Equal("PROJECT"))
363-
Expect(i.Repo).To(Equal("sigs.k8s.io/kubebuilder/pkg/scaffold/project"))
363+
Expect(i.Repo).To(Equal("project"))
364364
})
365365
})
366366
})

pkg/scaffold/scaffoldtest/scaffoldtest.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"path"
2525
"path/filepath"
26+
"go/build"
2627

2728
"github.com/onsi/ginkgo"
2829
"github.com/onsi/gomega"
@@ -36,7 +37,7 @@ type TestResult struct {
3637
// Actual is the bytes written to a scaffolded file.
3738
Actual bytes.Buffer
3839

39-
// Golden is the golden file contents read from the controller-tools/test package
40+
// Golden is the golden file contents read from the controller-tools/testdata package
4041
Golden string
4142
}
4243

@@ -45,17 +46,17 @@ func getProjectRoot() string {
4546
return path.Join(gopath, "src", "sigs.k8s.io", "kubebuilder")
4647
}
4748

48-
// ProjectPath is the path to the controller-tools/test project file
49+
// ProjectPath is the path to the controller-tools/testdata project file
4950
func ProjectPath() string {
50-
return filepath.Join(getProjectRoot(), "test", "project", "PROJECT")
51+
return filepath.Join(getProjectRoot(), "testdata", "gopath", "src", "project", "PROJECT")
5152
}
5253

53-
// BoilerplatePath is the path to the controller-tools/test boilerplate file
54+
// BoilerplatePath is the path to the controller-tools/testdata boilerplate file
5455
func BoilerplatePath() string {
55-
return filepath.Join(getProjectRoot(), "test", "project", "hack", "boilerplate.go.txt")
56+
return filepath.Join(getProjectRoot(), "testdata", "gopath", "src", "project", "hack", "boilerplate.go.txt")
5657
}
5758

58-
// Options are the options for scaffolding in the controller-tools/test directory
59+
// Options are the options for scaffolding in the controller-tools/testdata directory
5960
func Options() input.Options {
6061
return input.Options{
6162
BoilerplatePath: BoilerplatePath(),
@@ -73,11 +74,12 @@ func NewTestScaffold(writeToPath, goldenPath string) (*scaffold.Scaffold, *TestR
7374
gomega.Expect(path).To(gomega.Equal(writeToPath))
7475
return &r.Actual, nil
7576
},
76-
ProjectPath: filepath.Join(getProjectRoot(), "test", "project"),
77+
ProjectPath: filepath.Join(getProjectRoot(), "testdata", "gopath", "src", "project"),
7778
}
79+
build.Default.GOPATH = filepath.Join(getProjectRoot(), "testdata", "gopath")
7880

7981
if len(goldenPath) > 0 {
80-
b, err := ioutil.ReadFile(filepath.Join(getProjectRoot(), "test", "project", goldenPath))
82+
b, err := ioutil.ReadFile(filepath.Join(getProjectRoot(), "testdata", "gopath", "src", "project", goldenPath))
8183
gomega.Expect(err).NotTo(gomega.HaveOccurred())
8284
r.Golden = string(b)
8385
}

test.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ function test_project {
8989
project_dir=$1
9090
version=$2
9191
header_text "performing tests in dir $project_dir for project version v$version"
92-
cd test/$project_dir
93-
tar -zxf ../vendor.v$version.tgz
92+
vendor_tarball=$(pwd)/testdata/vendor.v$version.tgz
93+
cd testdata/$project_dir
94+
# v2 uses modules, and thus doesn't have a vendor directory
95+
[[ -e ${vendor_tarball} ]] && tar -zxf $vendor_tarball
9496
make
95-
rm -rf ./vendor && rm -f Gopkg.lock
97+
[[ -e ${vendor_tarball} ]] && rm -rf ./vendor && rm -f Gopkg.lock
9698
cd -
9799
}
98100

@@ -139,7 +141,9 @@ cd ${go_workspace}/src/sigs.k8s.io/kubebuilder
139141
go test ./cmd/... ./pkg/...
140142

141143
# test project v1
142-
test_project project 1
144+
# auto is roughly equivalent to off in our case,
145+
# since we'll be in a gopath (basically, reset to default)
146+
GO111MODULE=auto test_project gopath/src/project 1
143147

144148
# test project v2
145149
test_project project_v2 2

0 commit comments

Comments
 (0)