Skip to content

Commit 226fcd8

Browse files
kishanpsSRAVANI KANASANI
authored andcommitted
CGO interface for pinstesthelper through thinkit.
1 parent c9f46ce commit 226fcd8

18 files changed

+363
-53
lines changed

sdn_tests/pins_ondatra/bazel/patches/ondatra.patch

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ index 4d431d1..0ff43a4 100644
77
pathzpb "github.com/openconfig/gnsi/pathz"
88

99
- grpb "github.com/openconfig/gribi/v1/proto/service"
10+
+ grpb "github.com/openconfig/gribi/proto/service"
11+
opb "github.com/openconfig/ondatra/proto"
12+
p4pb "github.com/p4lang/p4runtime/go/p4/v1"
13+
)
14+
diff --git a/fakebind/fakebind.go b/fakebind/fakebind.go
15+
index 7dd0538..800a272 100644
16+
--- a/fakebind/fakebind.go
17+
+++ b/fakebind/fakebind.go
18+
@@ -28,7 +28,7 @@ import (
19+
"google.golang.org/grpc"
20+
21+
gpb "github.com/openconfig/gnmi/proto/gnmi"
22+
- grpb "github.com/openconfig/gribi/v1/proto/service"
1023
+ grpb "github.com/openconfig/gribi/proto/service"
1124
opb "github.com/openconfig/ondatra/proto"
1225
p4pb "github.com/p4lang/p4runtime/go/p4/v1"
@@ -141,4 +154,4 @@ index 7145250..85cb489 100644
141154
+import "github.com/openconfig/ondatra/proto/testbed.proto";
142155

143156
option go_package = "github.com/openconfig/ondatra/proto/reservation";
144-
157+

sdn_tests/pins_ondatra/infrastructure/testhelper/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
load("@io_bazel_rules_go//go:def.bzl", "go_library")
22

3+
def_visibility = [
4+
"//platforms/networking/gpins/testing:__subpackages__",
5+
"//third_party/pins_infra:__subpackages__",
6+
]
7+
38
package(
49
default_visibility = ["//visibility:public"],
510
licenses = ["notice"],

sdn_tests/pins_ondatra/infrastructure/testhelper/config_restorer.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,16 @@ func NewConfigRestorerWithIgnorePaths(t *testing.T, ignorePaths []string) *Confi
9393
t.Fatalf("config_restorer creation failed, errors: %v", err)
9494
}
9595

96-
// Register a cleanup to restore the configs on test end.
97-
t.Cleanup(func() {
98-
cr.RestoreConfigsAndClose(t)
99-
})
96+
// Cleanup is only registered if running in a go test.
97+
// As the cleanup callback would not be called otherwise.
98+
if testing.Testing() { // True if running in a go test.
99+
// Register a cleanup to restore the configs on test end.
100+
log.InfoContextf(ctx, "Registering config_restorer for cleanup tasks.")
101+
t.Cleanup(func() {
102+
cr.RestoreConfigsAndClose(t)
103+
})
104+
}
105+
100106
return cr
101107
}
102108

@@ -238,12 +244,10 @@ func (cr *ConfigRestorer) restoreConfigOnDiff(ctx context.Context, t *testing.T,
238244

239245
// restoreReservedDevices tries to restore the config of the reserved devices
240246
// if the config differs from the saved config.
241-
func (cr *ConfigRestorer) restoreReservedDevices(t *testing.T) {
242-
t.Helper()
247+
func (cr *ConfigRestorer) restoreReservedDevices(t *testing.T) error {
243248
ctx := context.Background()
244249
if cr.savedConfigs == nil {
245-
log.InfoContextf(ctx, "configRestorer.savedConfigs is not initialized.")
246-
return
250+
return fmt.Errorf("savedConfigs are not initialized")
247251
}
248252

249253
wg := sync.WaitGroup{}
@@ -268,16 +272,24 @@ func (cr *ConfigRestorer) restoreReservedDevices(t *testing.T) {
268272

269273
// Collect all the errors and fail the test on error.
270274
if err := collectErrors(errCh); err != nil {
271-
t.Fatalf("failed to restore config, errors: %v", err)
275+
return fmt.Errorf("failed to restore config, errors: %v", err)
272276
}
273277
log.InfoContextf(ctx, "Config restored for all the reserved devices.")
278+
return nil
279+
}
280+
281+
// RestoreConfigs restores the config of reserved devices.s
282+
func (cr *ConfigRestorer) RestoreConfigs(t *testing.T) error {
283+
return cr.restoreReservedDevices(t)
274284
}
275285

276286
// RestoreConfigsAndClose restores the config of reserved devices
277287
// and closes the configRestorer object.
278288
func (cr *ConfigRestorer) RestoreConfigsAndClose(t *testing.T) {
279289
t.Helper()
280-
cr.restoreReservedDevices(t)
290+
if err := cr.RestoreConfigs(t); err != nil {
291+
t.Fatalf("config_restorer failed, errors: %v", err)
292+
}
281293
cr.savedConfigs = nil
282294
cr.ignorePaths = nil
283295
}

sdn_tests/pins_ondatra/infrastructure/testhelper/platform_components.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
// SwitchNameRegex returns the regex for switch name.
1616
func SwitchNameRegex() string {
17-
return ""
17+
return "^(ju|df|mn|ocs)(\\d+).*\\.([a-z]{3})(\\d{2})([a-z]?)\\.(net|prod).google.com$"
1818
}
1919

2020
// ImageVersionRegex returns the regular expressions for the image version of the switch.

sdn_tests/pins_ondatra/infrastructure/testhelper/port_management.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package testhelper
44

55
import (
66
"fmt"
7+
"slices"
78
"strconv"
89
"strings"
910
"testing"
@@ -840,6 +841,7 @@ func BreakoutStateInfoForPort(t *testing.T, dut *ondatra.DUTDevice, port string,
840841
if portInfo == nil {
841842
return nil, errors.Errorf("got empty port information for breakout mode %v for port %v", currBreakoutMode, port)
842843
}
844+
FilterSkippedPorts(t, dut, port, portInfo)
843845
// Get physical channels and operational statuses for list of ports in given breakout mode.
844846
for p := range portInfo {
845847
physicalChannels := testhelperIntfPhysicalChannelsGet(t, dut, p)
@@ -850,6 +852,32 @@ func BreakoutStateInfoForPort(t *testing.T, dut *ondatra.DUTDevice, port string,
850852
return portInfo, nil
851853
}
852854

855+
// FilterSkippedPorts filters out ports that are skipped from the portInfo map.
856+
func FilterSkippedPorts(t *testing.T, dut *ondatra.DUTDevice, port string,
857+
portInfo map[string]*PortBreakoutInfo) {
858+
if dut == nil {
859+
return
860+
}
861+
for p, pInfo := range portInfo {
862+
if testhelperIntfLookup(t, dut, p).IsPresent() {
863+
physicalChannels := testhelperIntfPhysicalChannelsGet(t, dut, p)
864+
if !slices.Equal(physicalChannels, pInfo.PhysicalChannels) {
865+
t.Fatalf("physical channels mismatch for port on switch, get: %v, want: %v",
866+
physicalChannels, pInfo.PhysicalChannels)
867+
}
868+
portSpeed := testhelperStatePortSpeedGet(t, dut, p)
869+
if portSpeed != pInfo.PortSpeed {
870+
t.Fatalf("port speed mismatch for port on switch, get: %v, want: %v",
871+
portSpeed, pInfo.PortSpeed)
872+
}
873+
} else {
874+
// Filter out skipped
875+
delete(portInfo, p)
876+
t.Logf("Port %v skipped, port Info: %v", p, pInfo)
877+
}
878+
}
879+
}
880+
853881
// WaitForInterfaceState polls interface oper-status until it matches the expected oper-status.
854882
func WaitForInterfaceState(t *testing.T, dut *ondatra.DUTDevice, intfName string, expectedOperSatus oc.E_Interface_OperStatus, timeout time.Duration) error {
855883
t.Helper()

sdn_tests/pins_ondatra/infrastructure/testhelper/testhelper.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
package testhelper
33

44
import (
5+
"fmt"
56
"crypto/rand"
67
"math/big"
7-
"fmt"
88
"strings"
99
"testing"
1010
"time"
@@ -218,8 +218,8 @@ type TearDownOptions struct {
218218
}
219219

220220
// NewTearDownOptions creates the TearDownOptions structure with default values.
221-
func NewTearDownOptions(t *testing.T) TearDownOptions {
222-
return TearDownOptions{
221+
func NewTearDownOptions(t *testing.T) *TearDownOptions {
222+
return &TearDownOptions{
223223
StartTime: time.Now(),
224224
DUTName: teardownDUTNameGet(t),
225225
DUTDeviceInfo: teardownDUTDeviceInfoGet(t),
@@ -228,13 +228,13 @@ func NewTearDownOptions(t *testing.T) TearDownOptions {
228228
}
229229

230230
// WithID attaches an ID to the test.
231-
func (o TearDownOptions) WithID(id string) TearDownOptions {
231+
func (o *TearDownOptions) WithID(id string) *TearDownOptions {
232232
o.IDs = append(o.IDs, id)
233233
return o
234234
}
235235

236236
// WithIDs attaches a list of IDs to the test.
237-
func (o TearDownOptions) WithIDs(ids []string) TearDownOptions {
237+
func (o *TearDownOptions) WithIDs(ids []string) *TearDownOptions {
238238
for _, id := range ids {
239239
o.IDs = append(o.IDs, id)
240240
}
@@ -245,11 +245,19 @@ func (o TearDownOptions) WithIDs(ids []string) TearDownOptions {
245245
// of the reserved devices on teardown.
246246
// Accepts a list of paths to ignore while checking for config changes.
247247
// The test will fail if the config is not restored.
248-
func (o TearDownOptions) WithConfigRestorer(t *testing.T, ignorePaths []string) TearDownOptions {
248+
func (o *TearDownOptions) WithConfigRestorer(t *testing.T, ignorePaths []string) *TearDownOptions {
249249
o.configRestorer = NewConfigRestorerWithIgnorePaths(t, ignorePaths)
250250
return o
251251
}
252252

253+
// RestoreConfigs restores the configs of the reserved devices.
254+
func (o *TearDownOptions) RestoreConfigs(t *testing.T) error {
255+
if o.configRestorer == nil {
256+
return fmt.Errorf("configRestorer was not initialized")
257+
}
258+
return o.configRestorer.RestoreConfigs(t)
259+
}
260+
253261
// TearDown provides an interface to implement the teardown routine.
254262
type TearDown interface {
255263
Teardown(t *testing.T)

sdn_tests/pins_ondatra/infrastructure/thinkit/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,26 @@ package(
2020
licenses = ["notice"],
2121
)
2222

23+
cc_library(
24+
name = "thinkit_go_hdr",
25+
testonly = True,
26+
hdrs = ["thinkit_go_interface.h"],
27+
)
28+
2329
go_library(
2430
name = "thinkit",
2531
testonly = True,
2632
srcs = ["thinkit.go"],
33+
cdeps = [":thinkit_go_hdr"],
2734
cgo = True,
2835
importpath = "github.com/sonic-net/sonic-mgmt/sdn_tests/pins_ondatra/infrastructure/thinkit/thinkit",
2936
deps = [
3037
"//infrastructure/binding:pinsbind",
38+
"//infrastructure/testhelper",
3139
"@com_github_golang_glog//:glog",
3240
"@com_github_openconfig_gnmi//errlist",
3341
"@com_github_openconfig_ondatra//binding",
42+
"@com_github_openconfig_ondatra//fakebind",
3443
"@com_github_openconfig_ondatra//proto:go_default_library",
3544
"@com_github_openconfig_ondatra//proxy",
3645
"@com_github_openconfig_ondatra//proxy/proto/reservation:go_default_library",
@@ -54,13 +63,16 @@ cc_library(
5463
hdrs = ["thinkit.h"],
5564
deps = [
5665
":thinkit_cgo.cc",
66+
":thinkit_go_hdr", # keep
5767
"@com_github_sonic_net_sonic_pins//gutil:status",
5868
"@com_github_openconfig_ondatra//proto:ondatra_cc_proto",
5969
"@com_github_openconfig_ondatra//proxy/proto:reservation_cc_proto",
70+
"@com_google_absl//absl/log",
6071
"@com_google_absl//absl/status",
6172
"@com_google_absl//absl/status:statusor",
6273
"@com_google_absl//absl/strings:string_view",
6374
"@com_google_absl//absl/time",
75+
"@com_google_googletest//:gtest",
6476
"@com_google_protobuf//:protobuf",
6577
],
6678
)
@@ -117,6 +129,7 @@ cc_test(
117129
deps = [
118130
":cthinkit",
119131
":ondatra_generic_testbed_fixture",
132+
":thinkit_go_hdr",
120133
"@com_github_sonic_net_sonic_pins//gutil:proto_matchers",
121134
"@com_github_sonic_net_sonic_pins//gutil:status",
122135
"@com_github_sonic_net_sonic_pins//gutil:status_matchers",

sdn_tests/pins_ondatra/infrastructure/thinkit/ondatra_generic_testbed_fixture.cc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ GetSutInterfaceInfoFromReservation(
183183
} // namespace
184184

185185
void OndatraGenericTestbedFixture::TearDown() {
186+
ondatra_hooks_.teardown(teardown_handler_id_);
186187
EXPECT_OK(ondatra_hooks_.release());
187188
}
188189

@@ -197,6 +198,19 @@ OndatraGenericTestbedFixture::GetTestbedWithRequirements(
197198
ASSIGN_OR_RETURN(reservation::Reservation reservation,
198199
ondatra_hooks_.testbed());
199200

201+
// Setup testhelper teardown.
202+
teardown_handler_id_ = ondatra_hooks_.new_teardown_handler(/*opts=*/{
203+
.with_config_restorer = false,
204+
});
205+
auto set_test_case_ids =
206+
[this](const std::vector<std::string>& test_case_ids) {
207+
for (const std::string& test_case_id : test_case_ids) {
208+
std::string test_case_id_go_str = test_case_id;
209+
ondatra_hooks_.add_test_case_id(teardown_handler_id_,
210+
test_case_id_go_str.data());
211+
}
212+
};
213+
200214
ASSIGN_OR_RETURN(auto sut_interface_info, GetSutInterfaceInfoFromReservation(
201215
testbed_request, reservation));
202216
ASSIGN_OR_RETURN(const reservation::ResolvedDevice* dut,
@@ -209,7 +223,7 @@ OndatraGenericTestbedFixture::GetTestbedWithRequirements(
209223
if (!control.ok()) {
210224
return std::make_unique<pins_test::OndatraGenericTestbed>(
211225
std::move(sut), /*control_device=*/nullptr,
212-
std::move(sut_interface_info));
226+
std::move(sut_interface_info), set_test_case_ids);
213227
}
214228

215229
ASSIGN_OR_RETURN(auto control_switch, CreateSwitchFromDevice(*(*control)));
@@ -224,9 +238,10 @@ OndatraGenericTestbedFixture::GetTestbedWithRequirements(
224238
std::move(*response.mutable_config()->mutable_p4info())));
225239
auto control_device_pointer =
226240
std::make_unique<pins_test::PinsControlDevice>(std::move(control_device));
241+
227242
return std::make_unique<pins_test::OndatraGenericTestbed>(
228243
std::move(sut), std::move(control_device_pointer),
229-
std::move(sut_interface_info));
244+
std::move(sut_interface_info), set_test_case_ids);
230245
}
231246

232247
} // namespace pins_test

sdn_tests/pins_ondatra/infrastructure/thinkit/ondatra_generic_testbed_fixture.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class OndatraGenericTestbedFixture : public thinkit::GenericTestbedInterface {
4444

4545
private:
4646
OndatraHooks ondatra_hooks_;
47+
int teardown_handler_id_ = 0;
4748
};
4849

4950
} // namespace pins_test

sdn_tests/pins_ondatra/infrastructure/thinkit/ondatra_generic_testbed_fixture_test.cc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "gutil/status_matchers.h"
4141
#include "gutil/testing.h"
4242
#include "infrastructure/thinkit/thinkit.h"
43+
#include "infrastructure/thinkit/thinkit_go_interface.h"
4344
#include "p4/v1/p4runtime.grpc.pb.h"
4445
#include "p4/v1/p4runtime.pb.h"
4546
#include "proto/gnmi/gnmi.grpc.pb.h"
@@ -165,6 +166,9 @@ TEST_P(OndatraGenericTestbedFixtureTest, Test) {
165166
Call(EqualsProto(GetParam().expected_testbed_request), _, _))
166167
.WillOnce(Return(absl::OkStatus()));
167168
MockFunction<absl::StatusOr<reservation::Reservation>()> mock_testbed;
169+
MockFunction<int(struct testhelper_TeardownCreateOpts)>
170+
mock_new_teardown_handler;
171+
MockFunction<void(int)> mock_teardown;
168172
std::list<FakeSwitch> fake_switches;
169173
if (GetParam().returned_reservation.ok()) {
170174
reservation::Reservation reservation = *GetParam().returned_reservation;
@@ -180,17 +184,21 @@ TEST_P(OndatraGenericTestbedFixtureTest, Test) {
180184
absl::StrCat("[::]:", fake_switch.GetPort()));
181185
}
182186
EXPECT_CALL(mock_testbed, Call).WillOnce(Return(reservation));
187+
EXPECT_CALL(mock_new_teardown_handler, Call(_)).WillOnce(Return(0));
188+
EXPECT_CALL(mock_teardown, Call(_)).WillOnce(Return());
183189
} else {
184190
EXPECT_CALL(mock_testbed, Call)
185191
.WillOnce(Return(GetParam().returned_reservation));
186192
}
187193
MockFunction<absl::Status()> mock_release;
188194
EXPECT_CALL(mock_release, Call).WillOnce(Return(absl::OkStatus()));
189195

190-
OndatraGenericTestbedFixture fixture(
191-
OndatraHooks{.init = mock_init.AsStdFunction(),
192-
.testbed = mock_testbed.AsStdFunction(),
193-
.release = mock_release.AsStdFunction()});
196+
OndatraGenericTestbedFixture fixture(OndatraHooks{
197+
.init = mock_init.AsStdFunction(),
198+
.testbed = mock_testbed.AsStdFunction(),
199+
.release = mock_release.AsStdFunction(),
200+
.new_teardown_handler = mock_new_teardown_handler.AsStdFunction(),
201+
.teardown = mock_teardown.AsStdFunction()});
194202

195203
fixture.SetUp();
196204
if (GetParam().expected_sut_interface_info.ok()) {

0 commit comments

Comments
 (0)