f-46: dhcp: add the dhcp binary, its unit and its wrapper #46
cmd/dhcp reçoit quatre paramètres en clair — interface, state, socket, conf — sur le modèle de dnsmasq. Il ne compose aucun chemin, ne découpe aucun nom composite et ignore le netns dans lequel il tourne : seul le wrapper en a besoin, pour y entrer. La boucle de lecture UDP est écrite à la main plutôt que confiée à server4.Serve, qui lance une goroutine par datagramme sans borne et ne pose aucun recover. Elle traite en ligne, réutilise un tampon de 1500 octets et pose un recover par datagramme. NewIPv4UDPConn est conservé pour le SO_BROADCAST et le bind à l'interface. Une réponse destinée à un client sans adresse part en broadcast. dhcp.run_dir n'est pas une clé de configuration : le wrapper code /run/two/dhcp en dur et le Go utilise dhcpapi.DefaultRunDir, comme dhcp.DefaultConfDir pour dnsmasq. Un test lit le script et vérifie que les deux s'accordent. Le défaut dhcp.backend reste dnsmasq : un agent.yml de 0.1.0 se comporte comme avant. deploy.sh et le pipeline publient le binaire, l'unit et le script. La boucle est testée sur une vraie socket UDP en loopback. Neuf mutations, dont une qui a révélé que le recover n'était couvert par rien. Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
37a9353dc0
commit
9b90defbdb
13 changed files with 579 additions and 2 deletions
17
internal/config/agent/dhcp.go
Normal file
17
internal/config/agent/dhcp.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package configuration
|
||||
|
||||
import "fmt"
|
||||
|
||||
const (
|
||||
BackendDnsmasq = "dnsmasq"
|
||||
BackendTwo = "two"
|
||||
)
|
||||
|
||||
func ValidBackend(backend string) error {
|
||||
switch backend {
|
||||
case BackendDnsmasq, BackendTwo:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("unknown dhcp backend %q: expected %q or %q", backend, BackendDnsmasq, BackendTwo)
|
||||
}
|
||||
}
|
||||
56
internal/config/agent/dhcp_test.go
Normal file
56
internal/config/agent/dhcp_test.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package configuration
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func writeConfig(t *testing.T, body string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "agent.yml")
|
||||
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func TestValidBackend_AcceptsTheTwoKnownBackends(t *testing.T) {
|
||||
for _, backend := range []string{BackendDnsmasq, BackendTwo} {
|
||||
if err := ValidBackend(backend); err != nil {
|
||||
t.Errorf("ValidBackend(%q) = %v, want nil", backend, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidBackend_RejectsAnythingElse(t *testing.T) {
|
||||
for _, backend := range []string{"", "dhcpd", "DNSMASQ", "two "} {
|
||||
if err := ValidBackend(backend); err == nil {
|
||||
t.Errorf("ValidBackend(%q) = nil, want an error", backend)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfig_DefaultsToDnsmasq(t *testing.T) {
|
||||
path := writeConfig(t, "database:\n path: /tmp/two\n")
|
||||
|
||||
cfg, err := LoadConfig(path)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig: %v", err)
|
||||
}
|
||||
if cfg.DHCP.Backend != BackendDnsmasq {
|
||||
t.Errorf("backend = %q, want %q: a 0.1.0 config must keep behaving as before", cfg.DHCP.Backend, BackendDnsmasq)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfig_ReadsTheTwoBackend(t *testing.T) {
|
||||
path := writeConfig(t, "dhcp:\n backend: two\n")
|
||||
|
||||
cfg, err := LoadConfig(path)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig: %v", err)
|
||||
}
|
||||
if cfg.DHCP.Backend != BackendTwo {
|
||||
t.Errorf("backend = %q, want two", cfg.DHCP.Backend)
|
||||
}
|
||||
}
|
||||
|
|
@ -31,6 +31,9 @@ type Config struct {
|
|||
Metadata struct {
|
||||
RunDir string `mapstructure:"run_dir"`
|
||||
} `mapstructure:"metadata"`
|
||||
DHCP struct {
|
||||
Backend string `mapstructure:"backend"`
|
||||
} `mapstructure:"dhcp"`
|
||||
Admin struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
Address string `mapstructure:"address"`
|
||||
|
|
@ -67,6 +70,7 @@ func LoadConfig(path string) (*Config, error) {
|
|||
v.SetDefault("dispatcher.timeout_seconds", 300)
|
||||
v.SetDefault("dispatcher.poll_seconds", 2)
|
||||
v.SetDefault("metadata.run_dir", "/run/two/metadata")
|
||||
v.SetDefault("dhcp.backend", BackendDnsmasq)
|
||||
v.SetDefault("qemu.ovmf_code_path", "/usr/share/OVMF/OVMF_CODE.fd")
|
||||
v.SetDefault("qemu.ovmf_vars_template", "/usr/share/OVMF/OVMF_VARS.fd")
|
||||
v.SetDefault("qemu.uefi_vars_dir", "/run/two/vms/uefi")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue