mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-11 22:14:02 +01:00
flow: encode boundary as a string for JSON
This is only used for debugging.
This commit is contained in:
@@ -271,6 +271,8 @@ func TestCore(t *testing.T) {
|
||||
"OutIfName": "Gi0/0/677",
|
||||
"InIfSpeed": 1000,
|
||||
"OutIfSpeed": 1000,
|
||||
"InIfBoundary": "UNDEFINED",
|
||||
"OutIfBoundary": "UNDEFINED",
|
||||
"DstCountry": "GB",
|
||||
"SrcCountry": "BT",
|
||||
"SrcAS": 35908,
|
||||
|
||||
@@ -12,6 +12,8 @@ type prettierFlowMessage struct {
|
||||
PrettierSrcAddr string `json:"SrcAddr,omitempty"`
|
||||
PrettierDstAddr string `json:"DstAddr,omitempty"`
|
||||
PrettierSamplerAddress string `json:"SamplerAddress,omitempty"`
|
||||
PrettierInIfBoundary string `json:"InIfBoundary,omitempty"`
|
||||
PrettierOutIfBoundary string `json:"OutIfBoundary,omitempty"`
|
||||
}
|
||||
|
||||
// MarshalJSON marshals a flow message to JSON. It uses a textual
|
||||
@@ -23,6 +25,8 @@ func (fm FlowMessage) MarshalJSON() ([]byte, error) {
|
||||
PrettierSrcAddr: net.IP(fm.SrcAddr).String(),
|
||||
PrettierDstAddr: net.IP(fm.DstAddr).String(),
|
||||
PrettierSamplerAddress: net.IP(fm.SamplerAddress).String(),
|
||||
PrettierInIfBoundary: fm.InIfBoundary.String(),
|
||||
PrettierOutIfBoundary: fm.OutIfBoundary.String(),
|
||||
}
|
||||
prettier.SrcAddr = nil
|
||||
prettier.DstAddr = nil
|
||||
|
||||
68
flow/encoder_test.go
Normal file
68
flow/encoder_test.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package flow
|
||||
|
||||
import (
|
||||
"akvorado/helpers"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestJSONEncoding(t *testing.T) {
|
||||
flow := &FlowMessage{
|
||||
TimeReceived: 200,
|
||||
SequenceNum: 1000,
|
||||
SamplingRate: 1000,
|
||||
FlowDirection: 1,
|
||||
SamplerAddress: net.ParseIP("192.0.2.42"),
|
||||
TimeFlowStart: 100,
|
||||
TimeFlowEnd: 200,
|
||||
Bytes: 6765,
|
||||
Packets: 4,
|
||||
InIf: 300,
|
||||
OutIf: 200,
|
||||
SrcAddr: net.ParseIP("67.43.156.77"),
|
||||
DstAddr: net.ParseIP("2.125.160.216"),
|
||||
Etype: 0x800,
|
||||
Proto: 6,
|
||||
SrcPort: 8534,
|
||||
DstPort: 80,
|
||||
InIfProvider: "Telia",
|
||||
InIfBoundary: FlowMessage_EXTERNAL,
|
||||
OutIfBoundary: FlowMessage_INTERNAL,
|
||||
}
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
encoder := json.NewEncoder(buf)
|
||||
encoder.SetIndent("", " ")
|
||||
if err := encoder.Encode(flow); err != nil {
|
||||
t.Fatalf("Encode() error:\n%+v", err)
|
||||
}
|
||||
got := strings.Split(buf.String(), "\n")
|
||||
expected := strings.Split(`{
|
||||
"TimeReceived": 200,
|
||||
"SequenceNum": 1000,
|
||||
"SamplingRate": 1000,
|
||||
"FlowDirection": 1,
|
||||
"TimeFlowStart": 100,
|
||||
"TimeFlowEnd": 200,
|
||||
"Bytes": 6765,
|
||||
"Packets": 4,
|
||||
"Etype": 2048,
|
||||
"Proto": 6,
|
||||
"SrcPort": 8534,
|
||||
"DstPort": 80,
|
||||
"InIf": 300,
|
||||
"OutIf": 200,
|
||||
"InIfProvider": "Telia",
|
||||
"SrcAddr": "67.43.156.77",
|
||||
"DstAddr": "2.125.160.216",
|
||||
"SamplerAddress": "192.0.2.42",
|
||||
"InIfBoundary": "EXTERNAL",
|
||||
"OutIfBoundary": "INTERNAL"
|
||||
}
|
||||
`, "\n")
|
||||
if diff := helpers.Diff(got, expected); diff != "" {
|
||||
t.Errorf("Encode() (-got, +want):\n%s", diff)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user