func TestProcessTemplateParameters(t *testing.T) { var template, expectedTemplate api.Template jsonData, _ := ioutil.ReadFile("../../test/templates/fixtures/guestbook.json") if err := latest.Codec.DecodeInto(jsonData, &template); err != nil { t.Fatalf("unexpected error: %v", err) } expectedData, _ := ioutil.ReadFile("../../test/templates/fixtures/guestbook_list.json") if err := latest.Codec.DecodeInto(expectedData, &expectedTemplate); err != nil { t.Fatalf("unexpected error: %v", err) } generators := map[string]generator.Generator{ "expression": generator.NewExpressionValueGenerator(rand.New(rand.NewSource(1337))), } processor := NewProcessor(generators) // Define custom parameter for the transformation: AddParameter(&template, makeParameter("CUSTOM_PARAM1", "1", "")) // Transform the template config into the result config errs := processor.Process(&template) if len(errs) > 0 { t.Fatalf("unexpected error: %v", errs) } result, err := v1beta3.Codec.Encode(&template) if err != nil { t.Fatalf("unexpected error during encoding Config: %#v", err) } exp, _ := v1beta3.Codec.Encode(&expectedTemplate) if string(result) != string(exp) { t.Errorf("unexpected output: %s", util.StringDiff(string(exp), string(result))) } }
// Create processes a Template and creates a new list of objects func (s *REST) Create(ctx kapi.Context, obj runtime.Object) (runtime.Object, error) { tpl, ok := obj.(*api.Template) if !ok { return nil, errors.NewBadRequest("not a template") } if errs := templatevalidation.ValidateProcessedTemplate(tpl); len(errs) > 0 { return nil, errors.NewInvalid("template", tpl.Name, errs) } generators := map[string]generator.Generator{ "expression": generator.NewExpressionValueGenerator(rand.New(rand.NewSource(time.Now().UnixNano()))), } processor := template.NewProcessor(generators) if errs := processor.Process(tpl); len(errs) > 0 { glog.V(1).Infof(utilerr.NewAggregate(errs).Error()) return nil, errors.NewInvalid("template", tpl.Name, errs) } return tpl, nil }
func TestProcessValueEscape(t *testing.T) { var template api.Template if err := latest.Codec.DecodeInto([]byte(`{ "kind":"Template", "apiVersion":"v1", "objects": [ { "kind": "Service", "apiVersion": "v1beta3${VALUE}", "metadata": { "labels": { "key1": "${VALUE}", "key2": "$${VALUE}" } } } ] }`), &template); err != nil { t.Fatalf("unexpected error: %v", err) } generators := map[string]generator.Generator{ "expression": generator.NewExpressionValueGenerator(rand.New(rand.NewSource(1337))), } processor := NewProcessor(generators) // Define custom parameter for the transformation: AddParameter(&template, makeParameter("VALUE", "1", "")) // Transform the template config into the result config errs := processor.Process(&template) if len(errs) > 0 { t.Fatalf("unexpected error: %v", errs) } result, err := v1beta3.Codec.Encode(&template) if err != nil { t.Fatalf("unexpected error during encoding Config: %#v", err) } expect := `{"kind":"Template","apiVersion":"v1beta3","metadata":{"creationTimestamp":null},"objects":[{"apiVersion":"v1beta31","kind":"Service","metadata":{"labels":{"key1":"1","key2":"$1"}}}],"parameters":[{"name":"VALUE","value":"1"}]}` if expect != string(result) { t.Errorf("unexpected output: %s", util.StringDiff(expect, string(result))) } }
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 } } }