From 8f660dc24e3ddb9a81c39a2115c8f22a990d1daf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20K=C3=A4ldstr=C3=B6m?= Date: Wed, 1 Feb 2017 21:03:32 +0200 Subject: [PATCH] Add a ParseTemplate util function for parsing go text templates easily --- cmd/kubeadm/app/util/BUILD | 2 + cmd/kubeadm/app/util/template.go | 37 +++++++++++ cmd/kubeadm/app/util/template_test.go | 90 +++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 cmd/kubeadm/app/util/template.go create mode 100644 cmd/kubeadm/app/util/template_test.go diff --git a/cmd/kubeadm/app/util/BUILD b/cmd/kubeadm/app/util/BUILD index 062590cdc18..8e369653048 100644 --- a/cmd/kubeadm/app/util/BUILD +++ b/cmd/kubeadm/app/util/BUILD @@ -12,6 +12,7 @@ go_library( name = "go_default_library", srcs = [ "error.go", + "template.go", "tokens.go", "version.go", ], @@ -32,6 +33,7 @@ go_test( name = "go_default_test", srcs = [ "error_test.go", + "template_test.go", "tokens_test.go", "version_test.go", ], diff --git a/cmd/kubeadm/app/util/template.go b/cmd/kubeadm/app/util/template.go new file mode 100644 index 00000000000..7e1e8702c5f --- /dev/null +++ b/cmd/kubeadm/app/util/template.go @@ -0,0 +1,37 @@ +/* +Copyright 2016 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package util + +import ( + "bytes" + "fmt" + "text/template" +) + +// TODO: Should be unit-tested +func ParseTemplate(strtmpl string, obj interface{}) ([]byte, error) { + var buf bytes.Buffer + tmpl, err := template.New("template").Parse(strtmpl) + if err != nil { + return nil, fmt.Errorf("error when parsing template: %v", err) + } + err = tmpl.Execute(&buf, obj) + if err != nil { + return nil, fmt.Errorf("error when executing template: %v", err) + } + return buf.Bytes(), nil +} diff --git a/cmd/kubeadm/app/util/template_test.go b/cmd/kubeadm/app/util/template_test.go new file mode 100644 index 00000000000..ba65b7421d3 --- /dev/null +++ b/cmd/kubeadm/app/util/template_test.go @@ -0,0 +1,90 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package util + +import ( + "testing" +) + +const ( + validTmpl = "image: {{ .ImageRepository }}/pause-{{ .Arch }}:3.0" + validTmplOut = "image: gcr.io/google_containers/pause-amd64:3.0" + doNothing = "image: gcr.io/google_containers/pause-amd64:3.0" + invalidTmpl1 = "{{ .baz }/d}" + invalidTmpl2 = "{{ !foobar }}" +) + +func TestParseTemplate(t *testing.T) { + var tmplTests = []struct { + template string + data interface{} + output string + errExpected bool + }{ + // should parse a valid template and set the right values + { + template: validTmpl, + data: struct{ ImageRepository, Arch string }{ + ImageRepository: "gcr.io/google_containers", + Arch: "amd64", + }, + output: validTmplOut, + errExpected: false, + }, + // should noop if there aren't any {{ .foo }} present + { + template: doNothing, + data: struct{ ImageRepository, Arch string }{ + ImageRepository: "gcr.io/google_containers", + Arch: "amd64", + }, + output: doNothing, + errExpected: false, + }, + // invalid syntax, passing nil + { + template: invalidTmpl1, + data: nil, + output: "", + errExpected: true, + }, + // invalid syntax + { + template: invalidTmpl2, + data: struct{}{}, + output: "", + errExpected: true, + }, + } + for _, tt := range tmplTests { + outbytes, err := ParseTemplate(tt.template, tt.data) + if tt.errExpected != (err != nil) { + t.Errorf( + "failed TestParseTemplate:\n\texpected err: %s\n\t actual: %s", + tt.errExpected, + err, + ) + } + if tt.output != string(outbytes) { + t.Errorf( + "failed TestParseTemplate:\n\texpected bytes: %s\n\t actual: %s", + tt.output, + outbytes, + ) + } + } +}