-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathruntests.go
More file actions
127 lines (117 loc) · 4.26 KB
/
runtests.go
File metadata and controls
127 lines (117 loc) · 4.26 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
// Copyright 2022 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
//
// 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 fptest
import (
"flag"
"fmt"
"path/filepath"
"strconv"
"testing"
"unicode/utf8"
log "github.com/golang/glog"
"github.com/openconfig/featureprofiles/internal/metadata"
"github.com/openconfig/featureprofiles/internal/pathutil"
mpb "github.com/openconfig/featureprofiles/proto/metadata_go_proto"
"github.com/openconfig/featureprofiles/topologies/binding"
gpb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/ondatra"
"github.com/openconfig/ygnmi/ygnmi"
)
// RunTests initializes the appropriate binding and runs the tests.
// It should be called from every featureprofiles tests like this:
//
// package test
//
// import "github.com/openconfig/featureprofiles/internal/fptest"
//
// func TestMain(m *testing.M) {
// fptest.RunTests(m)
// }
func RunTests(m *testing.M) {
if err := initMetadata(); err != nil {
log.Errorf("Unable to initialize test metadata: %v", err)
}
ygnmi.WithDatapointValidator(datapointValidator)
ondatra.RunTests(m, binding.New)
}
func initMetadata() error {
if err := metadata.Init(); err != nil {
return err
}
// Set the testbed path from the metadata if it is not set.
flag.Parse()
if flagVal := flag.Lookup("testbed").Value; flagVal.String() == "" {
testbedPath, err := testbedPathFromMetadata()
if err != nil {
return err
}
if err := flagVal.Set(testbedPath); err != nil {
return err
}
log.Infof("Testbed flag set from metadata to %q", testbedPath)
}
return nil
}
func testbedPathFromMetadata() (string, error) {
testbed := metadata.Get().Testbed
testbedToFile := map[mpb.Metadata_Testbed]string{
mpb.Metadata_TESTBED_DUT: "dut.testbed",
mpb.Metadata_TESTBED_DUT_DUT_4LINKS: "dutdut.testbed",
mpb.Metadata_TESTBED_DUT_ATE_2LINKS: "atedut_2.testbed",
mpb.Metadata_TESTBED_DUT_ATE_4LINKS: "atedut_4.testbed",
mpb.Metadata_TESTBED_DUT_ATE_9LINKS_LAG: "atedut_9_lag.testbed",
mpb.Metadata_TESTBED_DUT_DUT_ATE_2LINKS: "dutdutate.testbed",
mpb.Metadata_TESTBED_DUT_ATE_8LINKS: "atedut_8.testbed",
mpb.Metadata_TESTBED_DUT_400ZR: "dut_400zr.testbed",
mpb.Metadata_TESTBED_DUT_400ZR_PLUS: "dut_400zr_plus.testbed",
mpb.Metadata_TESTBED_DUT_800ZR: "dut_800zr.testbed",
mpb.Metadata_TESTBED_DUT_400ZR_100G_4LINKS: "dut_400zr_100g_4links.testbed",
mpb.Metadata_TESTBED_DUT_400FR_100G_4LINKS: "dut_400fr_100g_4links.testbed",
mpb.Metadata_TESTBED_DUT_2LINKS: "dut_2links.testbed",
}
testbedFile, ok := testbedToFile[testbed]
if !ok {
return "", fmt.Errorf("no testbed file for testbed %v", testbed)
}
rootPath, err := pathutil.RootPath()
if err != nil {
return "", err
}
return filepath.Join(rootPath, "topologies", testbedFile), nil
}
// datapointValidator is a ygnmi.ValidateFn that validates the timestamp of an input datapoint.
// It is called for each gNMI datapoint (<timestamp, path, value> tuple) received by any test that
// uses the ONDATRA gnmi library.
func datapointValidator(dp *ygnmi.DataPoint) error {
// Validate the timestamp
if !dp.Timestamp.IsZero() {
ns := dp.Timestamp.UnixNano()
if len(strconv.FormatInt(ns, 10)) != 19 {
return fmt.Errorf("datapoint timestamp %v does not have nanosecond accuracy", dp.Timestamp)
}
if dp.RecvTimestamp.Before(dp.Timestamp) {
return fmt.Errorf("datapoint receive timestamp %v is before notification timestamp %v", dp.RecvTimestamp, dp.Timestamp)
}
}
if dp.Value != nil {
typedVal := dp.Value
switch typedVal.Value.(type) {
case *gpb.TypedValue_StringVal:
if typedVal.GetStringVal() != "" && !utf8.ValidString(typedVal.GetStringVal()) {
return fmt.Errorf("datapoint string value %v is not a valid UTF-8 string", typedVal.GetStringVal())
}
}
}
return nil
}