From 1b80b1c7b60d3063cf9f941b4274e6403a3ab4d1 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sun, 11 Oct 2026 22:15:01 +0200 Subject: [PATCH] =?UTF-8?q?f-41:=20frr:=20remarques=20de=20vtysh=20accept?= =?UTF-8?q?=C3=A9es,=20l3vni=20lib=C3=A9r=C3=A9e=20de=20fa=C3=A7on=20async?= =?UTF-8?q?hrone=20par=20bgpd=20#41?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: GnomeZworc --- internal/frr/client.go | 36 ++++++++++++---- internal/frr/client_test.go | 84 +++++++++++++++++++++++++++++++++---- 2 files changed, 106 insertions(+), 14 deletions(-) diff --git a/internal/frr/client.go b/internal/frr/client.go index 04b2ea8..3f38204 100644 --- a/internal/frr/client.go +++ b/internal/frr/client.go @@ -4,18 +4,30 @@ import ( "fmt" "os/exec" "strings" + "time" +) + +const ( + appliedWithNotes = "% Configuration applied with notes:" + l3vniStillBound = "% Please unconfigure l3vni" + l3vniRetries = 20 + l3vniRetryDelay = 250 * time.Millisecond ) type runner func(args ...string) ([]byte, error) type Client struct { - run runner + run runner + sleep func(time.Duration) } func NewClient() *Client { - return &Client{run: func(args ...string) ([]byte, error) { - return exec.Command("vtysh", args...).CombinedOutput() - }} + return &Client{ + run: func(args ...string) ([]byte, error) { + return exec.Command("vtysh", args...).CombinedOutput() + }, + sleep: time.Sleep, + } } func (c *Client) ApplyBase(b Base) error { @@ -79,7 +91,7 @@ func (c *Client) teardown(v VRF, plan func(VRF, string) []step) error { return err } for _, s := range plan(v, running) { - if err := c.configure(s...); err != nil { + if err := c.configureStep(s); err != nil { return err } } @@ -92,6 +104,15 @@ func (c *Client) teardown(v VRF, plan func(VRF, string) []step) error { return remaining(plan(v, running)) } +func (c *Client) configureStep(s step) error { + err := c.configure(s...) + for i := 0; i < l3vniRetries && err != nil && strings.Contains(err.Error(), l3vniStillBound); i++ { + c.sleep(l3vniRetryDelay) + err = c.configure(s...) + } + return err +} + func (c *Client) runningConfig() (string, error) { return c.exec("show running-config") } @@ -116,8 +137,9 @@ func (c *Client) exec(cmds ...string) (string, error) { return "", fmt.Errorf("vtysh %q: %w: %s", cmds, err, strings.TrimSpace(string(out))) } for _, l := range strings.Split(string(out), "\n") { - if strings.HasPrefix(strings.TrimSpace(l), "%") { - return "", fmt.Errorf("vtysh %q: %s", cmds, strings.TrimSpace(l)) + l = strings.TrimSpace(l) + if strings.HasPrefix(l, "%") && l != appliedWithNotes { + return "", fmt.Errorf("vtysh %q: %s", cmds, l) } } return string(out), nil diff --git a/internal/frr/client_test.go b/internal/frr/client_test.go index 96050be..ea68e1e 100644 --- a/internal/frr/client_test.go +++ b/internal/frr/client_test.go @@ -6,13 +6,16 @@ import ( "reflect" "strings" "testing" + "time" ) type fakeVtysh struct { - calls [][]string - running []string - fail map[string]string - status map[string]error + calls [][]string + running []string + fail map[string]string + status map[string]error + failTimes map[string]int + slept int } func (f *fakeVtysh) run(args ...string) ([]byte, error) { @@ -31,15 +34,23 @@ func (f *fakeVtysh) run(args ...string) ([]byte, error) { return []byte(out), nil } for _, c := range cmds { - if out, ok := f.fail[c]; ok { - return []byte(out), f.status[c] + out, ok := f.fail[c] + if !ok { + continue } + if n, limited := f.failTimes[c]; limited { + if n == 0 { + continue + } + f.failTimes[c] = n - 1 + } + return []byte(out), f.status[c] } return nil, nil } func (f *fakeVtysh) client() *Client { - return &Client{run: f.run} + return &Client{run: f.run, sleep: func(time.Duration) { f.slept++ }} } func (f *fakeVtysh) sent(cmd string) bool { @@ -249,3 +260,62 @@ func TestApplyBaseStopsWhenTheRunningConfigIsUnreadable(t *testing.T) { t.Errorf("appels : %q", f.calls) } } + +func TestExecAcceptsAConfigurationAppliedWithNotes(t *testing.T) { + f := &fakeVtysh{fail: map[string]string{"vni 4001": "% Configuration applied with notes:\nNo changes found to be committed!\n"}} + if err := f.client().ApplyVRF(labVRF(t)); err != nil { + t.Errorf("remarques prises pour une erreur : %v", err) + } +} + +func TestExecStillReportsOtherPercentLines(t *testing.T) { + f := &fakeVtysh{fail: map[string]string{"vni 4001": "% Configuration applied with notes:\n% Unknown command: vni 4001\n"}} + if err := f.client().ApplyVRF(labVRF(t)); err == nil || !strings.Contains(err.Error(), "% Unknown command") { + t.Errorf("erreur %v", err) + } +} + +func TestWithdrawVRFWaitsForBGPToReleaseTheL3VNI(t *testing.T) { + f := &fakeVtysh{ + running: []string{fullRunning, otherOnlyRunning}, + fail: map[string]string{"no router bgp 64600 vrf vrf-a1b2": "% Please unconfigure l3vni 4001\n"}, + status: map[string]error{"no router bgp 64600 vrf vrf-a1b2": errors.New("exit status 1")}, + failTimes: map[string]int{"no router bgp 64600 vrf vrf-a1b2": 3}, + } + if err := f.client().WithdrawVRF(labVRF(t)); err != nil { + t.Fatalf("refus transitoire non rattrapé : %v", err) + } + if f.slept != 3 { + t.Errorf("%d attentes, attendu 3", f.slept) + } + if !f.sent("no route-map EXPORT-a1b2") { + t.Errorf("démontage interrompu : %q", f.calls) + } +} + +func TestWithdrawVRFGivesUpOnAPersistentL3VNIRefusal(t *testing.T) { + f := &fakeVtysh{ + running: []string{fullRunning}, + fail: map[string]string{"no router bgp 64600 vrf vrf-a1b2": "% Please unconfigure l3vni 4001\n"}, + } + err := f.client().WithdrawVRF(labVRF(t)) + if err == nil || !strings.Contains(err.Error(), "Please unconfigure l3vni 4001") { + t.Fatalf("erreur %v", err) + } + if f.slept != l3vniRetries { + t.Errorf("%d attentes, attendu %d", f.slept, l3vniRetries) + } + if f.sent("no route-map EXPORT-a1b2") { + t.Error("étape suivante exécutée après l'abandon") + } +} + +func TestOtherTeardownErrorsAreNotRetried(t *testing.T) { + f := &fakeVtysh{ + running: []string{fullRunning}, + fail: map[string]string{"no router bgp 64600 vrf vrf-a1b2": "% Unknown command\n"}, + } + if err := f.client().WithdrawVRF(labVRF(t)); err == nil || f.slept != 0 { + t.Errorf("erreur %v, %d attentes", err, f.slept) + } +}