Skip to content

Commit

Permalink
Add unit tests for controlsvc
Browse files Browse the repository at this point in the history
  • Loading branch information
resoluteCoder committed Sep 21, 2023
1 parent eae8dd4 commit 5384a77
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 136 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ build-all:
GOOS=windows go build -o receptor.exe ./cmd/receptor-cl && \
GOOS=darwin go build -o receptor.app ./cmd/receptor-cl && \
go build example/*.go && \
go build -o receptor --tags no_controlsvc,no_backends,no_services,no_tls_config,no_workceptor,no_cert_auth ./cmd/receptor-cl && \
go build -o receptor --tags no_backends,no_services,no_tls_config,no_workceptor,no_cert_auth ./cmd/receptor-cl && \
go build -o receptor ./cmd/receptor-cl

DIST := receptor_$(shell echo '$(VERSION)' | sed 's/^v//')_$(GOOS)_$(GOARCH)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controlsvc/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (c *connectCommand) ControlFunc(_ context.Context, nc NetceptorForControlCo
if err != nil {
return nil, err
}
err = cfo.BridgeConn("Connecting\n", rc, "connected service", nc.GetLogger())
err = cfo.BridgeConn("Connecting\n", rc, "connected service", nc.GetLogger(), &Util{})
if err != nil {
return nil, err
}
Expand Down
50 changes: 23 additions & 27 deletions pkg/controlsvc/controlsvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,58 +73,55 @@ type Tlser interface {
NewListener(inner net.Listener, config *tls.Config) net.Listener
}

type Tls struct{}
type TLS struct{}

func (t *Tls) NewListener(inner net.Listener, config *tls.Config) net.Listener {
func (t *TLS) NewListener(inner net.Listener, config *tls.Config) net.Listener {
return tls.NewListener(inner, config)
}

// SockControl implements the ControlFuncOperations interface that is passed back to control functions.
type SockControl struct {
conn net.Conn
utils Utiler
io Copier
conn net.Conn
}

func NewSockControl(conn net.Conn, utils Utiler, copier Copier) *SockControl {
func NewSockControl(conn net.Conn) *SockControl {
return &SockControl{
conn: conn,
utils: utils,
io: copier,
conn: conn,
}
}

func (s *SockControl) RemoteAddr() net.Addr {
return s.conn.RemoteAddr()
}

// WriteMessage attempts to write a message to a connection
// WriteMessage attempts to write a message to a connection.
func (s *SockControl) WriteMessage(message string) error {
if message != "" {
_, err := s.conn.Write([]byte(message))
if err != nil {
return err
}
}

return nil
}

// BridgeConn bridges the socket to another socket.
func (s *SockControl) BridgeConn(message string, bc io.ReadWriteCloser, bcName string, logger *logger.ReceptorLogger) error {
func (s *SockControl) BridgeConn(message string, bc io.ReadWriteCloser, bcName string, logger *logger.ReceptorLogger, utils Utiler) error {
if err := s.WriteMessage(message); err != nil {
return err
}
s.utils.BridgeConns(s.conn, "control service", bc, bcName, logger)
utils.BridgeConns(s.conn, "control service", bc, bcName, logger)

return nil
}

// ReadFromConn copies from the socket to an io.Writer, until EOF.
func (s *SockControl) ReadFromConn(message string, out io.Writer) error {
func (s *SockControl) ReadFromConn(message string, out io.Writer, io Copier) error {
if err := s.WriteMessage(message); err != nil {
return err
}
if _, err := s.io.Copy(out, s.conn); err != nil {
if _, err := io.Copy(out, s.conn); err != nil {
return err
}

Expand Down Expand Up @@ -155,10 +152,9 @@ type Server struct {
nc NetceptorForControlsvc
controlFuncLock sync.RWMutex
controlTypes map[string]ControlCommandType
// new stuff
serverUtils Utiler
serverNet Neter
serverTls Tlser
serverUtils Utiler
serverNet Neter
serverTLS Tlser
}

// New returns a new instance of a control service.
Expand All @@ -169,7 +165,7 @@ func New(stdServices bool, nc NetceptorForControlsvc) *Server {
controlTypes: make(map[string]ControlCommandType),
serverUtils: &Util{},
serverNet: &Net{},
serverTls: &Tls{},
serverTLS: &TLS{},
}
if stdServices {
s.controlTypes["ping"] = &pingCommandType{}
Expand All @@ -190,8 +186,8 @@ func (s *Server) SetServerNet(n Neter) {
s.serverNet = n
}

func (s *Server) SetServerTls(t Tlser) {
s.serverTls = t
func (s *Server) SetServerTLS(t Tlser) {
s.serverTLS = t
}

// MainInstance is the global instance of the control service instantiated by the command-line main() function.
Expand All @@ -217,11 +213,13 @@ func errorNormal(nc NetceptorForControlsvc, logMessage string, err error) bool {
if !strings.HasSuffix(err.Error(), normalCloseError) {
nc.GetLogger().Error("%s: %s\n", logMessage, err)
}

return true
}

func writeToConnWithLog(conn net.Conn, nc NetceptorForControlsvc, writeMessage string, logMessage string) bool {
_, err := conn.Write([]byte(writeMessage))

return errorNormal(nc, logMessage, err)
}

Expand Down Expand Up @@ -318,7 +316,7 @@ func (s *Server) RunControlSession(conn net.Conn) {
}
s.controlFuncLock.RUnlock()
if ct != nil {
cfo := NewSockControl(conn, &Util{}, &SocketConnIO{})
cfo := NewSockControl(conn)

var cfr map[string]interface{}
var cc ControlCommand
Expand Down Expand Up @@ -435,7 +433,7 @@ func (s *Server) RunControlSvc(ctx context.Context, service string, tlscfg *tls.
return fmt.Errorf("error listening on TCP socket: %s", err)
}
if tcptls != nil {
tli = s.serverTls.NewListener(tli, tcptls)
tli = s.serverTLS.NewListener(tli, tcptls)
}
} else {
tli = nil
Expand Down Expand Up @@ -469,12 +467,10 @@ func (s *Server) RunControlSvc(ctx context.Context, service string, tlscfg *tls.
}
}()
for _, listener := range []net.Listener{uli, tli, li} {
if reflect.ValueOf(listener).IsNil() {
if listener == nil || reflect.ValueOf(listener).IsNil() {
continue
}
if listener != nil {
go s.ConnectionListener(ctx, listener)
}
go s.ConnectionListener(ctx, listener)
}

return nil
Expand Down
Loading

0 comments on commit 5384a77

Please sign in to comment.