Skip to content

Commit deea6b9

Browse files
committed
rfac: added ut for buildV4Metric
Signed-off-by: Yash Patel <yp969803@gmail.com>
1 parent c9e7568 commit deea6b9

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

pkg/controller/telemetry/metric_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package telemetry
1818

1919
import (
20+
"bytes"
2021
"context"
2122
"net"
2223
"reflect"
@@ -1677,3 +1678,96 @@ func TestMetricController_updatePrometheusMetric(t *testing.T) {
16771678
})
16781679
}
16791680
}
1681+
1682+
func TestBuildV4Metric(t *testing.T) {
1683+
buff := bytes.NewBuffer([]byte{10, 244, 1, 13, 10, 244, 1, 12, 34, 208, 144, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1684+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 96, 46, 224, 144, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1685+
0, 3, 0, 0, 0, 147, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 167, 122, 203, 84, 2, 0, 0, 0, 153, 163,
1686+
210, 202, 232, 184, 0, 0, 64, 30, 158, 31, 235, 184, 0, 0, 0, 0, 0, 0, 150, 158, 0, 0, 19, 0, 0, 0, 0, 0,
1687+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
1688+
data := requestMetric{
1689+
conSrcDstInfo: connectionSrcDst{
1690+
src: [4]uint32{218231818, 0, 0, 0},
1691+
dst: [4]uint32{201454602, 0, 0, 0},
1692+
srcPort: 53282,
1693+
dstPort: 8080,
1694+
},
1695+
origDstAddr: [4]uint32{3761135626, 0, 0, 0},
1696+
origDstPort: 8080,
1697+
direction: 2,
1698+
receivedBytes: 147,
1699+
sentBytes: 3,
1700+
state: 1,
1701+
success: 1,
1702+
duration: 10012555943,
1703+
startTime: 203309974725529,
1704+
lastReportTime: 203319987281472,
1705+
srtt: 40598,
1706+
minRtt: 19,
1707+
totalRetrans: 0,
1708+
packetLost: 0,
1709+
}
1710+
1711+
tests := []struct {
1712+
name string
1713+
tcpConns map[connectionSrcDst]connMetric
1714+
newConnMetric connMetric
1715+
}{
1716+
{
1717+
name: "v4 metric test, tcpConn is empty",
1718+
tcpConns: map[connectionSrcDst]connMetric{},
1719+
newConnMetric: connMetric{
1720+
receivedBytes: 147,
1721+
sentBytes: 3,
1722+
packetLost: 0,
1723+
totalRetrans: 0,
1724+
totalReports: 1,
1725+
},
1726+
},
1727+
{
1728+
name: "v4 metric test, tcpConns in not empty",
1729+
tcpConns: map[connectionSrcDst]connMetric{
1730+
data.conSrcDstInfo: {
1731+
receivedBytes: 1,
1732+
sentBytes: 1,
1733+
packetLost: 0,
1734+
totalRetrans: 0,
1735+
totalReports: 1,
1736+
},
1737+
},
1738+
newConnMetric: connMetric{
1739+
receivedBytes: 147,
1740+
sentBytes: 3,
1741+
packetLost: 0,
1742+
totalRetrans: 0,
1743+
totalReports: 2,
1744+
},
1745+
},
1746+
}
1747+
1748+
for _, tt := range tests {
1749+
t.Run(tt.name, func(t *testing.T) {
1750+
copiedBytes := make([]byte, len(buff.Bytes()))
1751+
copy(copiedBytes, buff.Bytes())
1752+
1753+
copiedBuff := bytes.NewBuffer(copiedBytes)
1754+
prevTcpConns := make(map[connectionSrcDst]connMetric)
1755+
for k, v := range tt.tcpConns {
1756+
prevTcpConns[k] = v
1757+
}
1758+
1759+
got, err := buildV4Metric(copiedBuff, tt.tcpConns)
1760+
if err != nil {
1761+
t.Errorf("buildV4Metric() error = %v", err)
1762+
}
1763+
assert.Equal(t, tt.newConnMetric, tt.tcpConns[data.conSrcDstInfo])
1764+
1765+
temp := data
1766+
temp.sentBytes = temp.sentBytes - prevTcpConns[temp.conSrcDstInfo].sentBytes
1767+
temp.receivedBytes = temp.receivedBytes - prevTcpConns[temp.conSrcDstInfo].receivedBytes
1768+
temp.packetLost = temp.packetLost - prevTcpConns[temp.conSrcDstInfo].packetLost
1769+
temp.totalRetrans = temp.totalRetrans - prevTcpConns[temp.conSrcDstInfo].totalRetrans
1770+
assert.Equal(t, temp, got)
1771+
})
1772+
}
1773+
}

0 commit comments

Comments
 (0)