Skip to content
Snippets Groups Projects
application_test.go 2.91 KiB
Newer Older
  • Learn to ignore specific revisions
  • package integration_test_application
    
    import (
    	"context"
    	"fmt"
    	"testing"
    
    	mnepb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/networkelement"
    	tpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/transport"
    	"code.fbi.h-da.de/danet/gosdn/application-framework/event"
    	integration_test_utils "code.fbi.h-da.de/danet/gosdn/integration-tests/integrationTestUtils"
    	"github.com/sirupsen/logrus"
    	"github.com/stretchr/testify/assert"
    	"google.golang.org/grpc"
    )
    
    const targetAAdress = "gnmi-target_A:7030"
    const targetUserAndPW = "admin"
    const pndID = "5f20f34b-cbd0-4511-9ddc-c50cf6a3b49d"
    const pluginID = "d1c269a2-6482-4010-b0d8-679dff73153b"
    
    // The connection to the controller to use in each test.
    var conn *grpc.ClientConn
    
    // The context containing the credentials when authenticated.
    var ctx context.Context
    
    // A defaultSDN config with default/empty values.
    var defaultSDNConfig string
    
    var application *Application
    
    func TestMain(m *testing.M) {
    	localConn, localCtx, err := integration_test_utils.CreateSecureConnection()
    	if err != nil {
    		fmt.Println(err.Error())
    	}
    	conn = localConn
    	ctx = localCtx
    
    	sndConfig, err := integration_test_utils.ExportCurrentSDNConfig(conn, ctx)
    	defaultSDNConfig = sndConfig
    	if err != nil {
    		fmt.Println(err.Error())
    	}
    
    	integration_test_utils.ApplySDNConfig(conn, ctx, defaultSDNConfig)
    
    	topics := []event.Topic{event.ManagedNetworkElement, event.User}
    
    	application = NewApplication(ctx, conn, ":55055", topics)
    
    
    	eventTypeCallbackTuples := []event.TypeToCallbackTuple{
    		{
    			Type:     event.Add,
    			Callback: application.callback,
    		},
    		{
    			Type:     event.Update,
    			Callback: application.callback,
    		},
    		{
    			Type:     event.Delete,
    			Callback: application.callback,
    		},
    		{
    			Type:     event.Subscribe,
    			Callback: application.callback,
    		},
    	}
    	go application.Run(eventTypeCallbackTuples)
    
    	asdf := <-application.eventChannel
    	logrus.Info(asdf)
    
    	m.Run()
    }
    
    func TestAddEvent(t *testing.T) {
    	defer integration_test_utils.ApplySDNConfig(conn, ctx, defaultSDNConfig)
    
    	// setup required parameters
    	opt := &tpb.TransportOption{
    		Address:  targetAAdress,
    		Username: targetUserAndPW,
    		Password: targetUserAndPW,
    		TransportOption: &tpb.TransportOption_GnmiTransportOption{
    			GnmiTransportOption: &tpb.GnmiTransportOption{},
    		},
    		Tls: true,
    	}
    
    	addListRequest := &mnepb.AddListRequest{
    		Timestamp: integration_test_utils.GetTimestamp(),
    		Mne: []*mnepb.SetMne{
    			{
    				Address:         "gnmi-target_A:7030",
    				Pid:             pndID,
    				PluginId:        pluginID,
    				MneName:         "Horst",
    				TransportOption: opt,
    			},
    		},
    		Pid: pndID,
    	}
    
    	// setup gRPC services
    	mneService := mnepb.NewNetworkElementServiceClient(conn)
    
    	// add one device to the controller
    	_, err := mneService.AddList(ctx, addListRequest)
    	if err != nil {
    		t.Error(err)
    		t.FailNow()
    	}
    
    	// check if event is available and correct type
    	addEvent := <-application.eventChannel
    
    	assert.IsType(t, event.Add.String(), addEvent.Type)
    }