forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpdk_utils_test.go
137 lines (102 loc) · 3.98 KB
/
dpdk_utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// +build linux
package dpdk
import (
"fmt"
"net"
"os"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func Test_isSocket(t *testing.T) {
t.Run("when path points to non-existing file then error should be returned", func(t *testing.T) {
err := isSocket("/tmp/file-that-doesnt-exists")
require.Error(t, err)
require.Contains(t, err.Error(), "provided path does not exist")
})
t.Run("should pass if path points to socket", func(t *testing.T) {
pathToSocket, socket := createSocketForTest(t)
defer socket.Close()
err := isSocket(pathToSocket)
require.NoError(t, err)
})
t.Run("if path points to regular file instead of socket then error should be returned", func(t *testing.T) {
pathToFile := "/tmp/dpdk-text-file.txt"
_, err := os.Create(pathToFile)
require.NoError(t, err)
defer os.Remove(pathToFile)
err = isSocket(pathToFile)
require.Error(t, err)
require.Contains(t, err.Error(), "provided path does not point to a socket file")
})
}
func Test_stripParams(t *testing.T) {
command := "/mycommand"
params := "myParams"
t.Run("when passed string without params then passed string should be returned", func(t *testing.T) {
strippedCommand := stripParams(command)
require.Equal(t, command, strippedCommand)
})
t.Run("when passed string with params then string without params should be returned", func(t *testing.T) {
strippedCommand := stripParams(commandWithParams(command, params))
require.Equal(t, command, strippedCommand)
})
}
func Test_commandWithParams(t *testing.T) {
command := "/mycommand"
params := "myParams"
t.Run("when passed string with params then command with comma should be returned", func(t *testing.T) {
commandWithParams := commandWithParams(command, params)
require.Equal(t, command+","+params, commandWithParams)
})
t.Run("when passed command with no params then command should be returned", func(t *testing.T) {
commandWithParams := commandWithParams(command, "")
require.Equal(t, command, commandWithParams)
})
}
func Test_getParams(t *testing.T) {
command := "/mycommand"
params := "myParams"
t.Run("when passed string with params then command with comma should be returned", func(t *testing.T) {
commandParams := getParams(commandWithParams(command, params))
require.Equal(t, params, commandParams)
})
t.Run("when passed command with no params then empty string (representing empty params) should be returned", func(t *testing.T) {
commandParams := getParams(commandWithParams(command, ""))
require.Equal(t, "", commandParams)
})
}
func Test_jsonToArray(t *testing.T) {
key := "/ethdev/list"
t.Run("when got numeric array then string array should be returned", func(t *testing.T) {
firstValue := int64(0)
secondValue := int64(1)
jsonString := fmt.Sprintf(`{"%s": [%d, %d]}`, key, firstValue, secondValue)
arr, err := jsonToArray([]byte(jsonString), key)
require.NoError(t, err)
require.Equal(t, strconv.FormatInt(firstValue, 10), arr[0])
require.Equal(t, strconv.FormatInt(secondValue, 10), arr[1])
})
t.Run("if non-json string is supplied as input then error should be returned", func(t *testing.T) {
_, err := jsonToArray([]byte("{notAJson}"), key)
require.Error(t, err)
})
t.Run("when empty string is supplied as input then error should be returned", func(t *testing.T) {
jsonString := ""
_, err := jsonToArray([]byte(jsonString), key)
require.Error(t, err)
require.Contains(t, err.Error(), "got empty object instead of json")
})
t.Run("when valid json with json-object is supplied as input then error should be returned", func(t *testing.T) {
jsonString := fmt.Sprintf(`{"%s": {"testKey": "testValue"}}`, key)
_, err := jsonToArray([]byte(jsonString), key)
require.Error(t, err)
require.Contains(t, err.Error(), "failed to unmarshall json response")
})
}
func createSocketForTest(t *testing.T) (string, net.Listener) {
pathToSocket := "/tmp/dpdk-test-socket"
socket, err := net.Listen("unixpacket", pathToSocket)
require.NoError(t, err)
return pathToSocket, socket
}