Skip to content

Commit 804f002

Browse files
authored
Merge pull request #440 from mengqiy/add_test
Add test for test project
2 parents 331d228 + 188f3c9 commit 804f002

35 files changed

+218
-101
lines changed

common.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ function setup_envs {
140140
header_text "setting up env vars"
141141

142142
# Setup env vars
143-
export PATH=/tmp/kubebuilder/bin:$PATH
144-
export TEST_ASSET_KUBECTL=/tmp/kubebuilder/bin/kubectl
145-
export TEST_ASSET_KUBE_APISERVER=/tmp/kubebuilder/bin/kube-apiserver
146-
export TEST_ASSET_ETCD=/tmp/kubebuilder/bin/etcd
147-
export TEST_DEP=/tmp/kubebuilder/init_project
143+
export PATH=$tmp_root/kubebuilder/bin:$PATH
144+
export TEST_ASSET_KUBECTL=$tmp_root/kubebuilder/bin/kubectl
145+
export TEST_ASSET_KUBE_APISERVER=$tmp_root/kubebuilder/bin/kube-apiserver
146+
export TEST_ASSET_ETCD=$tmp_root/kubebuilder/bin/etcd
147+
export TEST_DEP=$tmp_root/kubebuilder/init_project
148148
}
149149

150150
function cache_dep {

pkg/scaffold/manager/manager_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ var _ = Describe("Manager", func() {
3232
file: filepath.Join("pkg", "controller", "controller.go"),
3333
instance: &Controller{},
3434
},
35+
{
36+
file: filepath.Join("pkg", "webhook", "webhook.go"),
37+
instance: &Webhook{},
38+
},
3539
{
3640
file: filepath.Join("Dockerfile"),
3741
instance: &Dockerfile{},

pkg/scaffold/project/project_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ Copyright 2019 Example Owners.
174174
})
175175
Context("with defaults ", func() {
176176
It("should match the golden file", func() {
177-
instance := &Makefile{Image: "controller:latest", ControllerToolsPath: ".."}
178-
instance.Repo = "sigs.k8s.io/controller-tools/test"
177+
instance := &Makefile{Image: "controller:latest"}
178+
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
179179
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
180180

181181
// Verify the contents matches the golden file.
@@ -191,8 +191,8 @@ Copyright 2019 Example Owners.
191191
})
192192
Context("with defaults ", func() {
193193
It("should match the golden file", func() {
194-
instance := &Kustomize{Prefix: "test"}
195-
instance.Repo = "sigs.k8s.io/controller-tools/test"
194+
instance := &Kustomize{Prefix: "project"}
195+
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
196196
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
197197

198198
// Verify the contents matches the golden file.
@@ -209,7 +209,7 @@ Copyright 2019 Example Owners.
209209
Context("with defaults ", func() {
210210
It("should match the golden file", func() {
211211
instance := &KustomizeImagePatch{}
212-
instance.Repo = "sigs.k8s.io/controller-tools/test"
212+
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
213213
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
214214

215215
// Verify the contents matches the golden file.
@@ -242,9 +242,9 @@ Copyright 2019 Example Owners.
242242
Context("with defaults", func() {
243243
It("should match the golden file", func() {
244244
instance := &Project{}
245-
instance.Version = "2"
245+
instance.Version = "1"
246246
instance.Domain = "testproject.org"
247-
instance.Repo = "sigs.k8s.io/controller-tools/test"
247+
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
248248
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
249249

250250
// Verify the contents matches the golden file.
@@ -256,10 +256,10 @@ Copyright 2019 Example Owners.
256256
It("should return the directory if it is under the gopath", func() {
257257
instance := &Project{}
258258
repo, err := instance.repoFromGopathAndWd("/home/fake/go", func() (string, error) {
259-
return "/home/fake/go/src/kubernetes-sigs/controller-tools", nil
259+
return "/home/fake/go/src/kubernetes-sigs/kubebuilder", nil
260260
})
261261
Expect(err).NotTo(HaveOccurred())
262-
Expect(repo).To(Equal("kubernetes-sigs/controller-tools"))
262+
Expect(repo).To(Equal("kubernetes-sigs/kubebuilder"))
263263
})
264264

265265
It("should return an error if the wd is not under GOPATH", func() {

pkg/scaffold/project/projectutil/projectutil.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

pkg/scaffold/scaffoldtest/scaffoldtest.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import (
2020
"bytes"
2121
"io"
2222
"io/ioutil"
23+
"os"
24+
"path"
2325
"path/filepath"
2426

2527
"github.com/onsi/ginkgo"
2628
"github.com/onsi/gomega"
29+
2730
"sigs.k8s.io/kubebuilder/pkg/scaffold"
2831
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
29-
"sigs.k8s.io/kubebuilder/pkg/scaffold/project/projectutil"
3032
)
3133

3234
// TestResult is the result of running the scaffolding.
@@ -38,18 +40,19 @@ type TestResult struct {
3840
Golden string
3941
}
4042

43+
func getProjectRoot() string {
44+
gopath := os.Getenv("GOPATH")
45+
return path.Join(gopath, "src", "sigs.k8s.io", "kubebuilder")
46+
}
47+
4148
// ProjectPath is the path to the controller-tools/test project file
4249
func ProjectPath() string {
43-
root, err := projectutil.GetProjectDir()
44-
gomega.Expect(err).NotTo(gomega.HaveOccurred())
45-
return filepath.Join(root, "test", "PROJECT")
50+
return filepath.Join(getProjectRoot(), "test", "project", "PROJECT")
4651
}
4752

4853
// BoilerplatePath is the path to the controller-tools/test boilerplate file
4954
func BoilerplatePath() string {
50-
root, err := projectutil.GetProjectDir()
51-
gomega.Expect(err).NotTo(gomega.HaveOccurred())
52-
return filepath.Join(root, "test", "hack", "boilerplate.go.txt")
55+
return filepath.Join(getProjectRoot(), "test", "project", "hack", "boilerplate.go.txt")
5356
}
5457

5558
// Options are the options for scaffolding in the controller-tools/test directory
@@ -63,22 +66,18 @@ func Options() input.Options {
6366
// NewTestScaffold returns a new Scaffold and TestResult instance for testing
6467
func NewTestScaffold(writeToPath, goldenPath string) (*scaffold.Scaffold, *TestResult) {
6568
r := &TestResult{}
66-
67-
root, err := projectutil.GetProjectDir()
68-
gomega.Expect(err).NotTo(gomega.HaveOccurred())
69-
7069
// Setup scaffold
7170
s := &scaffold.Scaffold{
7271
GetWriter: func(path string) (io.Writer, error) {
7372
defer ginkgo.GinkgoRecover()
7473
gomega.Expect(path).To(gomega.Equal(writeToPath))
7574
return &r.Actual, nil
7675
},
77-
ProjectPath: filepath.Join(root, "test"),
76+
ProjectPath: filepath.Join(getProjectRoot(), "test", "project"),
7877
}
7978

8079
if len(goldenPath) > 0 {
81-
b, err := ioutil.ReadFile(filepath.Join(root, "test", goldenPath))
80+
b, err := ioutil.ReadFile(filepath.Join(getProjectRoot(), "test", "project", goldenPath))
8281
gomega.Expect(err).NotTo(gomega.HaveOccurred())
8382
r.Golden = string(b)
8483
}

pkg/scaffold/webhook/add_admissionbuilder_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ package {{ .Server }}server
5656
import (
5757
"fmt"
5858
59-
"{{ .Repo }}/pkg/webhook/{{ .Server }}_server/{{ .Resource.Resource }}/{{ .Type }}"
59+
"{{ .Repo }}/pkg/webhook/{{ .Server }}_server/{{ lower .Resource.Kind }}/{{ .Type }}"
6060
)
6161
6262
func init() {

pkg/scaffold/webhook/admissionbuilder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (a *AdmissionWebhookBuilder) GetInput() (input.Input, error) {
5757
a.Mutating = true
5858
}
5959
a.Type = strings.ToLower(a.Type)
60-
a.BuilderName = builderName(a.Config, a.Resource.Resource)
60+
a.BuilderName = builderName(a.Config, strings.ToLower(a.Resource.Kind))
6161
ops := make([]string, len(a.Operations))
6262
for i, op := range a.Operations {
6363
ops[i] = "admissionregistrationv1beta1." + strings.Title(op)
@@ -67,7 +67,7 @@ func (a *AdmissionWebhookBuilder) GetInput() (input.Input, error) {
6767
if a.Path == "" {
6868
a.Path = filepath.Join("pkg", "webhook",
6969
fmt.Sprintf("%s_server", a.Server),
70-
a.Resource.Resource,
70+
strings.ToLower(a.Resource.Kind),
7171
a.Type,
7272
fmt.Sprintf("%s_webhook.go", strings.Join(a.Operations, "_")))
7373
}

pkg/scaffold/webhook/admissionhandler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (a *AdmissionHandler) GetInput() (input.Input, error) {
5656
if a.Type == "mutating" {
5757
a.Mutate = true
5858
}
59-
a.BuilderName = builderName(a.Config, a.Resource.Resource)
59+
a.BuilderName = builderName(a.Config, strings.ToLower(a.Resource.Kind))
6060
ops := make([]string, len(a.Operations))
6161
for i, op := range a.Operations {
6262
ops[i] = strings.Title(op)
@@ -66,9 +66,9 @@ func (a *AdmissionHandler) GetInput() (input.Input, error) {
6666
if a.Path == "" {
6767
a.Path = filepath.Join("pkg", "webhook",
6868
fmt.Sprintf("%s_server", a.Server),
69-
a.Resource.Resource,
69+
strings.ToLower(a.Resource.Kind),
7070
a.Type,
71-
fmt.Sprintf("%s_%s_handler.go", a.Resource.Resource, strings.Join(a.Operations, "_")))
71+
fmt.Sprintf("%s_%s_handler.go", strings.ToLower(a.Resource.Kind), strings.Join(a.Operations, "_")))
7272
}
7373
a.TemplateBody = addAdmissionHandlerTemplate
7474
return a.Input, nil

pkg/scaffold/webhook/admissionwebhooks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (a *AdmissionWebhooks) GetInput() (input.Input, error) {
4444
if a.Path == "" {
4545
a.Path = filepath.Join("pkg", "webhook",
4646
fmt.Sprintf("%s_server", a.Server),
47-
strings.ToLower(a.Resource.Resource),
47+
strings.ToLower(a.Resource.Kind),
4848
a.Type, "webhooks.go")
4949
}
5050
a.TemplateBody = webhooksTemplate
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package webhook_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestController(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Webhook Suite")
13+
}

0 commit comments

Comments
 (0)