pkg/operator: Add unit tests
@@ -2,6 +2,7 @@ package operator
import (
"fmt"
+ "strings"
"testing"
)
@@ -35,3 +36,69 @@ func TestClusterDNSIP(t *testing.T) {
})
}
}
+
+func TestRenderAsset(t *testing.T) {
+ tests := []struct {
+ Path string
+ RenderConfig *renderConfig
+ FindExpected string
+ Error bool
+ }{{
+ // Simple test
+ Path: "manifests/machineconfigcontroller/clusterrolebinding.yaml",
+ RenderConfig: &renderConfig{
+ TargetNamespace: "testing-namespace",
+ },
+ FindExpected: "namespace: testing-namespace",
+ }, {
+ // Nested field test
+ Path: "manifests/machineconfigcontroller/deployment.yaml",
+ RenderConfig: &renderConfig{
+ TargetNamespace: "testing-namespace",
+ Images: &Images{
+ MachineConfigController: "{MCC: PLACEHOLDER}"},
+ },
+ FindExpected: "image: {MCC: PLACEHOLDER}",
+ }, {
+ // Render same template as previous test
+ // But with a template field missing
+ Path: "manifests/machineconfigcontroller/deployment.yaml",
+ RenderConfig: &renderConfig{
+ TargetNamespace: "testing-namespace",
+ },
+ Error: true,
+ }, {
+ // Bad path, will cause asset error
+ Path: "BAD PATH",
+ Error: true,
+ }}
+
+ for idx, test := range tests {
+ t.Run(fmt.Sprintf("case#%d", idx), func(t *testing.T) {
+ desc := fmt.Sprintf("Path(%#v), RenderConfig(%#v)", test.Path, test.RenderConfig)
+ buf, err := renderAsset(test.RenderConfig, test.Path)
+ // The template lib will throw an err if a template field is missing
+ if err != nil {
+ if !test.Error {
+ t.Fatalf("%s failed: %s", desc, err.Error())
+ } else {
+ return
+ }
+ }
+ if buf == nil || len(buf) == 0 {
+ t.Fatalf("Buffer is empty")
+ }
+ // Verify that the buf can be converted back into a string safely
+ str := fmt.Sprintf("%s", buf)
+ if str == "" || len(str) == 0 {
+ t.Fatalf("Buffer is not a valid string!")
+ }
+ // Verify that any FindExpected values are actually in the string
+ if test.FindExpected != "" {
+ if !strings.Contains(str, test.FindExpected) {
+ t.Fatalf("Rendered template does not contain expected values")
+ }
+ }
+ })
+ }
+}
