f-79: agent: section times, renvoi de system_powerdown, attentes #79

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-25 12:42:10 +01:00
commit 8e89865418
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
13 changed files with 602 additions and 80 deletions

View file

@ -1,6 +1,7 @@
package vm
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
@ -47,13 +48,7 @@ func StopVM(db *badger.DB, name string, cfg *configuration.Config) error {
if _, err := os.Stat(socketPath); err == nil {
// socket présent : tenter l'arrêt gracieux
if _, err := qmp.Send(socketPath, []string{`{"execute":"system_powerdown"}`}); err == nil {
waitQMPDead(socketPath,
time.Duration(cfg.Dispatcher.TimeoutSeconds)*time.Second,
time.Duration(cfg.Dispatcher.PollSeconds)*time.Second,
)
}
// connexion QMP échouée : QEMU déjà mort
shutdownQEMU(qmp.Send, socketPath, cfg.Times.VMsStop(), cfg.Times.VMsShutdown(), cfg.Times.Poll())
}
// socket absent ou QEMU déjà arrêté : cleanup direct
@ -99,15 +94,31 @@ func removeDHCPFiles(d vmData, name string, backend dhcpbackend.Backend) error {
return nil
}
func waitQMPDead(socketPath string, timeout, poll time.Duration) {
timer := time.After(timeout)
type qmpSender func(socketPath string, commands []string) ([]json.RawMessage, error)
const (
qmpPowerdown = `{"execute":"system_powerdown"}`
qmpQuit = `{"execute":"quit"}`
)
func shutdownQEMU(send qmpSender, socketPath string, timeout, resend, poll time.Duration) {
if _, err := send(socketPath, []string{qmpPowerdown}); err != nil {
return
}
deadline := time.After(timeout)
again := time.NewTicker(resend)
defer again.Stop()
for {
select {
case <-timer:
qmp.Send(socketPath, []string{`{"execute":"quit"}`})
case <-deadline:
send(socketPath, []string{qmpQuit})
return
case <-again.C:
if _, err := send(socketPath, []string{qmpPowerdown}); err != nil {
return
}
case <-time.After(poll):
if _, err := qmp.Send(socketPath, nil); err != nil {
if _, err := send(socketPath, nil); err != nil {
return
}
}

View file

@ -0,0 +1,102 @@
package vm
import (
"encoding/json"
"errors"
"sync"
"testing"
"time"
)
type fakeQEMU struct {
mu sync.Mutex
calls []string
dieAfter int
powerdown int
dead bool
}
func (f *fakeQEMU) send(_ string, commands []string) ([]json.RawMessage, error) {
f.mu.Lock()
defer f.mu.Unlock()
call := "ping"
if len(commands) > 0 {
call = commands[0]
}
f.calls = append(f.calls, call)
if f.dead {
return nil, errors.New("qmp dial: connection refused")
}
switch call {
case qmpPowerdown:
f.powerdown++
if f.dieAfter > 0 && f.powerdown >= f.dieAfter {
f.dead = true
}
case qmpQuit:
f.dead = true
}
return nil, nil
}
func (f *fakeQEMU) count(call string) int {
f.mu.Lock()
defer f.mu.Unlock()
n := 0
for _, c := range f.calls {
if c == call {
n++
}
}
return n
}
func TestShutdownQEMU_ResendsPowerdownUntilTheGuestListens(t *testing.T) {
f := &fakeQEMU{dieAfter: 3}
start := time.Now()
shutdownQEMU(f.send, "qmp.sock", 5*time.Second, 50*time.Millisecond, 10*time.Millisecond)
if d := time.Since(start); d > 2*time.Second {
t.Errorf("arrêt en %v : le renvoi aurait dû l'obtenir bien avant le délai", d)
}
if n := f.count(qmpPowerdown); n != 3 {
t.Errorf("%d system_powerdown envoyés, attendu 3 (le troisième est entendu)", n)
}
if n := f.count(qmpQuit); n != 0 {
t.Errorf("quit envoyé alors que l'invité s'est arrêté : %q", f.calls)
}
}
func TestShutdownQEMU_QuitsAtTheDeadline(t *testing.T) {
f := &fakeQEMU{}
start := time.Now()
shutdownQEMU(f.send, "qmp.sock", 200*time.Millisecond, 60*time.Millisecond, 10*time.Millisecond)
d := time.Since(start)
if d < 200*time.Millisecond || d > 2*time.Second {
t.Errorf("arrêt en %v, attendu au délai de 200ms", d)
}
if n := f.count(qmpPowerdown); n < 3 {
t.Errorf("%d system_powerdown envoyés en 200ms avec un renvoi toutes les 60ms", n)
}
if n := f.count(qmpQuit); n != 1 {
t.Errorf("%d quit envoyés, attendu 1 : %q", n, f.calls)
}
}
func TestShutdownQEMU_StopsWhenTheGuestShutsDownOnItsOwn(t *testing.T) {
f := &fakeQEMU{dieAfter: 1}
shutdownQEMU(f.send, "qmp.sock", 5*time.Second, time.Second, 10*time.Millisecond)
if n := f.count(qmpPowerdown); n != 1 {
t.Errorf("%d system_powerdown envoyés, attendu 1", n)
}
if n := f.count(qmpQuit); n != 0 {
t.Errorf("quit envoyé : %q", f.calls)
}
}
func TestShutdownQEMU_NothingMoreWhenQEMUIsAlreadyDead(t *testing.T) {
f := &fakeQEMU{dead: true}
shutdownQEMU(f.send, "qmp.sock", 5*time.Second, time.Second, 10*time.Millisecond)
if len(f.calls) != 1 {
t.Errorf("appels QMP : %q, attendu un seul essai", f.calls)
}
}