test(inputs.dpdk): Cleanup directories after unit tests (#14457)
This commit is contained in:
parent
73ed448f1f
commit
76da5ff9b9
|
|
@ -92,7 +92,6 @@ func Test_readMaxOutputLen(t *testing.T) {
|
||||||
func Test_connect(t *testing.T) {
|
func Test_connect(t *testing.T) {
|
||||||
t.Run("should pass if PathToSocket points to socket", func(t *testing.T) {
|
t.Run("should pass if PathToSocket points to socket", func(t *testing.T) {
|
||||||
pathToSocket, socket := createSocketForTest(t, "")
|
pathToSocket, socket := createSocketForTest(t, "")
|
||||||
defer socket.Close()
|
|
||||||
dpdk := dpdk{
|
dpdk := dpdk{
|
||||||
SocketPath: pathToSocket,
|
SocketPath: pathToSocket,
|
||||||
connectors: []*dpdkConnector{newDpdkConnector(pathToSocket, 0)},
|
connectors: []*dpdkConnector{newDpdkConnector(pathToSocket, 0)},
|
||||||
|
|
|
||||||
|
|
@ -58,8 +58,7 @@ func Test_Init(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("when commands are in invalid format (doesn't start with '/') then error should be returned", func(t *testing.T) {
|
t.Run("when commands are in invalid format (doesn't start with '/') then error should be returned", func(t *testing.T) {
|
||||||
pathToSocket, socket := createSocketForTest(t, "")
|
pathToSocket, _ := createSocketForTest(t, "")
|
||||||
defer socket.Close()
|
|
||||||
dpdk := dpdk{
|
dpdk := dpdk{
|
||||||
Log: testutil.Logger{},
|
Log: testutil.Logger{},
|
||||||
SocketPath: pathToSocket,
|
SocketPath: pathToSocket,
|
||||||
|
|
@ -84,8 +83,7 @@ func Test_Init(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("when device_types and additional_commands are empty, then error should be returned", func(t *testing.T) {
|
t.Run("when device_types and additional_commands are empty, then error should be returned", func(t *testing.T) {
|
||||||
pathToSocket, socket := createSocketForTest(t, "")
|
pathToSocket, _ := createSocketForTest(t, "")
|
||||||
defer socket.Close()
|
|
||||||
dpdk := dpdk{
|
dpdk := dpdk{
|
||||||
SocketPath: pathToSocket,
|
SocketPath: pathToSocket,
|
||||||
DeviceTypes: []string{},
|
DeviceTypes: []string{},
|
||||||
|
|
@ -141,7 +139,6 @@ func Test_Start(t *testing.T) {
|
||||||
|
|
||||||
t.Run("when all values are valid, then no error should be returned", func(t *testing.T) {
|
t.Run("when all values are valid, then no error should be returned", func(t *testing.T) {
|
||||||
pathToSocket, socket := createSocketForTest(t, "")
|
pathToSocket, socket := createSocketForTest(t, "")
|
||||||
defer socket.Close()
|
|
||||||
dpdk := dpdk{
|
dpdk := dpdk{
|
||||||
SocketPath: pathToSocket,
|
SocketPath: pathToSocket,
|
||||||
DeviceTypes: []string{"ethdev"},
|
DeviceTypes: []string{"ethdev"},
|
||||||
|
|
@ -193,7 +190,6 @@ func TestMaintainConnections(t *testing.T) {
|
||||||
|
|
||||||
t.Run("maintainConnections shouldn't return error with 1 socket", func(t *testing.T) {
|
t.Run("maintainConnections shouldn't return error with 1 socket", func(t *testing.T) {
|
||||||
pathToSocket, socket := createSocketForTest(t, "")
|
pathToSocket, socket := createSocketForTest(t, "")
|
||||||
defer socket.Close()
|
|
||||||
dpdk := dpdk{
|
dpdk := dpdk{
|
||||||
SocketPath: pathToSocket,
|
SocketPath: pathToSocket,
|
||||||
DeviceTypes: []string{"ethdev"},
|
DeviceTypes: []string{"ethdev"},
|
||||||
|
|
@ -214,11 +210,6 @@ func TestMaintainConnections(t *testing.T) {
|
||||||
numSockets := rand.Intn(5) + 1
|
numSockets := rand.Intn(5) + 1
|
||||||
|
|
||||||
pathToSockets, sockets := createMultipleSocketsForTest(t, numSockets, "")
|
pathToSockets, sockets := createMultipleSocketsForTest(t, numSockets, "")
|
||||||
defer func() {
|
|
||||||
for _, socket := range sockets {
|
|
||||||
socket.Close()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
dpdk := dpdk{
|
dpdk := dpdk{
|
||||||
SocketPath: pathToSockets[0],
|
SocketPath: pathToSockets[0],
|
||||||
|
|
@ -244,7 +235,6 @@ func TestMaintainConnections(t *testing.T) {
|
||||||
|
|
||||||
t.Run("Test maintainConnections without dpdkPluginOptionInMemory option", func(t *testing.T) {
|
t.Run("Test maintainConnections without dpdkPluginOptionInMemory option", func(t *testing.T) {
|
||||||
pathToSocket, socket := createSocketForTest(t, "")
|
pathToSocket, socket := createSocketForTest(t, "")
|
||||||
defer socket.Close()
|
|
||||||
dpdk := dpdk{
|
dpdk := dpdk{
|
||||||
SocketPath: pathToSocket,
|
SocketPath: pathToSocket,
|
||||||
DeviceTypes: []string{"ethdev"},
|
DeviceTypes: []string{"ethdev"},
|
||||||
|
|
@ -264,7 +254,6 @@ func TestMaintainConnections(t *testing.T) {
|
||||||
|
|
||||||
t.Run("Test maintainConnections with dpdkPluginOptionInMemory option", func(t *testing.T) {
|
t.Run("Test maintainConnections with dpdkPluginOptionInMemory option", func(t *testing.T) {
|
||||||
pathToSocket1, socket1 := createSocketForTest(t, "")
|
pathToSocket1, socket1 := createSocketForTest(t, "")
|
||||||
defer socket1.Close()
|
|
||||||
go simulateSocketResponse(socket1, t)
|
go simulateSocketResponse(socket1, t)
|
||||||
dpdk := dpdk{
|
dpdk := dpdk{
|
||||||
SocketPath: pathToSocket1,
|
SocketPath: pathToSocket1,
|
||||||
|
|
@ -307,7 +296,6 @@ func TestMaintainConnections(t *testing.T) {
|
||||||
func TestClose(t *testing.T) {
|
func TestClose(t *testing.T) {
|
||||||
t.Run("Num of connections should be 0 after Stop func", func(t *testing.T) {
|
t.Run("Num of connections should be 0 after Stop func", func(t *testing.T) {
|
||||||
pathToSocket, socket := createSocketForTest(t, "")
|
pathToSocket, socket := createSocketForTest(t, "")
|
||||||
defer socket.Close()
|
|
||||||
dpdk := dpdk{
|
dpdk := dpdk{
|
||||||
SocketPath: pathToSocket,
|
SocketPath: pathToSocket,
|
||||||
DeviceTypes: []string{"ethdev"},
|
DeviceTypes: []string{"ethdev"},
|
||||||
|
|
@ -577,8 +565,7 @@ func Test_getDpdkInMemorySocketPaths(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Should return one socket from socket path", func(t *testing.T) {
|
t.Run("Should return one socket from socket path", func(t *testing.T) {
|
||||||
socketPath, socket := createSocketForTest(t, "")
|
socketPath, _ := createSocketForTest(t, "")
|
||||||
defer socket.Close()
|
|
||||||
|
|
||||||
dpdk := dpdk{
|
dpdk := dpdk{
|
||||||
SocketPath: socketPath,
|
SocketPath: socketPath,
|
||||||
|
|
@ -593,12 +580,7 @@ func Test_getDpdkInMemorySocketPaths(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Should return 2 sockets from socket path", func(t *testing.T) {
|
t.Run("Should return 2 sockets from socket path", func(t *testing.T) {
|
||||||
socketPaths, sockets := createMultipleSocketsForTest(t, 2, "")
|
socketPaths, _ := createMultipleSocketsForTest(t, 2, "")
|
||||||
defer func() {
|
|
||||||
for _, socket := range sockets {
|
|
||||||
socket.Close()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
dpdk := dpdk{
|
dpdk := dpdk{
|
||||||
SocketPath: socketPaths[0],
|
SocketPath: socketPaths[0],
|
||||||
|
|
@ -875,35 +857,41 @@ func simulateResponse(mockConn *mocks.Conn, response string, readErr error) {
|
||||||
func createSocketForTest(t *testing.T, dirPath string) (string, net.Listener) {
|
func createSocketForTest(t *testing.T, dirPath string) (string, net.Listener) {
|
||||||
var err error
|
var err error
|
||||||
var pathToSocket string
|
var pathToSocket string
|
||||||
|
|
||||||
if len(dirPath) == 0 {
|
if len(dirPath) == 0 {
|
||||||
dirPath, err = os.MkdirTemp("", "dpdk-test-socket")
|
dirPath, err = os.MkdirTemp("", "dpdk-test-socket")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
pathToSocket = filepath.Join(dirPath, dpdkSocketTemplateName)
|
pathToSocket = filepath.Join(dirPath, dpdkSocketTemplateName)
|
||||||
} else {
|
} else {
|
||||||
pathToSocket = fmt.Sprintf("%s:%d", filepath.Join(dirPath, dpdkSocketTemplateName), rand.Intn(100)+1)
|
// Create a socket in provided dirPath without duplication (similar to os.CreateTemp without creating a file)
|
||||||
|
try := 1
|
||||||
|
for {
|
||||||
|
pathToSocket = fmt.Sprintf("%s:%d", filepath.Join(dirPath, dpdkSocketTemplateName), try)
|
||||||
|
if _, err = os.Stat(pathToSocket); err == nil {
|
||||||
|
if try++; try < 1000 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
t.Fatalf("Can't create a temporary file for socket")
|
||||||
|
}
|
||||||
|
require.ErrorIs(t, err, os.ErrNotExist)
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
socket, err := net.Listen("unixpacket", pathToSocket)
|
socket, err := net.Listen("unixpacket", pathToSocket)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
t.Cleanup(func() {
|
||||||
|
socket.Close()
|
||||||
|
os.RemoveAll(dirPath)
|
||||||
|
})
|
||||||
|
|
||||||
return pathToSocket, socket
|
return pathToSocket, socket
|
||||||
}
|
}
|
||||||
|
|
||||||
func createMultipleSocketsForTest(t *testing.T, numSockets int, dirPath string) (socketsPaths []string, sockets []net.Listener) {
|
func createMultipleSocketsForTest(t *testing.T, numSockets int, dirPath string) (socketsPaths []string, sockets []net.Listener) {
|
||||||
var err error
|
|
||||||
if len(dirPath) == 0 {
|
|
||||||
dirPath, err = os.MkdirTemp("", "dpdk-test-socket")
|
|
||||||
}
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
for i := 0; i < numSockets; i++ {
|
for i := 0; i < numSockets; i++ {
|
||||||
var pathToSocket string
|
pathToSocket, socket := createSocketForTest(t, dirPath)
|
||||||
if i == 0 {
|
dirPath = filepath.Dir(pathToSocket)
|
||||||
pathToSocket = filepath.Join(dirPath, dpdkSocketTemplateName)
|
|
||||||
} else {
|
|
||||||
pathToSocket = filepath.Join(dirPath, fmt.Sprintf("%s:%d", dpdkSocketTemplateName, 1000+i))
|
|
||||||
}
|
|
||||||
socket, err := net.Listen("unixpacket", pathToSocket)
|
|
||||||
require.NoError(t, err)
|
|
||||||
socketsPaths = append(socketsPaths, pathToSocket)
|
socketsPaths = append(socketsPaths, pathToSocket)
|
||||||
sockets = append(sockets, socket)
|
sockets = append(sockets, socket)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue