43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package nginxgateway
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
func TestSwapManagedSiteRestoresPreviousOnFailure(t *testing.T) {
|
|
for _, failAt := range []string{"validate", "reload", "persist"} {
|
|
t.Run(failAt, func(t *testing.T) {
|
|
site := "old"
|
|
validations, reloads := 0, 0
|
|
write := func(content string) error { site = content; return nil }
|
|
validate := func() error {
|
|
validations++
|
|
if failAt == "validate" && site == "new" {
|
|
return errors.New("invalid nginx config")
|
|
}
|
|
return nil
|
|
}
|
|
reload := func() error {
|
|
reloads++
|
|
if failAt == "reload" && site == "new" {
|
|
return errors.New("reload failed")
|
|
}
|
|
return nil
|
|
}
|
|
persist := func() error {
|
|
if failAt == "persist" {
|
|
return errors.New("save failed")
|
|
}
|
|
return nil
|
|
}
|
|
if err := swapManagedSite("old", "new", persist, write, validate, reload); err == nil {
|
|
t.Fatal("expected failure")
|
|
}
|
|
if site != "old" || validations < 2 || reloads < 1 {
|
|
t.Fatalf("old site not restored: site=%s validations=%d reloads=%d", site, validations, reloads)
|
|
}
|
|
})
|
|
}
|
|
}
|