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 26, 2023
1 parent 13109ee commit 17d4eb6
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 140 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{})

Check warning on line 86 in pkg/controlsvc/connect.go

View check run for this annotation

Codecov / codecov/patch

pkg/controlsvc/connect.go#L86

Added line #L86 was not covered by tests
if err != nil {
return nil, err
}
Expand Down
58 changes: 27 additions & 31 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)

Check warning on line 79 in pkg/controlsvc/controlsvc.go

View check run for this annotation

Codecov / codecov/patch

pkg/controlsvc/controlsvc.go#L78-L79

Added lines #L78 - L79 were not covered by tests
}

// 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

Check warning on line 190 in pkg/controlsvc/controlsvc.go

View check run for this annotation

Codecov / codecov/patch

pkg/controlsvc/controlsvc.go#L189-L190

Added lines #L189 - L190 were not covered by tests
}

// 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 @@ -293,7 +291,7 @@ func (s *Server) RunControlSession(conn net.Conn) {
}
}
if err != nil {
writeMsg := fmt.Sprintf("ERROR: %s", err)
writeMsg := fmt.Sprintf("ERROR: %s\n", err)
if writeToConnWithLog(conn, s.nc, writeMsg, writeControlServiceError) {
return
}
Expand All @@ -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 All @@ -336,14 +334,14 @@ func (s *Server) RunControlSession(conn net.Conn) {
if err != nil {
errorNormal(s.nc, "", err)

writeMsg := fmt.Sprintf("ERROR: %s", err)
writeMsg := fmt.Sprintf("ERROR: %s\n", err)
if writeToConnWithLog(conn, s.nc, writeMsg, writeControlServiceError) {
return
}
} else if cfr != nil {
rbytes, err := json.Marshal(cfr)
if err != nil {
writeMsg := fmt.Sprintf("ERROR: could not convert response to JSON: %s", err)
writeMsg := fmt.Sprintf("ERROR: could not convert response to JSON: %s\n", err)
if writeToConnWithLog(conn, s.nc, writeMsg, writeControlServiceError) {

Check warning on line 345 in pkg/controlsvc/controlsvc.go

View check run for this annotation

Codecov / codecov/patch

pkg/controlsvc/controlsvc.go#L344-L345

Added lines #L344 - L345 were not covered by tests
return
}
Expand All @@ -355,7 +353,7 @@ func (s *Server) RunControlSession(conn net.Conn) {
}
}
} else {
writeMsg := "ERROR: Unknown command"
writeMsg := "ERROR: Unknown command\n"
if writeToConnWithLog(conn, s.nc, writeMsg, writeControlServiceError) {
return
}
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)

Check warning on line 436 in pkg/controlsvc/controlsvc.go

View check run for this annotation

Codecov / codecov/patch

pkg/controlsvc/controlsvc.go#L436

Added line #L436 was not covered by tests
}
} 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

Check warning on line 471 in pkg/controlsvc/controlsvc.go

View check run for this annotation

Codecov / codecov/patch

pkg/controlsvc/controlsvc.go#L470-L471

Added lines #L470 - L471 were not covered by tests
}
if listener != nil {
go s.ConnectionListener(ctx, listener)
}
go s.ConnectionListener(ctx, listener)

Check warning on line 473 in pkg/controlsvc/controlsvc.go

View check run for this annotation

Codecov / codecov/patch

pkg/controlsvc/controlsvc.go#L473

Added line #L473 was not covered by tests
}

return nil
Expand Down
Loading

0 comments on commit 17d4eb6

Please sign in to comment.