From c97124a30f9033cf581cbc40cb975a330df331a9 Mon Sep 17 00:00:00 2001 From: huiwq1990 Date: Wed, 6 Jul 2022 15:42:06 +0800 Subject: [PATCH] Feat: add policy parser test (#4174) Signed-off-by: huiwq1990 --- pkg/appfile/parser_test.go | 32 ++++++++++++++++++++++++++++++++ pkg/oam/util/test_utils.go | 13 +++++++++++++ 2 files changed, 45 insertions(+) diff --git a/pkg/appfile/parser_test.go b/pkg/appfile/parser_test.go index c26a59a49..1c1eb8a14 100644 --- a/pkg/appfile/parser_test.go +++ b/pkg/appfile/parser_test.go @@ -204,6 +204,32 @@ spec: cmd?: [...string] }` +const policyDefinition = ` +# Code generated by KubeVela templates. DO NOT EDIT. Please edit the original cue file. +# Definition source cue file: vela-templates/definitions/internal/topology.cue +apiVersion: core.oam.dev/v1beta1 +kind: PolicyDefinition +metadata: + annotations: + definition.oam.dev/description: Determining the destination where components should be deployed to. + name: topology + namespace: {{ include "systemDefinitionNamespace" . }} +spec: + schematic: + cue: + template: | + parameter: { + // +usage=Specify the names of the clusters to select. + cluster?: [...string] + // +usage=Specify the label selector for clusters + clusterLabelSelector?: [string]: string + // +usage=Deprecated: Use clusterLabelSelector instead. + clusterSelector?: [string]: string + // +usage=Specify the target namespace to deploy in the selected clusters, default inherit the original namespace. + namespace?: string + } +` + const appfileYaml = ` apiVersion: core.oam.dev/v1beta1 kind: Application @@ -268,6 +294,12 @@ var _ = Describe("Test application parser", func() { return err } *o = *td + case *v1beta1.PolicyDefinition: + ppd, err := util.UnMarshalStringToPolicyDefinition(policyDefinition) + if err != nil { + return err + } + *o = *ppd } return nil }, diff --git a/pkg/oam/util/test_utils.go b/pkg/oam/util/test_utils.go index 5a0b40187..5f92fef3d 100644 --- a/pkg/oam/util/test_utils.go +++ b/pkg/oam/util/test_utils.go @@ -190,6 +190,19 @@ func UnMarshalStringToTraitDefinition(s string) (*v1beta1.TraitDefinition, error return obj, nil } +// UnMarshalStringToPolicyDefinition parse a string to a policyDefinition object +func UnMarshalStringToPolicyDefinition(s string) (*v1beta1.PolicyDefinition, error) { + obj := &v1beta1.PolicyDefinition{} + _body, err := yaml.YAMLToJSON([]byte(s)) + if err != nil { + return nil, err + } + if err := json.Unmarshal(_body, obj); err != nil { + return nil, err + } + return obj, nil +} + // CheckAppRevision check if appRevision list is right func CheckAppRevision(revs []v1beta1.ApplicationRevision, collection []int) (bool, error) { if len(revs) != len(collection) {