diff --git a/cmd/agent/main.go b/cmd/agent/main.go index e824dbc..197d1c3 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -31,11 +31,14 @@ const shutdownTimeout = 20 * time.Second func main() { confFile := flag.String("config", "/etc/two/agent.yml", "config file path") flag.Parse() + os.Exit(run(*confFile)) +} - cfg, err := configuration.LoadConfig(*confFile) +func run(confFile string) int { + cfg, err := configuration.LoadConfig(confFile) if err != nil { slog.Error("failed to load config", "error", err) - return + return 1 } log := logger.New(cfg.Logger.Level, cfg.Logger.Debug) @@ -52,18 +55,18 @@ func main() { // d'états, et des ressources transitoires orphelines d'un arrêt précédent. if err := migration.MigrateStates(db, log.With(slog.String("component", "migration"))); err != nil { log.Error("failed to migrate states", "error", err) - return + return 1 } if err := migration.MigrateVMNICs(db, log.With(slog.String("component", "migration"))); err != nil { log.Error("vm nic migration failed", "error", err) - return + return 1 } if cfg.BGP.Enabled() { if err := applyBGPBase(cfg, frr.NewClient()); err != nil { log.Error("failed to apply the bgp base configuration", "error", err) - return + return 1 } log.Info("bgp base configuration applied", "asn", cfg.BGP.ASN, "route_reflectors", cfg.BGP.RouteReflectors) } @@ -118,6 +121,7 @@ func main() { servers["admin"] = adminSrv } closeDB = shutdown(log, q, servers, shutdownTimeout) + return 0 } type baseApplier interface { diff --git a/cmd/agent/main_test.go b/cmd/agent/main_test.go index 302d0e7..9f3209c 100644 --- a/cmd/agent/main_test.go +++ b/cmd/agent/main_test.go @@ -5,6 +5,8 @@ import ( "errors" "io" "log/slog" + "os" + "path/filepath" "strings" "sync/atomic" "testing" @@ -117,3 +119,27 @@ func TestApplyBGPBase_RefusesAnInvalidSectionBeforeTouchingFRR(t *testing.T) { t.Error("ApplyBase appelé avec une section bgp invalide") } } + +func TestRunExitsNonZeroWhenTheConfigCannotBeLoaded(t *testing.T) { + conf := filepath.Join(t.TempDir(), "agent.yml") + if err := os.WriteFile(conf, []byte("bgp: [non: valide\n"), 0o600); err != nil { + t.Fatal(err) + } + if code := run(conf); code != 1 { + t.Errorf("code de sortie %d, 1 attendu", code) + } +} + +func TestRunExitsNonZeroWhenTheBGPBaseCannotBeApplied(t *testing.T) { + dir := t.TempDir() + conf := filepath.Join(dir, "agent.yml") + yml := "database:\n path: " + filepath.Join(dir, "db") + "\n" + + "default_interface: two-test-absent0\n" + + "bgp:\n asn: 64600\n route_reflectors: [10.255.255.1]\n" + if err := os.WriteFile(conf, []byte(yml), 0o600); err != nil { + t.Fatal(err) + } + if code := run(conf); code != 1 { + t.Errorf("code de sortie %d, 1 attendu", code) + } +}