f-39: api: /register et /ping, mesure périodique de l'état de l'hôte #39

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-31 15:19:42 +01:00
commit 9a1e941b15
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
13 changed files with 537 additions and 8 deletions

View file

@ -78,6 +78,7 @@ func LoadConfig(path string) (*Config, error) {
v.SetDefault("times.timeout_subnet_start", TimeoutSubnetStart)
v.SetDefault("times.timeout_vms_start", TimeoutVMsStart)
v.SetDefault("times.poll_interval", PollInterval)
v.SetDefault("times.host_state_interval", HostStateInterval)
v.SetDefault("metadata.run_dir", "/run/two/metadata")
v.SetDefault("dhcp.backend", BackendTwo)
v.SetDefault("hostsvc.dns_upstream", []string{})

View file

@ -13,6 +13,7 @@ const (
TimeoutSubnetStart = 300
TimeoutVMsStart = 300
PollInterval = 2
HostStateInterval = 10
)
type Times struct {
@ -23,6 +24,7 @@ type Times struct {
TimeoutSubnetStart int `mapstructure:"timeout_subnet_start"`
TimeoutVMsStart int `mapstructure:"timeout_vms_start"`
PollInterval int `mapstructure:"poll_interval"`
HostStateInterval int `mapstructure:"host_state_interval"`
}
func seconds(value, fallback int) time.Duration {
@ -39,6 +41,7 @@ func (t Times) VPCStop() time.Duration { return seconds(t.TimeoutVPCStop, Ti
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) HostState() time.Duration { return seconds(t.HostStateInterval, HostStateInterval) }
func (t Times) Validate() error {
for _, f := range []struct {
@ -52,6 +55,7 @@ func (t Times) Validate() error {
{"timeout_subnet_start", t.TimeoutSubnetStart},
{"timeout_vms_start", t.TimeoutVMsStart},
{"poll_interval", t.PollInterval},
{"host_state_interval", t.HostStateInterval},
} {
if f.value <= 0 {
return fmt.Errorf("times.%s %d must be a positive number of seconds", f.key, f.value)

View file

@ -19,6 +19,7 @@ func TestLoadConfig_TimesDefauts(t *testing.T) {
TimeoutSubnetStart: 300,
TimeoutVMsStart: 300,
PollInterval: 2,
HostStateInterval: 10,
}
if cfg.Times != want {
t.Errorf("times par défaut : %+v, attendu %+v", cfg.Times, want)
@ -26,11 +27,11 @@ func TestLoadConfig_TimesDefauts(t *testing.T) {
}
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"))
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 host_state_interval: 5\n"))
if err != nil {
t.Fatalf("LoadConfig a échoué : %v", err)
}
want := Times{600, 10, 700, 800, 50, 60, 1}
want := Times{600, 10, 700, 800, 50, 60, 1, 5}
if cfg.Times != want {
t.Errorf("times : %+v, attendu %+v", cfg.Times, want)
}
@ -55,6 +56,7 @@ func TestLoadConfig_TimesInvalidesRefuses(t *testing.T) {
{"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 host_state_interval: 0\n", "times.host_state_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) {
@ -79,14 +81,15 @@ func TestTimesDurees(t *testing.T) {
{"SubnetStart", zero.SubnetStart(), 300 * time.Second},
{"VMsStart", zero.VMsStart(), 300 * time.Second},
{"Poll", zero.Poll(), 2 * time.Second},
{"HostState", zero.HostState(), 10 * 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()}
set := Times{11, 12, 13, 14, 15, 16, 17, 18}
got := []time.Duration{set.VMsStop(), set.VMsShutdown(), set.SubnetStop(), set.VPCStop(), set.SubnetStart(), set.VMsStart(), set.Poll(), set.HostState()}
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)