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

Commit

Permalink
Add unit-test
Browse files Browse the repository at this point in the history
Signed-off-by: zzxwill <[email protected]>
  • Loading branch information
zzxwill committed Sep 18, 2020
1 parent 8183ea4 commit 5f60904
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ bin/
tmp/

# Vscode files
.vscode
.vscode

# Webhook certificates
csr.conf
oam-kubernetes-runtime-webhook.csr
oam-kubernetes-runtime-webhook.key
oam-kubernetes-runtime-webhook.pem
4 changes: 4 additions & 0 deletions pkg/oam/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import (
runtimev1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
)

// DefinitionAnnotation is one automatically generated annotation of workload and trait to indicate which
// workloadDefinition/traitDefinition it is generated by
const DefinitionAnnotation = "definition.oam.dev/name"

// ScopeKind contains the type metadata for a kind of an OAM scope resource.
type ScopeKind schema.GroupVersionKind

Expand Down
5 changes: 1 addition & 4 deletions pkg/oam/util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ var (
const (
// TraitPrefixKey is prefix of trait name
TraitPrefixKey = "trait"
// DefinitionAnnotation is one automatically generated annotation of workload and trait to indicate which
//workloadDefinition/traitDefinition it is generated by
DefinitionAnnotation = "definition.oam.dev/name"
)

const (
Expand Down Expand Up @@ -232,7 +229,7 @@ func PassLabelAndAnnotation(parentObj oam.Object, childObj labelAnnotationObject
// Now the CRD name of a resource could also be defined as `definition.oam.dev/name` in `metadata.annotations`
func GetCRDName(u *unstructured.Unstructured) string {
if annotations := u.GetAnnotations(); annotations != nil {
if crdName, ok := annotations[DefinitionAnnotation]; ok {
if crdName, ok := annotations[oam.DefinitionAnnotation]; ok {
return crdName
}
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/oam/util/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,19 @@ var _ = Describe("Test unstructured related helper utils", func() {
}},
exp: "simplerollouttraits.extend.oam.dev",
},
"extended resource with annotation": {
u: &unstructured.Unstructured{Object: map[string]interface{}{
"apiVersion": "extend.oam.dev/v1alpha2",
"kind": "ContainerizedWorkload",
"metadata": map[string]interface{}{
"annotations": map[string]interface{}{
"definition.oam.dev/name": "web-service",
},
},
},
},
exp: "web-service",
},
}
for name, ti := range tests {
got := util.GetCRDName(ti.u)
Expand Down
3 changes: 2 additions & 1 deletion pkg/webhook/v1alpha2/component/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package component_test
import (
"context"
"fmt"
"github.com/crossplane/oam-kubernetes-runtime/pkg/oam"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -121,7 +122,7 @@ var _ = Describe("Component Admission controller Test", func() {
mutatedWorkload := baseWorkload.DeepCopy()
mutatedWorkload.SetNamespace(component.GetNamespace())
mutatedWorkload.SetLabels(util.MergeMap(label, map[string]string{WorkloadTypeLabel: workloadTypeName}))
mutatedWorkload.SetAnnotations(map[string]string{util.DefinitionAnnotation: ""})
mutatedWorkload.SetAnnotations(map[string]string{oam.DefinitionAnnotation: ""})
tests := map[string]struct {
client client.Client
workload interface{}
Expand Down
3 changes: 2 additions & 1 deletion pkg/webhook/v1alpha2/component/mutating_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/crossplane/oam-kubernetes-runtime/pkg/oam"
"net/http"

crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -126,7 +127,7 @@ func (h *MutatingHandler) Mutate(obj *v1alpha2.Component) error {
workload.SetNamespace(obj.GetNamespace())
workload.SetLabels(util.MergeMap(obj.GetLabels(), map[string]string{WorkloadTypeLabel: workloadType}))
// Add another annotation DefinitionAnnotation which can mark the name of WorkloadDefinition
workload.SetAnnotations(util.MergeMap(obj.GetAnnotations(), map[string]string{util.DefinitionAnnotation: workloadType}))
workload.SetAnnotations(util.MergeMap(obj.GetAnnotations(), map[string]string{oam.DefinitionAnnotation: workloadType}))
mutatelog.Info("Set annotation definition.oam.dev/name for workload", "annotation value", workloadType)
// copy back the object
rawBye, err := json.Marshal(workload.Object)
Expand Down

0 comments on commit 5f60904

Please sign in to comment.