Add missing flags (--local-storage-shared-path, --debug, --pprof-server, --portainer-edge-id, --portainer-edge-key, --portainer-edge-async) so all 10 documented KubeSolo parameters can be configured via cloud-init YAML. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
187 lines
5.0 KiB
Go
187 lines
5.0 KiB
Go
package cloudinit
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestBuildExtraFlags(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
cfg Config
|
|
want string
|
|
}{
|
|
{
|
|
name: "empty",
|
|
cfg: Config{},
|
|
want: "",
|
|
},
|
|
{
|
|
name: "extra flags only",
|
|
cfg: Config{
|
|
KubeSolo: KubeSoloConfig{ExtraFlags: "--disable traefik"},
|
|
},
|
|
want: "--disable traefik",
|
|
},
|
|
{
|
|
name: "extra sans only",
|
|
cfg: Config{
|
|
KubeSolo: KubeSoloConfig{
|
|
ExtraSANs: []string{"node.local", "192.168.1.100"},
|
|
},
|
|
},
|
|
want: "--apiserver-extra-sans node.local --apiserver-extra-sans 192.168.1.100",
|
|
},
|
|
{
|
|
name: "flags and sans",
|
|
cfg: Config{
|
|
KubeSolo: KubeSoloConfig{
|
|
ExtraFlags: "--disable servicelb",
|
|
ExtraSANs: []string{"edge.local"},
|
|
},
|
|
},
|
|
want: "--disable servicelb --apiserver-extra-sans edge.local",
|
|
},
|
|
{
|
|
name: "debug flag",
|
|
cfg: Config{
|
|
KubeSolo: KubeSoloConfig{Debug: true},
|
|
},
|
|
want: "--debug",
|
|
},
|
|
{
|
|
name: "pprof-server flag",
|
|
cfg: Config{
|
|
KubeSolo: KubeSoloConfig{PprofServer: true},
|
|
},
|
|
want: "--pprof-server",
|
|
},
|
|
{
|
|
name: "local-storage-shared-path",
|
|
cfg: Config{
|
|
KubeSolo: KubeSoloConfig{LocalStorageSharedPath: "/mnt/shared"},
|
|
},
|
|
want: "--local-storage-shared-path /mnt/shared",
|
|
},
|
|
{
|
|
name: "portainer edge flags",
|
|
cfg: Config{
|
|
KubeSolo: KubeSoloConfig{
|
|
PortainerEdgeID: "test-id-123",
|
|
PortainerEdgeKey: "test-key-456",
|
|
PortainerEdgeAsync: true,
|
|
},
|
|
},
|
|
want: "--portainer-edge-id test-id-123 --portainer-edge-key test-key-456 --portainer-edge-async",
|
|
},
|
|
{
|
|
name: "all new flags",
|
|
cfg: Config{
|
|
KubeSolo: KubeSoloConfig{
|
|
ExtraFlags: "--disable traefik",
|
|
ExtraSANs: []string{"node.local"},
|
|
LocalStorageSharedPath: "/mnt/data/shared",
|
|
Debug: true,
|
|
PprofServer: true,
|
|
PortainerEdgeID: "eid",
|
|
PortainerEdgeKey: "ekey",
|
|
PortainerEdgeAsync: true,
|
|
},
|
|
},
|
|
want: "--disable traefik --apiserver-extra-sans node.local --local-storage-shared-path /mnt/data/shared --debug --pprof-server --portainer-edge-id eid --portainer-edge-key ekey --portainer-edge-async",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := buildExtraFlags(&tt.cfg)
|
|
if got != tt.want {
|
|
t.Errorf("buildExtraFlags() = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyKubeSolo(t *testing.T) {
|
|
dir := t.TempDir()
|
|
tr := true
|
|
cfg := &Config{
|
|
KubeSolo: KubeSoloConfig{
|
|
ExtraFlags: "--disable traefik",
|
|
LocalStorage: &tr,
|
|
ExtraSANs: []string{"test.local"},
|
|
LocalStorageSharedPath: "/mnt/shared",
|
|
Debug: true,
|
|
PortainerEdgeID: "eid",
|
|
PortainerEdgeKey: "ekey",
|
|
PortainerEdgeAsync: true,
|
|
},
|
|
}
|
|
|
|
if err := ApplyKubeSolo(cfg, dir); err != nil {
|
|
t.Fatalf("ApplyKubeSolo error: %v", err)
|
|
}
|
|
|
|
// Check extra-flags file
|
|
flagsData, err := os.ReadFile(filepath.Join(dir, "extra-flags"))
|
|
if err != nil {
|
|
t.Fatalf("reading extra-flags: %v", err)
|
|
}
|
|
flags := strings.TrimSpace(string(flagsData))
|
|
if !strings.Contains(flags, "--disable traefik") {
|
|
t.Errorf("extra-flags missing '--disable traefik': %q", flags)
|
|
}
|
|
if !strings.Contains(flags, "--apiserver-extra-sans test.local") {
|
|
t.Errorf("extra-flags missing SANs: %q", flags)
|
|
}
|
|
if !strings.Contains(flags, "--local-storage-shared-path /mnt/shared") {
|
|
t.Errorf("extra-flags missing local-storage-shared-path: %q", flags)
|
|
}
|
|
if !strings.Contains(flags, "--debug") {
|
|
t.Errorf("extra-flags missing --debug: %q", flags)
|
|
}
|
|
if !strings.Contains(flags, "--portainer-edge-id eid") {
|
|
t.Errorf("extra-flags missing --portainer-edge-id: %q", flags)
|
|
}
|
|
if !strings.Contains(flags, "--portainer-edge-key ekey") {
|
|
t.Errorf("extra-flags missing --portainer-edge-key: %q", flags)
|
|
}
|
|
if !strings.Contains(flags, "--portainer-edge-async") {
|
|
t.Errorf("extra-flags missing --portainer-edge-async: %q", flags)
|
|
}
|
|
|
|
// Check config.yaml
|
|
configData, err := os.ReadFile(filepath.Join(dir, "config.yaml"))
|
|
if err != nil {
|
|
t.Fatalf("reading config.yaml: %v", err)
|
|
}
|
|
config := string(configData)
|
|
if !strings.Contains(config, "local-storage: true") {
|
|
t.Errorf("config.yaml missing local-storage: %q", config)
|
|
}
|
|
if !strings.Contains(config, "data-dir: /var/lib/kubesolo") {
|
|
t.Errorf("config.yaml missing data-dir: %q", config)
|
|
}
|
|
}
|
|
|
|
func TestApplyKubeSoloNoFlags(t *testing.T) {
|
|
dir := t.TempDir()
|
|
cfg := &Config{}
|
|
|
|
if err := ApplyKubeSolo(cfg, dir); err != nil {
|
|
t.Fatalf("ApplyKubeSolo error: %v", err)
|
|
}
|
|
|
|
// extra-flags should not exist when empty
|
|
if _, err := os.Stat(filepath.Join(dir, "extra-flags")); !os.IsNotExist(err) {
|
|
t.Error("extra-flags file should not exist when no flags configured")
|
|
}
|
|
|
|
// config.yaml should still be created with defaults
|
|
if _, err := os.Stat(filepath.Join(dir, "config.yaml")); err != nil {
|
|
t.Error("config.yaml should be created even with empty config")
|
|
}
|
|
}
|