Skip to content
Snippets Groups Projects
Commit 329250ca authored by Malte Bauch's avatar Malte Bauch
Browse files

Adjusted gnmi_transport and transport test cases

parent 682ee713
Branches
Tags
1 merge request!401Change the current gosdn plugin implementation from Go's internal plugin system to hashicorp's go-plugins
Pipeline #131069 failed
......@@ -20,8 +20,8 @@ import (
eventservice "code.fbi.h-da.de/danet/gosdn/controller/eventService"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/networkdomain"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/networkelement"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/plugin"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/rbac"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/southbound"
"code.fbi.h-da.de/danet/gosdn/controller/mocks"
nbi "code.fbi.h-da.de/danet/gosdn/controller/northbound/server"
"code.fbi.h-da.de/danet/gosdn/controller/nucleus"
......@@ -59,7 +59,7 @@ const mneID = "7e0ed8cc-ebf5-46fa-9794-741494914883"
var pndStore networkdomain.PndStore
var userService rbac.UserService
var roleService rbac.RoleService
var sbiStore southbound.Store
var pluginStore plugin.Store
var lis *bufconn.Listener
var pndUUID uuid.UUID
var sbiUUID uuid.UUID
......@@ -95,7 +95,6 @@ func bootstrapUnitTest() {
eventService := eventservice.NewMockEventService()
pndStore = nucleus.NewMemoryPndStore()
sbiStore = nucleus.NewMemorySbiStore()
userService = rbacImpl.NewUserService(rbacImpl.NewMemoryUserStore(), eventService)
roleService = rbacImpl.NewRoleService(rbacImpl.NewMemoryRoleStore(), eventService)
if err := clearAndCreateAuthTestSetup(); err != nil {
......@@ -123,12 +122,12 @@ func bootstrapUnitTest() {
},
})
plugin := &mocks.Plugin{}
pluginMock := &mocks.Plugin{}
mockNetworkElement := &mocks.NetworkElement{}
mockNetworkElement.On("Plugin").Return(plugin)
mockNetworkElement.On("Plugin").Return(pluginMock)
mockNetworkElement.On("ID").Return(mneUUID)
mockNetworkElement.On("GetModel").Return(plugin.Model())
mockNetworkElement.On("GetModel").Return(pluginMock.Model())
mockNetworkElement.On("Name").Return("openconfig")
mockNetworkElement.On("TransportAddress").Return("127.0.0.1:6030")
mockNetworkElement.On("GetMetadata").Return(conflict.Metadata{ResourceVersion: 0})
......@@ -172,6 +171,9 @@ func bootstrapUnitTest() {
routeStore := store.NewGenericStore[routingtables.RoutingTable]()
routeService := routingtables.NewRoutingTableService(routeStore, nodeService, portService, eventService)
pluginStore = nucleus.NewMemoryPluginStore()
pluginService := nucleus.NewPluginService(pluginStore, eventService)
northbound := nbi.NewNBI(
pndStore,
userService,
......@@ -182,6 +184,7 @@ func bootstrapUnitTest() {
portService,
routeService,
appService,
pluginService,
rpb.NewPluginRegistryServiceClient(&grpc.ClientConn{}),
csbi.NewCsbiServiceClient(&grpc.ClientConn{}),
func(u uuid.UUID, c chan networkelement.Details) {},
......
......@@ -140,13 +140,19 @@ func (g *Gnmi) applyDiff(ctx context.Context, payload change.Payload, path *gpb.
// apply diffs to the plugin of the managed network element.
for _, update := range updates {
plugin.SetNode(update.GetPath(), update.GetVal())
err := plugin.SetNode(update.GetPath(), update.GetVal())
if err != nil {
return err
}
}
for _, del := range deletes {
plugin.DeleteNode(del)
err := plugin.DeleteNode(del)
if err != nil {
return err
}
}
return err
return nil
}
func createSetRequest(ctx context.Context, diff *gpb.Notification, json []byte, path *gpb.Path) (*gpb.SetRequest, error) {
......
......@@ -3,6 +3,7 @@ package nucleus
import (
"context"
"errors"
"fmt"
"reflect"
"testing"
......@@ -10,13 +11,12 @@ import (
"code.fbi.h-da.de/danet/gosdn/controller/nucleus/types"
ppb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/pnd"
spb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/southbound"
tpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/transport"
"code.fbi.h-da.de/danet/gosdn/controller/mocks"
"code.fbi.h-da.de/danet/gosdn/forks/goarista/gnmi"
"code.fbi.h-da.de/danet/gosdn/models/generated/openconfig"
openconfigPlugin "code.fbi.h-da.de/danet/gosdn/plugins/openconfig"
openconfig "code.fbi.h-da.de/danet/gosdn/plugins/examples/openconfig/generated"
pluginSDK "code.fbi.h-da.de/danet/gosdn/plugins/sdk"
gpb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/ygot/ygot"
"github.com/stretchr/testify/mock"
......@@ -38,7 +38,7 @@ func testSetupGnmi() {
}
func TestGnmi_Capabilities(t *testing.T) {
transport := mockTransport()
transport := mockTransport(t)
capabilityResponse := &gpb.CapabilityResponse{
SupportedModels: nil,
SupportedEncodings: []gpb.Encoding{gpb.Encoding_PROTO, gpb.Encoding_JSON_IETF, gpb.Encoding_JSON},
......@@ -127,7 +127,7 @@ func TestGnmi_Close(t *testing.T) {
}
func TestGnmi_Get(t *testing.T) {
transport := mockTransport()
transport := mockTransport(t)
getRequest := &gpb.GetRequest{
Prefix: nil,
Path: nil,
......@@ -246,7 +246,7 @@ func TestGnmi_ProcessResponse(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
networkElementModel, err := openconfigPlugin.NewDeviceModel()
networkElementModel, err := pluginSDK.NewDeviceModel(openconfig.Schema, openconfig.Unmarshal, openconfig.SchemaTreeGzip)
if err != nil {
t.Errorf("ProcessResponse() error = %v", err)
}
......@@ -265,10 +265,32 @@ func TestGnmi_ProcessResponse(t *testing.T) {
}
func TestGnmi_Set(t *testing.T) {
schema, err := openconfig.Schema()
if err != nil {
t.Errorf("Set() error = %v", err)
mockPlugin := mockPlugin(t)
mockCall := mockPlugin.(*mocks.Plugin).On("Diff", mock.IsType([]byte{}), mock.IsType([]byte{}))
mockCall.RunFn = func(args mock.Arguments) {
path, err := ygot.StringToStructuredPath("/system/config/hostname")
if err != nil {
mockCall.ReturnArguments = mock.Arguments{nil, fmt.Errorf("StringToStructuredPath failed in ReturnArguments")}
}
if string(args.Get(0).([]byte)) == "" {
mockCall.ReturnArguments = mock.Arguments{&gpb.Notification{}, nil}
} else if string(args.Get(0).([]byte)) == "update" {
mockCall.ReturnArguments = mock.Arguments{&gpb.Notification{
Update: []*gpb.Update{{Path: path, Val: &gpb.TypedValue{Value: &gpb.TypedValue_StringVal{StringVal: "newName"}}}},
},
nil,
}
} else if string(args.Get(0).([]byte)) == "delete" {
mockCall.ReturnArguments = mock.Arguments{&gpb.Notification{
Delete: []*gpb.Path{path},
},
nil,
}
}
}
mockPlugin.(*mocks.Plugin).On("Unmarshal", mock.Anything, mock.Anything).Return(nil)
mockPlugin.(*mocks.Plugin).On("SetNode", mock.Anything, mock.Anything).Return(nil)
mockPlugin.(*mocks.Plugin).On("DeleteNode", mock.Anything).Return(nil)
setResponse := &gpb.SetResponse{}
......@@ -289,7 +311,7 @@ func TestGnmi_Set(t *testing.T) {
}{
{
name: "uninitialised",
fields: fields{transport: mockTransport()},
fields: fields{transport: mockTransport(t)},
args: args{
payload: change.Payload{},
path: "/",
......@@ -300,31 +322,27 @@ func TestGnmi_Set(t *testing.T) {
{
name: "updateValue",
fields: fields{
transport: mockTransport(),
transport: mockTransport(t),
mockArgumentMatcher: mock.MatchedBy(func(input *gpb.SetRequest) bool {
if len(input.Update) == 0 {
return false
}
test, _ := ygot.PathToString(input.Update[0].Path)
return test == "/system/config/hostname"
path, err := ygot.PathToString(input.Update[0].Path)
if err != nil {
return false
}
value := input.Update[0].Val.GetStringVal()
valCheck := value == "newName"
pathCheck := path == "/system/config/hostname"
return valCheck && pathCheck
}),
},
args: args{
payload: change.Payload{
Original: &openconfig.Device{
System: &openconfig.OpenconfigSystem_System{
Config: &openconfig.OpenconfigSystem_System_Config{
Hostname: ygot.String("oldName"),
},
},
},
Modified: &openconfig.Device{
System: &openconfig.OpenconfigSystem_System{
Config: &openconfig.OpenconfigSystem_System_Config{
Hostname: ygot.String("newName"),
},
},
},
Original: []byte("update"),
Modified: []byte("update2"),
},
path: "/system/config/hostname",
ctx: context.WithValue(context.Background(), types.CtxKeyOperation, ppb.ApiOperation_API_OPERATION_UPDATE), // nolint
......@@ -334,7 +352,7 @@ func TestGnmi_Set(t *testing.T) {
{
name: "removeValue",
fields: fields{
transport: mockTransport(),
transport: mockTransport(t),
mockArgumentMatcher: mock.MatchedBy(func(input *gpb.SetRequest) bool {
if len(input.Delete) == 0 {
return false
......@@ -345,20 +363,8 @@ func TestGnmi_Set(t *testing.T) {
},
args: args{
payload: change.Payload{
Original: &openconfig.Device{
System: &openconfig.OpenconfigSystem_System{
Config: &openconfig.OpenconfigSystem_System_Config{
Hostname: ygot.String("oldName"),
},
},
},
Modified: &openconfig.Device{
System: &openconfig.OpenconfigSystem_System{
Config: &openconfig.OpenconfigSystem_System_Config{
Hostname: nil,
},
},
},
Original: []byte("delete"),
Modified: []byte("delete2"),
},
path: "/system/config/hostname",
ctx: context.WithValue(context.Background(), types.CtxKeyOperation, ppb.ApiOperation_API_OPERATION_DELETE), // nolint
......@@ -370,7 +376,7 @@ func TestGnmi_Set(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tt.fields.transport.client.(*mocks.GNMIClient).
On("Set", mockContext, tt.fields.mockArgumentMatcher).Return(setResponse, nil)
err := tt.fields.transport.Set(tt.args.ctx, tt.args.payload, tt.args.path, schema)
err := tt.fields.transport.Set(tt.args.ctx, tt.args.payload, tt.args.path, mockPlugin)
if (err != nil) != tt.wantErr {
t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr)
}
......@@ -419,7 +425,7 @@ func TestGnmi_Type(t *testing.T) {
//nolint:errcheck
func TestGnmi_getWithRequest(t *testing.T) {
transport := mockTransport()
transport := mockTransport(t)
reqFullNode := gnmiMessages["../test/proto/req-full-node"].(*gpb.GetRequest)
reqInterfacesWildcard := gnmiMessages["../test/proto/req-interfaces-wildcard"].(*gpb.GetRequest)
respFullNode := gnmiMessages["../test/proto/resp-full-node"].(*gpb.GetResponse)
......@@ -523,12 +529,9 @@ func TestNewGnmiTransport(t *testing.T) {
startGnmiTarget <- gnmiConfig.Addr
}
sbi, err := NewSBI(spb.Type_TYPE_OPENCONFIG)
if err != nil {
t.Errorf("NewSBI() error = %v", err)
return
}
got, err := newGnmiTransport(tt.args.opts, sbi)
mockPlugin := mockPlugin(t)
got, err := newGnmiTransport(tt.args.opts, mockPlugin)
if (err != nil) != tt.wantErr {
t.Errorf("NewGnmiTransport() error = %v, wantErr %v", err, tt.wantErr)
return
......
......@@ -97,7 +97,7 @@ func mockTransport(t testing.TB) Gnmi {
}
func mockPlugin(t testing.TB) plugin.Plugin {
mockPlugin := mocks.NewPlugin(t)
mockPlugin := &mocks.Plugin{}
return mockPlugin
}
......
......@@ -4,7 +4,7 @@ import (
"testing"
tpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/transport"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/southbound"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/plugin"
gpb "github.com/openconfig/gnmi/proto/gnmi"
)
......@@ -12,8 +12,8 @@ import (
// are conducted at the transport implementation constructors.
func TestNewTransport(t *testing.T) {
type args struct {
opts *tpb.TransportOption
sbi southbound.SouthboundInterface
opts *tpb.TransportOption
plugin plugin.Plugin
}
tests := []struct {
name string
......@@ -34,15 +34,15 @@ func TestNewTransport(t *testing.T) {
},
},
},
sbi: &OpenConfig{},
plugin: mockPlugin(t),
},
wantErr: false,
},
{
name: "no opt",
args: args{
opts: nil,
sbi: &OpenConfig{},
opts: nil,
plugin: mockPlugin(t),
},
wantErr: true,
},
......@@ -60,7 +60,7 @@ func TestNewTransport(t *testing.T) {
},
},
},
sbi: nil,
plugin: nil,
},
wantErr: true,
},
......@@ -73,7 +73,7 @@ func TestNewTransport(t *testing.T) {
Password: "test",
Tls: false,
},
sbi: &OpenConfig{},
plugin: mockPlugin(t),
},
wantErr: true,
},
......@@ -87,14 +87,14 @@ func TestNewTransport(t *testing.T) {
Tls: false,
TransportOption: &tpb.TransportOption_GnmiTransportOption{},
},
sbi: &OpenConfig{},
plugin: mockPlugin(t),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NewTransport(tt.args.opts, tt.args.sbi)
_, err := NewTransport(tt.args.opts, tt.args.plugin)
if (err != nil) != tt.wantErr {
t.Errorf("NewTransport() error = %v, wantErr %v", err, tt.wantErr)
return
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment