forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbd_state_test.go
179 lines (149 loc) · 5.78 KB
/
bd_state_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
178
179
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package l2plugin_test
import (
"testing"
"git.fd.io/govpp.git/core"
"git.fd.io/govpp.git/adapter/mock"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/logging/logrus"
"github.com/ligato/vpp-agent/idxvpp/nametoidx"
l2api "github.com/ligato/vpp-agent/plugins/vpp/binapi/l2"
"github.com/ligato/vpp-agent/plugins/vpp/ifplugin/ifaceidx"
"github.com/ligato/vpp-agent/plugins/vpp/l2plugin"
"github.com/ligato/vpp-agent/plugins/vpp/l2plugin/l2idx"
intf "github.com/ligato/vpp-agent/plugins/vpp/model/interfaces"
"github.com/ligato/vpp-agent/plugins/vpp/model/l2"
"github.com/ligato/vpp-agent/tests/vppcallmock"
. "github.com/onsi/gomega"
"golang.org/x/net/context"
)
func bdStateTestInitialization(t *testing.T) (*l2plugin.BridgeDomainStateUpdater, *core.Connection, l2idx.BDIndexRW, ifaceidx.SwIfIndexRW, chan l2plugin.BridgeDomainStateMessage, chan *l2plugin.BridgeDomainStateNotification) {
RegisterTestingT(t)
// Initialize notification channel
notifChan := make(chan l2plugin.BridgeDomainStateMessage, 100)
// Initialize index
nameToIdx := nametoidx.NewNameToIdx(logrus.DefaultLogger(), "bd_index_test", l2idx.IndexMetadata)
index := l2idx.NewBDIndex(nameToIdx)
names := nameToIdx.ListNames()
// Check if names were empty
Expect(names).To(BeEmpty())
// Initialize sw if index
nameToIdxSW := nametoidx.NewNameToIdx(logrus.DefaultLogger(), "ifaceidx_test", ifaceidx.IndexMetadata)
swIfIndex := ifaceidx.NewSwIfIndex(nameToIdxSW)
names = nameToIdxSW.ListNames()
// Check if names were empty
Expect(names).To(BeEmpty())
// Create publish state function
publishChan := make(chan *l2plugin.BridgeDomainStateNotification, 100)
publishIfState := func(notification *l2plugin.BridgeDomainStateNotification) {
t.Logf("Received notification change %v", notification)
publishChan <- notification
}
// Create context
ctx, _ := context.WithCancel(context.Background())
// Create connection
mockCtx := &vppcallmock.TestCtx{
MockVpp: mock.NewVppAdapter(),
}
connection, err := core.Connect(mockCtx.MockVpp)
Expect(err).To(BeNil())
// Create plugin logger
pluginLogger := logging.ForPlugin("testname")
// Test initialization
plugin := &l2plugin.BridgeDomainStateUpdater{}
err = plugin.Init(ctx, pluginLogger, connection, index, swIfIndex, notifChan, publishIfState)
Expect(err).To(BeNil())
return plugin, connection, index, swIfIndex, notifChan, publishChan
}
func bdStateTestTeardown(plugin *l2plugin.BridgeDomainStateUpdater, conn *core.Connection) {
conn.Disconnect()
logging.DefaultRegistry.ClearRegistry()
}
// Tests notification processing in bridge domain state updater with zero index
func TestBridgeDomainStateUpdater_watchVppNotificationsZero(t *testing.T) {
plugin, conn, _, _, notifChan, publishChan := bdStateTestInitialization(t)
defer bdStateTestTeardown(plugin, conn)
// Test notifications
notifChan <- l2plugin.BridgeDomainStateMessage{
Name: "test",
Message: &l2api.BridgeDomainDetails{
BdID: 0,
},
}
var notif *l2plugin.BridgeDomainStateNotification
Eventually(publishChan).Should(Receive(¬if))
}
// Tests notification processing in bridge domain state updater with zero index and no name (invalid)
func TestBridgeDomainStateUpdater_watchVppNotificationsZeroNoName(t *testing.T) {
plugin, conn, _, _, notifChan, publishChan := bdStateTestInitialization(t)
defer bdStateTestTeardown(plugin, conn)
// Test notifications
notifChan <- l2plugin.BridgeDomainStateMessage{
Message: &l2api.BridgeDomainDetails{
BdID: 0,
},
}
var notif *l2plugin.BridgeDomainStateNotification
Eventually(publishChan).ShouldNot(Receive(¬if))
}
// Tests notification processing in bridge domain state updater
func TestBridgeDomainStateUpdater_watchVppNotifications(t *testing.T) {
plugin, conn, bdIndex, swIfIndex, notifChan, publishChan := bdStateTestInitialization(t)
defer bdStateTestTeardown(plugin, conn)
// Register interface name
swIfIndex.RegisterName("test", 1, &intf.Interfaces_Interface{
Name: "test",
Enabled: true,
Type: intf.InterfaceType_MEMORY_INTERFACE,
IpAddresses: []string{"192.168.0.1/24"},
})
// Register bridge domain name
bdIndex.RegisterName("bdTest", 1, &l2idx.BdMetadata{
ConfiguredInterfaces: []string{"test"},
BridgeDomain: &l2.BridgeDomains_BridgeDomain{},
})
// Test notifications
notifChan <- l2plugin.BridgeDomainStateMessage{
Name: "test",
Message: &l2api.BridgeDomainDetails{
BdID: 1,
Flood: 1,
UuFlood: 2,
Forward: 3,
Learn: 4,
ArpTerm: 5,
MacAge: 6,
BdTag: []byte("test"),
BviSwIfIndex: 1,
NSwIfs: 1,
SwIfDetails: []l2api.BridgeDomainSwIf{
{
SwIfIndex: 1,
Context: 0,
Shg: 20,
},
},
},
}
var notif *l2plugin.BridgeDomainStateNotification
Eventually(publishChan).Should(Receive(¬if))
Expect(notif.State).To(Not(BeNil()))
Expect(notif.State.BviInterface).To(Equal("test"))
Expect(notif.State.BviInterfaceIndex).To(BeEquivalentTo(1))
Expect(notif.State.InterfaceCount).To(BeEquivalentTo(1))
Expect(notif.State.Interfaces).To(Not(BeEmpty()))
Expect(notif.State.Interfaces[0].Name).To(Equal("test"))
Expect(notif.State.Interfaces[0].SplitHorizonGroup).To(BeEquivalentTo(20))
}