package main import ( "context" "fmt" "os" "path/filepath" "strings" "testing" "testing/synctest" "time" "github.com/fsnotify/fsnotify" ) func startConfigWatcher(t *testing.T, file string, config *Config) { t.Helper() watcher, err := fsnotify.NewWatcher() if err != nil { t.Fatal(err) } if err := watcher.Add(filepath.Dir(file)); err != nil { watcher.Close() t.Fatal(err) } ctx, cancel := context.WithCancel(t.Context()) done := make(chan struct{}) go func() { defer close(done) watchConfig(ctx, watcher, config) }() t.Cleanup(func() { cancel() <-done watcher.Close() }) } func waitForConfigHost(t *testing.T, config *Config, host string) { t.Helper() deadline := time.Now().Add(5 * time.Second) for time.Now().Before(deadline) { if config.current.Load().Host == host { return } time.Sleep(10 * time.Millisecond) } t.Fatalf("host=%q; want %q", config.current.Load().Host, host) } func TestWatchConfigFileUpdates(t *testing.T) { for _, replacement := range []bool{false, true} { t.Run(fmt.Sprintf("replacement=%t", replacement), func(t *testing.T) { file := writeConfig(t, "old.example.com") config := Config{configPath: file, templateDir: "templates"} if err := config.Reload(); err != nil { t.Fatal(err) } startConfigWatcher(t, file, &config) destination := file if replacement { destination = filepath.Join(filepath.Dir(file), "replacement") } if err := os.WriteFile(destination, []byte("host: new.example.com\n"), 0600); err != nil { t.Fatal(err) } if replacement { if err := os.Rename(destination, file); err != nil { t.Fatal(err) } } waitForConfigHost(t, &config, "new.example.com") }) } } func TestWatchConfigMapSymlinkSwap(t *testing.T) { dir := t.TempDir() for _, version := range []string{"old", "new"} { versionDir := filepath.Join(dir, ".."+version) if err := os.Mkdir(versionDir, 0700); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(versionDir, "config.yaml"), []byte("host: "+version+".example.com\n"), 0600); err != nil { t.Fatal(err) } } if err := os.Symlink("..old", filepath.Join(dir, "..data")); err != nil { t.Fatal(err) } file := filepath.Join(dir, "config.yaml") if err := os.Symlink("..data/config.yaml", file); err != nil { t.Fatal(err) } config := Config{configPath: file, templateDir: "templates"} if err := config.Reload(); err != nil { t.Fatal(err) } startConfigWatcher(t, file, &config) if err := os.Symlink("..new", filepath.Join(dir, "..data_tmp")); err != nil { t.Fatal(err) } if err := os.Rename(filepath.Join(dir, "..data_tmp"), filepath.Join(dir, "..data")); err != nil { t.Fatal(err) } waitForConfigHost(t, &config, "new.example.com") } func TestWatchConfigDebouncesAndKeepsLastGoodSnapshot(t *testing.T) { synctest.Test(t, func(t *testing.T) { file := writeConfig(t, "old.example.com") config := Config{configPath: file, templateDir: "templates"} if err := config.Reload(); err != nil { t.Fatal(err) } before := config.current.Load() watcher := &fsnotify.Watcher{Events: make(chan fsnotify.Event), Errors: make(chan error)} ctx, cancel := context.WithCancel(t.Context()) defer cancel() go watchConfig(ctx, watcher, &config) if err := os.WriteFile(file, []byte("host: ["), 0600); err != nil { t.Fatal(err) } // An unrelated filename still schedules a reload of the whole configuration. event := fsnotify.Event{Name: filepath.Join(filepath.Dir(file), "other"), Op: fsnotify.Write} watcher.Events <- event time.Sleep(100 * time.Millisecond) synctest.Wait() if config.current.Load() != before { t.Fatal("invalid reload replaced live snapshot") } if err := os.WriteFile(file, []byte("host: new.example.com\n"), 0600); err != nil { t.Fatal(err) } for range 3 { watcher.Events <- event time.Sleep(50 * time.Millisecond) synctest.Wait() if config.current.Load() != before { t.Fatal("reloaded before event burst settled") } } time.Sleep(50 * time.Millisecond) synctest.Wait() after := config.current.Load() if after.Host != "new.example.com" { t.Fatalf("host=%q", after.Host) } time.Sleep(time.Second) synctest.Wait() if config.current.Load() != after { t.Fatal("event burst caused additional reloads") } }) } func TestWatchSeparateTemplateDirectory(t *testing.T) { file := writeConfig(t, "go.example.com") config := Config{configPath: file, templateDir: copyTemplates(t)} if err := config.Reload(); err != nil { t.Fatal(err) } watcher, err := fsnotify.NewWatcher() if err != nil { t.Fatal(err) } defer watcher.Close() if err := watcher.Add(config.templateDir); err != nil { t.Fatal(err) } ctx, cancel := context.WithCancel(t.Context()) done := make(chan struct{}) go func() { defer close(done) watchConfig(ctx, watcher, &config) }() defer func() { cancel(); <-done }() before := config.current.Load() if err := os.WriteFile(filepath.Join(config.templateDir, "mod.html"), []byte("custom module"), 0600); err != nil { t.Fatal(err) } deadline := time.Now().Add(5 * time.Second) for config.current.Load() == before && time.Now().Before(deadline) { time.Sleep(10 * time.Millisecond) } if config.current.Load() == before { t.Fatal("template update did not reload snapshot") } var body strings.Builder if err := config.current.Load().Template.ExecuteTemplate(&body, "mod.html", nil); err != nil { t.Fatal(err) } if body.String() != "custom module" { t.Fatalf("template=%q", body.String()) } }