91 lines
2.8 KiB
Go
91 lines
2.8 KiB
Go
package demos
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"go.yaml.in/yaml/v2"
|
|
|
|
"github.com/omnex/control-plane/api/internal/config"
|
|
)
|
|
|
|
func ptr[T any](v T) *T { return &v }
|
|
|
|
func TestVitrineValuesRenderWithCharts(t *testing.T) {
|
|
if _, err := exec.LookPath("helm"); err != nil {
|
|
t.Skip("helm absent du PATH")
|
|
}
|
|
chartsDir, err := filepath.Abs("../../../../deploy/chart-vitrine")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
h := &HelmProvisioner{cfg: &config.Config{
|
|
VitrineBackendImage: "registry.example/vitrine-backend:1.2.3",
|
|
VitrineFrontendImage: "registry.example/vitrine-frontend:1.2.3",
|
|
}}
|
|
p := ProjectSpec{
|
|
Namespace: "vitrine-ab12cd34",
|
|
Host: "vitrine-ab12cd34.vitrine-omnex.club",
|
|
URL: "https://vitrine-ab12cd34.vitrine-omnex.club",
|
|
AdminUsername: "admin",
|
|
AdminPassword: "un-mot-de-passe-solide",
|
|
AdminNumber: 3,
|
|
}
|
|
|
|
cases := []struct {
|
|
chart string
|
|
values map[string]interface{}
|
|
wants []string
|
|
}{
|
|
{"postgresql", h.buildVitrinePostgresValues("pgpass"), []string{"name: postgres\n"}},
|
|
{"redis", h.buildVitrineRedisValues("redispass"), []string{"name: redis\n"}},
|
|
{"backend", h.buildVitrineBackendValues(p, "pgpass", "redispass", "jwt"), []string{
|
|
"name: " + vitrineBackendService(p.Namespace) + "\n",
|
|
`ADMIN_NUMBER: "3"`,
|
|
`SEED_ADMIN_USERNAME: "admin"`,
|
|
"registry.example/vitrine-backend:1.2.3",
|
|
"post-install", // seedJob activé
|
|
"postgres://postgres:pgpass@postgres:5432/vitrine_db",
|
|
}},
|
|
{"frontend", h.buildVitrineFrontendValues(), []string{
|
|
"name: " + vitrineFrontendService(p.Namespace) + "\n",
|
|
"registry.example/vitrine-frontend:1.2.3",
|
|
}},
|
|
{"ingressroute", h.buildVitrineIngressRouteValues(p), []string{
|
|
"Host(`vitrine-ab12cd34.vitrine-omnex.club`)",
|
|
"name: " + vitrineBackendService(p.Namespace) + "\n",
|
|
"name: " + vitrineFrontendService(p.Namespace) + "\n",
|
|
"tls: {}", // certificat servi par le TLSStore, pas par projet
|
|
}},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.chart, func(t *testing.T) {
|
|
raw, err := yaml.Marshal(c.values)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
valuesFile := filepath.Join(t.TempDir(), "values.yaml")
|
|
if err := writeFile(valuesFile, raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Release et namespace comme installChartFrom : "<ns>-<chart>".
|
|
out, err := exec.Command("helm", "template", p.Namespace+"-"+c.chart,
|
|
filepath.Join(chartsDir, c.chart), "--namespace", p.Namespace, "--values", valuesFile).CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("helm template: %v\n%s", err, out)
|
|
}
|
|
for _, want := range c.wants {
|
|
if !strings.Contains(string(out), want) {
|
|
t.Errorf("rendu de %s : %q introuvable", c.chart, want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func writeFile(path string, data []byte) error { return os.WriteFile(path, data, 0o600) }
|