Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
principalNetworkDomain_test.go 12.99 KiB
package nucleus

import (
	"code.fbi.h-da.de/cocsn/gosdn/mocks"
	"code.fbi.h-da.de/cocsn/yang-models/generated/openconfig"
	"errors"
	"github.com/google/uuid"
	log "github.com/sirupsen/logrus"
	"github.com/stretchr/testify/mock"
	"reflect"
	"testing"
)

func testSetupPnd() {
	var err error
	did, err = uuid.Parse("4d8246f8-e884-41d6-87f5-c2c784df9e44")
	if err != nil {
		log.Fatal(err)
	}

	mdid, err = uuid.Parse("688a264e-5f85-40f8-bd13-afc42fcd5c7a")
	if err != nil {
		log.Fatal(err)
	}

	defaultSbiId, err = uuid.Parse("b70c8425-68c7-4d4b-bb5e-5586572bd64b")
	if err != nil {
		log.Fatal(err)
	}

	defaultPndId, err = uuid.Parse("b4016412-eec5-45a1-aa29-f59915357bad")
	if err != nil {
		log.Fatal(err)
	}
}

func mockDevice() Device {
	return Device{
		Uuid:      mdid,
		GoStruct:  nil,
		SBI:       &OpenConfig{},
		Transport: &mocks.Transport{},
	}
}

func newPnd() pndImplementation {
	return pndImplementation{
		name:        "default",
		description: "default test pnd",
		sbic:        sbiStore{store{}},
		devices:     deviceStore{store{}},
	}
}

func newPndWithId() pndImplementation {
	return pndImplementation{
		name:        "default",
		description: "default test pnd",
		sbic:        sbiStore{store{}},
		devices:     deviceStore{store{}},
		id:          defaultPndId,
	}
}

var did uuid.UUID
var mdid uuid.UUID
var defaultSbiId uuid.UUID
var defaultPndId uuid.UUID

func TestNewPND(t *testing.T) {
	pnd := newPnd()
	if err := pnd.addSbi(&OpenConfig{id: defaultSbiId}); err != nil {
		t.Error(err)
	}
	type args struct {
		name        string
		description string
		sbi         SouthboundInterface
	}
	tests := []struct {
		name    string
		args    args
		want    PrincipalNetworkDomain
		wantErr bool
	}{
		{
			name: "default",
			args: args{
				name:        "default",
				description: "default test pnd",
				sbi:         &OpenConfig{id: defaultSbiId},
			},
			want:    &pnd,
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := NewPND(tt.args.name, tt.args.description, tt.args.sbi)
			if (err != nil) != tt.wantErr {
				t.Errorf("NewPND() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("NewPND() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestNewPNDwithId(t *testing.T) {
	pnd := newPndWithId()
	if err := pnd.addSbi(&OpenConfig{id: defaultSbiId}); err != nil {
		t.Error(err)
	}
	type args struct {
		name        string
		description string
		id          uuid.UUID
		sbi         SouthboundInterface
	}
	tests := []struct {
		name    string
		args    args
		want    PrincipalNetworkDomain
		wantErr bool
	}{
		{
			name: "default",
			args: args{
				name:        "default",
				description: "default test pnd",
				id:          defaultPndId,
				sbi:         &OpenConfig{id: defaultSbiId},
			},
			want:    &pnd,
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := NewPNDwithId(tt.args.name, tt.args.description, tt.args.id, tt.args.sbi)
			if (err != nil) != tt.wantErr {
				t.Errorf("NewPNDwithId() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("NewPNDwithId() got = %v, want %v", got, tt.want)
			}
		})
	}
}

func Test_destroy(t *testing.T) {
	tests := []struct {
		name    string
		wantErr bool
	}{
		{name: "dummy", wantErr: false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if err := destroy(); (err != nil) != tt.wantErr {
				t.Errorf("destroy() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func Test_pndImplementation_AddDevice(t *testing.T) {
	type args struct {
		device interface{}
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{
			name: "default",
			args: args{device: &Device{
				Uuid: did,
			}},
			wantErr: false,
		},
		{
			name: "fails wrong type",
			args: args{device: &pndImplementation{
				id: did,
			}},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pnd := newPnd()
			if err := pnd.AddDevice(tt.args.device); (err != nil) != tt.wantErr {
				t.Errorf("AddDevice() error = %v, wantErr %v", err, tt.wantErr)
			}
			_, ok := pnd.devices.store[did]
			if !ok {
				t.Errorf("AddDevice() Device %v not in device store %v",
					tt.args.device, pnd.devices)
			}
			if err := pnd.devices.delete(did); err != nil {
				t.Error(err)
			}
		})
	}
}

func Test_pndImplementation_AddSbi(t *testing.T) {
	type args struct {
		sbi interface{}
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{
			name: "default",
			args: args{
				sbi: &OpenConfig{
					id: defaultSbiId,
				},
			},
			wantErr: false,
		},
		{
			name: "fails wrong type",
			args: args{
				sbi: &pndImplementation{
					id: defaultSbiId,
				},
			},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pnd := newPnd()
			if err := pnd.AddSbi(tt.args.sbi); (err != nil) != tt.wantErr {
				t.Errorf("AddSbi() error = %v, wantErr %v", err, tt.wantErr)
			}
			_, ok := pnd.sbic.store[defaultSbiId]
			if !ok {
				t.Errorf("AddSbi() SBI %v not in device store %v",
					tt.args.sbi, pnd.GetSBIs())
			}
			if err := pnd.sbic.delete(defaultSbiId); err != nil {
				t.Error(err)
			}
		})
	}
}

func Test_pndImplementation_ContainsDevice(t *testing.T) {
	type args struct {
		uuid   uuid.UUID
		device *Device
	}
	tests := []struct {
		name string
		args args
		want bool
	}{
		{name: "default", args: args{
			uuid:   did,
			device: &Device{Uuid: did},
		}, want: true},
		{name: "fails", args: args{
			uuid:   uuid.New(),
			device: &Device{Uuid: did},
		}, want: false},
		{name: "fails empty", args: args{
			uuid:   uuid.New(),
			device: &Device{Uuid: did},
		}, want: false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pnd := newPnd()
			if tt.name != "fails empty" {
				if err := pnd.devices.add(tt.args.device); err != nil {
					t.Error(err)
				}
			}
			if got := pnd.ContainsDevice(tt.args.uuid); got != tt.want {
				t.Errorf("ContainsDevice() = %v, want %v", got, tt.want)
			}
			if err := pnd.devices.delete(did); err != nil && tt.name != "fails empty" {
				t.Error(err)
			}
		})
	}
}

func Test_pndImplementation_Destroy(t *testing.T) {
	type fields struct {
		name        string
		description string
		sbi         sbiStore
		devices     deviceStore
	}
	tests := []struct {
		name    string
		fields  fields
		wantErr bool
	}{
		{name: "dummy", fields: fields{}, wantErr: false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pnd := &pndImplementation{
				name:        tt.fields.name,
				description: tt.fields.description,
				sbic:        tt.fields.sbi,
				devices:     tt.fields.devices,
			}
			if err := pnd.Destroy(); (err != nil) != tt.wantErr {
				t.Errorf("Destroy() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func Test_pndImplementation_GetDescription(t *testing.T) {
	tests := []struct {
		name string
		want string
	}{
		{name: "default", want: "default test pnd"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pnd := newPnd()
			if got := pnd.GetDescription(); got != tt.want {
				t.Errorf("GetDescription() = %v, want %v", got, tt.want)
			}
		})
	}
}

func Test_pndImplementation_GetName(t *testing.T) {
	tests := []struct {
		name string
		want string
	}{
		{name: "default", want: "default"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pnd := newPnd()
			if got := pnd.GetName(); got != tt.want {
				t.Errorf("GetName() = %v, want %v", got, tt.want)
			}
		})
	}
}

func Test_pndImplementation_GetSBIs(t *testing.T) {
	pnd := newPnd()
	tests := []struct {
		name string
		want *sbiStore
	}{
		{name: "default", want: &pnd.sbic},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := pnd.GetSBIs(); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("GetSBIs() = %v, want %v", got, tt.want)
			}
		})
	}
}

func Test_pndImplementation_MarshalDevice(t *testing.T) {
	type args struct {
		uuid uuid.UUID
	}
	tests := []struct {
		name    string
		args    args
		want    string
		wantErr bool
	}{
		{
			name:    "default",
			args:    args{did},
			want:    "{\n\t\"Acl\": null,\n\t\"Bgp\": null,\n\t\"Components\": null,\n\t\"Interfaces\": null,\n\t\"LocalRoutes\": null,\n\t\"Messages\": null,\n\t\"NetworkInstances\": null,\n\t\"RoutingPolicy\": null,\n\t\"System\": null\n}",
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pnd := newPnd()
			d := &Device{
				Uuid:      tt.args.uuid,
				GoStruct:  &openconfig.Device{},
				SBI:       nil,
				Transport: nil,
			}
			if err := pnd.addDevice(d); err != nil {
				t.Error(err)
			}
			got, err := pnd.MarshalDevice(tt.args.uuid)
			if (err != nil) != tt.wantErr {
				t.Errorf("MarshalDevice() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if got != tt.want {
				t.Errorf("MarshalDevice() got = %v, want %v", got, tt.want)
			}
			if err := pnd.devices.delete(did); err != nil {
				t.Error(err)
			}
		})
	}
}

func Test_pndImplementation_RemoveDevice(t *testing.T) {
	type args struct {
		uuid uuid.UUID
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{name: "default", args: args{uuid: did}, wantErr: false},
		{name: "fails", args: args{uuid: uuid.New()}, wantErr: true},
		{name: "fails empty", args: args{uuid: did}, wantErr: true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pnd := newPnd()
			if tt.name != "fails empty" {
				d := &Device{Uuid: did}
				if err := pnd.addDevice(d); err != nil {
					t.Error(err)
				}
			}
			if err := pnd.RemoveDevice(tt.args.uuid); (err != nil) != tt.wantErr {
				t.Errorf("RemoveDevice() error = %v, wantErr %v", err, tt.wantErr)
			}
			if pnd.devices.exists(did) && tt.name == "default" {
				t.Errorf("RemoveDevice() device still in device store %v", pnd.devices)
			}
		})
	}
}

func Test_pndImplementation_RemoveSbi(t *testing.T) {
	type args struct {
		id uuid.UUID
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{name: "default", args: args{id: defaultSbiId}, wantErr: false},
		{name: "fails", args: args{id: uuid.New()}, wantErr: true},
		{name: "fails empty", args: args{id: defaultSbiId}, wantErr: true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			pnd := &pndImplementation{
				name:        "test-remove-sbi",
				description: "test-remove-sbi",
				sbic:        sbiStore{store{}},
				devices:     deviceStore{store{}},
				id:          defaultPndId,
			}
			if tt.name != "fails empty" {
				if err := pnd.addSbi(&OpenConfig{id: defaultSbiId}); err != nil {
					t.Error(err)
				}
			}
			if err := pnd.RemoveSbi(tt.args.id); (err != nil) != tt.wantErr {
				t.Errorf("RemoveSbi() error = %v, wantErr %v", err, tt.wantErr)
			}
			if pnd.sbic.exists(tt.args.id) {
				t.Errorf("RemoveDevice() SBI still in SBI store %v", pnd.sbic)
			}
		})
	}
}

func Test_pndImplementation_Request(t *testing.T) {
	type args struct {
		uuid uuid.UUID
		path string
		rErr error
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{
			name: "default",
			args: args{
				uuid: mdid,
				path: "",
				rErr: nil,
			},
			wantErr: false,
		},
		{
			name: "error",
			args: args{
				uuid: did,
				path: "",
				rErr: errors.New("deliberate test fail"),
			},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			deviceWithMockTransport := mockDevice()
			pnd := newPnd()
			tr := deviceWithMockTransport.Transport.(*mocks.Transport)
			tr.On("Get", mockContext, mock.Anything).Return(mock.Anything, tt.args.rErr)
			tr.On("ProcessResponse", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.rErr)
			_ = pnd.addDevice(&deviceWithMockTransport)
			if err := pnd.Request(tt.args.uuid, tt.args.path); (err != nil) != tt.wantErr {
				t.Errorf("Request() error = %v, wantErr %v", err, tt.wantErr)
			}
			if err := pnd.devices.delete(mdid); err != nil {
				t.Error(err)
			}
		})
	}
}

func Test_pndImplementation_RequestAll(t *testing.T) {
	type args struct {
		uuid uuid.UUID
		path string
		rErr error
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{
			name: "default",
			args: args{
				uuid: mdid,
				path: "",
				rErr: nil,
			},
			wantErr: false,
		},
		{
			name: "error",
			args: args{
				uuid: did,
				path: "",
				rErr: errors.New("deliberate test fail"),
			},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			deviceWithMockTransport := mockDevice()
			pnd := newPnd()
			tr := deviceWithMockTransport.Transport.(*mocks.Transport)
			tr.On("Get", mockContext, mock.Anything).Return(mock.Anything, tt.args.rErr)
			tr.On("ProcessResponse", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.rErr)
			_ = pnd.addDevice(&deviceWithMockTransport)
			if err := pnd.RequestAll(tt.args.path); (err != nil) != tt.wantErr {
				t.Errorf("RequestAll() error = %v, wantErr %v", err, tt.wantErr)
			}
			if err := pnd.devices.delete(mdid); err != nil {
				t.Error(err)
			}
		})
	}
}