diff --git a/dev_env_data/docker-compose/integration-test_docker-compose.yml b/dev_env_data/docker-compose/integration-test_docker-compose.yml index 3d355431e435558556165abddd7e6b05cb4cc2e0..1a4eb087eaf8b4a6b23bb32c55d968a92e0cd524 100644 --- a/dev_env_data/docker-compose/integration-test_docker-compose.yml +++ b/dev_env_data/docker-compose/integration-test_docker-compose.yml @@ -19,6 +19,8 @@ services: rabbitmq: image: rabbitmq:3-management + ports: + - 127.0.0.1:5672:5672 healthcheck: test: rabbitmq-diagnostics -q ping interval: 30s diff --git a/integration-tests/application_tests/appUtility_test.go b/integration-tests/application_tests/appUtility_test.go new file mode 100644 index 0000000000000000000000000000000000000000..05a7ad5b8540b9f3c162d1b2b2917451a55c309c --- /dev/null +++ b/integration-tests/application_tests/appUtility_test.go @@ -0,0 +1,73 @@ +package integration_test_application + +import ( + "context" + "os" + "os/signal" + "syscall" + + "code.fbi.h-da.de/danet/gosdn/application-framework/event" + "code.fbi.h-da.de/danet/gosdn/application-framework/registration" + "github.com/sirupsen/logrus" + "google.golang.org/grpc" +) + +// Application is an example for a sdn application. +type Application struct { + eventService event.ServiceInterface + stopChannel chan os.Signal + grpcClientConn *grpc.ClientConn + eventChannel chan event.Event +} + +func NewApplication(ctx context.Context, grpcClientConn *grpc.ClientConn, controllerAddress string, topics []event.Topic) *Application { + queueCredentials, err := registration.Register(ctx, controllerAddress, "integration-test-application", "SecurePresharedToken") + if err != nil { + logrus.Errorf("failed to register application on control plane. %v", err) + os.Exit(1) + } + + eventService, err := event.NewEventService( + queueCredentials, + topics, + ) + if err != nil { + logrus.Errorf("failed to create event service. %v", err) + os.Exit(1) + } + + return &Application{ + eventService: eventService, + stopChannel: make(chan os.Signal, 1), + grpcClientConn: grpcClientConn, + eventChannel: make(chan event.Event, 1), + } +} + +// Run runs the application. +func (a *Application) Run(eventTypeCallbackTuples []event.TypeToCallbackTuple) { + signal.Notify(a.stopChannel, os.Interrupt, syscall.SIGTERM) + + a.eventService.SubscribeToEventType(eventTypeCallbackTuples) + a.eventService.SetupEventReciever(a.stopChannel) + + var forever chan struct{} + + go func() { + for { + select { + case <-a.stopChannel: + close(forever) + _ = a.grpcClientConn.Close() + + return + } + } + }() + + <-forever +} + +func (a *Application) callback(event *event.Event) { + a.eventChannel <- *event +} diff --git a/integration-tests/application_tests/application_test.go b/integration-tests/application_tests/application_test.go new file mode 100644 index 0000000000000000000000000000000000000000..eb608325be2c2d4f79defa4ebea1804b98a1e650 --- /dev/null +++ b/integration-tests/application_tests/application_test.go @@ -0,0 +1,120 @@ +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, localConn, ":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) +}