Skip to content
Snippets Groups Projects

Add basic application framework and example application to show interaction between events an NBI

Merged Ghost User requested to merge istaester/init-application-framework into develop
1 file
+ 45
23
Compare changes
  • Side-by-side
  • Inline
@@ -5,23 +5,46 @@ import (
@@ -5,23 +5,46 @@ import (
"log"
"log"
"net"
"net"
"testing"
"testing"
 
"time"
apb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac"
apb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac"
spb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/southbound"
spb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/southbound"
 
"code.fbi.h-da.de/danet/gosdn/controller/nucleus"
 
"code.fbi.h-da.de/danet/gosdn/controller/rbac"
"google.golang.org/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/test/bufconn"
"google.golang.org/grpc/test/bufconn"
)
)
func dialer() func(context.Context, string) (net.Conn, error) {
func getTestAuthInterceptorServer(t *testing.T) (*AuthInterceptor, *User, *Role, *sbiServer) {
 
jwtManager := rbac.NewJWTManager("test", time.Minute)
 
userStore := rbac.NewMemoryUserStore()
 
userService := rbac.NewUserService(userStore)
 
 
roleStore := rbac.NewMemoryRoleStore()
 
roleService := rbac.NewRoleService(roleStore)
 
 
pndStore := nucleus.NewMemoryPndStore()
 
 
s := NewAuthInterceptor(jwtManager, userService, roleService)
 
u := NewUserServer(jwtManager, userService)
 
r := NewRoleServer(jwtManager, roleService)
 
sbi := NewSbiServer(pndStore)
 
 
clearAndCreateAuthTestSetup(userService, roleService)
 
 
return s, u, r, sbi
 
}
 
 
func dialer(interceptorServer *AuthInterceptor, userServer *User, roleServer *Role, sbiServer *sbiServer) func(context.Context, string) (net.Conn, error) {
listener := bufconn.Listen(1024 * 1024)
listener := bufconn.Listen(1024 * 1024)
interceptor := NewAuthInterceptor(jwt)
interceptor := interceptorServer
server := grpc.NewServer(grpc.UnaryInterceptor(interceptor.Unary()), grpc.StreamInterceptor(interceptor.Stream()))
server := grpc.NewServer(grpc.UnaryInterceptor(interceptor.Unary()), grpc.StreamInterceptor(interceptor.Stream()))
apb.RegisterUserServiceServer(server, &User{})
apb.RegisterUserServiceServer(server, userServer)
spb.RegisterSbiServiceServer(server, &sbiServer{})
spb.RegisterSbiServiceServer(server, sbiServer)
go func() {
go func() {
if err := server.Serve(listener); err != nil {
if err := server.Serve(listener); err != nil {
@@ -35,20 +58,21 @@ func dialer() func(context.Context, string) (net.Conn, error) {
@@ -35,20 +58,21 @@ func dialer() func(context.Context, string) (net.Conn, error) {
}
}
func TestAuthInterceptor_Unary(t *testing.T) {
func TestAuthInterceptor_Unary(t *testing.T) {
validToken, err := createTestUserToken("testAdmin", true)
a, u, r, s := getTestAuthInterceptorServer(t)
 
validToken, err := createTestUserToken("testAdmin", true, a.userService, a.jwtManager)
if err != nil {
if err != nil {
log.Fatal(err)
t.Fatal(err)
}
}
wrongUserToken, err := createTestUserToken("foo", false)
wrongUserToken, err := createTestUserToken("foo", false, a.userService, a.jwtManager)
if err != nil {
if err != nil {
log.Fatal(err)
t.Fatal(err)
}
}
ctx := context.Background()
ctx := context.Background()
conn, err := grpc.DialContext(ctx, "", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(dialer()))
conn, err := grpc.DialContext(ctx, "", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(dialer(a, u, r, s)))
if err != nil {
if err != nil {
log.Fatal(err)
t.Fatal(err)
}
}
defer conn.Close()
defer conn.Close()
@@ -121,15 +145,16 @@ func TestAuthInterceptor_Unary(t *testing.T) {
@@ -121,15 +145,16 @@ func TestAuthInterceptor_Unary(t *testing.T) {
}
}
func TestAuthInterceptor_Stream(t *testing.T) {
func TestAuthInterceptor_Stream(t *testing.T) {
validToken, err := createTestUserToken("testAdmin", true)
a, u, r, s := getTestAuthInterceptorServer(t)
 
validToken, err := createTestUserToken("testAdmin", true, a.userService, a.jwtManager)
if err != nil {
if err != nil {
log.Fatal(err)
t.Fatal(err)
}
}
ctx := context.Background()
ctx := context.Background()
conn, err := grpc.DialContext(ctx, "", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(dialer()))
conn, err := grpc.DialContext(ctx, "", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(dialer(a, u, r, s)))
if err != nil {
if err != nil {
log.Fatal(err)
t.Fatal(err)
}
}
defer conn.Close()
defer conn.Close()
@@ -186,14 +211,15 @@ func TestAuthInterceptor_Stream(t *testing.T) {
@@ -186,14 +211,15 @@ func TestAuthInterceptor_Stream(t *testing.T) {
}
}
func TestAuthInterceptor_authorize(t *testing.T) {
func TestAuthInterceptor_authorize(t *testing.T) {
validToken, err := createTestUserToken("testAdmin", true)
a, _, _, _ := getTestAuthInterceptorServer(t)
 
validToken, err := createTestUserToken("testAdmin", true, a.userService, a.jwtManager)
if err != nil {
if err != nil {
log.Fatal(err)
t.Fatal(err)
}
}
wrongUserToken, err := createTestUserToken("foo", false)
wrongUserToken, err := createTestUserToken("foo", false, a.userService, a.jwtManager)
if err != nil {
if err != nil {
log.Fatal(err)
t.Fatal(err)
}
}
type args struct {
type args struct {
@@ -232,11 +258,7 @@ func TestAuthInterceptor_authorize(t *testing.T) {
@@ -232,11 +258,7 @@ func TestAuthInterceptor_authorize(t *testing.T) {
}
}
for _, tt := range tests {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
auth := &AuthInterceptor{
if err := a.authorize(tt.args.ctx, tt.args.method); (err != nil) != tt.wantErr {
jwtManager: jwt,
}
if err := auth.authorize(tt.args.ctx, tt.args.method); (err != nil) != tt.wantErr {
t.Errorf("AuthInterceptor.authorize() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("AuthInterceptor.authorize() error = %v, wantErr %v", err, tt.wantErr)
}
}
})
})
Loading