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

@ -24,10 +24,7 @@ type Config struct {
Count int `mapstructure:"count"`
BufferSize int `mapstructure:"buffer_size"`
} `mapstructure:"worker"`
Dispatcher struct {
TimeoutSeconds int `mapstructure:"timeout_seconds"`
PollSeconds int `mapstructure:"poll_seconds"`
} `mapstructure:"dispatcher"`
Times Times `mapstructure:"times"`
Logger struct {
Level string `mapstructure:"level"`
Debug bool `mapstructure:"debug"`
@ -73,8 +70,13 @@ func LoadConfig(path string) (*Config, error) {
v.SetDefault("prometheus.port", 9090)
v.SetDefault("worker.count", 4)
v.SetDefault("worker.buffer_size", 100)
v.SetDefault("dispatcher.timeout_seconds", 300)
v.SetDefault("dispatcher.poll_seconds", 2)
v.SetDefault("times.timeout_vms_stop", TimeoutVMsStop)
v.SetDefault("times.next_vms_shutdown", NextVMsShutdown)
v.SetDefault("times.timeout_subnet_stop", TimeoutSubnetStop)
v.SetDefault("times.timeout_vpc_stop", TimeoutVPCStop)
v.SetDefault("times.timeout_subnet_start", TimeoutSubnetStart)
v.SetDefault("times.timeout_vms_start", TimeoutVMsStart)
v.SetDefault("times.poll_interval", PollInterval)
v.SetDefault("metadata.run_dir", "/run/two/metadata")
v.SetDefault("dhcp.backend", BackendTwo)
v.SetDefault("qemu.ovmf_code_path", "/usr/share/OVMF/OVMF_CODE.fd")
@ -106,6 +108,9 @@ func LoadConfig(path string) (*Config, error) {
if err := cfg.Network.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w", path, err)
}
if err := cfg.Times.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w", path, err)
}
return &cfg, nil
}

View file

@ -0,0 +1,64 @@
package configuration
import (
"fmt"
"time"
)
const (
TimeoutVMsStop = 300
NextVMsShutdown = 30
TimeoutSubnetStop = 360
TimeoutVPCStop = 420
TimeoutSubnetStart = 300
TimeoutVMsStart = 300
PollInterval = 2
)
type Times struct {
TimeoutVMsStop int `mapstructure:"timeout_vms_stop"`
NextVMsShutdown int `mapstructure:"next_vms_shutdown"`
TimeoutSubnetStop int `mapstructure:"timeout_subnet_stop"`
TimeoutVPCStop int `mapstructure:"timeout_vpc_stop"`
TimeoutSubnetStart int `mapstructure:"timeout_subnet_start"`
TimeoutVMsStart int `mapstructure:"timeout_vms_start"`
PollInterval int `mapstructure:"poll_interval"`
}
func seconds(value, fallback int) time.Duration {
if value == 0 {
value = fallback
}
return time.Duration(value) * time.Second
}
func (t Times) VMsStop() time.Duration { return seconds(t.TimeoutVMsStop, TimeoutVMsStop) }
func (t Times) VMsShutdown() time.Duration { return seconds(t.NextVMsShutdown, NextVMsShutdown) }
func (t Times) SubnetStop() time.Duration { return seconds(t.TimeoutSubnetStop, TimeoutSubnetStop) }
func (t Times) VPCStop() time.Duration { return seconds(t.TimeoutVPCStop, TimeoutVPCStop) }
func (t Times) SubnetStart() time.Duration { return seconds(t.TimeoutSubnetStart, TimeoutSubnetStart) }
func (t Times) VMsStart() time.Duration { return seconds(t.TimeoutVMsStart, TimeoutVMsStart) }
func (t Times) Poll() time.Duration { return seconds(t.PollInterval, PollInterval) }
func (t Times) Validate() error {
for _, f := range []struct {
key string
value int
}{
{"timeout_vms_stop", t.TimeoutVMsStop},
{"next_vms_shutdown", t.NextVMsShutdown},
{"timeout_subnet_stop", t.TimeoutSubnetStop},
{"timeout_vpc_stop", t.TimeoutVPCStop},
{"timeout_subnet_start", t.TimeoutSubnetStart},
{"timeout_vms_start", t.TimeoutVMsStart},
{"poll_interval", t.PollInterval},
} {
if f.value <= 0 {
return fmt.Errorf("times.%s %d must be a positive number of seconds", f.key, f.value)
}
}
if t.NextVMsShutdown >= t.TimeoutVMsStop {
return fmt.Errorf("times.next_vms_shutdown %d must be below times.timeout_vms_stop %d", t.NextVMsShutdown, t.TimeoutVMsStop)
}
return nil
}

View file

@ -0,0 +1,95 @@
package configuration
import (
"strings"
"testing"
"time"
)
func TestLoadConfig_TimesDefauts(t *testing.T) {
cfg, err := LoadConfig(writeYAML(t, ""))
if err != nil {
t.Fatalf("LoadConfig a échoué : %v", err)
}
want := Times{
TimeoutVMsStop: 300,
NextVMsShutdown: 30,
TimeoutSubnetStop: 360,
TimeoutVPCStop: 420,
TimeoutSubnetStart: 300,
TimeoutVMsStart: 300,
PollInterval: 2,
}
if cfg.Times != want {
t.Errorf("times par défaut : %+v, attendu %+v", cfg.Times, want)
}
}
func TestLoadConfig_TimesValeursExplicites(t *testing.T) {
cfg, err := LoadConfig(writeYAML(t, "times:\n timeout_vms_stop: 600\n next_vms_shutdown: 10\n timeout_subnet_stop: 700\n timeout_vpc_stop: 800\n timeout_subnet_start: 50\n timeout_vms_start: 60\n poll_interval: 1\n"))
if err != nil {
t.Fatalf("LoadConfig a échoué : %v", err)
}
want := Times{600, 10, 700, 800, 50, 60, 1}
if cfg.Times != want {
t.Errorf("times : %+v, attendu %+v", cfg.Times, want)
}
}
func TestLoadConfig_AncienneSectionDispatcherIgnoree(t *testing.T) {
cfg, err := LoadConfig(writeYAML(t, "dispatcher:\n timeout_seconds: 900\n poll_seconds: 7\n"))
if err != nil {
t.Fatalf("une section dispatcher restante ne doit pas empêcher le démarrage : %v", err)
}
if cfg.Times.TimeoutVMsStop != 300 || cfg.Times.PollInterval != 2 {
t.Errorf("dispatcher.* ne doit rien changer : %+v", cfg.Times)
}
}
func TestLoadConfig_TimesInvalidesRefuses(t *testing.T) {
for _, c := range []struct{ yaml, msg string }{
{"times:\n timeout_vms_stop: 0\n", "times.timeout_vms_stop 0 must be a positive number of seconds"},
{"times:\n next_vms_shutdown: -1\n", "times.next_vms_shutdown -1 must be a positive number of seconds"},
{"times:\n timeout_subnet_stop: 0\n", "times.timeout_subnet_stop 0"},
{"times:\n timeout_vpc_stop: 0\n", "times.timeout_vpc_stop 0"},
{"times:\n timeout_subnet_start: 0\n", "times.timeout_subnet_start 0"},
{"times:\n timeout_vms_start: 0\n", "times.timeout_vms_start 0"},
{"times:\n poll_interval: 0\n", "times.poll_interval 0"},
{"times:\n next_vms_shutdown: 300\n", "times.next_vms_shutdown 300 must be below times.timeout_vms_stop 300"},
} {
if _, err := LoadConfig(writeYAML(t, c.yaml)); err == nil || !strings.Contains(err.Error(), c.msg) {
t.Errorf("%q : erreur %v, attendu %q", c.yaml, err, c.msg)
}
}
if _, err := LoadConfig(writeYAML(t, "times:\n next_vms_shutdown: 299\n")); err != nil {
t.Errorf("next_vms_shutdown 299 sous timeout_vms_stop 300 : %v", err)
}
}
func TestTimesDurees(t *testing.T) {
var zero Times
cases := []struct {
name string
got, want time.Duration
}{
{"VMsStop", zero.VMsStop(), 300 * time.Second},
{"VMsShutdown", zero.VMsShutdown(), 30 * time.Second},
{"SubnetStop", zero.SubnetStop(), 360 * time.Second},
{"VPCStop", zero.VPCStop(), 420 * time.Second},
{"SubnetStart", zero.SubnetStart(), 300 * time.Second},
{"VMsStart", zero.VMsStart(), 300 * time.Second},
{"Poll", zero.Poll(), 2 * time.Second},
}
for _, c := range cases {
if c.got != c.want {
t.Errorf("Times{}.%s() = %v, attendu %v", c.name, c.got, c.want)
}
}
set := Times{11, 12, 13, 14, 15, 16, 17}
got := []time.Duration{set.VMsStop(), set.VMsShutdown(), set.SubnetStop(), set.VPCStop(), set.SubnetStart(), set.VMsStart(), set.Poll()}
for i, d := range got {
if want := time.Duration(11+i) * time.Second; d != want {
t.Errorf("durée %d = %v, attendu %v", i, d, want)
}
}
}