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...
This commit is contained in:
2024-04-11 09:35:25 +02:00
parent c41fa22c59
commit ab15614076
3 changed files with 122 additions and 1 deletions

57
generator/basic_test.go Normal file
View File

@@ -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)
}
}

View File

@@ -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")

64
generator/tools_test.go Normal file
View File

@@ -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
}