Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RenderTemplate using the text/template package, not the html one #416

Merged
merged 3 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion workflow/template_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"text/template"

"github.com/docker/distribution/reference"
"github.com/pkg/errors"
Expand Down
77 changes: 66 additions & 11 deletions workflow/template_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"testing"

"github.com/google/uuid"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -233,17 +233,37 @@ func TestValidateTemplate(t *testing.T) {

func TestRenderTemplate(t *testing.T) {
tests := []struct {
name string
hwAddress []byte
expectedError func(t *testing.T, err error)
name string
hwAddress []byte
templateID string
templateData string
skip string
expectedError func(t *testing.T, err error)
expectedTemplate string
}{
{
name: "valid-hardware-address",
hwAddress: []byte("{\"device_1\":\"08:00:27:00:00:01\"}"),
name: "valid-hardware-address",
hwAddress: []byte("{\"device_1\":\"08:00:27:00:00:01\"}"),
templateID: "49748301-d0d9-4ee9-84df-b64e6e1ef3dd",
templateData: validTemplate,
expectedTemplate: `
version: "0.1"
name: hello_world_workflow
global_timeout: 600
tasks:
- name: "hello world"
worker: "08:00:27:00:00:01"
actions:
- name: "hello_world"
image: hello-world
timeout: 60
`,
},
{
name: "invalid-hardware-address",
hwAddress: []byte("{\"invalid_device\":\"08:00:27:00:00:01\"}"),
name: "invalid-hardware-address",
templateData: validTemplate,
skip: "Not sure if this is an expected error or not but this test is not well written",
gauravgahlot marked this conversation as resolved.
Show resolved Hide resolved
hwAddress: []byte("{\"invalid_device\":\"08:00:27:00:00:01\"}"),
expectedError: func(t *testing.T, err error) {
if err == nil {
t.Error("expected error, got nil")
Expand All @@ -253,14 +273,49 @@ func TestRenderTemplate(t *testing.T) {
}
},
},
{
name: "template with << should not be escaped in any way",
hwAddress: []byte("{\"device_1\":\"08:00:27:00:00:01\"}"),
templateID: "98788301-d0d9-4ee9-84df-b64e6e1ef1cc",
templateData: `
version: "0.1"
name: hello_world_workflow
global_timeout: 600
tasks:
- name: "hello world<<"
worker: "{{.device_1}}"
actions:
- name: "hello_world"
image: hello-world
timeout: 60
`,
expectedTemplate: `
version: "0.1"
name: hello_world_workflow
global_timeout: 600
tasks:
- name: "hello world<<"
worker: "08:00:27:00:00:01"
actions:
- name: "hello_world"
image: hello-world
timeout: 60
`,
},
}

templateID := uuid.New().String()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := RenderTemplate(templateID, validTemplate, test.hwAddress)
if err != nil {
if test.skip != "" {
gauravgahlot marked this conversation as resolved.
Show resolved Hide resolved
t.Skip(test.skip)
}
temp, err := RenderTemplate(test.templateID, test.templateData, test.hwAddress)
if test.expectedError != nil {
test.expectedError(t, err)
return
}
if diff := cmp.Diff(test.expectedTemplate, temp); diff != "" {
t.Error(diff)
}
})
}
Expand Down