From 49383cf7b4628430c2b3845b87ed34bf4dab6de6 Mon Sep 17 00:00:00 2001 From: spacewander Date: Wed, 6 Nov 2024 11:45:59 +0800 Subject: [PATCH] more tests Signed-off-by: spacewander --- .../virtualservice_embedded_mode.in.yml | 60 +++++++++++++++++++ .../virtualservice_embedded_mode.out.yml | 39 ++++++++++++ .../internal/translation/translation_test.go | 24 ++++++++ 3 files changed, 123 insertions(+) create mode 100644 controller/internal/translation/testdata/translation/virtualservice_embedded_mode.in.yml create mode 100644 controller/internal/translation/testdata/translation/virtualservice_embedded_mode.out.yml diff --git a/controller/internal/translation/testdata/translation/virtualservice_embedded_mode.in.yml b/controller/internal/translation/testdata/translation/virtualservice_embedded_mode.in.yml new file mode 100644 index 00000000..596f19c9 --- /dev/null +++ b/controller/internal/translation/testdata/translation/virtualservice_embedded_mode.in.yml @@ -0,0 +1,60 @@ +istioGateway: +- apiVersion: networking.istio.io/v1beta1 + kind: Gateway + metadata: + name: httpbin-gateway + namespace: default + spec: + selector: + istio: ingressgateway + servers: + - hosts: + - httpbin.example.com + port: + name: http + number: 80 + protocol: HTTP +virtualService: + httpbin-gateway: + - apiVersion: networking.istio.io/v1beta1 + kind: VirtualService + metadata: + name: httpbin + namespace: default + annotations: + htnn.mosn.io/filterpolicy: | + {"apiVersion":"htnn.mosn.io/v1","kind":"FilterPolicy","metadata":{"name":"policy","namespace":"default"},"spec":{"filters":{"animal":{"config":{"hostName":"fish"}},"localReply":{"config":{"code":404}}}}} + spec: + gateways: + - httpbin-gateway + hosts: + - httpbin.example.com + http: + - match: + - uri: + prefix: /status + name: policy + route: + - destination: + host: httpbin + port: + number: 8000 +filterPolicy: + httpbin: + - apiVersion: htnn.mosn.io/v1 + kind: FilterPolicy + metadata: + name: policy + namespace: default + spec: + targetRef: + group: networking.istio.io + kind: VirtualService + name: httpbin + filters: + animal: + config: + hostName: goldfish + demo: + config: + hostName: micky diff --git a/controller/internal/translation/testdata/translation/virtualservice_embedded_mode.out.yml b/controller/internal/translation/testdata/translation/virtualservice_embedded_mode.out.yml new file mode 100644 index 00000000..31a5aa77 --- /dev/null +++ b/controller/internal/translation/testdata/translation/virtualservice_embedded_mode.out.yml @@ -0,0 +1,39 @@ +- metadata: + annotations: + htnn.mosn.io/info: '{"filterpolicies":["default/embedded-virtualservice-httpbin","default/policy"]}' + creationTimestamp: null + labels: + htnn.mosn.io/created-by: FilterPolicy + name: htnn-h-httpbin.example.com + namespace: default + spec: + configPatches: + - applyTo: HTTP_ROUTE + match: + routeConfiguration: + vhost: + name: httpbin.example.com:80 + route: + name: policy + patch: + operation: MERGE + value: + typed_per_filter_config: + htnn.filters.http.golang: + '@type': type.googleapis.com/envoy.extensions.filters.http.golang.v3alpha.ConfigsPerRoute + plugins_config: + fm: + config: + '@type': type.googleapis.com/xds.type.v3.TypedStruct + value: + plugins: + - config: + hostName: fish + name: animal + - config: + hostName: micky + name: demo + - config: + code: 404 + name: localReply + status: {} diff --git a/controller/internal/translation/translation_test.go b/controller/internal/translation/translation_test.go index 1df60a22..844ce6d1 100644 --- a/controller/internal/translation/translation_test.go +++ b/controller/internal/translation/translation_test.go @@ -16,6 +16,7 @@ package translation import ( "context" + "encoding/json" "maps" "os" "path/filepath" @@ -32,6 +33,7 @@ import ( "mosn.io/htnn/controller/internal/config" "mosn.io/htnn/controller/internal/istio" "mosn.io/htnn/controller/internal/log" + "mosn.io/htnn/controller/pkg/constant" _ "mosn.io/htnn/controller/plugins" // register plugins _ "mosn.io/htnn/controller/registries" // register registries mosniov1 "mosn.io/htnn/types/apis/v1" @@ -213,6 +215,17 @@ func TestTranslate(t *testing.T) { } s.AddPolicyForVirtualService(fp, wrapper.vs, wrapper.gws) } + + ann := wrapper.vs.Annotations + if ann[constant.AnnotationFilterPolicy] != "" { + var policy mosniov1.FilterPolicy + err := json.Unmarshal([]byte(ann[constant.AnnotationFilterPolicy]), &policy) + require.NoError(t, err) + policy.Namespace = wrapper.vs.Namespace + // Name convention is "embedded-$kind-$name" + policy.Name = "embedded-virtualservice-" + wrapper.vs.Name + s.AddPolicyForVirtualService(&policy, wrapper.vs, wrapper.gws) + } } // For gateway-only cases @@ -225,6 +238,17 @@ func TestTranslate(t *testing.T) { } s.AddPolicyForIstioGateway(fp, gw) } + + ann := gw.Annotations + if ann[constant.AnnotationFilterPolicy] != "" { + var policy mosniov1.FilterPolicy + err := json.Unmarshal([]byte(ann[constant.AnnotationFilterPolicy]), &policy) + require.NoError(t, err) + policy.Namespace = gw.Namespace + // Name convention is "embedded-$kind-$name" + policy.Name = "embedded-gateway-" + gw.Name + s.AddPolicyForIstioGateway(&policy, gw) + } } if config.EnableLDSPluginViaECDS() { for _, gw := range input.IstioGateway {