Add command to render a Go template with config and state as data context

Signed-off-by: Denis Luchkin-Zhou <wyvernzora@gmail.com>
This commit is contained in:
Denis Luchkin-Zhou
2023-10-10 12:04:41 -07:00
parent 01e0a0048c
commit 234bb4b36e
4 changed files with 134 additions and 2 deletions

View File

@@ -0,0 +1,38 @@
package action
import (
"fmt"
agentConfig "github.com/kairos-io/kairos-agent/v2/pkg/config"
"github.com/kairos-io/kairos-sdk/collector"
"github.com/kairos-io/kairos-sdk/state"
"gopkg.in/yaml.v3"
"os"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("RenderTemplate action test", func() {
It("renders the template with config and state", func() {
config := agentConfig.NewConfig()
config.Config = collector.Config{
"testKey": "testValue",
}
runtime, err := state.NewRuntime()
fmt.Println(os.Getwd())
result, err := RenderTemplate("../../tests/fixtures/template/test.yaml", config, runtime)
Expect(err).ToNot(HaveOccurred())
Expect(result).ToNot(BeNil())
Expect(len(result)).ToNot(BeZero())
var data map[string]string
err = yaml.Unmarshal(result, &data)
Expect(err).ToNot(HaveOccurred())
Expect(data).To(HaveKeyWithValue("configTest", "TESTVALUE"))
Expect(data["stateTest"]).To(MatchRegexp("^[0-9a-f]{8}$"))
})
})