From ab1561407603cce2753a8311e04a7559aa69d7eb Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Thu, 11 Apr 2024 09:35:25 +0200 Subject: [PATCH] Use "helm" filtype first for vim modeline + tests "helm" can be managed by vim/neovim plugins, so it's a good idea to add it as default, then use "gotmpl.yaml". Add basic tests... --- generator/basic_test.go | 57 ++++++++++++++++++++++++++++++++++++ generator/converter.go | 2 +- generator/tools_test.go | 64 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 generator/basic_test.go create mode 100644 generator/tools_test.go diff --git a/generator/basic_test.go b/generator/basic_test.go new file mode 100644 index 0000000..1c649a8 --- /dev/null +++ b/generator/basic_test.go @@ -0,0 +1,57 @@ +package generator + +import ( + "log" + "os" + "testing" + + "sigs.k8s.io/yaml" +) + +func setup(content string) string { + // write the _compose_file in temporary directory + tmpDir, err := os.MkdirTemp("", "katenary") + if err != nil { + panic(err) + } + os.WriteFile(tmpDir+"/compose.yml", []byte(content), 0o644) + return tmpDir +} + +func teardown(tmpDir string) { + // remove the temporary directory + log.Println("Removing temporary directory: ", tmpDir) + if err := os.RemoveAll(tmpDir); err != nil { + panic(err) + } +} + +func TestGenerate(t *testing.T) { + _compose_file := ` +services: + web: + image: nginx:1.29 +` + tmpDir := setup(_compose_file) + defer teardown(tmpDir) + + currentDir, _ := os.Getwd() + os.Chdir(tmpDir) + defer os.Chdir(currentDir) + + output := _compile_test(t) + + dt := DeploymentTest{} + if err := yaml.Unmarshal([]byte(output), &dt); err != nil { + t.Errorf("Failed to unmarshal the output: %s", err) + } + + if dt.Spec.Replicas != 1 { + t.Errorf("Expected replicas to be 1, got %d", dt.Spec.Replicas) + t.Errorf("Output: %s", output) + } + + if dt.Spec.Template.Spec.Containers[0].Image != "nginx:1.29" { + t.Errorf("Expected image to be nginx:1.29, got %s", dt.Spec.Template.Spec.Containers[0].Image) + } +} diff --git a/generator/converter.go b/generator/converter.go index ade8b20..d7eb267 100644 --- a/generator/converter.go +++ b/generator/converter.go @@ -317,7 +317,7 @@ func addStorageClassHelp(values []byte) []byte { // addModeline adds a modeline to the values.yaml file to make sure that vim // will use the correct syntax highlighting. func addModeline(values []byte) []byte { - modeline := "# vi" + "m: ft=gotmpl.yaml" + modeline := "# vi" + "m: ft=helm.gotmpl.yaml" // if the values ends by `{{- end }}` we need to add the modeline before lines := strings.Split(string(values), "\n") diff --git a/generator/tools_test.go b/generator/tools_test.go new file mode 100644 index 0000000..02b0f25 --- /dev/null +++ b/generator/tools_test.go @@ -0,0 +1,64 @@ +package generator + +import ( + "os/exec" + "testing" + + "katenary/parser" +) + +type DeploymentTest struct { + Spec struct { + Replicas int `yaml:"replicas"` + Template struct { + Spec struct { + Containers []struct { + Image string `yaml:"image"` + } `yaml:"containers"` + } `yaml:"spec"` + } `yaml:"template"` + } `yaml:"spec"` +} + +func _compile_test(t *testing.T) string { + _, err := parser.Parse(nil, "compose.yml") + if err != nil { + t.Errorf("Failed to parse the project: %s", err) + } + + force := false + outputDir := "./chart" + profiles := make([]string, 0) + helmdepUpdate := false + var appVersion *string + chartVersion := "0.1.0" + convertOptions := ConvertOptions{ + Force: force, + OutputDir: outputDir, + Profiles: profiles, + HelmUpdate: helmdepUpdate, + AppVersion: appVersion, + ChartVersion: chartVersion, + } + Convert(convertOptions, "compose.yml") + // launch helm lint to check the generated chart + if helmLint(convertOptions) != nil { + t.Errorf("Failed to lint the generated chart") + } + // try with helm template + var output string + if output, err = helmTemplate(convertOptions); err != nil { + t.Errorf("Failed to template the generated chart") + t.Errorf("Output: %s", output) + } + return output +} + +func helmTemplate(options ConvertOptions) (string, error) { + cmd := exec.Command("helm", "template", options.OutputDir) + output, err := cmd.CombinedOutput() + if err != nil { + return string(output), err + } + return string(output), nil +}