forked from CiscoDevNet/bigmuddy-network-telemetry-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencap_st_test.go
131 lines (111 loc) · 2.97 KB
/
encap_st_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
//
// February 2016, cisco
//
// Copyright (c) 2016 by cisco Systems, Inc.
// All rights reserved.
//
//
package main
import (
"bytes"
"encoding/binary"
samples "github.com/cisco/bigmuddy-network-telemetry-pipeline/mdt_msg_samples"
"testing"
)
type testSource struct{}
func (t *testSource) String() string {
return "TEST source"
}
var encapSTTestSource testSource
func TestSTParser(t *testing.T) {
err, gp := getNewEncapSTParser("ENCAP ST TEST", &encapSTTestSource)
if err != nil {
t.Errorf("Failed to get ST test parser: %v", err)
}
// Type assert gp to the more specific so we can manipulate ST
// specific state.
p := gp.(*encapSTParser)
err, _ = p.nextBlockBuffer()
if err != nil {
t.Errorf("Failed to get header buffer: %v", err)
}
//
// Cheat to force error cases
p.state = ENC_ST_WAIT_FOR_DATA
err, _ = p.nextBlockBuffer()
if err == nil {
t.Errorf("Should have failed with request to get 0 size buffer")
}
p.nextBlockSize = ENC_ST_MAX_PAYLOAD + 1
err, _ = p.nextBlockBuffer()
if err == nil {
t.Errorf("Should have failed with request to get oversize buffer")
}
//
// Get a GPB message and encap it in ST header.
sample := samples.MDTSampleTelemetryTableFetchOne(
samples.SAMPLE_TELEMETRY_DATABASE_BASIC)
if sample == nil {
t.Errorf("Failed to fetch data")
return
}
payload := sample.SampleStreamGPB
encap := encapSTHdr{
MsgType: ENC_ST_HDR_MSG_TYPE_TELEMETRY_DATA,
MsgEncap: ENC_ST_HDR_MSG_ENCAP_GPB,
MsgHdrVersion: ENC_ST_HDR_VERSION,
Msgflag: ENC_ST_HDR_MSG_FLAGS_NONE,
Msglen: uint32(len(payload)),
}
encodedMsgForLen := make([]byte, 0, len(payload)+256)
hdrbuf := bytes.NewBuffer(encodedMsgForLen)
err = binary.Write(hdrbuf, binary.BigEndian, &encap)
if err != nil {
t.Errorf("Failed to write data header")
return
}
_, err = hdrbuf.Write(payload)
if err != nil {
t.Errorf("Failed write data 1")
return
}
encodedMsg := hdrbuf.Bytes()
// Star afresh
err, gp = getNewEncapSTParser("ENCAP ST TEST", &encapSTTestSource)
if err != nil {
t.Errorf("Failed to get fresh ST test parser: %v", err)
}
p = gp.(*encapSTParser)
err, dMs := p.nextBlock(encodedMsg, nil)
if err != nil {
t.Errorf("Failed to parse header: %v", err)
}
if dMs != nil {
t.Errorf("Expected header but got data.")
}
if p.state != ENC_ST_WAIT_FOR_DATA {
t.Errorf("Failed to transition to wait for payload")
}
if p.nextBlockSize != uint32(len(payload)) {
t.Errorf("Failed to parser and track msg len")
}
err, dataBuf := p.nextBlockBuffer()
if err != nil {
t.Errorf("Failed to get data buffer")
}
if len(*dataBuf) != len(payload) {
t.Errorf("Databuf returned len %d expected %d",
len(*dataBuf), 16)
}
err, dMs = p.nextBlock(payload, nil)
if err != nil {
t.Errorf("Failed to get data message")
}
if dMs == nil || len(dMs) != 1 {
t.Errorf("Failed to extract expected data")
}
err, _ = dMs[0].produceByteStream(dataMsgStreamSpecDefault)
if err != nil {
t.Errorf("Failed to produce byte stream from dataMsg")
}
}