Skip to content

Commit

Permalink
add cloudinit.Generate test
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-sakamoto committed Jun 15, 2024
1 parent 1f82219 commit a679f4e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
14 changes: 7 additions & 7 deletions pkg/cloudinit/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cloudinit

import (
"bytes"
"fmt"
"text/template"

"github.com/goccy/go-yaml"
Expand All @@ -26,17 +25,18 @@ func Generate(c Config, vars map[string]string) (string, error) {
return "", err
}

temp, err := template.New("cloud-config").Parse(buff.String())
temp, err := template.New("cloud-config").Parse(`#cloud-config
package_update: true
` + buff.String())
if err != nil {
return "", err
}

if err := temp.Execute(buff, vars); err != nil {
output := &bytes.Buffer{}
if err := temp.Execute(output, vars); err != nil {
return "", err
}

return fmt.Sprintf(`#cloud-config
package_update: true
%s`, buff.String()), nil
return output.String(), nil
}
51 changes: 51 additions & 0 deletions pkg/cloudinit/generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cloudinit_test

import (
"testing"

"github.com/ryota-sakamoto/kubernetes-on-multipass/pkg/cloudinit"
"github.com/stretchr/testify/assert"
)

func TestGenerate(t *testing.T) {
result, err := cloudinit.Generate(cloudinit.Config{
Packages: []string{
"git",
"vim",
},
WriteFiles: []cloudinit.WriteFile{
{
Content: `file {{ .Content }}`,
Path: "/opt/ok",
},
{
Content: `echo ok`,
Path: "/use/bin/ok",
Permissions: "0755",
},
},
RunCmds: []string{
"/use/bin/ok",
},
}, map[string]string{
"Content": "content",
})
assert.NoError(t, err)

expectedResult := `#cloud-config
package_update: true
packages:
- git
- vim
write_files:
- path: /opt/ok
content: file content
- path: /use/bin/ok
content: echo ok
permissions: "0755"
runcmd:
- /use/bin/ok
`
assert.Equal(t, expectedResult, result)
}

0 comments on commit a679f4e

Please sign in to comment.