Repository: clouditor/clouditor
Pull request #231 (2021-06-01 12:06:53)

Small test refactor
Added function to start auth server as standalone, used in multiple tests.


Patch:
diff --git a/service/auth/login.go b/service/auth/auth.go
similarity index 86%
rename from service/auth/login.go
rename to service/auth/auth.go
index 0db67263..79e53b46 100644
--- a/service/auth/login.go
+++ b/service/auth/auth.go
@@ -28,6 +28,8 @@ package auth
 import (
 	"context"
 	"errors"
+	"fmt"
+	"net"
 	"time"
 
 	"clouditor.io/clouditor/api/auth"
@@ -35,6 +37,7 @@ import (
 	argon2 "github.com/alexedwards/argon2id"
 	"github.com/golang-jwt/jwt"
 	"github.com/sirupsen/logrus"
+	"google.golang.org/grpc"
 	"google.golang.org/grpc/codes"
 	"google.golang.org/grpc/status"
 	"gorm.io/gorm"
@@ -178,3 +181,35 @@ func (s Service) issueToken(subject string, fullName string, email string, expir
 	token, err = claims.SignedString(key)
 	return
 }
+
+// StartStandaloneAuthServer starts a gRPC server containing just the auth service
+func StartStandaloneAuthServer(address string) (sock net.Listener, server *grpc.Server, err error) {
+	var (
+		authService *Service
+	)
+
+	// create a new socket for gRPC communication
+	sock, err = net.Listen("tcp", address)
+	if err != nil {
+		return nil, nil, fmt.Errorf("could not listen: %w", err)
+	}
+
+	err = persistence.InitDB(true, "", 0)
+
+	if err != nil {
+		return nil, nil, fmt.Errorf("could not initialize in-memory DB: %w", err)
+	}
+
+	authService = &Service{}
+	authService.CreateDefaultUser("clouditor", "clouditor")
+
+	server = grpc.NewServer()
+	auth.RegisterAuthenticationServer(server, authService)
+
+	go func() {
+		// serve the gRPC socket
+		_ = server.Serve(sock)
+	}()
+
+	return sock, server, nil
+}


Test patch:
diff --git a/cli/commands/login/login_test.go b/cli/commands/login/login_test.go
index 585e6090..5b69526a 100644
--- a/cli/commands/login/login_test.go
+++ b/cli/commands/login/login_test.go
@@ -28,14 +28,11 @@ package login_test
 import (
 	"fmt"
 	"io/ioutil"
-	"log"
 	"net"
 	"os"
 	"testing"
 
-	"clouditor.io/clouditor/api/auth"
 	"clouditor.io/clouditor/cli/commands/login"
-	"clouditor.io/clouditor/persistence"
 	service_auth "clouditor.io/clouditor/service/auth"
 	"github.com/spf13/viper"
 	"github.com/stretchr/testify/assert"
@@ -43,39 +40,18 @@ import (
 )
 
 var sock net.Listener
+var server *grpc.Server
 
-func init() {
-	StartAuthServer()
-}
+func TestMain(m *testing.M) {
+	var err error
 
-func StartAuthServer() {
-	var (
-		err         error
-		authService *service_auth.Service
-	)
+	sock, server, err = service_auth.StartStandaloneAuthServer(":0")
 
-	// create a new socket for gRPC communication
-	sock, err = net.Listen("tcp", ":0") // random open port
 	if err != nil {
-		log.Fatalf("Could not listen: %v", err)
+		panic(err)
 	}
 
-	err = persistence.InitDB(true, "", 0)
-
-	if err != nil {
-		log.Fatalf("Server exited: %v", err)
-	}
-
-	authService = &service_auth.Service{}
-	authService.CreateDefaultUser("clouditor", "clouditor")
-
-	server := grpc.NewServer()
-	auth.RegisterAuthenticationServer(server, authService)
-
-	go func() {
-		// serve the gRPC socket
-		_ = server.Serve(sock)
-	}()
+	os.Exit(m.Run())
 }
 
 func TestLogin(t *testing.T) {
@@ -85,6 +61,7 @@ func TestLogin(t *testing.T) {
 	)
 
 	defer sock.Close()
+	defer server.Stop()
 
 	dir, err = ioutil.TempDir(os.TempDir(), ".clouditor")
 	assert.Nil(t, err)
diff --git a/cli/session_test.go b/cli/session_test.go
index 6f8e325a..1c4fcdfb 100644
--- a/cli/session_test.go
+++ b/cli/session_test.go
@@ -29,14 +29,12 @@ import (
 	"context"
 	"fmt"
 	"io/ioutil"
-	"log"
 	"net"
 	"os"
 	"testing"
 
 	"clouditor.io/clouditor/api/auth"
 	"clouditor.io/clouditor/cli"
-	"clouditor.io/clouditor/persistence"
 	service_auth "clouditor.io/clouditor/service/auth"
 	"github.com/spf13/viper"
 	"github.com/stretchr/testify/assert"
@@ -46,42 +44,20 @@ import (
 )
 
 var sock net.Listener
+var server *grpc.Server
 
-func init() {
-	StartAuthServer()
-}
+func TestMain(m *testing.M) {
+	var err error
 
-func StartAuthServer() {
-	var (
-		err         error
-		authService *service_auth.Service
-	)
+	sock, server, err = service_auth.StartStandaloneAuthServer(":0")
 
-	// create a new socket for gRPC communication
-	sock, err = net.Listen("tcp", ":0") // random open port
 	if err != nil {
-		log.Fatalf("Could not listen: %v", err)
+		panic(err)
 	}
 
-	err = persistence.InitDB(true, "", 0)
-
-	if err != nil {
-		log.Fatalf("Server exited: %v", err)
-	}
-
-	authService = &service_auth.Service{}
-	authService.CreateDefaultUser("clouditor", "clouditor")
-
-	server := grpc.NewServer()
-	auth.RegisterAuthenticationServer(server, authService)
-
-	go func() {
-		// serve the gRPC socket
-		_ = server.Serve(sock)
-	}()
+	os.Exit(m.Run())
 }
 
-// TODO(oxisto): instead of replicating the things we do in the cmd, it would be good to call the command with arguments
 func TestSession(t *testing.T) {
 	var (
 		err     error
@@ -89,6 +65,7 @@ func TestSession(t *testing.T) {
 		dir     string
 	)
 	defer sock.Close()
+	defer server.Stop()
 
 	dir, err = ioutil.TempDir(os.TempDir(), ".clouditor")
 	assert.Nil(t, err)