Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdn_tests/pins_ondatra/infrastructure/binding/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ go_library(
"@com_github_openconfig_ondatra//proto:go_default_library",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//credentials",
"@org_golang_google_grpc//credentials/insecure",
],
)
28 changes: 19 additions & 9 deletions sdn_tests/pins_ondatra/infrastructure/binding/pins_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"time"
"flag"

log "github.com/golang/glog"
gpb "github.com/openconfig/gnmi/proto/gnmi"
Expand All @@ -16,8 +17,14 @@ import (
"github.com/sonic-net/sonic-mgmt/sdn_tests/pins_ondatra/infrastructure/binding/bindingbackend"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)

var (
supportedSecurityModes = []string{"insecure", "mtls"}
securityMode = flag.String("security_mode", "insecure", fmt.Sprintf("define the security mode of the conntections to gnmi server, choose from : %v. Uses insecure as default.", supportedSecurityModes))
)

// Backend can reserve Ondatra DUTs and provide clients to interact with the DUTs.
type Backend struct {
configs map[string]*tls.Config
Expand Down Expand Up @@ -168,17 +175,20 @@ func (b *Backend) Release(ctx context.Context) error {

// DialGRPC connects to grpc service and returns the opened grpc client for use.
func (b *Backend) DialGRPC(ctx context.Context, addr string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
tlsConfig, ok := b.configs[addr]
if !ok {
return nil, fmt.Errorf("failed to find TLS config for %s", addr)
}

opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
if *securityMode == "mtls" {
tlsConfig, ok := b.configs[addr]
if !ok {
return nil, fmt.Errorf("failed to find TLS config for %s", addr)
}

opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
} else {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
conn, err := grpc.DialContext(ctx, addr, opts...)
if err != nil {
return nil, fmt.Errorf("DialContext(%s, %v) : %v", addr, opts, err)
if err != nil {
return nil, fmt.Errorf("DialContext(%s, %v) : %v", addr, opts, err)
}

return conn, nil
}

Expand Down
78 changes: 78 additions & 0 deletions sdn_tests/pins_ondatra/tests/gnmi_wildcard_subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,84 @@ func TestWCOnChangeComponentType(t *testing.T) {
}
}

func TestWCOnChangeComponentParent(t *testing.T) {
defer testhelper.NewTearDownOptions(t).WithID("9c889baf-c3c2-4ce3-bb74-36b78c5b77ca").Teardown(t)
dut := ondatra.DUT(t, "DUT")

// Determine expected components.
wantUpdates := make(map[string]string)
components := gnmi.GetAll(t, dut, gnmi.OC().ComponentAny().State())
for _, component := range components {
if component == nil || component.Name == nil || component.GetName() == "" {
continue
}
if component.Parent != nil && component.GetParent() != "" {
wantUpdates[component.GetName()] = component.GetParent()
}
}

// Collect component updates from ON_CHANGE subscription.
initialValues := gnmi.CollectAll(t, gnmiOpts(t, dut, gpb.SubscriptionMode_ON_CHANGE), gnmi.OC().
ComponentAny().
Parent().State(), shortWait).
Await(t)

gotUpdates := make(map[string]string)
for _, val := range initialValues {
component, err := fetchPathKey(val.Path, componentKey)
if err != nil {
t.Errorf("fetchPathKey() failed: %v", err)
continue
}
if upd, present := val.Val(); present && upd != "" {
gotUpdates[component] = upd
}
}

if diff := cmp.Diff(wantUpdates, gotUpdates); diff != "" {
t.Errorf("Update notifications comparison failed! (-want +got):\n%v", diff)
}
}

func TestWCOnChangeComponentSoftwareVersion(t *testing.T) {
defer testhelper.NewTearDownOptions(t).WithID("25e36fae-82e7-4d51-8f60-df6fb139f6ca").Teardown(t)
dut := ondatra.DUT(t, "DUT")

// Determine expected components.
wantUpdates := make(map[string]string)
components := gnmi.GetAll(t, dut, gnmi.OC().ComponentAny().State())
for _, component := range components {
if component == nil || component.Name == nil || component.GetName() == "" {
continue
}
if component.SoftwareVersion != nil && component.GetSoftwareVersion() != "" {
wantUpdates[component.GetName()] = component.GetSoftwareVersion()
}
}

// Collect component updates from ON_CHANGE subscription.
initialValues := gnmi.CollectAll(t, gnmiOpts(t, dut, gpb.SubscriptionMode_ON_CHANGE), gnmi.OC().
ComponentAny().
SoftwareVersion().State(), shortWait).
Await(t)

gotUpdates := make(map[string]string)
for _, val := range initialValues {
component, err := fetchPathKey(val.Path, componentKey)
if err != nil {
t.Errorf("fetchPathKey() failed: %v", err)
continue
}
if upd, present := val.Val(); present && upd != "" {
gotUpdates[component] = upd
}
}

if diff := cmp.Diff(wantUpdates, gotUpdates); diff != "" {
t.Errorf("Update notifications comparison failed! (-want +got):\n%v", diff)
}
}

func gnmiOpts(t *testing.T, dut *ondatra.DUTDevice, mode gpb.SubscriptionMode) *gnmi.Opts {
client, err := dut.RawAPIs().BindingDUT().DialGNMI(context.Background())
if err != nil {
Expand Down