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 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanzhang-oss committed Sep 10, 2020
1 parent 0096ecd commit 3c01d27
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 128 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
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.Stderr
}))
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 3c01d27

Please sign in to comment.