Class SshGSpec
- java.lang.Object
-
- com.privalia.qa.specs.BaseGSpec
-
- com.privalia.qa.specs.SshGSpec
-
public class SshGSpec extends BaseGSpec
Steps definitions for running bash commands and establishing SSH connections with a remote host- Author:
- Jose Fernandez
-
-
Field Summary
-
Fields inherited from class com.privalia.qa.specs.BaseGSpec
commonspec
-
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
assertCommandExistsOnTimeOut(Integer timeout, Integer wait, String command, String search)
Validates command output with timeoutvoid
checkShellExitStatus(int expectedExitStatus)
Deprecated.Success exit status is directly checked in the "execute remote command" method, so this is not needed anymore.void
copyFromRemoteFile(String remotePath, String localPath)
Copies file/s from remote system into local systemvoid
copyToRemoteFile(String localPath, String remotePath)
Copies file/s from local system to remote systemvoid
executeCommand(String command, String foo, Integer exitStatus, String bar, String envVar)
Executes the command specified in remote systemvoid
executeLocalCommand(String command, Integer exitStatus, String envVar)
Executes the command specified in the local systemvoid
findShellOutput(String search)
Check the existence of a text at a command outputvoid
notFindShellOutput(String search)
Check the non existence of a text at a command outputvoid
openSSHConnection(String remoteHost, String user, String foo, String password, String bar, String pemFile)
Opens a ssh connection to remote host-
Methods inherited from class com.privalia.qa.specs.BaseGSpec
getCommonSpec
-
-
-
-
Constructor Detail
-
SshGSpec
public SshGSpec(CommonG spec)
Default constructor.- Parameters:
spec
- CommonG object
-
-
Method Detail
-
openSSHConnection
@Given("^I open a ssh connection to \'(.+?)\' with user \'(.+?)\'( and password \'(.+?)\')?( using pem file \'(.+?)\')?$") public void openSSHConnection(String remoteHost, String user, String foo, String password, String bar, String pemFile) throws Exception
Opens a ssh connection to remote hostConnects to the remote server by ssh with the given username and password. After the connection is open, you can execute commands on the remote server or send and receive commands.
Example: Connecting to a remote server
Given I open a ssh connection to '10.200.56.59' with user 'myuser' and password 'temporal' When I run 'ls -l' in the ssh connection and save the value in environment variable 'RESULT' Then '${RESULT}' contains 'total'
- Parameters:
remoteHost
- remote hostuser
- remote userfoo
- the foopassword
- (required if pemFile null)bar
- the barpemFile
- (required if password null)- Throws:
Exception
- exception- See Also:
executeCommand(String, String, Integer, String, String)
,UtilsGSpec.checkValue(String, String, String)
-
copyFromRemoteFile
@Given("^I inbound copy \'(.+?)\' through a ssh connection to \'(.+?)\'$") public void copyFromRemoteFile(String remotePath, String localPath) throws Exception
Copies file/s from remote system into local systemCopies a file located in a remote server to a local destination via scp (secure copy) command in Linux. Before using this step, you must first open a ssh connection with
openSSHConnection(String, String, String, String, String, String)
Example:
Given I open a ssh connection to '10.200.56.59' with user 'myuser' and password 'temporal' Then I inbound copy '/tmp/exampleJSON.conf' through a ssh connection to 'fileFromSsh.conf'
- Parameters:
remotePath
- path where file is going to be copylocalPath
- path where file is located- Throws:
Exception
- exception- See Also:
openSSHConnection(String, String, String, String, String, String)
,copyToRemoteFile(String, String)
-
copyToRemoteFile
@Given("^I outbound copy \'(.+?)\' through a ssh connection to \'(.+?)\'$") public void copyToRemoteFile(String localPath, String remotePath) throws Exception
Copies file/s from local system to remote systemCopies a file located in the local machine to a remote server via scp (secure copy) command in Linux. Before using this step, you must first open a ssh connection with
openSSHConnection(String, String, String, String, String, String)
Example:
Given I open a ssh connection to '10.200.56.59' with user 'myuser' and password 'temporal' Then I outbound copy 'exampleJSON.conf' through a ssh connection to '/tmp/exampleJSON.conf'
- Parameters:
localPath
- path where file is locatedremotePath
- path where file is going to be copy- Throws:
Exception
- exception- See Also:
openSSHConnection(String, String, String, String, String, String)
,copyFromRemoteFile(String, String)
-
executeLocalCommand
@Given("^I run \'(.+?)\' locally( with exit status \'(.+?)\')?( and save the value in environment variable \'(.+?)\')?$") public void executeLocalCommand(String command, Integer exitStatus, String envVar) throws Exception
Executes the command specified in the local systemExecutes the given command in the local system. Unless specified, this step expects the returned exit status of the executed command to be zero (more info here), however, you can also specify any exit code you like. This step also provides the possibility of saving the returned response into a variable and use it in the following steps
Example: Run a command locally
Given I run 'ls /tmp | wc -l' locally
Example: Run a command locally and expect exit status to be 127Then I run 'lss /tmp' in the ssh connection with exit status '127'
Example: Run a command and save its value in variable for further inspectionGiven I run 'ls /tmp | wc -l' locally and save the value in environment variable 'WORDCOUNT' Then '${WORDCOUNT}' is '14'
- Parameters:
command
- command to be run locallyexitStatus
- command exit statusenvVar
- environment variable name- Throws:
Exception
- exception- See Also:
UtilsGSpec.checkValue(String, String, String)
, Exit status
-
executeCommand
@Given("^I run \'(.+?)\' in the ssh connection( with exit status \'(.+?)\')?( and save the value in environment variable \'(.+?)\')?$") public void executeCommand(String command, String foo, Integer exitStatus, String bar, String envVar) throws Exception
Executes the command specified in remote systemExecutes the given command in the ssh connection. For this step to work, you must first connect to a remote server by ssh using the step
openSSHConnection(String, String, String, String, String, String)
. Unless specified, this step expects the returned exit status of the executed command to be zero. (more info here), however, you can also specify any exit code you like. This step also provides the possibility of saving the returned response into a variable and use it in the following stepsExample: Run a command locally
Given I open a ssh connection to '10.200.56.59' with user 'myuser' and password 'temporal' Then I run 'ls /tmp' in the ssh connection
Example: Run a command locally and expect exit status to be 127Given I open a ssh connection to '10.200.56.59' with user 'myuser' and password 'temporal' When I run 'lss /tmp' in the ssh connection with exit status '127'
Example: Run a command and save its value in variable for further inspectionGiven I open a ssh connection to '10.200.56.59' with user 'myuser' and password 'temporal' When I run 'ls -la /tmp' in the ssh connection and save the value in environment variable 'DEFEXSTAT' Then '${DEFEXSTAT}' contains 'total'
- Parameters:
command
- command to be run locallyfoo
- regex needed to match methodexitStatus
- command exit statusbar
- regex needed to match methodenvVar
- environment variable name- Throws:
Exception
- exception- See Also:
openSSHConnection(String, String, String, String, String, String)
,UtilsGSpec.checkValue(String, String, String)
, Exit status
-
assertCommandExistsOnTimeOut
@Then("^in less than \'(\\d+?)\' seconds, checking each \'(\\d+?)\' seconds, the command output \'(.+?)\' contains \'(.+?)\'$") public void assertCommandExistsOnTimeOut(Integer timeout, Integer wait, String command, String search) throws Exception
Validates command output with timeoutThis step executes a shell command periodically on a remote ssh connection and evaluates the command output for a given string. The control flow of the feature continue if the string is found before the maximun amount of time (in seconds), otherwise, the feature fails. An ssh connection needs to be established for this step to work
For example, to check every 2 seconds, for a maximum of 20 seconds if the file "text.txt" exists in the remote server
Given I open a ssh connection to 'my-remote-server-address' with user 'root' and password '1234' Then in less than '20' seconds, checking each '2' seconds, the command output 'ls' contains 'test.txt'
- Parameters:
timeout
- the max time to waitwait
- checking intervalcommand
- the command to executesearch
- text to search on the command output- Throws:
Exception
- exception- See Also:
openSSHConnection(String, String, String, String, String, String)
-
findShellOutput
@Then("^the command output contains \'(.+?)\'$") public void findShellOutput(String search) throws Exception
Check the existence of a text at a command outputVerifies that the result of a previously executed command contains the given text.
Example: Check if the result of the command contains the string '2'
Given I run 'wc -l testOutput.txt' locally Then the command output contains '2'
- Parameters:
search
- Text to search- Throws:
Exception
- Exception- See Also:
executeLocalCommand(String, Integer, String)
,notFindShellOutput(String)
-
notFindShellOutput
@Then("^the command output does not contain \'(.+?)\'$") public void notFindShellOutput(String search) throws Exception
Check the non existence of a text at a command outputVerifies that the result of a previously executed command does not contains the given text.
Example: Check if the result of the command does not contains the string '2'
Given I run 'wc -l testOutput.txt' locally Then the command output does not contains '2'
- Parameters:
search
- Text to search- Throws:
Exception
- Exception- See Also:
executeLocalCommand(String, Integer, String)
,findShellOutput(String)
-
checkShellExitStatus
@Deprecated @Then("^the command exit status is \'(.+?)\'$") public void checkShellExitStatus(int expectedExitStatus) throws Exception
Deprecated.Success exit status is directly checked in the "execute remote command" method, so this is not needed anymore.Check the exitStatus of previous command execution matches the expected one- Parameters:
expectedExitStatus
- Expected result of the command execution- Throws:
Exception
- Exception
-
-