Raise netlink buffers for large batches; restore snapshot when try apply fails
This commit is contained in:
@@ -175,10 +175,28 @@ func Revert(id string) (reverted bool, err error) {
|
||||
if err != nil || p == nil || (id != "" && p.ID != id) {
|
||||
return false, err
|
||||
}
|
||||
if err := restore(p.Snapshot); err != nil {
|
||||
return false, fmt.Errorf("restoring snapshot: %w", err)
|
||||
return true, restorePending(p)
|
||||
}
|
||||
|
||||
// Abort restores the pending snapshot after a failed apply, which may have
|
||||
// committed partially. A failed restore keeps the snapshot and timer so the
|
||||
// timer still reverts. The caller must hold the lock.
|
||||
func Abort() error {
|
||||
p, err := load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return true, Discard()
|
||||
if p == nil {
|
||||
return errors.New("no pending try to abort")
|
||||
}
|
||||
return restorePending(p)
|
||||
}
|
||||
|
||||
func restorePending(p *pending) error {
|
||||
if err := restore(p.Snapshot); err != nil {
|
||||
return fmt.Errorf("restoring snapshot: %w", err)
|
||||
}
|
||||
return Discard()
|
||||
}
|
||||
|
||||
func load() (*pending, error) {
|
||||
|
||||
@@ -209,3 +209,41 @@ func TestRevertStaleIDIgnored(t *testing.T) {
|
||||
t.Errorf("newer try's snapshot removed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbortRestoresAndDisarms(t *testing.T) {
|
||||
cmds := setup(t)
|
||||
restored := stubRestore(t, nil)
|
||||
snap := &nftables.Snapshot{Table: "tomswall", Present: true}
|
||||
arm(t, snap)
|
||||
|
||||
if err := Abort(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(*restored) != 1 || !reflect.DeepEqual((*restored)[0], snap) {
|
||||
t.Errorf("restored %+v, want the armed snapshot", *restored)
|
||||
}
|
||||
if _, err := os.Stat(snapshotPath()); !os.IsNotExist(err) {
|
||||
t.Error("snapshot not removed")
|
||||
}
|
||||
if last := (*cmds)[len(*cmds)-1]; last != "systemctl stop "+Unit+".timer" {
|
||||
t.Errorf("timer not stopped, last command %q", last)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbortFailureKeepsSnapshotAndTimer(t *testing.T) {
|
||||
cmds := setup(t)
|
||||
boom := errors.New("netlink down")
|
||||
stubRestore(t, boom)
|
||||
arm(t, &nftables.Snapshot{Table: "tomswall"})
|
||||
armed := len(*cmds)
|
||||
|
||||
if err := Abort(); !errors.Is(err, boom) {
|
||||
t.Fatalf("Abort error = %v, want %v", err, boom)
|
||||
}
|
||||
if _, err := os.Stat(snapshotPath()); err != nil {
|
||||
t.Fatalf("snapshot gone after failed abort: %v", err)
|
||||
}
|
||||
if len(*cmds) != armed {
|
||||
t.Errorf("revert timer touched after failed abort: %v", (*cmds)[armed:])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user