Permalink
Browse files

add test for webhook scaffolding

  • Loading branch information...
mengqiy committed Oct 19, 2018
1 parent d19d1ad commit 188f3c9ee120dc700d06d67cc868859fe19dc5d7
@@ -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 {
@@ -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{},
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -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() {

This file was deleted.

Oops, something went wrong.
@@ -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.
@@ -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
@@ -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)
}
@@ -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() {
@@ -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)
@@ -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, "_")))
}
@@ -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)
@@ -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
@@ -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
@@ -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")
}
Oops, something went wrong.

0 comments on commit 188f3c9

Please sign in to comment.