Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
add component webhook test
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Zhang <yangzhangrice@hotmail.com>
  • Loading branch information
ryanzhang-oss committed Sep 10, 2020
1 parent 0096ecd commit 36d9a48
Show file tree
Hide file tree
Showing 6 changed files with 361 additions and 140 deletions.
6 changes: 3 additions & 3 deletions pkg/oam/util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ func Object2Map(obj interface{}) (map[string]interface{}, error) {
return res, err
}

// DumpJSON returns the JSON encoding
func DumpJSON(o interface{}) string {
// JSONMarshal returns the JSON encoding
func JSONMarshal(o interface{}) []byte {
j, _ := json.Marshal(o)
return string(j)
return j
}

// GenTraitName generate trait name
Expand Down
43 changes: 43 additions & 0 deletions pkg/webhook/v1alpha2/component/component.coverprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
mode: atomic
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:62.97,66.16 3 2
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:70.2,70.38 1 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:74.2,77.16 3 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:81.2,82.27 2 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:86.2,86.13 1 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:66.16,68.3 1 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:70.38,73.3 2 0
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:77.16,79.3 1 0
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:82.27,85.3 1 0
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:90.65,93.72 3 6
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:96.2,96.31 1 5
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:137.2,137.12 1 3
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:93.72,95.3 1 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:96.31,98.10 2 3
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:101.3,104.116 3 2
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:108.3,109.148 2 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:113.3,131.17 11 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:134.3,134.33 1 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:98.10,100.4 1 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:104.116,106.4 1 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:109.148,111.4 1 0
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:131.17,133.4 1 0
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:143.63,146.2 2 5
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:151.69,154.2 2 3
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/mutating_handler.go:157.51,160.2 2 0
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:54.99,58.16 3 6
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:64.2,64.40 1 5
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:77.2,77.30 1 2
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:58.16,62.3 2 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:65.31,66.64 1 3
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:70.31,71.64 1 2
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:66.64,69.4 2 2
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:71.64,74.4 2 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:81.71,87.72 5 5
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:92.2,92.31 1 4
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:96.2,99.72 2 4
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:103.2,103.16 1 4
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:87.72,91.3 2 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:92.31,95.3 1 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:99.72,102.3 1 2
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:118.71,121.2 2 1
github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component/validating_handler.go:124.53,127.2 2 0
66 changes: 61 additions & 5 deletions pkg/webhook/v1alpha2/component/component_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,86 @@
package component_test

import (
"os"
"testing"

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

crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/crossplane/oam-kubernetes-runtime/apis/core"
)

var scheme = runtime.NewScheme()
var crd crdv1.CustomResourceDefinition
var reqResource metav1.GroupVersionResource
var decoder *admission.Decoder

func TestMetrics(t *testing.T) {
func TestComponentWebHandler(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Metrics Suite")
RunSpecs(t, "Component Web handler")
}

var _ = BeforeSuite(func(done Done) {
By("Bootstrapping test environment")
err := clientgoscheme.AddToScheme(scheme)
ctrl.SetLogger(zap.New(func(o *zap.Options) {
o.Development = true
o.DestWritter = os.Stdout
}))
By("Setup scheme")
err := core.AddToScheme(scheme)
Expect(err).Should(BeNil())
err = core.AddToScheme(scheme)
err = clientgoscheme.AddToScheme(scheme)
Expect(err).Should(BeNil())
err = crdv1.AddToScheme(scheme)
// the crd we will refer to
crd = crdv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "foo.example.com",
Labels: map[string]string{"crd": "dependency"},
},
Spec: crdv1.CustomResourceDefinitionSpec{
Group: "example.com",
Names: crdv1.CustomResourceDefinitionNames{
Kind: "Foo",
ListKind: "FooList",
Plural: "foo",
Singular: "foo",
},
Versions: []crdv1.CustomResourceDefinitionVersion{
{
Name: "v1",
Served: true,
Storage: true,
Schema: &crdv1.CustomResourceValidation{
OpenAPIV3Schema: &crdv1.JSONSchemaProps{
Type: "object",
Properties: map[string]crdv1.JSONSchemaProps{
"status": {
Type: "object",
Properties: map[string]crdv1.JSONSchemaProps{
"key": {Type: "string"},
},
},
},
},
},
},
},
Scope: crdv1.NamespaceScoped,
},
}
By("Prepare for the admission resource")
reqResource = metav1.GroupVersionResource{Group: "core.oam.dev", Version: "v1alpha2", Resource: "components"}
By("Prepare for the admission decoder")
decoder, err = admission.NewDecoder(scheme)
Expect(err).Should(BeNil())
By("Finished test bootstrap")
close(done)
})
Loading

0 comments on commit 36d9a48

Please sign in to comment.