|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package saiserver |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "google.golang.org/grpc" |
| 22 | + "google.golang.org/protobuf/proto" |
| 23 | + "google.golang.org/protobuf/testing/protocmp" |
| 24 | + |
| 25 | + "github.com/google/go-cmp/cmp" |
| 26 | + "github.com/openconfig/gnmi/errdiff" |
| 27 | + |
| 28 | + "github.com/openconfig/lemming/dataplane/saiserver/attrmgr" |
| 29 | + |
| 30 | + saipb "github.com/openconfig/lemming/dataplane/proto/sai" |
| 31 | +) |
| 32 | + |
| 33 | +func TestBridgePort(t *testing.T) { |
| 34 | + tests := []struct { |
| 35 | + desc string |
| 36 | + req *saipb.CreateBridgePortRequest |
| 37 | + want *saipb.CreateBridgePortResponse |
| 38 | + wantAttr *saipb.BridgePortAttribute |
| 39 | + wantErr string |
| 40 | + }{{ |
| 41 | + desc: "success", |
| 42 | + req: &saipb.CreateBridgePortRequest{ |
| 43 | + Switch: 1, |
| 44 | + Type: saipb.BridgePortType_BRIDGE_PORT_TYPE_PORT.Enum(), |
| 45 | + PortId: proto.Uint64(10), |
| 46 | + FdbLearningMode: saipb.BridgePortFdbLearningMode_BRIDGE_PORT_FDB_LEARNING_MODE_DISABLE.Enum(), |
| 47 | + AdminState: proto.Bool(true), |
| 48 | + }, |
| 49 | + want: &saipb.CreateBridgePortResponse{ |
| 50 | + Oid: 1, |
| 51 | + }, |
| 52 | + wantAttr: &saipb.BridgePortAttribute{ |
| 53 | + Type: saipb.BridgePortType_BRIDGE_PORT_TYPE_PORT.Enum(), |
| 54 | + PortId: proto.Uint64(10), |
| 55 | + FdbLearningMode: saipb.BridgePortFdbLearningMode_BRIDGE_PORT_FDB_LEARNING_MODE_DISABLE.Enum(), |
| 56 | + AdminState: proto.Bool(true), |
| 57 | + }, |
| 58 | + }} |
| 59 | + for _, tt := range tests { |
| 60 | + t.Run(tt.desc, func(t *testing.T) { |
| 61 | + conn, mgr, stopFn := newTestBridge(t) |
| 62 | + defer stopFn() |
| 63 | + c := saipb.NewBridgeClient(conn) |
| 64 | + |
| 65 | + got, gotErr := c.CreateBridgePort(context.TODO(), tt.req) |
| 66 | + if diff := errdiff.Check(gotErr, tt.wantErr); diff != "" { |
| 67 | + t.Fatalf("CreateBridgePort() unexpected err: %s w/ error %v", diff, gotErr) |
| 68 | + } |
| 69 | + if gotErr != nil { |
| 70 | + return |
| 71 | + } |
| 72 | + if d := cmp.Diff(got, tt.want, protocmp.Transform()); d != "" { |
| 73 | + t.Errorf("CreateBridgePort() failed: diff(-got,+want)\n:%s", d) |
| 74 | + } |
| 75 | + attr := &saipb.BridgePortAttribute{} |
| 76 | + if err := mgr.PopulateAllAttributes("1", attr); err != nil { |
| 77 | + t.Fatal(err) |
| 78 | + } |
| 79 | + if d := cmp.Diff(attr, tt.wantAttr, protocmp.Transform()); d != "" { |
| 80 | + t.Errorf("CreateBridgePort() failed: diff(-got,+want)\n:%s", d) |
| 81 | + } |
| 82 | + |
| 83 | + // Test Set Attribute |
| 84 | + _, err := c.SetBridgePortAttribute(context.TODO(), &saipb.SetBridgePortAttributeRequest{ |
| 85 | + Oid: got.Oid, |
| 86 | + AdminState: proto.Bool(false), |
| 87 | + }) |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("SetBridgePortAttribute() failed: %v", err) |
| 90 | + } |
| 91 | + if err := mgr.PopulateAllAttributes("1", attr); err != nil { |
| 92 | + t.Fatal(err) |
| 93 | + } |
| 94 | + if attr.GetAdminState() != false { |
| 95 | + t.Errorf("SetBridgePortAttribute() failed: got %v, want false", attr.GetAdminState()) |
| 96 | + } |
| 97 | + |
| 98 | + // Test Get Attribute |
| 99 | + resp, err := c.GetBridgePortAttribute(context.TODO(), &saipb.GetBridgePortAttributeRequest{ |
| 100 | + Oid: got.Oid, |
| 101 | + AttrType: []saipb.BridgePortAttr{saipb.BridgePortAttr_BRIDGE_PORT_ATTR_ADMIN_STATE}, |
| 102 | + }) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("GetBridgePortAttribute() failed: %v", err) |
| 105 | + } |
| 106 | + if resp.GetAttr().GetAdminState() != false { |
| 107 | + t.Errorf("GetBridgePortAttribute() failed: got %v, want false", resp.GetAttr().GetAdminState()) |
| 108 | + } |
| 109 | + // Test Remove |
| 110 | + if _, err := c.RemoveBridgePort(context.TODO(), &saipb.RemoveBridgePortRequest{Oid: got.Oid}); err != nil { |
| 111 | + t.Fatalf("RemoveBridgePort() failed: %v", err) |
| 112 | + } |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func newTestBridge(t testing.TB) (grpc.ClientConnInterface, *attrmgr.AttrMgr, func()) { |
| 118 | + conn, mgr, stopFn := newTestServer(t, func(mgr *attrmgr.AttrMgr, srv *grpc.Server) { |
| 119 | + newBridge(mgr, nil, srv) |
| 120 | + }) |
| 121 | + return conn, mgr, stopFn |
| 122 | +} |
0 commit comments