VMware Cloud Community
StefanSchnell
Enthusiast
Enthusiast

Unit Test Library for Actions

Testing software is important, because programming errors can be very expensive and dangerous. A unit test is a kind of software testing. It checks the smallest piece of code that can be logically isolated. In Aria Automation it is an action, and it contains JavaScript functions, methods or attributes. Unit tests helps us to ensure that the software meets the requirements and the possibility of errors and bugs occurring are minimized.

When developing in the Aria Automation web GUI, the possibility of using a test framework is not really easy. The common JavaScript test frameworks are very fixated on browser environments and usually require Node.js for their execution. This makes an integration into this kind of development scenario difficult, because the specific requirements and conditions can hardly be considered. Therefore it can be seen as a valid approach to build an own unit test library, which fits seamlessly into this scenario.

Therefore I have programmed a unit test library.
This Aria Automation assert library, called Aromatical, offers exactly this possibility.

aromatical001.jpg

It provides 16 test methods to check the datatype of a value, such as isNumber or isString. 17 assert methods to check the content of a value versus an expected value, such as deepEqual or notMatch. 9 structuring and description methods to make the tests readable and clear, such as describe or testTodo. And last but not least classification properties for the test to specify the duration and risk level.

E.g. with the command var assert = System.getModule("yourModule").assert(); these methods can be called easily. Here is a small example that shows the use of the structuring and documentation methods describe and describeEach, as well as the assert method strictEqual and the test method isArray.

aromatical002.jpg

This assert library has been successfully tested with VMware Aria Automation versions 8.5.1 and 8.12.2. Furthermore, it has been successfully verified standalone with different operating systems, versions of Java and JavaScript Rhino engines.

Conclusion

This approach shows us that it is possible to implement an own unit test library inside Aria Automation. This integration scenario allows us to use it seamlessly in the context of the JavaScript development in the Aria Automation web GUI, without having a connection to the outside. A base for those of us who e.g. cannot use a CI/CD pipeline.

You can find the source code of the class at blog.stschnell.de or at my GitHub account.


More interesting information at blog.stschnell.de

Labels (1)
0 Kudos
2 Replies
mayank_goyal
Enthusiast
Enthusiast

Looks interesting. Can you please share examples of how to get started with it using regular orchestrator scripts, let's say something with vSphere Virtual Machines?



-
For more interesting content on Aria Automation, check my blog:
https://cloudblogger.co.in
Tags (1)
0 Kudos
StefanSchnell
Enthusiast
Enthusiast

Hello @mayank_goyal,

here an example of a possible unit test of the action getAllVMs of the module com.vmware.library.vc.vm.

 

// Begin ---------------------------------------------------------------

var assert = System.getModule("de.stschnell").assert();

assert.setRuntimeCategory(assert.getRuntimeCategoryShort());
System.log("runtimeCategory: " + assert.getRuntimeCategory());

assert.setRiskLevel(assert.getRiskLevelHarmless());
System.log("riskLevel: " + assert.getRiskLevel());

assert.describe("Test of getAllVMs action", function() {

  var allVMs = System.getModule("com.vmware.library.vc.vm").getAllVMs();

  assert.test("Is it an arry?", function() {
    assert.strictEqual(assert.isArray(allVMs), true);
  });

  assert.test("Contains this array elements?", function() {
    assert.greaterThan(allVMs.length, 0);
  });

  var hostName = allVMs[0].hostName;

  assert.test("Is the first hostname not null?", function() {
    assert.strictEqual(assert.isUndefinedOrNull(hostName), false);
  });

  assert.test("Is the first hostname a string?", function() {
    assert.strictEqual(assert.isString(hostName), true);
  });

  assert.test("Contains the first hostname characters and numbers?", function() {
    assert.match(hostName, /[A-Z0-9]/);
  });

  assert.test("Contains the first hostname numbers?", function() {
    assert.match(hostName, /\d/);
  });

  assert.test("Contains the first hostanme special characters?", function() {
    assert.match(hostName, /\W/);
  });

  assert.test("Is the first hostanme 8 characters long or shorter?", function() {
    assert.strictEqual(hostName.length, 8);
    assert.lessThanOrEqual(hostName.length, 8);
  });

});

// End -----------------------------------------------------------------

 

This could deliver this result:

/**
 * runtimeCategory: Duration Short
 * riskLevel: Risk level harmless
 * > Test of getAllVMs action
 * Is it an arry?
 * Pass: strictEqual(true === true)
 * Contains this array elements?
 * Pass: greaterThan(42 > 0)
 * Is the first hostname not null?
 * Pass: strictEqual(false === false)
 * Is the first hostname a string?
 * Pass: strictEqual(true === true)
 * Contains the first hostname characters and numbers?
 * Pass: The input 'abcd4711' match the regular expression /[A-Z0-9]/
 * Contains the first hostname numbers?
 * Pass: The input 'abcd4711' match the regular expression /\d/
 * Contains the first hostanme special characters?
 * Fail: The input 'abcd4711' did not match the regular expression /\W/
 * Is the first hostanme 8 characters long or shorter?
 * Pass: strictEqual(8 === 8)
 * Pass: lessThanOrEqual(8 <= 8)
 * < Test of getAllVMs action
 */

Best regards
Stefan


More interesting information at blog.stschnell.de

0 Kudos