Class SshGSpec


  • public class SshGSpec
    extends BaseGSpec
    Steps definitions for running bash commands and establishing SSH connections with a remote host
    Author:
    Jose Fernandez
    • 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 host

        Connects 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 host
        user - remote user
        foo - the foo
        password - (required if pemFile null)
        bar - the bar
        pemFile - (required if password null)
        Throws:
        Exception - exception
        See Also:
        executeCommand(String, String, Integer, String, String), UtilsGSpec.checkValue(String, 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 system

        Executes 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 127
         
              Then I run 'lss /tmp' in the ssh connection with exit status '127'
         
         Example: Run a command and save its value in variable for further inspection
         
              Given 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 locally
        exitStatus - command exit status
        envVar - 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 system

        Executes 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 steps

         Example: 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 127
         
              Given 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 inspection
         
              Given 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 locally
        foo - regex needed to match method
        exitStatus - command exit status
        bar - regex needed to match method
        envVar - 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 timeout

        This 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 wait
        wait - checking interval
        command - the command to execute
        search - 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 output

        Verifies 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 output

        Verifies 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