-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtap_test.go
177 lines (149 loc) · 3.75 KB
/
tap_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// November 2016, cisco
//
// Copyright (c) 2016 by cisco Systems, Inc.
// All rights reserved.
//
//
package main
import (
samples "github.com/cisco-ie/pipeline-gnmi/mdt_msg_samples"
"github.com/dlintw/goconf"
"testing"
"time"
)
type tapTestCtrl struct {
}
func (t *tapTestCtrl) String() string {
return "TapTest"
}
//
// TestTAPBinaryWriteRead reads a sample message, generates the binary
// dump file, and rereads that file using tap in
func TestTAPBinaryWriteRead(t *testing.T) {
var nc nodeConfig
var tapTestCtrlSrc tapTestCtrl
const numDataMsg = 10
logger.Info("Start TestTAPBinaryWriteRead")
sample := samples.MDTSampleTelemetryTableFetchOne(
samples.SAMPLE_TELEMETRY_DATABASE_BASIC)
if sample == nil {
t.Errorf("Failed to fetch data")
return
}
err, codec := getNewCodecGPB("test", ENCODING_GPB)
if err != nil {
t.Error("Failed to get a GPB codec to test")
}
err, msgs := codec.blockToDataMsgs(&tapTestCtrlSrc, sample.SampleStreamGPB)
if err != nil {
t.Error("Failed to get a GPB codec to test")
}
logger.Info("Read config")
cfg, err := goconf.ReadConfigFile("pipeline_test_tap.conf")
if err != nil {
t.Fatal("read config failed")
}
section := "tap_out_bin"
nc.config = cfg
out := tapOutputModuleNew()
err, inject, ocmc := out.configure(section, nc)
if err != nil {
t.Errorf("tap section [%v] failed\n", section)
}
logger.Info("Injecting content")
for i := 0; i < numDataMsg; i++ {
inject <- msgs[0]
//
// Now that messages are injected, we should be able to pick
// them up again.
}
in := replayInputModuleNew()
section = "replay_bin"
dataChan := make(chan dataMsg, 1000)
dataChans := []chan<- dataMsg{dataChan}
err, icmc := in.configure(section, nc, dataChans)
if err != nil {
t.Errorf("tap section [%v] failed\n", section)
}
logger.Info("Now let's read it")
var i int
for i = 0; i < numDataMsg; i++ {
<-dataChan
}
if i != numDataMsg {
t.Fatalf("Received %d out of 10 messages", i+1)
}
respChan := make(chan *ctrlMsg)
request := &ctrlMsg{
id: SHUTDOWN,
respChan: respChan,
}
logger.Info("Shutdown reader")
icmc <- request
// Wait for ACK
ack := <-respChan
if ack.id != ACK {
t.Error("failed to recieve ack for tap in shutdown complete")
}
logger.Info("Shutdown writer")
ocmc <- request
// Wait for ACK
ack = <-respChan
if ack.id != ACK {
t.Error("failed to recieve ack for tap out shutdown complete")
}
}
//
// TestTAPBinaryReadWrite taps in from a binary dump, and produces a
// hex dump
func TestTAPBinaryReadWrite(t *testing.T) {
var nc nodeConfig
cfg, err := goconf.ReadConfigFile("pipeline_test.conf")
if err != nil {
t.Error("read config failed")
}
nc.config = cfg
out := tapOutputModuleNew()
section := "tap_out_bin_hexdump"
err, inject, ocmc := out.configure(section, nc)
if err != nil {
t.Errorf("tap section [%v] failed\n", section)
}
in := replayInputModuleNew()
section = "replay_bin_archive"
dataChan := make(chan dataMsg, 1000)
dataChans := []chan<- dataMsg{dataChan}
err, icmc := in.configure(section, nc, dataChans)
if err != nil {
t.Errorf("tap section [%v] failed\n", section)
}
//
// 7 message in replay_bin_archive
for i := 0; i < 7; i++ {
dM := <-dataChan
inject <- dM
}
//
// Check handling of closing of data chan before shutdown giving
// handler opportunity to exercise before actually shutting down.
close(inject)
time.Sleep(100 * time.Millisecond)
respChan := make(chan *ctrlMsg)
request := &ctrlMsg{
id: SHUTDOWN,
respChan: respChan,
}
icmc <- request
// Wait for ACK
ack := <-respChan
if ack.id != ACK {
t.Error("failed to recieve ack for tap in shutdown complete")
}
ocmc <- request
// Wait for ACK
ack = <-respChan
if ack.id != ACK {
t.Error("failed to recieve ack for tap out shutdown complete")
}
}