// AddParameter adds new custom parameter to the Template. It overrides // the existing parameter, if already defined. func AddParameter(t *api.Template, param api.Parameter) { if existing := GetParameterByName(t, param.Name); existing != nil { *existing = param } else { t.Parameters = append(t.Parameters, param) } }
func TestEvaluateLabels(t *testing.T) { testCases := map[string]struct { Input string Output string Labels map[string]string }{ "no labels": { Input: `{ "kind":"Template", "apiVersion":"v1", "objects": [ { "kind": "Service", "apiVersion": "v1beta3", "metadata": {"labels": {"key1": "v1", "key2": "v2"} } } ] }`, Output: `{ "kind":"Template","apiVersion":"v1beta3","metadata":{"creationTimestamp":null}, "objects":[ { "apiVersion":"v1beta3","kind":"Service","metadata":{ "labels":{"key1":"v1","key2":"v2"}} } ] }`, }, "one different label": { Input: `{ "kind":"Template", "apiVersion":"v1", "objects": [ { "kind": "Service", "apiVersion": "v1beta3", "metadata": {"labels": {"key1": "v1", "key2": "v2"} } } ] }`, Output: `{ "kind":"Template","apiVersion":"v1beta3","metadata":{"creationTimestamp":null}, "objects":[ { "apiVersion":"v1beta3","kind":"Service","metadata":{ "labels":{"key1":"v1","key2":"v2","key3":"v3"}} } ], "labels":{"key3":"v3"} }`, Labels: map[string]string{"key3": "v3"}, }, "when the root object has labels and no metadata": { Input: `{ "kind":"Template", "apiVersion":"v1", "objects": [ { "kind": "Service", "apiVersion": "v1beta1", "labels": { "key1": "v1", "key2": "v2" } } ] }`, Output: `{ "kind":"Template","apiVersion":"v1beta3","metadata":{"creationTimestamp":null}, "objects":[ { "apiVersion":"v1beta1","kind":"Service", "labels":{"key1":"v1","key2":"v2","key3":"v3"} } ], "labels":{"key3":"v3"} }`, Labels: map[string]string{"key3": "v3"}, }, "when the root object has labels and metadata": { Input: `{ "kind":"Template", "apiVersion":"v1", "objects": [ { "kind": "Service", "apiVersion": "v1beta1", "metadata": {}, "labels": { "key1": "v1", "key2": "v2" } } ] }`, Output: `{ "kind":"Template","apiVersion":"v1beta3","metadata":{"creationTimestamp":null}, "objects":[ { "apiVersion":"v1beta1","kind":"Service", "labels":{"key1":"v1","key2":"v2"}, "metadata":{"labels":{"key3":"v3"}} } ], "labels":{"key3":"v3"} }`, Labels: map[string]string{"key3": "v3"}, }, "overwrites label": { Input: `{ "kind":"Template", "apiVersion":"v1", "objects": [ { "kind": "Service", "apiVersion": "v1beta3", "metadata": {"labels": {"key1": "v1", "key2": "v2"} } } ] }`, Output: `{ "kind":"Template","apiVersion":"v1beta3","metadata":{"creationTimestamp":null}, "objects":[ { "apiVersion":"v1beta3","kind":"Service","metadata":{ "labels":{"key1":"v1","key2":"v3"}} } ], "labels":{"key2":"v3"} }`, Labels: map[string]string{"key2": "v3"}, }, } for k, testCase := range testCases { var template api.Template if err := latest.Codec.DecodeInto([]byte(testCase.Input), &template); err != nil { t.Errorf("%s: unexpected error: %v", k, err) continue } generators := map[string]generator.Generator{ "expression": generator.NewExpressionValueGenerator(rand.New(rand.NewSource(1337))), } processor := NewProcessor(generators) template.ObjectLabels = testCase.Labels // Transform the template config into the result config errs := processor.Process(&template) if len(errs) > 0 { t.Errorf("%s: unexpected error: %v", k, errs) continue } result, err := v1beta3.Codec.Encode(&template) if err != nil { t.Errorf("%s: unexpected error: %v", k, err) continue } expect := testCase.Output expect = trailingWhitespace.ReplaceAllString(expect, "") if expect != string(result) { t.Errorf("%s: unexpected output: %s", k, util.StringDiff(expect, string(result))) continue } } }