1// Copyright 2017 Istio Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package e2e 16 17import ( 18 "testing" 19 20 "istio.io/api/mixer/adapter/model/v1beta1" 21 spyadapter "istio.io/istio/mixer/test/spyAdapter" 22 e2eTmpl "istio.io/istio/mixer/test/spyAdapter/template" 23 apaTmpl "istio.io/istio/mixer/test/spyAdapter/template/apa" 24 reportTmpl "istio.io/istio/mixer/test/spyAdapter/template/report" 25) 26 27const ( 28 apaGlobalCfg = ` 29apiVersion: "config.istio.io/v1alpha2" 30kind: attributemanifest 31metadata: 32 name: istio-proxy 33 namespace: default 34spec: 35 attributes: 36 source.name: 37 value_type: STRING 38 target.name: 39 value_type: STRING 40 response.count: 41 value_type: INT64 42 attr.bool: 43 value_type: BOOL 44 attr.string: 45 value_type: STRING 46 attr.double: 47 value_type: DOUBLE 48 attr.int64: 49 value_type: INT64 50 generated.string: 51 value_type: STRING 52 generated.int64: 53 value_type: INT64 54 generated.bool: 55 value_type: BOOL 56 generated.double: 57 value_type: DOUBLE 58 generated.strmap: 59 value_type: STRING_MAP 60--- 61` 62 apaSvcCfg = ` 63apiVersion: "config.istio.io/v1alpha2" 64kind: fakeHandler 65metadata: 66 name: fakeHandlerConfig 67 namespace: istio-system 68 69--- 70# YAML for apa 71apiVersion: "config.istio.io/v1alpha2" 72kind: instance 73metadata: 74 name: apaInstance 75 namespace: istio-system 76spec: 77 compiledTemplate: sampleapa 78 params: 79 int64Primitive: "2" 80 boolPrimitive: "true" 81 doublePrimitive: "2.2" 82 stringPrimitive: "\"mysrc\"" 83 attribute_bindings: 84 generated.string: $out.stringPrimitive 85 generated.bool: $out.boolPrimitive 86 generated.double: $out.doublePrimitive 87 generated.int64: $out.int64Primitive 88 generated.strmap: $out.stringMap 89--- 90 91apiVersion: "config.istio.io/v1alpha2" 92kind: rule 93metadata: 94 name: rule2 95 namespace: istio-system 96spec: 97 match: match(target.name, "*") 98 actions: 99 - handler: fakeHandlerConfig.fakeHandler 100 instances: 101 - apaInstance 102 103--- 104# YAML for report that depend on APA output 105apiVersion: "config.istio.io/v1alpha2" 106kind: samplereport 107metadata: 108 name: reportInstance 109 namespace: istio-system 110spec: 111 value: "2" 112 dimensions: 113 genStr: generated.string 114 genBool: generated.bool 115 genDouble: generated.double 116 genInt64: generated.int64 117 genStrMapEntry: generated.strmap["k1"] 118 119--- 120 121apiVersion: "config.istio.io/v1alpha2" 122kind: rule 123metadata: 124 name: rule1 125 namespace: istio-system 126spec: 127 match: match(target.name, "*") 128 actions: 129 - handler: fakeHandlerConfig.fakeHandler 130 instances: 131 - reportInstance.samplereport 132 133--- 134` 135) 136 137func TestApa(t *testing.T) { 138 139 out := apaTmpl.NewOutput() 140 out.SetStringPrimitive("gen-str") 141 out.SetInt64Primitive(int64(1000)) 142 out.SetDoublePrimitive(float64(1000.1000)) 143 out.SetBoolPrimitive(true) 144 out.SetStringMap(map[string]string{"k1": "v1"}) 145 146 tests := []testData{ 147 { 148 name: "Apa", 149 behaviors: []spyadapter.AdapterBehavior{ 150 { 151 Name: "fakeHandler", 152 Handler: spyadapter.HandlerBehavior{ 153 GenerateSampleApaOutput: out, 154 }, 155 }, 156 }, 157 attrs: map[string]interface{}{"target.name": "somesrvcname"}, 158 expectCalls: []spyadapter.CapturedCall{ 159 { 160 Name: "HandleSampleApaAttributes", 161 Instances: []interface{}{ 162 &apaTmpl.Instance{ 163 Name: "apaInstance.instance.istio-system", 164 BoolPrimitive: true, 165 DoublePrimitive: float64(2.2), 166 Int64Primitive: int64(2), 167 StringPrimitive: "mysrc", 168 }, 169 }, 170 }, 171 172 { 173 Name: "HandleSampleReport", 174 Instances: []interface{}{ 175 &reportTmpl.Instance{ 176 Name: "reportInstance.samplereport.istio-system", 177 Value: int64(2), 178 Dimensions: map[string]interface{}{ 179 "genStr": "gen-str", 180 "genBool": true, 181 "genDouble": float64(1000.1), 182 "genInt64": int64(1000), 183 "genStrMapEntry": "v1", 184 }, 185 }, 186 }, 187 }, 188 }, 189 }, 190 } 191 for _, tt := range tests { 192 // Set the defaults for the test. 193 if tt.cfg == "" { 194 tt.cfg = apaSvcCfg 195 } 196 197 if tt.templates == nil { 198 tt.templates = e2eTmpl.SupportedTmplInfo 199 } 200 201 t.Run(tt.name, func(t *testing.T) { 202 tt.run(t, v1beta1.TEMPLATE_VARIETY_REPORT, apaGlobalCfg) 203 }) 204 } 205} 206