-
Notifications
You must be signed in to change notification settings - Fork 10
/
load.go
136 lines (112 loc) · 4.3 KB
/
load.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
// SPDX-FileCopyrightText: 2022-present Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
package basic
import (
"context"
"github.com/onosproject/fabric-sim/pkg/topo"
"github.com/onosproject/fabric-sim/test/client"
simapi "github.com/onosproject/onos-api/go/onos/fabricsim"
"github.com/onosproject/onos-net-lib/pkg/p4utils"
p4api "github.com/p4lang/p4runtime/go/p4/v1"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"strings"
"testing"
)
// TestTopologyLoad loads simulator with custom.yaml topology and validates proper startup
func (s *TestSuite) TestTopologyLoad(t *testing.T) {
devices, _, _ := LoadAndValidate(t, "topologies/custom.yaml", 6, 2*8, 16,
spineAndLeafPorts, func(host *simapi.Host) int { return 1 })
defer CleanUp(t)
ProbeAllDevices(t, devices)
}
func spineAndLeafPorts(device *simapi.Device) int {
if strings.Contains(string(device.ID), "spine") {
return 4
}
return 8
}
// DevicePortCount returns the expected number of ports for a devuce
type DevicePortCount func(device *simapi.Device) int
// HostNICCount returns the expected number of NICs for a host
type HostNICCount func(host *simapi.Host) int
// LoadAndValidate loads the specified topology and validates the correct counts of devices, links and hosts
func LoadAndValidate(t *testing.T, path string, devices int, links int, hosts int,
portsPerDevice DevicePortCount, nicsPerHost HostNICCount) ([]*simapi.Device, []*simapi.Link, []*simapi.Host) {
conn, err := client.CreateConnection()
assert.NoError(t, err)
defer conn.Close()
err = topo.ClearTopology(conn)
assert.NoError(t, err)
err = topo.LoadTopology(conn, path)
assert.NoError(t, err)
// Validate that everything got loaded correctly
deviceClient := simapi.NewDeviceServiceClient(conn)
linkClient := simapi.NewLinkServiceClient(conn)
hostClient := simapi.NewHostServiceClient(conn)
t.Logf("Validating topology")
// Do we have all the devices?
ctx := context.Background()
dr, err := deviceClient.GetDevices(ctx, &simapi.GetDevicesRequest{})
assert.NoError(t, err)
assert.Equal(t, devices, len(dr.Devices))
// Do we have all the links?
lr, err := linkClient.GetLinks(ctx, &simapi.GetLinksRequest{})
assert.NoError(t, err)
assert.Equal(t, links, len(lr.Links))
// Do we have all the hosts?
hr, err := hostClient.GetHosts(ctx, &simapi.GetHostsRequest{})
assert.NoError(t, err)
assert.Equal(t, hosts, len(hr.Hosts))
// What about all the device ports?
for _, device := range dr.Devices {
assert.Equal(t, portsPerDevice(device), len(device.Ports))
}
// What about all the host NICs?
for _, host := range hr.Hosts {
assert.Equal(t, nicsPerHost(host), len(host.Interfaces))
}
return dr.Devices, lr.Links, hr.Hosts
}
// CleanUp cleans up the simulation
func CleanUp(t *testing.T) {
t.Log("Cleaning up topology")
if conn, err := client.CreateConnection(); err == nil {
if err := topo.ClearTopology(conn); err != nil {
t.Log("Unable to clear topology")
assert.NoError(t, err)
}
} else {
t.Log("Unable to clear topology; no connection")
t.Fail()
}
}
// ProbeAllDevices tests each device P4Runtime agent port by requesting capabilities
func ProbeAllDevices(t *testing.T, devices []*simapi.Device) {
ctx := context.Background()
for _, device := range devices {
t.Logf("Connecting to agent for device %s", device.ID)
p4Client, p4conn := GetP4Client(t, device)
defer p4conn.Close()
t.Logf("Getting P4 capabilities device %s", device.ID)
cr, err := p4Client.Capabilities(ctx, &p4api.CapabilitiesRequest{})
assert.NoError(t, err)
assert.Equal(t, "1.1.0", cr.P4RuntimeApiVersion)
// Open message stream and negotiate mastership for default (no) role
t.Logf("Negotiating mastership for device %s", device.ID)
stream, err := p4Client.StreamChannel(ctx)
assert.NoError(t, err)
err = stream.Send(p4utils.CreateMastershipArbitration(&p4api.Uint128{High: 0, Low: 1}, nil))
assert.NoError(t, err)
msg, err := stream.Recv()
assert.NoError(t, err)
assert.Equal(t, int32(0), msg.GetArbitration().Status.Code)
}
}
// GetP4Client returns a new P4Runtime service client and its underlying connection for the given device
func GetP4Client(t *testing.T, device *simapi.Device) (p4api.P4RuntimeClient, *grpc.ClientConn) {
conn, err := client.CreateDeviceConnection(device)
assert.NoError(t, err)
return p4api.NewP4RuntimeClient(conn), conn
}