In this short blog post I will talk about simple way of checking whether unix services on remote machine are working or not.
Target audience: QA Automation engineers / SDET
Sometimes while implementing automated tests for some UI website or testing web services, you need to make sure that some specific unix scripts on remote machine are started / stopped or even - you need to restart a couple of such services.
The following short post will explain, how to integrate such checks into existing Serenity based test automation solution.
What are unix init scripts?
Unix - based systems have a number of predefined System V init scripts under /etc/init.d directory as well user - defined ones. In order to manipulate with these scripts (such as starting, stopping or getting it’s status) you can use one suitable command - “service”.
The pattern of usage is simple: service your_script_name chosen_command.
status command is used for retrieving services status - whether it’s running of not.
start and stop commands are pretty obvious - used for starting or stopping given service.
restart command performs a full restarting of the given service.
For our example, I will use such script as syslog-ng. It’s an open-source log management solution, which are widely used in development.
Algorithm for testing
The method of verification is simple:
connect to the remote machine by SSH;
verify service status before running the command;
perform desired command for the chosen service;
verify that command was successfully run by getting new service’s status;
Preparing BDD scenarios
For sample purposes, I have prepared the most basic scenarios using init scripts - starting, stopping and restarting - all using service command.
Implementing scenario steps
Connecting to the remote machine
For ssh connection I am using sshj - it is an open - source library for Java. Only host, user login and password are required for the connection. RSA keys can be provided as keys.
The interesting point here, is the usage of small library - awaitility, for performing smarter waiting operations. In this particular case - waiting till command status will not be equal null.
Do not forget to use your own app_host parameter, as well as user_password parameter for SSH connection.
JBehave scenarios can be run either from maven (mvn integration-test serenity:aggregate) or from Intellij IDEA’s run configuration.
The usage of service command for verification init scripts is pretty straightforward - just run service status and check the terminal output. The status code of the command can be checked as well. Pay attention that if command was run without errors - the status code will be equal to 0. In other way - status code will be equal some value greater than 0, depends on the type of error which was occurred.