lux-colorizer, Lux Syntax Highlight VS Code Extension

Lux is a universal system-level test tool written in Erlang/OTP by Håkan
Mattsson and other contributors. Based on
expect-like pattern, Lux offers
simplified syntax, regular expressions support, switching between multiple
concurrent sessions, extensive logging, post mortem analysis, debugging, and
more. Testing with Lux can be a part of more valuable processes, like
developing new services for Cisco NSO, CI/CD pipeline testing, troubleshooting,
etc. It is well known for test automation for developing Cisco Network Service
Orchestrator (NSO) with 4500 test cases per run.

An example using Monakai color theme

Some explanations "Why Lux" can be found here.

For more on Lux features, please check
the official documentation.

How to install the extension

You may install the extension via VS Code Quick Open (Ctrl/Cmd+P), paste the
following command, press enter, and click install.

ext install andreygrechin.lux-colorizer

Also, you may install it via VS Code
marketplace by searching the name
andreygrechin.lux-colorizer.

How to use the extension

After the installation, the extension will be enabled automatically.

To use snippets, start typing a meta statement, like [loop], and VS Code will
offer possible options. Use TAB to autocomplete a statement and jumping between
input fields.

Useful References

  1. Lux
    1. Lux repo
    2. Lux documentation
    3. How to install Lux (Note: Lux doesn't support Windows, use WSL)
  2. TextMate
    1. TextMate Language Grammars
    2. Oniguruma regexp syntax
    3. Oniguruma flavor regexp online editor
    4. RegEx101 regexp tester
  3. More on understanding TextMate
    1. https://flight-manual.atom.io/hacking-atom/sections/creating-a-legacy-textmate-grammar/
    2. https://www.apeth.com/nonblog/stories/textmatebundle.html
    3. https://gist.github.com/Aerijo/b8c82d647db783187804e86fa0a604a1
    4. https://gist.github.com/DamnedScholar/622926bcd222eb1ddc483d12103fd315
  4. Visual Studio Code Extensions
    1. Overview
    2. Syntax Highlight Guide
    3. Semantic Highlight Guide
    4. Snippets

Use Case

Using Lux for fast transition from manual to automated testing

Lux scripts by nature are a simple way to move from manual testing to consistent, repeatable, and automated testing while keeping time to write tests to a minimum. A little or no experience is required to start. However, understanding some basic regular expression patterns will benefit you.

Firstly, you need to collect some raw input and output strings to transform them into a Lux script. For example, connect to a host and do a manual test as you do before.

ssh rt | tee log.txt

Next, you can easily create a Lux script by editing log.txt (not shown for brevity) by marking send and receive strings with prefixes and cutting down insignificant parts of the output. ! means send a line (command), ? is expected output regexp match, and ??? is expected output exact match. Pretty simple.

An example

An example above does some basic tricks:

  1. Decorate stdout and logs outputs (lines 2, 6, 21)
  2. Start a new shell shell1 (line 4)
  3. Connect to the host rt (line 7), and wait for a regular expression to match (regexp '.*' means any character, followed by a hash as a privileged level prompt ending, line 8)
  4. Turn off pagination and execute "show version" command (lines 9, 11)
  5. Check that outputs contain 16.12.04 IOS version, UNIVERSALK9_IAS-M image reference, and on followed lines configuration register is 0x2102 (lines 12-16)
  6. Ping cisco.com, expecting 100% success rate (lines 18, 19)

To run a test case:

lux example1.lux

Example of success

Lux shows a progress report during tests run on stdout. In case of any unexpected results, the test will fail, showing a brief explanation of what was sent and what was received from the device under test. More detailed log files will be available for further troubleshooting.

Example of fail

For more on Lux features, like regular expressions support, switching between multiple concurrent sessions, extensive logging, post mortem analysis, debugging, and more, check the official documentation.

lux-colorizer, Lux Syntax Highlight VS Code Extension

Each Lux script may have hundreds+ of send/expect lines and comments, which are hard to read and work with. The extension significantly increases readability and visually distinguishes input and output streams, keywords, comments, meta statements, special characters and variables. This simplification will help test writers to produce test in a more structured format and see syntax errors early before run, increasing the speed of writing, reviewing, and maintaining test scripts.

Cisco DevNet organization provides a great selection of always-on and reservation-based sandboxes to test different solutions, products, and APIs, including Cisco NSO.

Unfortunately, existing NSO sandboxes don't have lux package installed, to the best of my knowledge. Moreover, users cannot add lux package themselves due to security policies. To use lux user have to build own environment and install lux using standard installation guidelines.

Useful References

  1. Lux overview presentation from authors
  2. Network Automation Delivery Model Testing
  3. Getting Free Trial NSO installer

Using Lux for fast transition from manual to automated testing

Lux scripts by nature are a simple way to move from manual testing to consistent, repeatable, and automated testing while keeping time to write tests to a minimum. A little or no experience is required to start. However, understanding some basic regular expression patterns will benefit you.

Firstly, you need to collect some raw input and output strings to transform them into a Lux script. For example, connect to a host and do a manual test as you do before.

ssh rt | tee log.txt

Next, you can easily create a Lux script by editing log.txt (not shown for brevity) by marking send and receive strings with prefixes and cutting down insignificant parts of the output. ! means send a line (command), ? is expected output regexp match, and ??? is expected output exact match. Pretty simple.

An example

An example above does some basic tricks:

  1. Decorate stdout and logs outputs (lines 2, 6, 21)
  2. Start a new shell shell1 (line 4)
  3. Connect to the host rt (line 7), and wait for a regular expression to match (regexp '.*' means any character, followed by a hash as a privileged level prompt ending, line 8)
  4. Turn off pagination and execute "show version" command (lines 9, 11)
  5. Check that outputs contain 16.12.04 IOS version, UNIVERSALK9_IAS-M image reference, and on followed lines configuration register is 0x2102 (lines 12-16)
  6. Ping cisco.com, expecting 100% success rate (lines 18, 19)

To run a test case:

lux example1.lux

Example of success

Lux shows a progress report during tests run on stdout. In case of any unexpected results, the test will fail, showing a brief explanation of what was sent and what was received from the device under test. More detailed log files will be available for further troubleshooting.

Example of fail

For more on Lux features, like regular expressions support, switching between multiple concurrent sessions, extensive logging, post mortem analysis, debugging, and more, check the official documentation.

lux-colorizer, Lux Syntax Highlight VS Code Extension

Each Lux script may have hundreds+ of send/expect lines and comments, which are hard to read and work with. The extension significantly increases readability and visually distinguishes input and output streams, keywords, comments, meta statements, special characters and variables. This simplification will help test writers to produce test in a more structured format and see syntax errors early before run, increasing the speed of writing, reviewing, and maintaining test scripts.

Cisco DevNet organization provides a great selection of always-on and reservation-based sandboxes to test different solutions, products, and APIs, including Cisco NSO.

Unfortunately, existing NSO sandboxes don't have lux package installed, to the best of my knowledge. Moreover, users cannot add lux package themselves due to security policies. To use lux user have to build own environment and install lux using standard installation guidelines.

Useful References

  1. Lux overview presentation from authors
  2. Network Automation Delivery Model Testing
  3. Getting Free Trial NSO installer
View code on GitHub

Code Exchange Community

Get help, share code, and collaborate with other developers in the Code Exchange community.View Community
Disclaimer:
Cisco provides Code Exchange for convenience and informational purposes only, with no support of any kind. This page contains information and links from third-party websites that are governed by their own separate terms. Reference to a project or contributor on this page does not imply any affiliation with or endorsement by Cisco.