2020-04-12 08:55:13 -04:00
|
|
|
package routing
|
|
|
|
|
|
|
|
|
|
import (
|
2020-04-19 18:13:48 +00:00
|
|
|
"context"
|
2020-04-12 08:55:13 -04:00
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2020-04-12 18:09:46 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
|
"github.com/qdm12/golibs/command/mock_command"
|
2020-04-12 08:55:13 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Test_removeRoute(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
tests := map[string]struct {
|
|
|
|
|
subnet net.IPNet
|
|
|
|
|
runOutput string
|
|
|
|
|
runErr error
|
|
|
|
|
err error
|
|
|
|
|
}{
|
|
|
|
|
"no output no error": {
|
|
|
|
|
subnet: net.IPNet{
|
|
|
|
|
IP: net.IP{192, 168, 1, 0},
|
|
|
|
|
Mask: net.IPMask{255, 255, 255, 0},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"error only": {
|
|
|
|
|
subnet: net.IPNet{
|
|
|
|
|
IP: net.IP{192, 168, 1, 0},
|
|
|
|
|
Mask: net.IPMask{255, 255, 255, 0},
|
|
|
|
|
},
|
|
|
|
|
runErr: fmt.Errorf("error"),
|
|
|
|
|
err: fmt.Errorf("cannot delete route for 192.168.1.0/24: : error"),
|
|
|
|
|
},
|
|
|
|
|
"error and output": {
|
|
|
|
|
subnet: net.IPNet{
|
|
|
|
|
IP: net.IP{192, 168, 1, 0},
|
|
|
|
|
Mask: net.IPMask{255, 255, 255, 0},
|
|
|
|
|
},
|
|
|
|
|
runErr: fmt.Errorf("error"),
|
|
|
|
|
runOutput: "output",
|
|
|
|
|
err: fmt.Errorf("cannot delete route for 192.168.1.0/24: output: error"),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for name, tc := range tests {
|
|
|
|
|
tc := tc
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
mockCtrl := gomock.NewController(t)
|
|
|
|
|
defer mockCtrl.Finish()
|
2020-04-12 18:09:46 +00:00
|
|
|
commander := mock_command.NewMockCommander(mockCtrl)
|
2020-04-12 08:55:13 -04:00
|
|
|
|
2020-04-19 18:13:48 +00:00
|
|
|
commander.EXPECT().Run(context.Background(), "ip", "route", "del", tc.subnet.String()).
|
2020-04-12 08:55:13 -04:00
|
|
|
Return(tc.runOutput, tc.runErr).Times(1)
|
2020-04-12 18:09:46 +00:00
|
|
|
r := &routing{commander: commander}
|
2020-04-19 18:13:48 +00:00
|
|
|
err := r.removeRoute(context.Background(), tc.subnet)
|
2020-04-12 08:55:13 -04:00
|
|
|
if tc.err != nil {
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
assert.Equal(t, tc.err.Error(), err.Error())
|
|
|
|
|
} else {
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|