Browse Source

refactor(vpn): make parseStatusLog a mockable func

Mustafa Arici 7 years ago
parent
commit
762e9c867a
1 changed files with 6 additions and 4 deletions
  1. 6 4
      vpn.go

+ 6 - 4
vpn.go

@@ -60,19 +60,21 @@ type Server struct {
 	dbServerModel
 	webPort string
 
-	emitToFileFunc func(path, content string, mode uint) error
-	openFunc       func(path string) (io.Reader, error)
+	emitToFileFunc     func(path, content string, mode uint) error
+	openFunc           func(path string) (io.Reader, error)
+	parseStatusLogFunc func(f io.Reader) ([]clEntry, []rtEntry)
 }
 
 // TheServer returns a pointer to the server instance.
 func TheServer() *Server {
 	once.Do(func() {
-		// Initialize the server instance.
+		// Initialize the server instance by setting default mockable funcs & attributes.
 		serverInstance = &Server{
 			emitToFileFunc: emitToFile,
 			openFunc: func(path string) (io.Reader, error) {
 				return os.Open(path)
 			},
+			parseStatusLogFunc: parseStatusLog,
 		}
 	})
 	if db != nil {
@@ -668,7 +670,7 @@ func (svr *Server) GetConnectedUsers() ([]User, error) {
 		panic(err)
 	}
 
-	cl, _ := parseStatusLog(f) // client list from OpenVPN status log
+	cl, _ := svr.parseStatusLogFunc(f) // client list from OpenVPN status log
 	for _, c := range cl {
 		var u dbUserModel
 		q := db.Where(dbUserModel{Username: c.CommonName}).First(&u)