CloudTest – Anything is po-ssh-ible with Java

The need for a POC

I came across a situation where I needed to time an SSH call to a UNIX server and wanted to see if CloudTest was a viable option to perform this testing. SOASTA CloudTest gives you the capability to run Java code within a Clip. There is a short tutorial on how to use Java code in CloudTest available on the CloudLink forum. I read through the document and got to work on my proof of concept project!

Sending an SSH message with Java

The first step was to figure out how to send an SSH method through Java.  To be honest, it had been some time since I used Java, so off we go to our favorite search engine!

oooooh

A few quick clicks later, I found example code for a Java implementation of ssh2 called JSch (Java Secure Channel).  The provided code is exactly what I needed to connect to an sshd server.  The next step was downloading a Java IDE.  I chose Eclipse Standard 4.4 which you can find on the Eclipse download site.

Prepping your Eclipse IDE for use with CloudTest

One key requirement that wasn’t mentioned specifically in the CloudTest documentation was the need to use Java 6 to build any code planned for use in the tool.  You most likely have a newer version of Java (7 or 8) so you’ll need to download and install Java JRE 1.6 on your computer.

Create a Java Package and Export a Runnable JAR

Once you have the appropriate JRE installed, we need to create a Java Package, import the example code for JSch, and export everything to a Runnable JAR file for CloudTest to use.  The steps below will walk you through the process:

  1. Open Eclipse JDE and Create a New Java Project
    CT_SSH_01_Capture
  2. Create a new Java Package
    CT_SSH_02_Capture
  3. Create a new Java Class
    CT_SSH_03_Capture
  4. Navigate to the Project Properties and add an External Jar to the Java Build Path.  Navigate to where you downloaded the jsch-0.1.47.jar file and add it to your project
    CT_SSH_04.1_Capture
  5. Add the example Java code into your new Class.  In my example, I added the import statements above the Class definition and make the SSH connection code part of the Class Constructor.  The main function was left blank for now. Note: Make sure you have a UNIX box available to test against and that you are able to SSH to it from your machine and from your CloudTest machine.  Enter your log-on information in the user, host, and password variables.  I used a Linux server on AWS for testing purposes.
    CT_SSH_04_Capture
  6. Test your code in Eclipse before uploading to CloudTest.  You should see the SSH connection and ls –l command in the Console tab before moving on to the next step.
  7. Navigate to File -> Export in Eclipse.  Select Runnable JAR File and make sure you select to “Extract required libraries into the generated JAR.”  This makes sure the JSch jar is available within CloudTest.  Note the location of your saved JAR file, we will be uploading this to CloudTest next.
    CT_SSH_05_Capture

Now we are ready to import everything into CloudTest!

Import Java code to CloudTest and implement in a Clip

  1. In your CloudTest instance, create a new Custom Module and import your JAR file.
    CT_SSH_06_Capture
  2. Next, create a new Script and add the newly created Custom Module to the Script.  Add the following code to your script to allow calls to your Java functions.  It is extremely important to put Packages. in front of your package name if you did not follow standards when naming the package (we did not).
    CT_SSH_07_Capture
    Code below

    //import the java code from the custom module 
    importPackage(Packages.ssh_for_cloudtest); 
    // create the java object and call it 
    try{ 
    var exampleVar = new SSH_for_CloudTest(); 
    } 
    catch(err)
    { 
    $context.result.postMessage($context.result.LEVEL_ERROR, "An error happened.", err); 
    }
  3. Save the Script and create a new Test Clip which calls the Script
    CT_SSH_07.1_Capture
  4. Play your Clip in a Composition and make sure it runs successfully
    CT_SSH_08_Capture

Notes: I recommend logging onto your UNIX server and monitoring the SSH logs to verify your Script is working as expected.

CT_SSH_09_Capture

Conclusion

CloudTest scripts use JavaScript in their Scripts, but also supports the ability to create and use custom Java code.  This makes CloudTest extremely powerful and flexible!

The following key information contributed to helping me make a working solution

  • Compile code in Java 6
  • Export a Runnable JAR file
  • Use Package. In front of import statement
  • Make sure UNIX box is available on the network and will allow SSH from CloudTest IP address

Go forth, code in Java, and expand your CloudTest capabilities!

Success_Strategies

Facebooktwittergoogle_pluspinterestlinkedinmail

Leave a Reply