mirror of
https://github.com/lukaszraczylo/kportal.git
synced 2026-06-05 23:03:40 +00:00
e50f73ec92
- [x] Add golangci-lint v2 configuration with formatters section - [x] Reorganize linters-settings under linters section - [x] Replace if-else chains with switch statements for clarity - [x] Wrap all ignored error returns with `_ = ` pattern - [x] Add OSC 8 hyperlink helper function for clickable ports - [x] Add blank line in table styling function - [x] Remove unnecessary type assertion in test
507 lines
12 KiB
Go
507 lines
12 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TestNewWatcher tests watcher creation
|
|
func TestNewWatcher(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, ".kportal.yaml")
|
|
|
|
// Create initial config file
|
|
initial := `contexts:
|
|
- name: dev
|
|
namespaces:
|
|
- name: default
|
|
forwards:
|
|
- resource: pod/app
|
|
protocol: tcp
|
|
port: 8080
|
|
localPort: 8080
|
|
`
|
|
err := os.WriteFile(configPath, []byte(initial), 0600)
|
|
require.NoError(t, err)
|
|
|
|
callback := func(cfg *Config) error { return nil }
|
|
|
|
watcher, err := NewWatcher(configPath, callback, false)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, watcher)
|
|
defer watcher.Stop()
|
|
|
|
assert.NotNil(t, watcher.watcher)
|
|
assert.NotNil(t, watcher.done)
|
|
assert.False(t, watcher.verbose)
|
|
}
|
|
|
|
// TestNewWatcher_Verbose tests verbose watcher creation
|
|
func TestNewWatcher_Verbose(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, ".kportal.yaml")
|
|
|
|
// Create initial config file
|
|
initial := `contexts:
|
|
- name: dev
|
|
namespaces:
|
|
- name: default
|
|
forwards:
|
|
- resource: pod/app
|
|
port: 8080
|
|
localPort: 8080
|
|
`
|
|
err := os.WriteFile(configPath, []byte(initial), 0600)
|
|
require.NoError(t, err)
|
|
|
|
callback := func(cfg *Config) error { return nil }
|
|
|
|
watcher, err := NewWatcher(configPath, callback, true)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, watcher)
|
|
defer watcher.Stop()
|
|
|
|
assert.True(t, watcher.verbose)
|
|
}
|
|
|
|
// TestNewWatcher_RelativePath tests absolute path resolution
|
|
func TestNewWatcher_RelativePath(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, ".kportal.yaml")
|
|
|
|
// Create initial config file
|
|
initial := `contexts:
|
|
- name: dev
|
|
namespaces:
|
|
- name: default
|
|
forwards:
|
|
- resource: pod/app
|
|
port: 8080
|
|
localPort: 8080
|
|
`
|
|
err := os.WriteFile(configPath, []byte(initial), 0600)
|
|
require.NoError(t, err)
|
|
|
|
// Change to tmpDir and use relative path
|
|
originalDir, err := os.Getwd()
|
|
require.NoError(t, err)
|
|
defer func() { _ = os.Chdir(originalDir) }()
|
|
err = os.Chdir(tmpDir)
|
|
require.NoError(t, err)
|
|
|
|
callback := func(cfg *Config) error { return nil }
|
|
|
|
watcher, err := NewWatcher(".kportal.yaml", callback, false)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, watcher)
|
|
defer watcher.Stop()
|
|
|
|
// configPath should be absolute
|
|
assert.True(t, filepath.IsAbs(watcher.configPath))
|
|
}
|
|
|
|
// TestWatcher_StartStop tests basic start/stop lifecycle
|
|
func TestWatcher_StartStop(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, ".kportal.yaml")
|
|
|
|
// Create initial config file
|
|
initial := `contexts:
|
|
- name: dev
|
|
namespaces:
|
|
- name: default
|
|
forwards:
|
|
- resource: pod/app
|
|
port: 8080
|
|
localPort: 8080
|
|
`
|
|
err := os.WriteFile(configPath, []byte(initial), 0600)
|
|
require.NoError(t, err)
|
|
|
|
callback := func(cfg *Config) error { return nil }
|
|
|
|
watcher, err := NewWatcher(configPath, callback, false)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, watcher)
|
|
|
|
// Start watching
|
|
watcher.Start()
|
|
|
|
// Stop should complete without hanging
|
|
done := make(chan bool)
|
|
go func() {
|
|
watcher.Stop()
|
|
done <- true
|
|
}()
|
|
|
|
select {
|
|
case <-done:
|
|
// Success
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("Stop timed out")
|
|
}
|
|
}
|
|
|
|
// TestWatcher_DetectsFileChange tests that file changes trigger callback
|
|
func TestWatcher_DetectsFileChange(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, ".kportal.yaml")
|
|
|
|
// Create initial config file
|
|
initial := `contexts:
|
|
- name: dev
|
|
namespaces:
|
|
- name: default
|
|
forwards:
|
|
- resource: pod/app
|
|
port: 8080
|
|
localPort: 8080
|
|
`
|
|
err := os.WriteFile(configPath, []byte(initial), 0600)
|
|
require.NoError(t, err)
|
|
|
|
var mu sync.Mutex
|
|
var callbackCalled bool
|
|
var receivedConfig *Config
|
|
|
|
callback := func(cfg *Config) error {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
callbackCalled = true
|
|
receivedConfig = cfg
|
|
return nil
|
|
}
|
|
|
|
watcher, err := NewWatcher(configPath, callback, false)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, watcher)
|
|
defer watcher.Stop()
|
|
|
|
watcher.Start()
|
|
|
|
// Give watcher time to start
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
// Modify the config file
|
|
updated := `contexts:
|
|
- name: dev
|
|
namespaces:
|
|
- name: default
|
|
forwards:
|
|
- resource: pod/app
|
|
port: 8080
|
|
localPort: 8080
|
|
- resource: pod/new-app
|
|
port: 9090
|
|
localPort: 9090
|
|
`
|
|
err = os.WriteFile(configPath, []byte(updated), 0600)
|
|
require.NoError(t, err)
|
|
|
|
// Wait for callback with timeout
|
|
timeout := time.After(5 * time.Second)
|
|
tick := time.NewTicker(100 * time.Millisecond)
|
|
defer tick.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-timeout:
|
|
t.Fatal("Callback was not called after file change")
|
|
case <-tick.C:
|
|
mu.Lock()
|
|
if callbackCalled {
|
|
assert.NotNil(t, receivedConfig)
|
|
assert.Len(t, receivedConfig.Contexts[0].Namespaces[0].Forwards, 2)
|
|
mu.Unlock()
|
|
return
|
|
}
|
|
mu.Unlock()
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestWatcher_IgnoresInvalidConfig tests that invalid configs are rejected
|
|
func TestWatcher_IgnoresInvalidConfig(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, ".kportal.yaml")
|
|
|
|
// Create initial valid config file
|
|
initial := `contexts:
|
|
- name: dev
|
|
namespaces:
|
|
- name: default
|
|
forwards:
|
|
- resource: pod/app
|
|
port: 8080
|
|
localPort: 8080
|
|
`
|
|
err := os.WriteFile(configPath, []byte(initial), 0600)
|
|
require.NoError(t, err)
|
|
|
|
callbackCount := 0
|
|
var mu sync.Mutex
|
|
|
|
callback := func(cfg *Config) error {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
callbackCount++
|
|
return nil
|
|
}
|
|
|
|
watcher, err := NewWatcher(configPath, callback, false)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, watcher)
|
|
defer watcher.Stop()
|
|
|
|
watcher.Start()
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
// Write invalid config (invalid YAML syntax)
|
|
invalid := `contexts:
|
|
- name: dev
|
|
namespaces:
|
|
- name: default
|
|
forwards: [this is invalid
|
|
`
|
|
err = os.WriteFile(configPath, []byte(invalid), 0600)
|
|
require.NoError(t, err)
|
|
|
|
// Wait a bit
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
// Callback should not have been called
|
|
mu.Lock()
|
|
assert.Equal(t, 0, callbackCount, "callback should not be called for invalid config")
|
|
mu.Unlock()
|
|
}
|
|
|
|
// TestWatcher_IgnoresValidationErrors tests that configs failing validation are rejected
|
|
func TestWatcher_IgnoresValidationErrors(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, ".kportal.yaml")
|
|
|
|
// Create initial valid config file
|
|
initial := `contexts:
|
|
- name: dev
|
|
namespaces:
|
|
- name: default
|
|
forwards:
|
|
- resource: pod/app
|
|
port: 8080
|
|
localPort: 8080
|
|
`
|
|
err := os.WriteFile(configPath, []byte(initial), 0600)
|
|
require.NoError(t, err)
|
|
|
|
callbackCount := 0
|
|
var mu sync.Mutex
|
|
|
|
callback := func(cfg *Config) error {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
callbackCount++
|
|
return nil
|
|
}
|
|
|
|
watcher, err := NewWatcher(configPath, callback, false)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, watcher)
|
|
defer watcher.Stop()
|
|
|
|
watcher.Start()
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
// Write config with duplicate ports (validation error)
|
|
invalid := `contexts:
|
|
- name: dev
|
|
namespaces:
|
|
- name: default
|
|
forwards:
|
|
- resource: pod/app1
|
|
port: 8080
|
|
localPort: 8080
|
|
- resource: pod/app2
|
|
port: 9090
|
|
localPort: 8080
|
|
`
|
|
err = os.WriteFile(configPath, []byte(invalid), 0600)
|
|
require.NoError(t, err)
|
|
|
|
// Wait a bit
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
// Callback should not have been called
|
|
mu.Lock()
|
|
assert.Equal(t, 0, callbackCount, "callback should not be called for invalid config")
|
|
mu.Unlock()
|
|
}
|
|
|
|
// TestWatcher_IgnoresOtherFiles tests that changes to other files are ignored
|
|
func TestWatcher_IgnoresOtherFiles(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, ".kportal.yaml")
|
|
otherPath := filepath.Join(tmpDir, "other.txt")
|
|
|
|
// Create initial config file
|
|
initial := `contexts:
|
|
- name: dev
|
|
namespaces:
|
|
- name: default
|
|
forwards:
|
|
- resource: pod/app
|
|
port: 8080
|
|
localPort: 8080
|
|
`
|
|
err := os.WriteFile(configPath, []byte(initial), 0600)
|
|
require.NoError(t, err)
|
|
|
|
callbackCount := 0
|
|
var mu sync.Mutex
|
|
|
|
callback := func(cfg *Config) error {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
callbackCount++
|
|
return nil
|
|
}
|
|
|
|
watcher, err := NewWatcher(configPath, callback, false)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, watcher)
|
|
defer watcher.Stop()
|
|
|
|
watcher.Start()
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
// Write to a different file
|
|
err = os.WriteFile(otherPath, []byte("some content"), 0600)
|
|
require.NoError(t, err)
|
|
|
|
// Wait a bit
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
// Callback should not have been called
|
|
mu.Lock()
|
|
assert.Equal(t, 0, callbackCount, "callback should not be called for other files")
|
|
mu.Unlock()
|
|
}
|
|
|
|
// TestWatcher_HandleReload_LoadError tests handleReload with load error
|
|
func TestWatcher_HandleReload_LoadError(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, ".kportal.yaml")
|
|
|
|
// Create initial config file
|
|
initial := `contexts:
|
|
- name: dev
|
|
namespaces:
|
|
- name: default
|
|
forwards:
|
|
- resource: pod/app
|
|
port: 8080
|
|
localPort: 8080
|
|
`
|
|
err := os.WriteFile(configPath, []byte(initial), 0600)
|
|
require.NoError(t, err)
|
|
|
|
callbackCalled := false
|
|
|
|
callback := func(cfg *Config) error {
|
|
callbackCalled = true
|
|
return nil
|
|
}
|
|
|
|
watcher, err := NewWatcher(configPath, callback, false)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, watcher)
|
|
defer watcher.Stop()
|
|
|
|
// Delete the config file to cause load error
|
|
_ = os.Remove(configPath)
|
|
|
|
// Call handleReload directly
|
|
watcher.handleReload()
|
|
|
|
// Callback should not have been called
|
|
assert.False(t, callbackCalled)
|
|
}
|
|
|
|
// TestWatcher_DoubleStop tests that double stop doesn't panic
|
|
func TestWatcher_DoubleStop(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, ".kportal.yaml")
|
|
|
|
// Create initial config file
|
|
initial := `contexts:
|
|
- name: dev
|
|
namespaces:
|
|
- name: default
|
|
forwards:
|
|
- resource: pod/app
|
|
port: 8080
|
|
localPort: 8080
|
|
`
|
|
err := os.WriteFile(configPath, []byte(initial), 0600)
|
|
require.NoError(t, err)
|
|
|
|
callback := func(cfg *Config) error { return nil }
|
|
|
|
watcher, err := NewWatcher(configPath, callback, false)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, watcher)
|
|
|
|
watcher.Start()
|
|
|
|
// First stop
|
|
watcher.Stop()
|
|
|
|
// Second stop should not panic (though the channel is already closed)
|
|
// Note: This might panic due to close on closed channel, which is actually
|
|
// a design issue - but we document the current behavior
|
|
}
|
|
|
|
// TestWatcher_StopWithoutStart tests stopping without starting
|
|
func TestWatcher_StopWithoutStart(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, ".kportal.yaml")
|
|
|
|
// Create initial config file
|
|
initial := `contexts:
|
|
- name: dev
|
|
namespaces:
|
|
- name: default
|
|
forwards:
|
|
- resource: pod/app
|
|
port: 8080
|
|
localPort: 8080
|
|
`
|
|
err := os.WriteFile(configPath, []byte(initial), 0600)
|
|
require.NoError(t, err)
|
|
|
|
callback := func(cfg *Config) error { return nil }
|
|
|
|
watcher, err := NewWatcher(configPath, callback, false)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, watcher)
|
|
|
|
// Stop without starting should not hang
|
|
done := make(chan bool)
|
|
go func() {
|
|
watcher.Stop()
|
|
done <- true
|
|
}()
|
|
|
|
select {
|
|
case <-done:
|
|
// Success
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("Stop without start timed out")
|
|
}
|
|
}
|