Skip to content

Commit

Permalink
add test for webhook scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
Mengqi Yu committed Oct 19, 2018
1 parent d19d1ad commit 188f3c9
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 82 deletions.
10 changes: 5 additions & 5 deletions common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ function setup_envs {
header_text "setting up env vars"

# Setup env vars
export PATH=/tmp/kubebuilder/bin:$PATH
export TEST_ASSET_KUBECTL=/tmp/kubebuilder/bin/kubectl
export TEST_ASSET_KUBE_APISERVER=/tmp/kubebuilder/bin/kube-apiserver
export TEST_ASSET_ETCD=/tmp/kubebuilder/bin/etcd
export TEST_DEP=/tmp/kubebuilder/init_project
export PATH=$tmp_root/kubebuilder/bin:$PATH
export TEST_ASSET_KUBECTL=$tmp_root/kubebuilder/bin/kubectl
export TEST_ASSET_KUBE_APISERVER=$tmp_root/kubebuilder/bin/kube-apiserver
export TEST_ASSET_ETCD=$tmp_root/kubebuilder/bin/etcd
export TEST_DEP=$tmp_root/kubebuilder/init_project
}

function cache_dep {
Expand Down
4 changes: 4 additions & 0 deletions pkg/scaffold/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ var _ = Describe("Manager", func() {
file: filepath.Join("pkg", "controller", "controller.go"),
instance: &Controller{},
},
{
file: filepath.Join("pkg", "webhook", "webhook.go"),
instance: &Webhook{},
},
{
file: filepath.Join("Dockerfile"),
instance: &Dockerfile{},
Expand Down
18 changes: 9 additions & 9 deletions pkg/scaffold/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ Copyright 2019 Example Owners.
})
Context("with defaults ", func() {
It("should match the golden file", func() {
instance := &Makefile{Image: "controller:latest", ControllerToolsPath: ".."}
instance.Repo = "sigs.k8s.io/controller-tools/test"
instance := &Makefile{Image: "controller:latest"}
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())

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

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

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

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

It("should return an error if the wd is not under GOPATH", func() {
Expand Down
48 changes: 0 additions & 48 deletions pkg/scaffold/project/projectutil/projectutil.go

This file was deleted.

25 changes: 12 additions & 13 deletions pkg/scaffold/scaffoldtest/scaffoldtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import (
"bytes"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"

"github.com/onsi/ginkgo"
"github.com/onsi/gomega"

"sigs.k8s.io/kubebuilder/pkg/scaffold"
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/project/projectutil"
)

// TestResult is the result of running the scaffolding.
Expand All @@ -38,18 +40,19 @@ type TestResult struct {
Golden string
}

func getProjectRoot() string {
gopath := os.Getenv("GOPATH")
return path.Join(gopath, "src", "sigs.k8s.io", "kubebuilder")
}

// ProjectPath is the path to the controller-tools/test project file
func ProjectPath() string {
root, err := projectutil.GetProjectDir()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return filepath.Join(root, "test", "PROJECT")
return filepath.Join(getProjectRoot(), "test", "project", "PROJECT")
}

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

// Options are the options for scaffolding in the controller-tools/test directory
Expand All @@ -63,22 +66,18 @@ func Options() input.Options {
// NewTestScaffold returns a new Scaffold and TestResult instance for testing
func NewTestScaffold(writeToPath, goldenPath string) (*scaffold.Scaffold, *TestResult) {
r := &TestResult{}

root, err := projectutil.GetProjectDir()
gomega.Expect(err).NotTo(gomega.HaveOccurred())

// Setup scaffold
s := &scaffold.Scaffold{
GetWriter: func(path string) (io.Writer, error) {
defer ginkgo.GinkgoRecover()
gomega.Expect(path).To(gomega.Equal(writeToPath))
return &r.Actual, nil
},
ProjectPath: filepath.Join(root, "test"),
ProjectPath: filepath.Join(getProjectRoot(), "test", "project"),
}

if len(goldenPath) > 0 {
b, err := ioutil.ReadFile(filepath.Join(root, "test", goldenPath))
b, err := ioutil.ReadFile(filepath.Join(getProjectRoot(), "test", "project", goldenPath))
gomega.Expect(err).NotTo(gomega.HaveOccurred())
r.Golden = string(b)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/scaffold/webhook/add_admissionbuilder_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ package {{ .Server }}server
import (
"fmt"
"{{ .Repo }}/pkg/webhook/{{ .Server }}_server/{{ .Resource.Resource }}/{{ .Type }}"
"{{ .Repo }}/pkg/webhook/{{ .Server }}_server/{{ lower .Resource.Kind }}/{{ .Type }}"
)
func init() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/scaffold/webhook/admissionbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (a *AdmissionWebhookBuilder) GetInput() (input.Input, error) {
a.Mutating = true
}
a.Type = strings.ToLower(a.Type)
a.BuilderName = builderName(a.Config, a.Resource.Resource)
a.BuilderName = builderName(a.Config, strings.ToLower(a.Resource.Kind))
ops := make([]string, len(a.Operations))
for i, op := range a.Operations {
ops[i] = "admissionregistrationv1beta1." + strings.Title(op)
Expand All @@ -67,7 +67,7 @@ func (a *AdmissionWebhookBuilder) GetInput() (input.Input, error) {
if a.Path == "" {
a.Path = filepath.Join("pkg", "webhook",
fmt.Sprintf("%s_server", a.Server),
a.Resource.Resource,
strings.ToLower(a.Resource.Kind),
a.Type,
fmt.Sprintf("%s_webhook.go", strings.Join(a.Operations, "_")))
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/scaffold/webhook/admissionhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (a *AdmissionHandler) GetInput() (input.Input, error) {
if a.Type == "mutating" {
a.Mutate = true
}
a.BuilderName = builderName(a.Config, a.Resource.Resource)
a.BuilderName = builderName(a.Config, strings.ToLower(a.Resource.Kind))
ops := make([]string, len(a.Operations))
for i, op := range a.Operations {
ops[i] = strings.Title(op)
Expand All @@ -66,9 +66,9 @@ func (a *AdmissionHandler) GetInput() (input.Input, error) {
if a.Path == "" {
a.Path = filepath.Join("pkg", "webhook",
fmt.Sprintf("%s_server", a.Server),
a.Resource.Resource,
strings.ToLower(a.Resource.Kind),
a.Type,
fmt.Sprintf("%s_%s_handler.go", a.Resource.Resource, strings.Join(a.Operations, "_")))
fmt.Sprintf("%s_%s_handler.go", strings.ToLower(a.Resource.Kind), strings.Join(a.Operations, "_")))
}
a.TemplateBody = addAdmissionHandlerTemplate
return a.Input, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/scaffold/webhook/admissionwebhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (a *AdmissionWebhooks) GetInput() (input.Input, error) {
if a.Path == "" {
a.Path = filepath.Join("pkg", "webhook",
fmt.Sprintf("%s_server", a.Server),
strings.ToLower(a.Resource.Resource),
strings.ToLower(a.Resource.Kind),
a.Type, "webhooks.go")
}
a.TemplateBody = webhooksTemplate
Expand Down
13 changes: 13 additions & 0 deletions pkg/scaffold/webhook/webhook_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package webhook_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestController(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Webhook Suite")
}
Loading

0 comments on commit 188f3c9

Please sign in to comment.