Documentation
¶
Overview ¶
Package testutils contains functions used as utility code for tests.
Because of that most of these functions do not have return values of type error, but instead call panic() when something goes wrong.
While these functions are stable and tested themselves, they are not intended to be used directly by proxyssh-external code. As such their signatures and edge case behaviour may change without notice.
Index ¶
- func AuthorizedKeysString(key ssh.PublicKey) string
- func GenerateRSATestKeyPair() (ssh.Signer, ssh.PublicKey)
- func GetTestLogger() *log.Logger
- func GetTestSessionProcess(session *ssh.Session) *os.Process
- func IsProcessAlive(proc *os.Process) (res bool)
- func NewTestListenAddress() string
- func NewTestServerSession(address string, options ssh.ClientConfig) (*ssh.Client, *ssh.Session, error)
- func RunComposeTest(config string, files map[string]string, testcode ComposeTestCode) error
- func RunTestServerCommand(address string, options ssh.ClientConfig, command, stdin string) (stdout string, stderr string, code int, err error)
- func SliceContainsString(haystack []string, needle string) bool
- func TCPConstantTestResponse(listener net.Listener, response string)
- func WriteTempFile(pattern, content string) (filepath string, cleanup func())
- type ComposeTestCode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AuthorizedKeysString ¶
AuthorizedKeysString turns a public key into a string that can be written to an authorized_keys file. If something goes wrong, calls panic()
func GenerateRSATestKeyPair ¶
GenerateRSATestKeyPair generates a new RSA Keypair for use within testing. A keypair consists of a private key (in the form of a Signer) and a public key.
If something goes wrong during the generation of the keypair, panic() is called.
The bitsize of the keypair is determined internally, but will be the same for subsequent calls of this function.
func GetTestLogger ¶
GetTestLogger returns a pointer to a logger that is to be shared amongst all test cases.
This function can safely be called from different test cases, and in different goroutines at once. It will always return the same logger.
func GetTestSessionProcess ¶
GetTestSessionProcess returns the process belonging to an ssh session. If the process can not be found panic() is called.
The session is assumed to run on the same host as where this function is called. The session is furthermore assumed to use a bash-compatible shell and that the '$$' variable returns the process id.
This function is itself untested.
func IsProcessAlive ¶
IsProcessAlive checks if the process refered to by the argument is still alive.
This function relies on the fact that the underlying operating system supports sending signals to the process. On Windows and Plan 9 this function may panic, as it can not be used.
This function is itself untested.
func NewTestListenAddress ¶
func NewTestListenAddress() string
NewTestListenAddress returns a new unused address that can be used to start a listener on. The address returned will be off the form 'host:port' and compatible with net.Dial() and friends.
The address is guaranteed to be on a port higher than 1024. Furthermore, it is guaranteed not be used by any other server, and listens only on the loopback interface. It will typically be something like "127.0.0.1:12345", but is not guaranteed to be of this form.
If no address is available, or something unexpected happens, panic() is called.
func NewTestServerSession ¶
func NewTestServerSession(address string, options ssh.ClientConfig) (*ssh.Client, *ssh.Session, error)
NewTestServerSession connects and starts a new ssh session on the sever listening at address.
This function sets reasonable defaults for the options. If options.HostKeyCallback is not set, sets it to a function that accepts every host key. If options.User is the empty string, uses the username "user". This function can thus be called with an empty options struct.
If no error occurs, the function expects the caller the Close() method on the client. If an error occurs during initialization, the client and session will be closed and an error will be returned. A typical invocation of this function should look like:
client, session, err := NewTestServerSession(address, ssh.ClientConfig{})
if err != nil {
return err
}
defer client.Close()
This function is itself untested.
func RunComposeTest ¶
func RunComposeTest(config string, files map[string]string, testcode ComposeTestCode) error
RunComposeTest runs a docker-compose based test. It first starts the docker-compose configuration contained in the string. Thenm it writes out addtional data files in the provided map and waits for it to become available. Finally, it runs the testcode function.
Calling this function is roughly equivalent to the following bash pseudo-code:
mktmp -d echo $config > tmpdir/docker-compose.yml docker-compose pull docker-compose up -d tmpdir/docker-compose.yml // ... call f() ... docker-compose down -v rm -rf tmpdir
For source code compatibility purposes, all occurences of '\t' in the compose file will be replace with four spaces.
This command depends on the 'docker-compose' executable to be available in path.
This function has two kinds of error conditions, those that occur during setting up the project, and those that occur in the testcode. If an error occurs during setup or teardown, panic() is called. If an error occurs during the testcase, and testcase does not call panic(), the error is returned by this function.
This function is itself untested.
func RunTestServerCommand ¶
func RunTestServerCommand(address string, options ssh.ClientConfig, command, stdin string) (stdout string, stderr string, code int, err error)
RunTestServerCommand runs a command on the ssh server listening at address, and returns its standard output and input. The address and options parameters are passed to NewTestServerSession. The command being run is determined by command. The standard input passed to the command is determined from stdin.
The output of the command (consisting of stdout and stderr), along with it's exit code will be returned. If something goes wrong running the command, an error will be returned.
This function is itself untested.
func SliceContainsString ¶
SliceContainsString returns true if haystack contains needle, and false otherwise.
func TCPConstantTestResponse ¶
TCPConstantTestResponse starts accepting connections on the listener. It then sends a constant response and closes the accepted connection.
This function performs blocking work on the goroutine it was called on. As such, it should typically be called like:
listener, err := net.Listen("tcp", address)
go TCPConstantTestResponse(listener, response)
defer listener.Close()
func WriteTempFile ¶
WriteTempFile writes content into a temporary file.
Pattern is used to generate the filename of the file, it is passed on to "ioutil".Tempfile. Content is written as a string into the file.
This function returns a pair of (filepath, cleanup) where filepath is the path to the temporary file and cleanup is a function that removes the temporary file.
It is the callers responsibility to call the cleanup function. A typical invocation of this function is something like:
tempfile, cleanup = WriteTempFile(pattern, content) defer cleanup()
If something goes wrong during the creation of the temporary file, panic() is called.
Types ¶
type ComposeTestCode ¶
type ComposeTestCode func(cli *client.Client, findService func(string) types.Container, stopService func(string)) error
ComposeTestCode is a function that runs tests inside a docker-compose file context. It returns an error if the test cases failed, and nil otherwise. See also RunComposeTest.
This function receives three parameters from the testrunner.
cli is a valid Docker Client that can be used for arbitary interactions with docker.
findService is a function that is provided a name of a service, and returns the container within the current docker-compose context.
stopService and is a function that can be used to stop a particular service in the docker-compose service.