forked from openconfig/featureprofiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgnmi_ni_test.go
More file actions
142 lines (118 loc) · 5.1 KB
/
gnmi_ni_test.go
File metadata and controls
142 lines (118 loc) · 5.1 KB
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
package gnmi_ni_test
import (
"testing"
"github.com/openconfig/featureprofiles/internal/attrs"
"github.com/openconfig/featureprofiles/internal/cfgplugins"
"github.com/openconfig/featureprofiles/internal/deviations"
"github.com/openconfig/featureprofiles/internal/fptest"
"github.com/openconfig/ondatra"
"github.com/openconfig/ondatra/gnmi"
"github.com/openconfig/ondatra/gnmi/oc"
)
func TestMain(m *testing.M) {
fptest.RunTests(m)
}
const (
customVRFName = "customVRFName"
)
var (
dutPort1 = attrs.Attributes{
Desc: "dutPort1",
IPv4: "192.0.2.1",
IPv4Len: 30,
IPv6: "2001:0db8::192:0:2:1",
IPv6Len: 126,
Subinterface: 0,
}
dutPort2 = attrs.Attributes{
Desc: "dutPort2",
IPv4: "192.0.2.5",
IPv4Len: 30,
IPv6: "2001:0db8::192:0:2:5",
IPv6Len: 126,
Subinterface: 0,
}
dutPort1NetworkInstanceIParams = cfgplugins.NetworkInstanceParams{
Name: "DEFAULT",
Default: true,
}
dutPort2NetworkInstanceIParams = cfgplugins.NetworkInstanceParams{
Name: customVRFName,
Default: false,
}
)
func TestCodelab(t *testing.T) {
// configure DUT
dut := ondatra.DUT(t, "dut")
batch := &gnmi.SetBatch{}
ConfigureDUT(batch, t, dut)
ConfigureAdditionalNetworkInstance(batch, t, dut, customVRFName)
}
// ConfigureDUT configures port1 and port2 on the DUT with default network instance.
func ConfigureDUT(batch *gnmi.SetBatch, t *testing.T, dut *ondatra.DUTDevice) {
dp1 := dut.Port(t, "port1")
t.Logf("\nConfiguring network instance and gNMI server: Network instance: %s \n", dutPort1NetworkInstanceIParams.Name)
// Configure default network instance.
cfgplugins.NewNetworkInstance(t, batch, dut, &dutPort1NetworkInstanceIParams)
// Configure gNMI server on default network instance.
cfgplugins.CreateGNMIServer(batch, t, dut, &dutPort1NetworkInstanceIParams)
// Configuring basic interface and network instance as some devices only populate OC after configuration.
port1IntfPath := dutPort1.NewOCInterface(dp1.Name(), dut)
gnmi.BatchUpdate(batch, gnmi.OC().Interface(dp1.Name()).Config(), port1IntfPath)
// Deviations for vendors that require explicit interface to network instance assignment.
if deviations.ExplicitInterfaceInDefaultVRF(dut) {
cfgplugins.AssignInterfaceToNetworkInstance(t, batch, dut, dp1.Name(), &dutPort1NetworkInstanceIParams, 0)
}
}
// ConfigureAdditionalNetworkInstance configures a new network instance in DUT and changes the network instance of port2
func ConfigureAdditionalNetworkInstance(batch *gnmi.SetBatch, t *testing.T, dut *ondatra.DUTDevice, ni string) {
// Configure interface, non-default network instance
t.Logf("\nConfiguring network instance and gNMI server: Network instance: %s \n", ni)
cfgplugins.NewNetworkInstance(t, batch, dut, &dutPort2NetworkInstanceIParams)
// Configure non-default gnmi server.
cfgplugins.CreateGNMIServer(batch, t, dut, &dutPort2NetworkInstanceIParams)
// Assign port2 to custom network instance for all vendors
dp2 := dut.Port(t, "port2")
port2IntfPath := dutPort2.NewOCInterface(dp2.Name(), dut)
gnmi.BatchUpdate(batch, gnmi.OC().Interface(dp2.Name()).Config(), port2IntfPath)
cfgplugins.AssignInterfaceToNetworkInstance(t, batch, dut, dp2.Name(), &dutPort2NetworkInstanceIParams, 0)
t.Log("\nApplying configuration to DUT\n")
batch.Set(t, dut)
for _, netInstance := range gnmi.GetAll(t, dut, gnmi.OC().NetworkInstanceAny().State()) {
t.Logf("Network instance: %s", netInstance.GetName())
}
// Get and validate states for default and custom networkinstances.
gnmiServerList := gnmi.GetAll(t, dut, gnmi.OC().System().GrpcServerAny().State())
for _, gnmiServer := range gnmiServerList {
if gnmiServer.GetName() == deviations.DefaultNetworkInstance(dut) && dut.Vendor() != ondatra.CISCO {
defaultInstanceState := gnmi.Get(t, dut, gnmi.OC().System().GrpcServer(deviations.DefaultNetworkInstance(dut)).State())
validateGnmiServerState(t, defaultInstanceState)
}
if gnmiServer.GetName() == deviations.GrpcDefaultServerName(dut) {
defaultInstanceState := gnmi.Get(t, dut, gnmi.OC().System().GrpcServer(deviations.GrpcDefaultServerName(dut)).State())
validateGnmiServerState(t, defaultInstanceState)
}
if gnmiServer.GetName() == customVRFName {
customInstanceState := gnmi.Get(t, dut, gnmi.OC().System().GrpcServer(customVRFName).State())
validateGnmiServerState(t, customInstanceState)
}
}
// Two Servers should be running on the DUT.
gnmiServerCount := len(gnmi.GetAll(t, dut, gnmi.OC().System().GrpcServerAny().State()))
// Two gNMI servers or more are expected.
if gnmiServerCount < 2 {
t.Fatalf("Expected 2+ gNMI servers, got %d.", gnmiServerCount)
}
}
// validateGnmiServerState checks and logs the state of a gNMI server.
func validateGnmiServerState(t *testing.T, state *oc.System_GrpcServer) {
if state == nil {
t.Errorf("gNMI server state is nil")
return
}
if !state.GetEnable() {
t.Errorf("Expected gNMI server '%s' to be enabled, but it is not.", state.GetName())
}
t.Logf("gNMI Server: %s, running on network instance: %s, listening port: %v, Enabled: %t",
state.GetName(), state.GetNetworkInstance(), state.GetPort(), state.GetEnable())
}