- Replace gopkg.in/yaml.v3 with github.com/BurntSushi/toml - Update struct tags from yaml to toml - Update config tests to use TOML syntax - Add testdata/config.toml, update Helm configmap template
134 lines
3.1 KiB
Go
134 lines
3.1 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoad_MissingFile(t *testing.T) {
|
|
_, err := Load("/nonexistent/config.toml")
|
|
if err == nil {
|
|
t.Fatal("expected error for missing file")
|
|
}
|
|
}
|
|
|
|
func TestLoad_MissingGitToken(t *testing.T) {
|
|
cfg := `[server]
|
|
addr = ":8080"
|
|
|
|
[git]
|
|
type = "gitea"
|
|
base_url = "https://git.example.com"
|
|
|
|
[cluster]
|
|
namespace = "proto-gen"`
|
|
path := writeTempConfig(t, cfg)
|
|
os.Setenv("GIT_TOKEN", "")
|
|
_, err := Load(path)
|
|
if err == nil {
|
|
t.Fatal("expected error for missing git.token")
|
|
}
|
|
}
|
|
|
|
func TestLoad_ValidConfig(t *testing.T) {
|
|
os.Setenv("GIT_TOKEN", "test-token")
|
|
defer os.Unsetenv("GIT_TOKEN")
|
|
|
|
cfg := `[server]
|
|
addr = ":9090"
|
|
|
|
[git]
|
|
type = "gitea"
|
|
base_url = "https://git.example.com"
|
|
token = "${GIT_TOKEN}"
|
|
|
|
[cluster]
|
|
namespace = "proto-gen"
|
|
job_timeout = 300
|
|
max_concurrent = 3
|
|
|
|
[[plugins]]
|
|
language = "go"
|
|
image = "proto-gen-go:v1.0"`
|
|
path := writeTempConfig(t, cfg)
|
|
|
|
got, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got.Server.Addr != ":9090" {
|
|
t.Errorf("Server.Addr = %q, want %q", got.Server.Addr, ":9090")
|
|
}
|
|
if got.Git.Token != "test-token" {
|
|
t.Errorf("Git.Token = %q, want %q", got.Git.Token, "test-token")
|
|
}
|
|
if got.Cluster.JobTimeout != 300 {
|
|
t.Errorf("Cluster.JobTimeout = %d, want 300", got.Cluster.JobTimeout)
|
|
}
|
|
if len(got.Plugins) != 1 {
|
|
t.Fatalf("len(Plugins) = %d, want 1", len(got.Plugins))
|
|
}
|
|
if got.Plugins[0].Language != "go" {
|
|
t.Errorf("Plugins[0].Language = %q, want %q", got.Plugins[0].Language, "go")
|
|
}
|
|
}
|
|
|
|
func TestLoad_DefaultValues(t *testing.T) {
|
|
os.Setenv("GIT_TOKEN", "test-token")
|
|
defer os.Unsetenv("GIT_TOKEN")
|
|
|
|
cfg := `[git]
|
|
type = "gitea"
|
|
base_url = "https://git.example.com"
|
|
token = "${GIT_TOKEN}"
|
|
|
|
[cluster]
|
|
namespace = "proto-gen"`
|
|
path := writeTempConfig(t, cfg)
|
|
|
|
got, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got.Server.Addr != ":8080" {
|
|
t.Errorf("Server.Addr = %q, want %q", got.Server.Addr, ":8080")
|
|
}
|
|
if got.Cluster.JobTimeout != 600 {
|
|
t.Errorf("Cluster.JobTimeout = %d, want 600", got.Cluster.JobTimeout)
|
|
}
|
|
if got.Cluster.MaxConcurrent != 2 {
|
|
t.Errorf("Cluster.MaxConcurrent = %d, want 2", got.Cluster.MaxConcurrent)
|
|
}
|
|
if got.TargetRepoTemplate != "{proto_repo}-gen-{lang}" {
|
|
t.Errorf("TargetRepoTemplate = %q, want %q", got.TargetRepoTemplate, "{proto_repo}-gen-{lang}")
|
|
}
|
|
}
|
|
|
|
func TestGetPluginByLanguage(t *testing.T) {
|
|
cfg := &Config{
|
|
Plugins: []PluginConfig{
|
|
{Language: "go", Image: "go:v1"},
|
|
{Language: "nodejs", Image: "ts:v1"},
|
|
},
|
|
}
|
|
if p := cfg.GetPluginByLanguage("go"); p == nil {
|
|
t.Error("GetPluginByLanguage(go) = nil")
|
|
} else if p.Image != "go:v1" {
|
|
t.Errorf("Image = %q, want %q", p.Image, "go:v1")
|
|
}
|
|
if p := cfg.GetPluginByLanguage("python"); p != nil {
|
|
t.Error("GetPluginByLanguage(python) != nil")
|
|
}
|
|
}
|
|
|
|
func writeTempConfig(t *testing.T, content string) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "config.toml")
|
|
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
|
t.Fatalf("writing temp config: %v", err)
|
|
}
|
|
return path
|
|
}
|