Skip to content
Snippets Groups Projects
Commit 9d5e7e12 authored by Fabian Seidl's avatar Fabian Seidl
Browse files

setup seems to work basically, added simple test, figure out how to test others events WIP

parent e5e8941d
No related branches found
No related tags found
1 merge request!691Resolve "Implement integration tests for applications"
Pipeline #178003 failed
This commit is part of merge request !691. Comments created here will be created in the context of that merge request.
......@@ -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
......
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
}
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)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment