testserver

package
v2.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TestUserName = "testUser"
	TestPassword = "testPassword"
)

Defines credentials used for test sessions.

Variables

View Source
var CloseRequestHandler = func(h *SessionHandler, req *rpcRequestMessage) {
	_ = h.ch.Close()
}

CloseRequestHandler closes the transport channel on request receipt.

View Source
var EchoRequestHandler = func(h *SessionHandler, req *rpcRequestMessage) {
	data := replyData{Data: req.Request.Body}
	reply := &RPCReplyMessage{Data: data, MessageID: req.MessageID}
	err := h.encode(reply)
	assert.NoError(h.t, err, "Failed to encode response")
}

EchoRequestHandler responds to a request with a reply containing a data element holding the body of the request.

View Source
var FailingRequestHandler = func(h *SessionHandler, req *rpcRequestMessage) {
	reply := &RPCReplyMessage{
		MessageID: req.MessageID,
		Errors: []common.RPCError{
			{Severity: "error", Message: "oops"},
		},
	}
	err := h.encode(reply)
	assert.NoError(h.t, err, "Failed to encode response")
}

FailingRequestHandler replies to a request with an error.

View Source
var IgnoreRequestHandler = func(h *SessionHandler, req *rpcRequestMessage) {}

IgnoreRequestHandler does in nothing on receipt of a request.

View Source
var SmartRequesttHandler = func(h *SessionHandler, req *rpcRequestMessage) {
	data := replyData{Data: responseFor(req)}
	reply := &RPCReplyMessage{Data: data, MessageID: req.MessageID}
	err := h.encode(reply)
	assert.NoError(h.t, err, "Failed to encode response")
}

SmartRequesttHandler responds to common requests with trivial content.

Functions

This section is empty.

Types

type HandlerFactory

type HandlerFactory func(t assert.TestingT) SSHHandler

HandlerFactory is a test function that will deliver an SSHHandler.

type NotifyMessage

type NotifyMessage struct {
	XMLName   xml.Name `xml:"urn:ietf:params:xml:ns:netconf:notification:1.0 notification"`
	EventTime string   `xml:"eventTime"`
	Data      string   `xml:",innerxml"`
}

NotifyMessage defines the contents of a notification message that will be sent to a client session, where the element type of the notification event is unknown.

type RPCReplyMessage

type RPCReplyMessage struct {
	XMLName   xml.Name          `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 rpc-reply"`
	Errors    []common.RPCError `xml:"rpc-error,omitempty"`
	Data      replyData         `xml:"data"`
	Ok        bool              `xml:",omitempty"`
	RawReply  string            `xml:"-"`
	MessageID string            `xml:"message-id,attr"`
}

RPCReplyMessage and replyData represent an rpc-reply message that will be sent to a client session, where the element type of the reply body (i.e. the content of the data element) is unknown.

type RPCRequest

type RPCRequest struct {
	XMLName xml.Name
	Body    string `xml:",innerxml"`
}

RPCRequest describes an RPC request.

type RequestHandler

type RequestHandler func(h *SessionHandler, req *rpcRequestMessage)

RequestHandler is a function type that will be invoked by the session handler to handle an RPC request.

type SSHHandler

type SSHHandler interface {
	// Handler is a function that handles i/o to/from an SSH channel
	Handle(t assert.TestingT, ch ssh.Channel)
}

SSHHandler is the interface that is implemented to handle an SSH channel.

type SSHServer

type SSHServer struct {
	// contains filtered or unexported fields
}

SSHServer represents a test SSH Server

func NewSSHServer

func NewSSHServer(t assert.TestingT, uname, password string) *SSHServer

NewSSHServer deflivers a new test SSH Server, with a Handler that simply echoes lines received. The server implements password authentication with the given credentials.

func NewSSHServerHandler

func NewSSHServerHandler(t assert.TestingT, uname, password string, factory HandlerFactory, opts ...ServerOption) *SSHServer

NewSSHServerHandler deflivers a new test SSH Server, with a custom channel handler. The server implements password authentication with the given credentials.

func (*SSHServer) Close

func (ts *SSHServer) Close()

Close closes any resources used by the server.

func (*SSHServer) Port

func (ts *SSHServer) Port() int

Port delivers the tcp port number on which the server is listening.

type ServerOption added in v2.5.0

type ServerOption func(*serverOptions)

ServerOption implements options for configuring test server behaviour.

func RequestTypes added in v2.5.0

func RequestTypes(types []string) ServerOption

RequestTypes defines the request types that will be 'accepted' - i.e. the request response will be 'ok' (true). Defaults to {"subsystem"}

type SessionHandler

type SessionHandler struct {

	// The HelloMessage sent by the connecting client.
	ClientHello *common.HelloMessage

	Reqs []RPCRequest
	// contains filtered or unexported fields
}

SessionHandler represents the server side of an active netconf SSH session.

func (*SessionHandler) Close

func (h *SessionHandler) Close()

Close initiates session tear-down by closing the underlying transport channel.

func (*SessionHandler) Handle

func (h *SessionHandler) Handle(t assert.TestingT, ch ssh.Channel)

Handle establishes a Netconf server session on a newly-connected SSH channel.

func (*SessionHandler) LastReq

func (h *SessionHandler) LastReq() *RPCRequest

LastReq delivers the last request received by the handler, or nil if no requests have been received.

func (*SessionHandler) ReqCount

func (h *SessionHandler) ReqCount() int

ReqCount delivers the number of requests received by the handler.

func (*SessionHandler) SendNotification

func (h *SessionHandler) SendNotification(body string) *SessionHandler

SendNotification sends a notification message with the supplied body to the client.

func (*SessionHandler) WaitStart

func (h *SessionHandler) WaitStart()

WaitStart waits until the session handler is ready.

type TestNCServer

type TestNCServer struct {
	*SSHServer
	// contains filtered or unexported fields
}

TestNCServer represents a Netconf Server that can be used for 'on-board' testing. It encapsulates a transport connection to an SSH server, and a netconf session handler that will be invoked to handle netconf messages.

func NewTestNetconfServer

func NewTestNetconfServer(tctx assert.TestingT) *TestNCServer

NewTestNetconfServer creates a new TestNCServer that will accept Netconf localhost connections on an ephemeral port (available via Port(), with credentials defined by TestUserName and TestPassword. tctx will be used for handling failures; if the supplied value is nil, a default test context will be used. The behaviour of the Netconf session handler can be conifgured using the WithCapabilities and WithRequestHandler methods.

func (*TestNCServer) Close

func (ncs *TestNCServer) Close()

Close closes any active transport to the test server and prevents subsequent connections.

func (*TestNCServer) Errorf

func (ncs *TestNCServer) Errorf(format string, args ...interface{})

Errorf provides testing.T compatibility if a test context is not provided when the test server is created.

func (*TestNCServer) FailNow

func (ncs *TestNCServer) FailNow()

FailNow provides testing.T compatibility if a test context is not provided when the test server is created.

func (*TestNCServer) LastHandler

func (ncs *TestNCServer) LastHandler() *SessionHandler

LastHandler delivers the most recently instantiated session handler.

func (*TestNCServer) SessionHandler

func (ncs *TestNCServer) SessionHandler(id uint64) *SessionHandler

SessionHandler delivers the netconf session handler associated with the specified session id.

func (*TestNCServer) WithCapabilities

func (ncs *TestNCServer) WithCapabilities(caps []string) *TestNCServer

WithCapabilities define the capabilities that the server will advertise when a netconf client connects.

func (*TestNCServer) WithRequestHandler

func (ncs *TestNCServer) WithRequestHandler(rh RequestHandler) *TestNCServer

WithRequestHandler adds a request handler to the netconf session.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL