From 683372858e4866d25e4a7de861944428a5c5b538 Mon Sep 17 00:00:00 2001 From: Boris Glimcher Date: Tue, 17 Oct 2023 01:14:01 +0300 Subject: [PATCH] feat(frr): implement ExitPrivileged mode Signed-off-by: Boris Glimcher --- pkg/utils/frr.go | 18 ++++-- pkg/utils/mocks/Frr.go | 126 +++++++++++++++++++++++++++-------------- 2 files changed, 98 insertions(+), 46 deletions(-) diff --git a/pkg/utils/frr.go b/pkg/utils/frr.go index 58a277f7..0893d860 100644 --- a/pkg/utils/frr.go +++ b/pkg/utils/frr.go @@ -52,7 +52,8 @@ type Frr interface { FrrZebraCmd(ctx context.Context, command string) (string, error) FrrBgpCmd(ctx context.Context, command string) (string, error) Password(conn *telnet.Conn, delim string) error - Privileged(conn *telnet.Conn) error + EnterPrivileged(conn *telnet.Conn) error + ExitPrivileged(conn *telnet.Conn) error } // FrrWrapper wrapper for Frr package @@ -82,8 +83,8 @@ func (n *FrrWrapper) Password(conn *telnet.Conn, delim string) error { return conn.SkipUntil(delim) } -// Privileged turns on privileged mode command -func (n *FrrWrapper) Privileged(conn *telnet.Conn) error { +// EnterPrivileged turns on privileged mode command +func (n *FrrWrapper) EnterPrivileged(conn *telnet.Conn) error { _, err := conn.Write([]byte("enable\n")) if err != nil { return err @@ -91,6 +92,15 @@ func (n *FrrWrapper) Privileged(conn *telnet.Conn) error { return n.Password(conn, "#") } +// ExitPrivileged turns off privileged mode command +func (n *FrrWrapper) ExitPrivileged(conn *telnet.Conn) error { + _, err := conn.Write([]byte("disable\n")) + if err != nil { + return err + } + return conn.SkipUntil(">") +} + // FrrZebraCmd connects to Zebra telnet with password and runs command func (n *FrrWrapper) FrrZebraCmd(ctx context.Context, command string) (string, error) { // ports defined here https://docs.frrouting.org/en/latest/setup.html#services @@ -136,7 +146,7 @@ func (n *FrrWrapper) TelnetDialAndCommunicate(ctx context.Context, command strin return "", err } - err = n.Privileged(conn) + err = n.EnterPrivileged(conn) if err != nil { return "", err } diff --git a/pkg/utils/mocks/Frr.go b/pkg/utils/mocks/Frr.go index 85a53fb3..42e55aae 100644 --- a/pkg/utils/mocks/Frr.go +++ b/pkg/utils/mocks/Frr.go @@ -25,6 +25,90 @@ func (_m *Frr) EXPECT() *Frr_Expecter { return &Frr_Expecter{mock: &_m.Mock} } +// EnterPrivileged provides a mock function with given fields: conn +func (_m *Frr) EnterPrivileged(conn *telnet.Conn) error { + ret := _m.Called(conn) + + var r0 error + if rf, ok := ret.Get(0).(func(*telnet.Conn) error); ok { + r0 = rf(conn) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// Frr_EnterPrivileged_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'EnterPrivileged' +type Frr_EnterPrivileged_Call struct { + *mock.Call +} + +// EnterPrivileged is a helper method to define mock.On call +// - conn *telnet.Conn +func (_e *Frr_Expecter) EnterPrivileged(conn interface{}) *Frr_EnterPrivileged_Call { + return &Frr_EnterPrivileged_Call{Call: _e.mock.On("EnterPrivileged", conn)} +} + +func (_c *Frr_EnterPrivileged_Call) Run(run func(conn *telnet.Conn)) *Frr_EnterPrivileged_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(*telnet.Conn)) + }) + return _c +} + +func (_c *Frr_EnterPrivileged_Call) Return(_a0 error) *Frr_EnterPrivileged_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *Frr_EnterPrivileged_Call) RunAndReturn(run func(*telnet.Conn) error) *Frr_EnterPrivileged_Call { + _c.Call.Return(run) + return _c +} + +// ExitPrivileged provides a mock function with given fields: conn +func (_m *Frr) ExitPrivileged(conn *telnet.Conn) error { + ret := _m.Called(conn) + + var r0 error + if rf, ok := ret.Get(0).(func(*telnet.Conn) error); ok { + r0 = rf(conn) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// Frr_ExitPrivileged_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ExitPrivileged' +type Frr_ExitPrivileged_Call struct { + *mock.Call +} + +// ExitPrivileged is a helper method to define mock.On call +// - conn *telnet.Conn +func (_e *Frr_Expecter) ExitPrivileged(conn interface{}) *Frr_ExitPrivileged_Call { + return &Frr_ExitPrivileged_Call{Call: _e.mock.On("ExitPrivileged", conn)} +} + +func (_c *Frr_ExitPrivileged_Call) Run(run func(conn *telnet.Conn)) *Frr_ExitPrivileged_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(*telnet.Conn)) + }) + return _c +} + +func (_c *Frr_ExitPrivileged_Call) Return(_a0 error) *Frr_ExitPrivileged_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *Frr_ExitPrivileged_Call) RunAndReturn(run func(*telnet.Conn) error) *Frr_ExitPrivileged_Call { + _c.Call.Return(run) + return _c +} + // FrrBgpCmd provides a mock function with given fields: ctx, command func (_m *Frr) FrrBgpCmd(ctx context.Context, command string) (string, error) { ret := _m.Called(ctx, command) @@ -174,48 +258,6 @@ func (_c *Frr_Password_Call) RunAndReturn(run func(*telnet.Conn, string) error) return _c } -// Privileged provides a mock function with given fields: conn -func (_m *Frr) Privileged(conn *telnet.Conn) error { - ret := _m.Called(conn) - - var r0 error - if rf, ok := ret.Get(0).(func(*telnet.Conn) error); ok { - r0 = rf(conn) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// Frr_Privileged_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Privileged' -type Frr_Privileged_Call struct { - *mock.Call -} - -// Privileged is a helper method to define mock.On call -// - conn *telnet.Conn -func (_e *Frr_Expecter) Privileged(conn interface{}) *Frr_Privileged_Call { - return &Frr_Privileged_Call{Call: _e.mock.On("Privileged", conn)} -} - -func (_c *Frr_Privileged_Call) Run(run func(conn *telnet.Conn)) *Frr_Privileged_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(*telnet.Conn)) - }) - return _c -} - -func (_c *Frr_Privileged_Call) Return(_a0 error) *Frr_Privileged_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *Frr_Privileged_Call) RunAndReturn(run func(*telnet.Conn) error) *Frr_Privileged_Call { - _c.Call.Return(run) - return _c -} - // TelnetDialAndCommunicate provides a mock function with given fields: ctx, command, port func (_m *Frr) TelnetDialAndCommunicate(ctx context.Context, command string, port int) (string, error) { ret := _m.Called(ctx, command, port)