-
Notifications
You must be signed in to change notification settings - Fork 37
/
ondatra_test.go
274 lines (254 loc) · 7.87 KB
/
ondatra_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2021 Google LLC
//
// 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
//
// https://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 ondatra
import (
"errors"
"os"
"strings"
"testing"
"time"
"golang.org/x/net/context"
"flag"
"github.com/openconfig/ondatra/binding"
"github.com/openconfig/ondatra/fakebind"
"github.com/openconfig/testt"
opb "github.com/openconfig/ondatra/proto"
)
func TestRunTests(t *testing.T) {
emptyTB, err := os.CreateTemp(t.TempDir(), "*.textproto")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
if err := emptyTB.Close(); err != nil {
t.Errorf("Failed to close temp file: %v", err)
}
flag.Set("testbed", emptyTB.Name())
tests := []struct {
desc string
interrupt bool
reserveErr, releaseErr error
wantErr string
}{{
desc: "success",
}, {
desc: "reserve error",
reserveErr: errors.New("reserve error"),
wantErr: "reserve error",
}, {
desc: "release error",
releaseErr: errors.New("release error"),
wantErr: "release error",
}, {
desc: "interrupted",
interrupt: true,
}}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
fakeBind := fakebind.Setup()
fakeBind.ReserveFn = func(context.Context, *opb.Testbed, time.Duration, time.Duration, map[string]string) (*binding.Reservation, error) {
return new(binding.Reservation), test.reserveErr
}
releaseCh := make(chan struct{}, 1)
defer close(releaseCh)
fakeBind.ReleaseFn = func(context.Context) error {
releaseCh <- struct{}{}
return test.releaseErr
}
runFn := func() int {
if test.interrupt {
sigc <- os.Interrupt
// Wait for the release immediately to confirm it is triggered from
// the interrupt and not from the release at the end of runTests.
<-releaseCh
}
return 0
}
gotErr := runTests(runFn, func() (binding.Binding, error) { return fakeBind, nil })
if (gotErr == nil) != (test.wantErr == "") || (gotErr != nil && !strings.Contains(gotErr.Error(), test.wantErr)) {
t.Errorf("runTests() got err %v, want %q", gotErr, test.wantErr)
}
// If there was no error reserving and the test wasn't interrupted
// wait for the deferred release at the end of runTests.
if test.reserveErr == nil && !test.interrupt {
<-releaseCh
}
})
}
}
func TestGetters(t *testing.T) {
const dutID, ateID, portID = "dut", "ate", "port"
fakeRes := &binding.Reservation{
DUTs: map[string]binding.DUT{
dutID: &fakebind.DUT{
AbstractDUT: &binding.AbstractDUT{Dims: &binding.Dims{
Name: "fakeDUT.net",
Vendor: opb.Device_OPENCONFIG,
HardwareModel: "fakeDUTModel",
SoftwareVersion: "fakeDUTVersion",
Ports: map[string]*binding.Port{portID: &binding.Port{
CardModel: "fakeDUTCardModel",
PMD: opb.Port_PMD_10GBASE_LR,
Speed: opb.Port_S_10GB,
}},
}},
},
},
ATEs: map[string]binding.ATE{
ateID: &fakebind.ATE{
AbstractATE: &binding.AbstractATE{Dims: &binding.Dims{
Name: "fakeATE.net",
Vendor: opb.Device_IXIA,
HardwareModel: "fakeATEModel",
SoftwareVersion: "fakeATEVersion",
Ports: map[string]*binding.Port{portID: &binding.Port{
CardModel: "fakeATECardModel",
PMD: opb.Port_PMD_100GBASE_FR,
Speed: opb.Port_S_100GB,
}},
}},
},
},
}
fakebind.Setup().WithReservation(fakeRes)
t.Run("DUT", func(t *testing.T) {
d := DUT(t, dutID)
if got, want := d.ID(), dutID; got != want {
t.Errorf("DUT id = %q, want %q", got, want)
}
if got, want := d.Name(), "fakeDUT.net"; got != want {
t.Errorf("DUT name = %q, want %q", got, want)
}
if got, want := d.Vendor(), OPENCONFIG; got != want {
t.Errorf("DUT vendor = %v, want %v", got, want)
}
if got, want := d.Model(), "fakeDUTModel"; got != want {
t.Errorf("DUT model = %v, want %v", got, want)
}
if got, want := d.Version(), "fakeDUTVersion"; got != want {
t.Errorf("DUT version = %v, want %v", got, want)
}
})
t.Run("DUTs", func(t *testing.T) {
duts := DUTs(t)
d, ok := duts[dutID]
if !ok {
t.Errorf("DUT id %q not present in DUTs map %q, must be present", dutID, duts)
}
if got, want := d.ID(), dutID; got != want {
t.Errorf("DUT id = %q, want %q", got, want)
}
})
t.Run("DUT failure", func(t *testing.T) {
id := "gaga"
got := testt.ExpectFatal(t, func(t testing.TB) {
DUT(t, id)
})
if !strings.Contains(got, id) {
t.Errorf("DUT(%q) failed with message %q, want %q", id, got, id)
}
})
t.Run("ATE", func(t *testing.T) {
a := ATE(t, ateID)
if got, want := a.ID(), ateID; got != want {
t.Errorf("ATE id = %q, want %q", got, want)
}
if got, want := a.Name(), "fakeATE.net"; got != want {
t.Errorf("ATE name = %q, want %q", got, want)
}
if got, want := a.Vendor(), IXIA; got != want {
t.Errorf("ATE vendor = %v, want %v", got, want)
}
if got, want := a.Model(), "fakeATEModel"; got != want {
t.Errorf("ATE model = %v, want %v", got, want)
}
if got, want := a.Version(), "fakeATEVersion"; got != want {
t.Errorf("ATE version = %v, want %v", got, want)
}
})
t.Run("ATEs", func(t *testing.T) {
ates := ATEs(t)
a, ok := ates[ateID]
if !ok {
t.Errorf("ATE id %q not present in ATEs map %q, must be present", ateID, ates)
}
if got, want := a.ID(), ateID; got != want {
t.Errorf("ATE id = %q, want %q", got, want)
}
})
t.Run("ATE failure", func(t *testing.T) {
id := "gaga"
got := testt.ExpectFatal(t, func(t testing.TB) {
ATE(t, id)
})
if !strings.Contains(got, id) {
t.Errorf("ATE(%q) failed with message %q, want %q", id, got, id)
}
})
t.Run("DUT Port", func(t *testing.T) {
p := DUT(t, dutID).Port(t, portID)
if got, want := p.ID(), portID; got != want {
t.Errorf("port id = %q, want %q", got, want)
}
if got, want := p.Device().ID(), dutID; got != want {
t.Errorf("port device id = %q, want %q", got, want)
}
if got, want := p.Speed(), Speed10Gb; got != want {
t.Errorf("port speed = %d, want %d", got, want)
}
if got, want := p.CardModel(), "fakeDUTCardModel"; got != want {
t.Errorf("card model = %q, want %q", got, want)
}
if got, want := p.PMD(), PMD10GBASELR; got != want {
t.Errorf("pmd = %q, want %q", got, want)
}
})
t.Run("DUT Port failure", func(t *testing.T) {
const portID = "gaga"
d := DUT(t, dutID)
got := testt.ExpectFatal(t, func(t testing.TB) {
d.Port(t, portID)
})
if !strings.Contains(got, portID) {
t.Errorf("Port(%q) failed with message %q, want %q", portID, got, portID)
}
})
t.Run("ATE Port", func(t *testing.T) {
p := ATE(t, ateID).Port(t, portID)
if got, want := p.ID(), portID; got != want {
t.Errorf("port id = %q, want %q", got, want)
}
if got, want := p.Device().ID(), ateID; got != want {
t.Errorf("port device id = %q, want %q", got, want)
}
if got, want := p.Speed(), Speed100Gb; got != want {
t.Errorf("port speed = %d, want %d", got, want)
}
if got, want := p.CardModel(), "fakeATECardModel"; got != want {
t.Errorf("card model = %q, want %q", got, want)
}
if got, want := p.PMD(), PMD100GBASEFR; got != want {
t.Errorf("pmd = %q, want %q", got, want)
}
})
t.Run("ATE Port failure", func(t *testing.T) {
const portID = "gaga"
a := ATE(t, ateID)
got := testt.ExpectFatal(t, func(t testing.TB) {
a.Port(t, portID)
})
if !strings.Contains(got, portID) {
t.Errorf("Port(%q) failed with message %q, want %q", portID, got, portID)
}
})
}