diff --git a/cli/grpc.go b/cli/grpc.go index 3293fe25cc2746fcb10287d269172d2d9675ca1f..3d731ab907d23c7f5c7dfdb567c72e655d2975c8 100644 --- a/cli/grpc.go +++ b/cli/grpc.go @@ -16,8 +16,13 @@ import ( "google.golang.org/grpc" ) -// TODO: Delete once proper certs are set up -var grpcWithInsecure = grpc.WithInsecure() +var dialOptions []grpc.DialOption + +func init() { + dialOptions = []grpc.DialOption{ + grpc.WithInsecure(), + } +} // Init initialises the CLI client. func Init(addr string) error { @@ -50,7 +55,7 @@ func GetIds(addr string) ([]*ppb.PrincipalNetworkDomain, error) { } func getAllCore(ctx context.Context, addr string) (*pb.GetResponse, error) { - coreClient, err := nbi.CoreClient(addr, grpcWithInsecure) + coreClient, err := nbi.CoreClient(addr, dialOptions...) if err != nil { return nil, err } @@ -63,10 +68,10 @@ func getAllCore(ctx context.Context, addr string) (*pb.GetResponse, error) { // AddPnd takes a name, description and SBI UUID to create a new // PrincipalNetworkDomain on the controller -func AddPnd(addr, name, description, sbi string) error { - coreClient, err := nbi.CoreClient(addr, grpcWithInsecure) +func AddPnd(addr, name, description, sbi string) (*pb.SetResponse, error) { + coreClient, err := nbi.CoreClient(addr, dialOptions...) if err != nil { - return err + return nil, err } ctx := context.Background() req := &pb.SetRequest{ @@ -80,19 +85,14 @@ func AddPnd(addr, name, description, sbi string) error { }, } - resp, err := coreClient.Set(ctx, req) - if err != nil { - return err - } - log.Info(resp.Status.String()) - return nil + return coreClient.Set(ctx, req) } // GetPnd requests one or several PrincipalNetworkDomains from the // controller. To request all PrincipalNetworkDomains without providing // names or UUIDs use GetIds() func GetPnd(addr string, args ...string) (*pb.GetResponse, error) { - coreClient, err := nbi.CoreClient(addr, grpcWithInsecure) + coreClient, err := nbi.CoreClient(addr, dialOptions...) if err != nil { return nil, err } @@ -110,7 +110,7 @@ func GetPnd(addr string, args ...string) (*pb.GetResponse, error) { // GetChanges requests all pending and unconfirmed changes from the controller func GetChanges(addr, pnd string) (*ppb.GetResponse, error) { ctx := context.Background() - client, err := nbi.PndClient(addr, grpcWithInsecure) + client, err := nbi.PndClient(addr, dialOptions...) if err != nil { return nil, err } @@ -154,7 +154,7 @@ func Confirm(addr, pnd string, cuids ...string) (*ppb.SetResponse, error) { func commitConfirm(addr, pnd string, changes []*ppb.SetChange) (*ppb.SetResponse, error) { ctx := context.Background() - client, err := nbi.PndClient(addr, grpcWithInsecure) + client, err := nbi.PndClient(addr, dialOptions...) if err != nil { return nil, err } @@ -168,10 +168,10 @@ func commitConfirm(addr, pnd string, changes []*ppb.SetChange) (*ppb.SetResponse // AddDevice adds a new device to the controller. The device name is optional. // If no name is provided a name will be generated upon device creation. -func AddDevice(addr, username, password, sbi, pnd, deviceAddress, deviceName string) error { - pndClient, err := nbi.PndClient(addr, grpcWithInsecure) +func AddDevice(addr, username, password, sbi, pnd, deviceAddress, deviceName string) (*ppb.SetResponse, error) { + pndClient, err := nbi.PndClient(addr, dialOptions...) if err != nil { - return err + return nil, err } req := &ppb.SetRequest{ @@ -196,21 +196,14 @@ func AddDevice(addr, username, password, sbi, pnd, deviceAddress, deviceName str Pid: pnd, } ctx := context.Background() - resp, err := pndClient.Set(ctx, req) - if err != nil { - return err - } else if resp.Status != ppb.SetResponse_OK { - log.Error(resp.Status) - } - log.Info(resp.String()) - return nil + return pndClient.Set(ctx, req) } -// GetDevice requests one or multiple devices belongin to a given +// GetDevice requests one or multiple devices belonging to a given // PrincipalNetworkDomain from the controller. If no device identifier // is provided, all devices are requested. func GetDevice(addr, pid, path string, did ...string) (*ppb.GetResponse, error) { - pndClient, err := nbi.PndClient(addr, grpcWithInsecure) + pndClient, err := nbi.PndClient(addr, dialOptions...) if err != nil { return nil, err } @@ -270,7 +263,7 @@ func Delete(addr, did, pid, path string) (*ppb.SetResponse, error) { } func sendChangeRequest(addr, pid string, req *ppb.ChangeRequest) (*ppb.SetResponse, error) { - pndClient, err := nbi.PndClient(addr, grpcWithInsecure) + pndClient, err := nbi.PndClient(addr, dialOptions...) if err != nil { return nil, err } diff --git a/cli/grpc_test.go b/cli/grpc_test.go index 0f8929f41b368a8fe7ebf526d9da4ff3f6a6f283..c3117c40710665c98bb97f37c45c5caf84c17bd3 100644 --- a/cli/grpc_test.go +++ b/cli/grpc_test.go @@ -3,525 +3,200 @@ package cli import ( "context" "net" - "reflect" - "strconv" "testing" pb "code.fbi.h-da.de/cocsn/api/go/gosdn/core" ppb "code.fbi.h-da.de/cocsn/api/go/gosdn/pnd" spb "code.fbi.h-da.de/cocsn/api/go/gosdn/southbound" + "code.fbi.h-da.de/cocsn/gosdn/mocks" nbi "code.fbi.h-da.de/cocsn/gosdn/northbound/server" "code.fbi.h-da.de/cocsn/gosdn/nucleus" + "code.fbi.h-da.de/cocsn/yang-models/generated/openconfig" "github.com/google/uuid" - "github.com/openconfig/ygot/ygot" log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/mock" "google.golang.org/grpc" - "k8s.io/apimachinery/pkg/util/rand" + "google.golang.org/grpc/test/bufconn" ) -const unreachable = "203.0.113.10:6030" -const testPath = "/system/config/hostname" -const testUser = "admin" -const testPassword = "admin" -const testPid = "06aa4994-80f7-4751-99a4-6826b72d0057" -const testCuid = "cf628f4b-393d-44a5-affb-949dfe5c3c27" -const testDid = "1c37ff20-0169-44e0-bf37-b64787a979d5" +/* +Based on this StackOverflow answer: https://stackoverflow.com/a/52080545/4378176 +*/ -func startGrpcServer() (*grpc.Server, string, string, error) { - port := rand.IntnRange(1025, 65536) - sock := "localhost:" + strconv.Itoa(port) - pndc := nucleus.NewPndStore() - pid, err := uuid.Parse(testPid) - if err != nil { - return nil, "", "", err +const bufSize = 1024 * 1024 +const bufnet = "bufnet" +const pndID = "2043519e-46d1-4963-9a8e-d99007e104b8" +const changeID = "0992d600-f7d4-4906-9559-409b04d59a5f" +const sbiID = "f6fd4b35-f039-4111-9156-5e4501bb8a5a" +const ondID = "7e0ed8cc-ebf5-46fa-9794-741494914883" + +var pndStore *nucleus.PndStore +var lis *bufconn.Listener + +func init() { + dialOptions = []grpc.DialOption{ + grpc.WithContextDialer(bufDialer), + grpc.WithInsecure(), } - sbi := nucleus.NewSBI(spb.Type_OPENCONFIG) - pnd, err := nucleus.NewPND("testpnd", "test pnd", pid, sbi) + lis = bufconn.Listen(bufSize) + s := grpc.NewServer() + pndStore = nucleus.NewPndStore() + + pndUUID, err := uuid.Parse(pndID) if err != nil { - return nil, "", "", err - } - if err := pndc.Add(pnd); err != nil { - return nil, "", "", err + log.Fatal(err) } - lis, err := net.Listen("tcp", sock) + changeUUID, err := uuid.Parse(changeID) if err != nil { - return nil, "", "", err + log.Fatal(err) } - grpcServer := grpc.NewServer() - northbound := nbi.NewNBI(pndc) - pb.RegisterCoreServer(grpcServer, northbound.Core) - ppb.RegisterPndServer(grpcServer, northbound.Pnd) + + deviceUUID, err := uuid.Parse(ondID) + if err != nil { + log.Fatal(err) + } + + mockPnd := mocks.PrincipalNetworkDomain{} + mockPnd.On("ID").Return(pndUUID) + mockPnd.On("GetName").Return("test") + mockPnd.On("GetDescription").Return("test") + mockPnd.On("PendingChanges").Return([]uuid.UUID{changeUUID}) + mockPnd.On("CommittedChanges").Return([]uuid.UUID{changeUUID}) + mockPnd.On("GetChange", mock.Anything).Return(&nucleus.Change{}, nil) + mockPnd.On("AddDevice", mock.Anything, mock.Anything, mock.Anything).Return(nil) + mockPnd.On("GetDevice", mock.Anything).Return(&nucleus.Device{ + UUID: deviceUUID, + GoStruct: &openconfig.Device{}, + }, nil) + mockPnd.On("Commit", mock.Anything).Return(nil) + mockPnd.On("Confirm", mock.Anything).Return(nil) + mockPnd.On("ChangeOND", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + + if err := pndStore.Add(&mockPnd); err != nil { + log.Fatal(err) + } + northbound := nbi.NewNBI(pndStore) + pb.RegisterCoreServer(s, northbound.Core) + ppb.RegisterPndServer(s, northbound.Pnd) go func() { - if err := grpcServer.Serve(lis); err != nil { - log.Fatal(err) + if err := s.Serve(lis); err != nil { + log.Fatalf("Server exited with error: %v", err) } }() - return grpcServer, sock, sbi.ID().String(), nil -} - -func TestAddDevice(t *testing.T) { - type args struct { - addr string - username string - password string - sbi string - pnd string - deviceAddress string - deviceName string - } - tests := []struct { - name string - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := AddDevice(tt.args.addr, tt.args.username, tt.args.password, tt.args.sbi, tt.args.pnd, tt.args.deviceAddress, tt.args.deviceName); (err != nil) != tt.wantErr { - t.Errorf("AddDevice() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } } -func TestAddPnd(t *testing.T) { - type args struct { - addr string - name string - description string - sbi string - } - tests := []struct { - name string - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := AddPnd(tt.args.addr, tt.args.name, tt.args.description, tt.args.sbi); (err != nil) != tt.wantErr { - t.Errorf("AddPnd() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } +func bufDialer(context.Context, string) (net.Conn, error) { + return lis.Dial() } -func TestCapabilities(t *testing.T) { - type args struct { - a string - u string - p string - } - tests := []struct { - name string - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := Capabilities(tt.args.a, tt.args.u, tt.args.p); (err != nil) != tt.wantErr { - t.Errorf("Capabilities() error = %v, wantErr %v", err, tt.wantErr) - } - }) +func Test_Init(t *testing.T) { + if err := Init(bufnet); err != nil { + t.Error(err) } } -func TestCommit(t *testing.T) { - type args struct { - addr string - pnd string - cuids []string - } - tests := []struct { - name string - args args - want *ppb.SetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := Commit(tt.args.addr, tt.args.pnd, tt.args.cuids...) - if (err != nil) != tt.wantErr { - t.Errorf("Commit() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Commit() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestConfirm(t *testing.T) { - type args struct { - addr string - pnd string - cuids []string - } - tests := []struct { - name string - args args - want *ppb.SetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := Confirm(tt.args.addr, tt.args.pnd, tt.args.cuids...) - if (err != nil) != tt.wantErr { - t.Errorf("Confirm() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Confirm() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestDelete(t *testing.T) { - type args struct { - addr string - did string - pid string - path string - } - tests := []struct { - name string - args args - want *ppb.SetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := Delete(tt.args.addr, tt.args.did, tt.args.pid, tt.args.path) - if (err != nil) != tt.wantErr { - t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Delete() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetChanges(t *testing.T) { - type args struct { - addr string - pnd string - } - tests := []struct { - name string - args args - want *ppb.GetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetChanges(tt.args.addr, tt.args.pnd) - if (err != nil) != tt.wantErr { - t.Errorf("GetChanges() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetChanges() got = %v, want %v", got, tt.want) - } - }) +func Test_GetIds(t *testing.T) { + resp, err := GetIds(bufnet) + if err != nil { + t.Error(err) + return } + log.Info(resp) } -func TestGetDevice(t *testing.T) { - type args struct { - addr string - pid string - path string - did []string - } - tests := []struct { - name string - args args - want *ppb.GetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetDevice(tt.args.addr, tt.args.pid, tt.args.path, tt.args.did...) - if (err != nil) != tt.wantErr { - t.Errorf("GetDevice() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetDevice() got = %v, want %v", got, tt.want) - } - }) +func Test_AddPnd(t *testing.T) { + sbi := nucleus.NewSBI(spb.Type_OPENCONFIG) + resp, err := AddPnd(bufnet, "test", "test pnd", sbi.ID().String()) + if err != nil { + t.Error(err) + return } + log.Info(resp) } -func TestGetIds(t *testing.T) { - type args struct { - addr string - } - tests := []struct { - name string - args args - want []*ppb.PrincipalNetworkDomain - wantErr bool - }{ - // TODO: Add test cases. +func Test_GetPnd(t *testing.T) { + resp, err := GetPnd(bufnet, pndID) + if err != nil { + t.Error(err) + return } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetIds(tt.args.addr) - if (err != nil) != tt.wantErr { - t.Errorf("GetIds() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetIds() got = %v, want %v", got, tt.want) - } - }) + got := resp.Pnd[0].Id + if got != pndID { + t.Errorf("PND ID is %v, expected %v", got, pndID) } } -func TestGetPnd(t *testing.T) { - type args struct { - addr string - args []string - } - tests := []struct { - name string - args args - want *pb.GetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetPnd(tt.args.addr, tt.args.args...) - if (err != nil) != tt.wantErr { - t.Errorf("GetPnd() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetPnd() got = %v, want %v", got, tt.want) - } - }) +func Test_GetChanges(t *testing.T) { + resp, err := GetChanges(bufnet, pndID) + if err != nil { + t.Error(err) + return } + log.Info(resp) } -func TestInit(t *testing.T) { - type args struct { - addr string - } - tests := []struct { - name string - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := Init(tt.args.addr); (err != nil) != tt.wantErr { - t.Errorf("Init() error = %v, wantErr %v", err, tt.wantErr) - } - }) +func Test_CommitConfirm(t *testing.T) { + resp, err := Commit(bufnet, pndID, changeID) + if err != nil { + t.Error(err) + return } -} + log.Info(resp) -func TestReplace(t *testing.T) { - type args struct { - addr string - did string - pid string - path string - value string - } - tests := []struct { - name string - args args - want *ppb.SetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := Replace(tt.args.addr, tt.args.did, tt.args.pid, tt.args.path, tt.args.value) - if (err != nil) != tt.wantErr { - t.Errorf("Replace() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Replace() got = %v, want %v", got, tt.want) - } - }) + resp, err = Confirm(bufnet, pndID, changeID) + if err != nil { + t.Error(err) + return } -} + log.Info(resp) -func TestSubscribe(t *testing.T) { - type args struct { - address string - username string - password string - deviceName string - sample int64 - heartbeat int64 - args []string - } - tests := []struct { - name string - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := Subscribe(tt.args.address, tt.args.username, tt.args.password, tt.args.deviceName, tt.args.sample, tt.args.heartbeat, tt.args.args...); (err != nil) != tt.wantErr { - t.Errorf("Subscribe() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } } -func TestTarget(t *testing.T) { - type args struct { - bindAddr string - } - tests := []struct { - name string - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := Target(tt.args.bindAddr); (err != nil) != tt.wantErr { - t.Errorf("Target() error = %v, wantErr %v", err, tt.wantErr) - } - }) +func Test_AddDevice(t *testing.T) { + resp, err := AddDevice(bufnet, "test", "test", sbiID, pndID, "test", "test") + if err != nil { + t.Error(err) + return } + log.Info(resp) } -func TestUpdate(t *testing.T) { - type args struct { - addr string - did string - pid string - path string - value string - } - tests := []struct { - name string - args args - want *ppb.SetResponse - wantErr bool - }{ - // TODO: Add test cases. +func Test_GetDevice(t *testing.T) { + resp, err := GetDevice(bufnet, pndID, "", ondID) + if err != nil { + t.Error(err) + return } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := Update(tt.args.addr, tt.args.did, tt.args.pid, tt.args.path, tt.args.value) - if (err != nil) != tt.wantErr { - t.Errorf("Update() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Update() got = %v, want %v", got, tt.want) - } - }) + got := resp.Ond[0].Id + if got != ondID { + t.Errorf("PND ID is %v, expected %v", got, ondID) } } -func Test_callback(t *testing.T) { - type args struct { - newConfig ygot.ValidatedGoStruct - } - tests := []struct { - name string - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := callback(tt.args.newConfig); (err != nil) != tt.wantErr { - t.Errorf("callback() error = %v, wantErr %v", err, tt.wantErr) - } - }) +func Test_Update(t *testing.T) { + resp, err := Update(bufnet, ondID, pndID, "", "") + if err != nil { + t.Error(err) + return } + log.Info(resp) } -func Test_commitConfirm(t *testing.T) { - type args struct { - addr string - pnd string - changes []*ppb.SetChange - } - tests := []struct { - name string - args args - want *ppb.SetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := commitConfirm(tt.args.addr, tt.args.pnd, tt.args.changes) - if (err != nil) != tt.wantErr { - t.Errorf("commitConfirm() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("commitConfirm() got = %v, want %v", got, tt.want) - } - }) +func Test_Replace(t *testing.T) { + resp, err := Replace(bufnet, ondID, pndID, "", "") + if err != nil { + t.Error(err) + return } + log.Info(resp) } -func Test_getAllCore(t *testing.T) { - type args struct { - ctx context.Context - addr string - } - tests := []struct { - name string - args args - want *pb.GetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := getAllCore(tt.args.ctx, tt.args.addr) - if (err != nil) != tt.wantErr { - t.Errorf("getAllCore() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("getAllCore() got = %v, want %v", got, tt.want) - } - }) +func Test_Delete(t *testing.T) { + resp, err := Delete(bufnet, ondID, pndID, "") + if err != nil { + t.Error(err) + return } + log.Info(resp) } diff --git a/cmd/addDevice.go b/cmd/addDevice.go index cf6773b501f705b7d078e94d568abf76276c8177..2304e22141967ed8947833b6999818f521f97a70 100644 --- a/cmd/addDevice.go +++ b/cmd/addDevice.go @@ -34,6 +34,8 @@ package cmd import ( "code.fbi.h-da.de/cocsn/gosdn/cli" "github.com/spf13/cobra" + + log "github.com/sirupsen/logrus" ) // addDeviceCmd represents the addDevice command @@ -45,7 +47,7 @@ var addDeviceCmd = &cobra.Command{ Device address and user credentials need to be provided if they diverge from the default credentials.`, RunE: func(cmd *cobra.Command, args []string) error { - return cli.AddDevice( + resp, err := cli.AddDevice( apiEndpoint, username, password, @@ -54,6 +56,11 @@ if they diverge from the default credentials.`, address, deviceName, ) + if err != nil { + return err + } + log.Info(resp) + return nil }, } diff --git a/cmd/addPnd.go b/cmd/addPnd.go index e4e59c2530756a1255e58413ca9bf82c9c75817a..d61a454a9a347e8ebc05657d031afc785cb2517d 100644 --- a/cmd/addPnd.go +++ b/cmd/addPnd.go @@ -34,6 +34,8 @@ package cmd import ( "code.fbi.h-da.de/cocsn/gosdn/cli" "github.com/spf13/cobra" + + log "github.com/sirupsen/logrus" ) // addCmd represents the add command @@ -47,7 +49,12 @@ A description must be passed as positional argument. A name and default SBI can passed using flags`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return cli.AddPnd(apiEndpoint, pndName, args[0], pndDefaultSbi) + resp, err := cli.AddPnd(apiEndpoint, pndName, args[0], pndDefaultSbi) + if err != nil { + return err + } + log.Info(resp) + return nil }, } diff --git a/mocks/PrincipalNetworkDomain.go b/mocks/PrincipalNetworkDomain.go index 6c795cb7388b80a8446f5c2c8314ed98fa3eba5f..ed11f411beeff2f5f7e44d7e49550f2d2d937476 100644 --- a/mocks/PrincipalNetworkDomain.go +++ b/mocks/PrincipalNetworkDomain.go @@ -1,13 +1,15 @@ -// Code generated by mockery 2.7.4. DO NOT EDIT. +// Code generated by mockery 2.7.5. DO NOT EDIT. package mocks import ( mock "github.com/stretchr/testify/mock" - uuid "github.com/google/uuid" + pnd "code.fbi.h-da.de/cocsn/api/go/gosdn/pnd" + + transport "code.fbi.h-da.de/cocsn/api/go/gosdn/transport" - ygot "github.com/openconfig/ygot/ygot" + uuid "github.com/google/uuid" ) // PrincipalNetworkDomain is an autogenerated mock type for the PrincipalNetworkDomain type @@ -15,13 +17,13 @@ type PrincipalNetworkDomain struct { mock.Mock } -// AddDevice provides a mock function with given fields: _a0 -func (_m *PrincipalNetworkDomain) AddDevice(_a0 interface{}) error { - ret := _m.Called(_a0) +// AddDevice provides a mock function with given fields: name, opts, sid +func (_m *PrincipalNetworkDomain) AddDevice(name string, opts *transport.TransportOption, sid uuid.UUID) error { + ret := _m.Called(name, opts, sid) var r0 error - if rf, ok := ret.Get(0).(func(interface{}) error); ok { - r0 = rf(_a0) + if rf, ok := ret.Get(0).(func(string, *transport.TransportOption, uuid.UUID) error); ok { + r0 = rf(name, opts, sid) } else { r0 = ret.Error(0) } @@ -44,7 +46,7 @@ func (_m *PrincipalNetworkDomain) AddSbi(_a0 interface{}) error { } // ChangeOND provides a mock function with given fields: _a0, operation, path, value -func (_m *PrincipalNetworkDomain) ChangeOND(_a0 uuid.UUID, operation interface{}, path string, value ...string) error { +func (_m *PrincipalNetworkDomain) ChangeOND(_a0 uuid.UUID, operation pnd.ApiOperation, path string, value ...string) error { _va := make([]interface{}, len(value)) for _i := range value { _va[_i] = value[_i] @@ -55,7 +57,7 @@ func (_m *PrincipalNetworkDomain) ChangeOND(_a0 uuid.UUID, operation interface{} ret := _m.Called(_ca...) var r0 error - if rf, ok := ret.Get(0).(func(uuid.UUID, interface{}, string, ...string) error); ok { + if rf, ok := ret.Get(0).(func(uuid.UUID, pnd.ApiOperation, string, ...string) error); ok { r0 = rf(_a0, operation, path, value...) } else { r0 = ret.Error(0) @@ -78,8 +80,8 @@ func (_m *PrincipalNetworkDomain) Commit(_a0 uuid.UUID) error { return r0 } -// Committed provides a mock function with given fields: -func (_m *PrincipalNetworkDomain) Committed() []uuid.UUID { +// CommittedChanges provides a mock function with given fields: +func (_m *PrincipalNetworkDomain) CommittedChanges() []uuid.UUID { ret := _m.Called() var r0 []uuid.UUID @@ -152,6 +154,36 @@ func (_m *PrincipalNetworkDomain) Devices() []uuid.UUID { return r0 } +// GetChange provides a mock function with given fields: _a0, _a1 +func (_m *PrincipalNetworkDomain) GetChange(_a0 uuid.UUID, _a1 ...int) (interface{}, error) { + _va := make([]interface{}, len(_a1)) + for _i := range _a1 { + _va[_i] = _a1[_i] + } + var _ca []interface{} + _ca = append(_ca, _a0) + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + var r0 interface{} + if rf, ok := ret.Get(0).(func(uuid.UUID, ...int) interface{}); ok { + r0 = rf(_a0, _a1...) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(interface{}) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(uuid.UUID, ...int) error); ok { + r1 = rf(_a0, _a1...) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // GetDescription provides a mock function with given fields: func (_m *PrincipalNetworkDomain) GetDescription() string { ret := _m.Called() @@ -166,22 +198,22 @@ func (_m *PrincipalNetworkDomain) GetDescription() string { return r0 } -// GetDevice provides a mock function with given fields: _a0 -func (_m *PrincipalNetworkDomain) GetDevice(_a0 uuid.UUID) (ygot.GoStruct, error) { - ret := _m.Called(_a0) +// GetDevice provides a mock function with given fields: identifier +func (_m *PrincipalNetworkDomain) GetDevice(identifier string) (interface{}, error) { + ret := _m.Called(identifier) - var r0 ygot.GoStruct - if rf, ok := ret.Get(0).(func(uuid.UUID) ygot.GoStruct); ok { - r0 = rf(_a0) + var r0 interface{} + if rf, ok := ret.Get(0).(func(string) interface{}); ok { + r0 = rf(identifier) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(ygot.GoStruct) + r0 = ret.Get(0).(interface{}) } } var r1 error - if rf, ok := ret.Get(1).(func(uuid.UUID) error); ok { - r1 = rf(_a0) + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(identifier) } else { r1 = ret.Error(1) } @@ -236,18 +268,18 @@ func (_m *PrincipalNetworkDomain) ID() uuid.UUID { } // MarshalDevice provides a mock function with given fields: _a0 -func (_m *PrincipalNetworkDomain) MarshalDevice(_a0 uuid.UUID) (string, error) { +func (_m *PrincipalNetworkDomain) MarshalDevice(_a0 string) (string, error) { ret := _m.Called(_a0) var r0 string - if rf, ok := ret.Get(0).(func(uuid.UUID) string); ok { + if rf, ok := ret.Get(0).(func(string) string); ok { r0 = rf(_a0) } else { r0 = ret.Get(0).(string) } var r1 error - if rf, ok := ret.Get(1).(func(uuid.UUID) error); ok { + if rf, ok := ret.Get(1).(func(string) error); ok { r1 = rf(_a0) } else { r1 = ret.Error(1) @@ -256,8 +288,8 @@ func (_m *PrincipalNetworkDomain) MarshalDevice(_a0 uuid.UUID) (string, error) { return r0, r1 } -// Pending provides a mock function with given fields: -func (_m *PrincipalNetworkDomain) Pending() []uuid.UUID { +// PendingChanges provides a mock function with given fields: +func (_m *PrincipalNetworkDomain) PendingChanges() []uuid.UUID { ret := _m.Called() var r0 []uuid.UUID diff --git a/mocks/SouthboundInterface.go b/mocks/SouthboundInterface.go index 38ffb81ff021989bb8153c33169ce40ad8863a33..560db283ced93dfae4790ac5d934e132feb5d92f 100644 --- a/mocks/SouthboundInterface.go +++ b/mocks/SouthboundInterface.go @@ -1,4 +1,4 @@ -// Code generated by mockery 2.7.4. DO NOT EDIT. +// Code generated by mockery 2.7.5. DO NOT EDIT. package mocks @@ -6,6 +6,8 @@ import ( gnmi "github.com/openconfig/gnmi/proto/gnmi" mock "github.com/stretchr/testify/mock" + southbound "code.fbi.h-da.de/cocsn/api/go/gosdn/southbound" + uuid "github.com/google/uuid" yang "github.com/openconfig/goyang/pkg/yang" @@ -79,3 +81,17 @@ func (_m *SouthboundInterface) SetNode() func(*yang.Entry, interface{}, *gnmi.Pa return r0 } + +// Type provides a mock function with given fields: +func (_m *SouthboundInterface) Type() southbound.Type { + ret := _m.Called() + + var r0 southbound.Type + if rf, ok := ret.Get(0).(func() southbound.Type); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(southbound.Type) + } + + return r0 +} diff --git a/mocks/Storable.go b/mocks/Storable.go index e55a23eb41a51d2715f2ee94dfbb9e7a285a0145..5c986ae2befaa84c3069e4e07b05cb209ecdf9f5 100644 --- a/mocks/Storable.go +++ b/mocks/Storable.go @@ -1,4 +1,4 @@ -// Code generated by mockery 2.7.4. DO NOT EDIT. +// Code generated by mockery 2.7.5. DO NOT EDIT. package mocks diff --git a/mocks/Transport.go b/mocks/Transport.go index 7d8531e57334fd330a698165132428273d618f39..4ba9ae182ce9df5909230a780182e161c6b4052e 100644 --- a/mocks/Transport.go +++ b/mocks/Transport.go @@ -1,4 +1,4 @@ -// Code generated by mockery 2.7.4. DO NOT EDIT. +// Code generated by mockery 2.7.5. DO NOT EDIT. package mocks @@ -45,22 +45,6 @@ func (_m *Transport) Get(ctx context.Context, params ...string) (interface{}, er return r0, r1 } -// GetOptions provides a mock function with given fields: -func (_m *Transport) GetOptions() interface{} { - ret := _m.Called() - - var r0 interface{} - if rf, ok := ret.Get(0).(func() interface{}); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(interface{}) - } - } - - return r0 -} - // ProcessResponse provides a mock function with given fields: resp, root, models func (_m *Transport) ProcessResponse(resp interface{}, root interface{}, models *ytypes.Schema) error { ret := _m.Called(resp, root, models) diff --git a/northbound/server/core_test.go b/northbound/server/core_test.go new file mode 100644 index 0000000000000000000000000000000000000000..3240fbb3c39808819ef57529b4bec4b934ce20af --- /dev/null +++ b/northbound/server/core_test.go @@ -0,0 +1,104 @@ +package server + +import ( + "context" + "reflect" + "testing" + + pb "code.fbi.h-da.de/cocsn/api/go/gosdn/core" +) + +func Test_core_Set(t *testing.T) { + type args struct { + ctx context.Context + request *pb.SetRequest + } + tests := []struct { + name string + args args + want *pb.SetResponse + wantErr bool + }{ + { + name: "default", + args: args{ + ctx: context.Background(), + request: &pb.SetRequest{ + Pnd: []*pb.SetPnd{ + { + Name: "test", + Description: "test", + Sbi: "test", + }, + }, + }, + }, + want: &pb.SetResponse{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := core{ + UnimplementedCoreServer: pb.UnimplementedCoreServer{}, + } + got, err := s.Set(tt.args.ctx, tt.args.request) + if (err != nil) != tt.wantErr { + t.Errorf("core.Set() error = %v, wantErr %v", err, tt.wantErr) + return + } + tt.want.Timestamp = got.Timestamp + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("core.Set() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_core_Get(t *testing.T) { + type args struct { + ctx context.Context + request *pb.GetRequest + } + tests := []struct { + name string + args args + want []string + wantErr bool + }{ + { + name: "default", + args: args{ + ctx: context.Background(), + request: &pb.GetRequest{ + All: true, + }, + }, + want: []string{ + pndID, + "test", + "test", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := core{ + UnimplementedCoreServer: pb.UnimplementedCoreServer{}, + } + resp, err := s.Get(tt.args.ctx, tt.args.request) + if (err != nil) != tt.wantErr { + t.Errorf("core.Get() error = %v, wantErr %v", err, tt.wantErr) + return + } + + got := []string{ + resp.Pnd[0].Id, + resp.Pnd[0].Name, + resp.Pnd[0].Description, + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("core.Get() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/northbound/server/pnd.go b/northbound/server/pnd.go index 29e06c1baed9c721319e78e173809786347c6461..d74119c207f9e4a6056096c5efdd4db15ba9c7c3 100644 --- a/northbound/server/pnd.go +++ b/northbound/server/pnd.go @@ -49,15 +49,20 @@ func handleGetPnd(pid uuid.UUID) (*ppb.GetResponse, error) { if err != nil { return nil, err } + changes, err := fillChanges(pnd, true) + if err != nil { + return nil, err + } + return &ppb.GetResponse{ Timestamp: time.Now().UnixNano(), Pnd: &ppb.PrincipalNetworkDomain{ - Id: "", - Name: "", - Description: "", + Id: pnd.ID().String(), + Name: pnd.GetName(), + Description: pnd.GetDescription(), Ond: onds, Sbi: sbis, - Change: nil, + Change: changes, }, }, nil } @@ -180,7 +185,7 @@ func fillOnds(pnd nucleus.PrincipalNetworkDomain, all bool, did ...string) ([]*p return nil, err } cfg := ygot.GNMINotificationsConfig{} - dev, err := ygot.TogNMINotifications(d, time.Now().UnixNano(), cfg) + dev, err := ygot.TogNMINotifications(d.(*nucleus.Device).GoStruct, time.Now().UnixNano(), cfg) if err != nil { return nil, err } @@ -240,36 +245,31 @@ func fillChanges(pnd nucleus.PrincipalNetworkDomain, all bool, cuid ...string) ( func (p pnd) Set(ctx context.Context, request *ppb.SetRequest) (*ppb.SetResponse, error) { pid, err := uuid.Parse(request.Pid) if err != nil { - return handleSetError(), err + return nil, err } pnd, err := pndc.Get(pid) if err != nil { - return handleSetError(), err + return nil, err } ondResp, err := handleSetOnd(pnd, request.Ond) if err != nil { - return handleSetError(), err - } - sbiResp, err := handleSetSbi(pnd, request.Sbi) - if err != nil { - return handleSetError(), err + return nil, err } changeResp, err := handleSetChange(pnd, request.Change) if err != nil { - return handleSetError(), err + return nil, err } changeRequestResp, err := handleChangeRequest(pnd, request.ChangeRequest) if err != nil { - return handleSetError(), err + return nil, err } return &ppb.SetResponse{ Timestamp: time.Now().UnixNano(), Status: ppb.SetResponse_OK, Responses: []*ppb.SetResponse{ ondResp, - sbiResp, changeResp, changeRequestResp, }, @@ -280,10 +280,10 @@ func handleSetOnd(pnd nucleus.PrincipalNetworkDomain, req []*ppb.SetOnd) (*ppb.S for _, r := range req { sid, err := uuid.Parse(r.Sbi.Id) if err != nil { - return handleSetError(), err + return nil, err } if err := pnd.AddDevice(r.DeviceName, r.TransportOption, sid); err != nil { - return handleSetError(), err + return nil, err } } return &ppb.SetResponse{ @@ -303,16 +303,16 @@ func handleSetChange(pnd nucleus.PrincipalNetworkDomain, req []*ppb.SetChange) ( for _, r := range req { cuid, err := uuid.Parse(r.Cuid) if err != nil { - return handleSetError(), err + return nil, err } switch r.Op { case ppb.SetChange_COMMIT: if err := pnd.Commit(cuid); err != nil { - return handleSetError(), err + return nil, err } case ppb.SetChange_CONFIRM: if err := pnd.Confirm(cuid); err != nil { - return handleSetError(), err + return nil, err } default: return nil, &errors.ErrInvalidParameters{ @@ -331,10 +331,10 @@ func handleChangeRequest(pnd nucleus.PrincipalNetworkDomain, req []*ppb.ChangeRe for _, r := range req { did, err := uuid.Parse(r.Id) if err != nil { - return handleSetError(), err + return nil, err } if err := pnd.ChangeOND(did, r.ApiOp, r.Path, r.Value); err != nil { - return handleSetError(), err + return nil, err } } return &ppb.SetResponse{ @@ -342,10 +342,3 @@ func handleChangeRequest(pnd nucleus.PrincipalNetworkDomain, req []*ppb.ChangeRe Status: ppb.SetResponse_OK, }, nil } - -func handleSetError() *ppb.SetResponse { - return &ppb.SetResponse{ - Timestamp: time.Now().UnixNano(), - Status: ppb.SetResponse_ERROR, - } -} diff --git a/northbound/server/pnd_test.go b/northbound/server/pnd_test.go new file mode 100644 index 0000000000000000000000000000000000000000..3ddee46005add6904f0990794c78c8941a0bfba0 --- /dev/null +++ b/northbound/server/pnd_test.go @@ -0,0 +1,264 @@ +package server + +import ( + "context" + "os" + "reflect" + "testing" + + ppb "code.fbi.h-da.de/cocsn/api/go/gosdn/pnd" + spb "code.fbi.h-da.de/cocsn/api/go/gosdn/southbound" + "code.fbi.h-da.de/cocsn/api/go/gosdn/transport" + "code.fbi.h-da.de/cocsn/gosdn/mocks" + "code.fbi.h-da.de/cocsn/gosdn/nucleus" + "code.fbi.h-da.de/cocsn/yang-models/generated/openconfig" + "github.com/google/uuid" + log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/mock" +) + +const pndID = "2043519e-46d1-4963-9a8e-d99007e104b8" +const pendingChangeID = "0992d600-f7d4-4906-9559-409b04d59a5f" +const committedChangeID = "804787d6-e5a8-4dba-a1e6-e73f96b0119e" +const sbiID = "f6fd4b35-f039-4111-9156-5e4501bb8a5a" +const ondID = "7e0ed8cc-ebf5-46fa-9794-741494914883" + +var hostname = "manfred" +var pndUUID uuid.UUID +var pendingChangeUUID uuid.UUID +var committedChangeUUID uuid.UUID +var deviceUUID uuid.UUID +var mockPnd *mocks.PrincipalNetworkDomain +var mockDevice *nucleus.Device +var sbiStore *nucleus.SbiStore + +func TestMain(m *testing.M) { + log.SetReportCaller(true) + var err error + pndUUID, err = uuid.Parse(pndID) + if err != nil { + log.Fatal(err) + } + + pendingChangeUUID, err = uuid.Parse(pendingChangeID) + if err != nil { + log.Fatal(err) + } + + committedChangeUUID, err = uuid.Parse(committedChangeID) + if err != nil { + log.Fatal(err) + } + + deviceUUID, err = uuid.Parse(ondID) + if err != nil { + log.Fatal(err) + } + + mockDevice = &nucleus.Device{ + GoStruct: &openconfig.Device{ + System: &openconfig.OpenconfigSystem_System{ + Config: &openconfig.OpenconfigSystem_System_Config{ + Hostname: &hostname, + }, + }, + }, + UUID: deviceUUID, + SBI: nucleus.NewSBI(spb.Type_OPENCONFIG), + Transport: &mocks.Transport{}, + Name: hostname, + } + + sbiStore = nucleus.NewSbiStore() + if err := sbiStore.Add(mockDevice.SBI); err != nil { + log.Fatal(err) + } + + mockPnd = &mocks.PrincipalNetworkDomain{} + mockPnd.On("ID").Return(pndUUID) + mockPnd.On("GetName").Return("test") + mockPnd.On("GetDescription").Return("test") + mockPnd.On("GetSBIs").Return(sbiStore) + mockPnd.On("Devices").Return([]uuid.UUID{deviceUUID}) + mockPnd.On("PendingChanges").Return([]uuid.UUID{pendingChangeUUID}) + mockPnd.On("CommittedChanges").Return([]uuid.UUID{committedChangeUUID}) + mockPnd.On("GetChange", mock.Anything).Return(&nucleus.Change{}, nil) + mockPnd.On("AddDevice", mock.Anything, mock.Anything, mock.Anything).Return(nil) + mockPnd.On("GetDevice", mock.Anything).Return(mockDevice, nil) + mockPnd.On("Commit", mock.Anything).Return(nil) + mockPnd.On("Confirm", mock.Anything).Return(nil) + mockPnd.On("ChangeOND", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + + pndc = nucleus.NewPndStore() + if err := pndc.Add(mockPnd); err != nil { + log.Fatal(err) + } + + os.Exit(m.Run()) +} + +func Test_pnd_Get(t *testing.T) { + type args struct { + ctx context.Context + request *ppb.GetRequest + } + tests := []struct { + name string + args args + want []string + wantErr bool + }{ + { + name: "get pnd", + args: args{ + ctx: context.Background(), + request: &ppb.GetRequest{ + Request: &ppb.GetRequest_Pnd{ + Pnd: &ppb.GetPnd{}, + }, + Pid: pndID, + }, + }, + want: []string{ + pndID, + ondID, + mockDevice.SBI.ID().String(), + pendingChangeID, + committedChangeID, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := pnd{ + UnimplementedPndServer: ppb.UnimplementedPndServer{}, + } + resp, err := p.Get(tt.args.ctx, tt.args.request) + if (err != nil) != tt.wantErr { + t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr) + return + } + + got := []string{ + resp.Pnd.Id, + resp.Pnd.Ond[0].Id, + resp.Pnd.Sbi[0].Id, + resp.Pnd.Change[0].Id, + resp.Pnd.Change[1].Id, + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("Get() got = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_pnd_Set(t *testing.T) { + type args struct { + ctx context.Context + request *ppb.SetRequest + } + tests := []struct { + name string + args args + want ppb.SetResponseStatus + wantErr bool + }{ + { + name: "set ond", + args: args{ + ctx: context.Background(), + request: &ppb.SetRequest{ + Ond: []*ppb.SetOnd{ + { + Sbi: &spb.SouthboundInterface{ + Id: sbiID, + Type: spb.Type_OPENCONFIG, + }, + DeviceName: hostname, + TransportOption: &transport.TransportOption{ + Address: "test", + Username: "test", + Password: "test", + TransportOption: &transport.TransportOption_GnmiTransportOption{}, + }, + }, + }, + Pid: pndID, + }, + }, + want: ppb.SetResponse_OK, + }, + { + name: "set change", + args: args{ + ctx: context.Background(), + request: &ppb.SetRequest{ + Pid: pndID, + Change: []*ppb.SetChange{ + { + Cuid: pendingChangeID, + Op: ppb.SetChange_COMMIT, + }, + { + Cuid: committedChangeID, + Op: ppb.SetChange_CONFIRM, + }, + }, + }, + }, + want: ppb.SetResponse_OK, + }, + { + name: "change request", + args: args{ + ctx: context.Background(), + request: &ppb.SetRequest{ + Pid: pndID, + ChangeRequest: []*ppb.ChangeRequest{ + { + Id: ondID, + Path: "/system/config/hostname", + Value: "herbert", + ApiOp: ppb.ApiOperation_UPDATE, + }, + { + Id: ondID, + Path: "/system/config/hostname", + Value: "fridolin", + ApiOp: ppb.ApiOperation_REPLACE, + }, + { + Id: ondID, + Path: "/system/config/hostname", + ApiOp: ppb.ApiOperation_DELETE, + }, + }, + }, + }, + want: ppb.SetResponse_OK, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := pnd{ + UnimplementedPndServer: ppb.UnimplementedPndServer{}, + } + resp, err := p.Set(tt.args.ctx, tt.args.request) + if (err != nil) != tt.wantErr { + t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr) + return + } + got := resp.Status + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("Set() got = %v, want %v", got, tt.want) + } + for _, r := range resp.Responses { + got = r.Status + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("Set() got = %v, want %v", got, tt.want) + } + } + }) + } +} diff --git a/northbound/server/server_test.go b/northbound/server/server_test.go deleted file mode 100644 index 5f509d72e05434228d2cce9b6d61a560b4b086f1..0000000000000000000000000000000000000000 --- a/northbound/server/server_test.go +++ /dev/null @@ -1,520 +0,0 @@ -package server - -import ( - "context" - "reflect" - "testing" - - pb "code.fbi.h-da.de/cocsn/api/go/gosdn/core" - ppb "code.fbi.h-da.de/cocsn/api/go/gosdn/pnd" - spb "code.fbi.h-da.de/cocsn/api/go/gosdn/southbound" - "code.fbi.h-da.de/cocsn/gosdn/nucleus" - "github.com/google/uuid" -) - -func TestNewNBI(t *testing.T) { - pndc := nucleus.NewPndStore() - type args struct { - pnds *nucleus.PndStore - } - tests := []struct { - name string - args args - want *NorthboundInterface - }{ - { - name: "default", - args: args{ - pnds: pndc, - }, - want: &NorthboundInterface{ - Pnd: &pnd{}, - Core: &core{}, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := NewNBI(tt.args.pnds); !reflect.DeepEqual(got, tt.want) { - t.Errorf("NewNBI() = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_core_Get(t *testing.T) { - type fields struct { - UnimplementedCoreServer pb.UnimplementedCoreServer - } - type args struct { - ctx context.Context - request *pb.GetRequest - } - tests := []struct { - name string - fields fields - args args - want *pb.GetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s := core{ - UnimplementedCoreServer: tt.fields.UnimplementedCoreServer, - } - got, err := s.Get(tt.args.ctx, tt.args.request) - if (err != nil) != tt.wantErr { - t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Get() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_core_Set(t *testing.T) { - type fields struct { - UnimplementedCoreServer pb.UnimplementedCoreServer - } - type args struct { - ctx context.Context - request *pb.SetRequest - } - tests := []struct { - name string - fields fields - args args - want *pb.SetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s := core{ - UnimplementedCoreServer: tt.fields.UnimplementedCoreServer, - } - got, err := s.Set(tt.args.ctx, tt.args.request) - if (err != nil) != tt.wantErr { - t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Set() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_fillChanges(t *testing.T) { - type args struct { - pnd nucleus.PrincipalNetworkDomain - all bool - cuid []string - } - tests := []struct { - name string - args args - want []*ppb.Change - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := fillChanges(tt.args.pnd, tt.args.all, tt.args.cuid...) - if (err != nil) != tt.wantErr { - t.Errorf("fillChanges() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("fillChanges() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_fillOnds(t *testing.T) { - type args struct { - pnd nucleus.PrincipalNetworkDomain - all bool - did []string - } - tests := []struct { - name string - args args - want []*ppb.OrchestratedNetworkingDevice - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := fillOnds(tt.args.pnd, tt.args.all, tt.args.did...) - if (err != nil) != tt.wantErr { - t.Errorf("fillOnds() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("fillOnds() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_fillSbis(t *testing.T) { - type args struct { - pnd nucleus.PrincipalNetworkDomain - all bool - sid []string - } - tests := []struct { - name string - args args - want []*spb.SouthboundInterface - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := fillSbis(tt.args.pnd, tt.args.all, tt.args.sid...) - if (err != nil) != tt.wantErr { - t.Errorf("fillSbis() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("fillSbis() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_handleChangeRequest(t *testing.T) { - type args struct { - pnd nucleus.PrincipalNetworkDomain - req []*ppb.ChangeRequest - } - tests := []struct { - name string - args args - want *ppb.SetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := handleChangeRequest(tt.args.pnd, tt.args.req) - if (err != nil) != tt.wantErr { - t.Errorf("handleChangeRequest() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("handleChangeRequest() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_handleGetChange(t *testing.T) { - type args struct { - pid uuid.UUID - req *ppb.GetRequest_Change - } - tests := []struct { - name string - args args - want *ppb.GetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := handleGetChange(tt.args.pid, tt.args.req) - if (err != nil) != tt.wantErr { - t.Errorf("handleGetChange() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("handleGetChange() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_handleGetOnd(t *testing.T) { - type args struct { - pid uuid.UUID - req *ppb.GetRequest_Ond - } - tests := []struct { - name string - args args - want *ppb.GetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := handleGetOnd(tt.args.pid, tt.args.req) - if (err != nil) != tt.wantErr { - t.Errorf("handleGetOnd() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("handleGetOnd() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_handleGetPnd(t *testing.T) { - type args struct { - pid uuid.UUID - } - tests := []struct { - name string - args args - want *ppb.GetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := handleGetPnd(tt.args.pid) - if (err != nil) != tt.wantErr { - t.Errorf("handleGetPnd() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("handleGetPnd() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_handleGetSbi(t *testing.T) { - type args struct { - pid uuid.UUID - req *ppb.GetRequest_Sbi - } - tests := []struct { - name string - args args - want *ppb.GetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := handleGetSbi(tt.args.pid, tt.args.req) - if (err != nil) != tt.wantErr { - t.Errorf("handleGetSbi() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("handleGetSbi() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_handleSetChange(t *testing.T) { - type args struct { - pnd nucleus.PrincipalNetworkDomain - req []*ppb.SetChange - } - tests := []struct { - name string - args args - want *ppb.SetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := handleSetChange(tt.args.pnd, tt.args.req) - if (err != nil) != tt.wantErr { - t.Errorf("handleSetChange() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("handleSetChange() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_handleSetError(t *testing.T) { - tests := []struct { - name string - want *ppb.SetResponse - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := handleSetError(); !reflect.DeepEqual(got, tt.want) { - t.Errorf("handleSetError() = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_handleSetOnd(t *testing.T) { - type args struct { - pnd nucleus.PrincipalNetworkDomain - req []*ppb.SetOnd - } - tests := []struct { - name string - args args - want *ppb.SetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := handleSetOnd(tt.args.pnd, tt.args.req) - if (err != nil) != tt.wantErr { - t.Errorf("handleSetOnd() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("handleSetOnd() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_handleSetSbi(t *testing.T) { - type args struct { - pnd nucleus.PrincipalNetworkDomain - req []*ppb.SetSbi - } - tests := []struct { - name string - args args - want *ppb.SetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := handleSetSbi(tt.args.pnd, tt.args.req) - if (err != nil) != tt.wantErr { - t.Errorf("handleSetSbi() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("handleSetSbi() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_pnd_Get(t *testing.T) { - type fields struct { - UnimplementedPndServer ppb.UnimplementedPndServer - } - type args struct { - ctx context.Context - request *ppb.GetRequest - } - tests := []struct { - name string - fields fields - args args - want *ppb.GetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := pnd{ - UnimplementedPndServer: tt.fields.UnimplementedPndServer, - } - got, err := p.Get(tt.args.ctx, tt.args.request) - if (err != nil) != tt.wantErr { - t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Get() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_pnd_Set(t *testing.T) { - type fields struct { - UnimplementedPndServer ppb.UnimplementedPndServer - } - type args struct { - ctx context.Context - request *ppb.SetRequest - } - tests := []struct { - name string - fields fields - args args - want *ppb.SetResponse - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := pnd{ - UnimplementedPndServer: tt.fields.UnimplementedPndServer, - } - got, err := p.Set(tt.args.ctx, tt.args.request) - if (err != nil) != tt.wantErr { - t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Set() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_stringToUUID(t *testing.T) { - type args struct { - sid []string - } - tests := []struct { - name string - args args - want []uuid.UUID - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := stringToUUID(tt.args.sid) - if (err != nil) != tt.wantErr { - t.Errorf("stringToUUID() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("stringToUUID() got = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/nucleus/principalNetworkDomain.go b/nucleus/principalNetworkDomain.go index afa6e36be051f50020bc3bad1a725902182ec374..f9bcd8e6c2f88a1ce1e08835dcf5f60ee50e976f 100644 --- a/nucleus/principalNetworkDomain.go +++ b/nucleus/principalNetworkDomain.go @@ -25,7 +25,7 @@ type PrincipalNetworkDomain interface { AddSbi(interface{}) error RemoveSbi(uuid.UUID) error AddDevice(name string, opts *tpb.TransportOption, sid uuid.UUID) error - GetDevice(identifier string) (*Device, error) + GetDevice(identifier string) (interface{}, error) RemoveDevice(uuid.UUID) error Devices() []uuid.UUID ChangeOND(uuid uuid.UUID, operation ppb.ApiOperation, path string, value ...string) error @@ -229,7 +229,7 @@ func (pnd *pndImplementation) AddDevice(name string, opt *tpb.TransportOption, s return pnd.addDevice(d) } -func (pnd *pndImplementation) GetDevice(identifier string) (*Device, error) { +func (pnd *pndImplementation) GetDevice(identifier string) (interface{}, error) { d, err := pnd.devices.Get(FromString(identifier)) if err != nil { return nil, err diff --git a/nucleus/principalNetworkDomain_test.go b/nucleus/principalNetworkDomain_test.go index d4ad28e5a6c3a853971397e28cac736175a5fce6..132c9b265069fb0565d739587b0d8f7e05e68e36 100644 --- a/nucleus/principalNetworkDomain_test.go +++ b/nucleus/principalNetworkDomain_test.go @@ -705,8 +705,8 @@ func Test_pndImplementation_GetDevice(t *testing.T) { return } if foundDevice != nil { - if !reflect.DeepEqual(foundDevice.GoStruct, tt.want) { - t.Errorf("GetDevice() got = %v, want %v", foundDevice.GoStruct, tt.want) + if !reflect.DeepEqual(foundDevice.(*Device).GoStruct, tt.want) { + t.Errorf("GetDevice() got = %v, want %v", foundDevice.(*Device).GoStruct, tt.want) } } @@ -756,8 +756,8 @@ func Test_pndImplementation_GetDeviceByName(t *testing.T) { return } if foundDevice != nil { - if !reflect.DeepEqual(foundDevice.GoStruct, tt.want) { - t.Errorf("GetDeviceByName() got = %v, want %v", foundDevice.GoStruct, tt.want) + if !reflect.DeepEqual(foundDevice.(*Device).GoStruct, tt.want) { + t.Errorf("GetDeviceByName() got = %v, want %v", foundDevice.(*Device).GoStruct, tt.want) } } diff --git a/test/integration/cmdIntegration_test.go b/test/integration/cmdIntegration_test.go index b91547203d7098d66689cfe13eed1d8bb7d11992..dd24e37caa1144b200e1aa47b9bc6f97ae77c298 100644 --- a/test/integration/cmdIntegration_test.go +++ b/test/integration/cmdIntegration_test.go @@ -109,7 +109,7 @@ func TestCmdIntegration(t *testing.T) { cliPnd := viper.GetString("CLI_PND") cliSbi := viper.GetString("CLI_SBI") - if err := cli.AddDevice( + if _, err := cli.AddDevice( testAPIEndpoint, testUsername, testPassword,