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

Commit

Permalink
Change function name and 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 21, 2020
1 parent a6c3e81 commit e172e6d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@ func TestServiceInjector(t *testing.T) {
err error
}

invalideDeployment := &appsv1.StatefulSet{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1alpha1",
}}

cases := map[string]struct {
reason string
args args
Expand All @@ -400,6 +405,21 @@ func TestServiceInjector(t *testing.T) {
},
want: want{},
},
"InvalidObject": {
reason: "invalid object should immediately return nil.",
args: args{
w: &mock.Workload{},
o: []oam.Object{
invalideDeployment,
},
},
want: want{
result: []oam.Object{
invalideDeployment,
},
err: nil,
},
},
"SuccessfulInjectService_1D_1C_1P": {
reason: "A Deployment with a port(s) should have a Service injected for first defined port.",
args: args{
Expand Down
14 changes: 7 additions & 7 deletions pkg/oam/util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func FetchWorkload(ctx context.Context, c client.Client, mLog logr.Logger, oamTr
func FetchScopeDefinition(ctx context.Context, r client.Reader,
scope *unstructured.Unstructured) (*v1alpha2.ScopeDefinition, error) {
// The name of the scopeDefinition CR is the CRD name of the scope
spName := GetCRDName(scope)
spName := GetDefinitionName(scope)
// the scopeDefinition crd is cluster scoped
nn := types.NamespacedName{Name: spName}
// Fetch the corresponding scopeDefinition CR
Expand All @@ -130,7 +130,7 @@ func FetchScopeDefinition(ctx context.Context, r client.Reader,
func FetchTraitDefinition(ctx context.Context, r client.Reader,
trait *unstructured.Unstructured) (*v1alpha2.TraitDefinition, error) {
// The name of the traitDefinition CR is the CRD name of the trait
trName := GetCRDName(trait)
trName := GetDefinitionName(trait)
// the traitDefinition crd is cluster scoped
nn := types.NamespacedName{Name: trName}
// Fetch the corresponding traitDefinition CR
Expand All @@ -145,7 +145,7 @@ func FetchTraitDefinition(ctx context.Context, r client.Reader,
func FetchWorkloadDefinition(ctx context.Context, r client.Reader,
workload *unstructured.Unstructured) (*v1alpha2.WorkloadDefinition, error) {
// The name of the workloadDefinition CR is the CRD name of the component
wldName := GetCRDName(workload)
wldName := GetDefinitionName(workload)
// the workloadDefinition crd is cluster scoped
nn := types.NamespacedName{Name: wldName}
// Fetch the corresponding workloadDefinition CR
Expand Down Expand Up @@ -224,10 +224,10 @@ func PassLabelAndAnnotation(parentObj oam.Object, childObj labelAnnotationObject
childObj.SetAnnotations(MergeMap(parentObj.GetAnnotations(), childObj.GetAnnotations()))
}

// GetCRDName return the CRD name of any resources
// the format of the CRD of a resource is <kind purals>.<group>
// 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 {
// GetDefinitionName return the Definition name of any resources
// the format of the definition of a resource is <kind plurals>.<group>
// Now the definition name of a resource could also be defined as `definition.oam.dev/name` in `metadata.annotations`
func GetDefinitionName(u *unstructured.Unstructured) string {
if annotations := u.GetAnnotations(); annotations != nil {
if crdName, ok := annotations[oam.DefinitionAnnotation]; ok {
return crdName
Expand Down
2 changes: 1 addition & 1 deletion pkg/oam/util/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ var _ = Describe("Test unstructured related helper utils", func() {
},
}
for name, ti := range tests {
got := util.GetCRDName(ti.u)
got := util.GetDefinitionName(ti.u)
By(fmt.Sprint("Running test: ", name))
Expect(ti.exp).Should(Equal(got))
}
Expand Down

0 comments on commit e172e6d

Please sign in to comment.