Skip to content

Commit

Permalink
feat(tests): add template render (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
skyoct authored Sep 22, 2022
1 parent 51f0a7e commit 9e31d2d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/util/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package util

import (
"bytes"
"text/template"
)

func RenderTemplate(temp string, data map[string]string) (string, error) {
tpl, err := template.New("temp").Parse(temp)
if err != nil {
return "", err
}

buf := bytes.NewBuffer([]byte{})
err = tpl.Execute(buf, data)
if err != nil {
return "", err
}
return buf.String(), nil
}
33 changes: 33 additions & 0 deletions tests/util/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package util

import "testing"

func TestRenderTemplate(t *testing.T) {

oldTempl := `
apiVersion: oss.laf.dev/v1
kind: Bucket
metadata:
name: {{ .name }}
namespace: {{ .namespace }}
spec:
policy: readonly
storage: 100Mi
status:
capacity:
maxStorage: 100Mi
objectCount: 0
storage: '0'
policy: readonly
user: app1
versioning: true
`
newTempl, err := RenderTemplate(oldTempl, map[string]string{
"name": "app1",
"namespace": "default",
})
if err != nil {
t.Fatal(err)
}
t.Log(newTempl)
}

0 comments on commit 9e31d2d

Please sign in to comment.