## Vagrant for Fun and Testing

While Vagrant is marketed as a development environment creator, it can also be great for quick test environments

In my first experimentations for using Vagrant to create a local, standardized development environment, an idea with a larger impact began to form in my mind. Dynamic, testing environment creation.

### Why?

One of the larger problems many engineers can face is having to install and configure older versions of specific software releases they develop in order to reproduct issues, diagnose causes, and devise solutions. Sometimes other teams or departments may also need this functionality to more easily help diagnose and report issues.

The following is how I went about setting up a testing toolset using Vagrant and some bash scripting implementations.

### The Test Bed Setup

Using some dynamic configurations, an engineer can start up a vagrant virtual machine with a specific installation of the proper sub-system versions and software version that they may need.

To begin to create this tool set, I had to devise a set of ways to determine which version of the software is intended to be installed, which pre-requisite software and versions are required, and then any post installation configurations that may be necessary to get the environment up and running quickly for testing to begin.

## Step 1. Determining the Software Version

As most software follows standard Semantic Versioning schemes, a property file or the installer file name is likely to have this inforamation available.

In my case, the version of the software is included in the installer file name, so it was as simple as pulling this information out of the file name using any relevant method. I chose to call an awk command to get this out of the file name:

ls setupLinux* | awk -F '-' '{print $2}' | awk -F '.' '{print$1"."$2}'  ## Step 2. Install Pre-requisites Now that the version of the software is known, gather necessary installation files or other dependencies and begin to install these. For my uses, this required some specific yum package installations and other more trivial or easily scripted installations. However, installing the Oracle Java version and pulling the latest version from their website required some creativity. Below is my implementation to pull the correct RPM file for Linux for the latest version of Java 8 from the Oracle web site using a variety of linux commands: echo -e "VAGRANT_INSTALLER: Installing Java 8..." javaUrl=$(curl "http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html" | grep "linux-x64.rpm" | grep "jdk" | awk -F ',' '{print $3}' | awk -F '\":\"' '{print$2}' | sed 's/.$//' | awk -F ' ' '{print$1}' | head -1)
curl -v -j -k -L -O -H "Cookie: oraclelicense=accept-securebackup-cookie" "$javaUrl" sudo rpm -ivh jdk-* --force if [$? != "0" ]; then
echo -e \$uncleanExitMessage
exit 1
fi
echo "export JAVA_HOME=/usr/java/latest/" >> ~/.bashrc
echo -e "VAGRANT_INSTALLER: Java installed...\n"


Some pre-requisites are not based on the software version but instead may be preferential or based on a particular configuration option. I have found that the marker file paradigm, similar to how many configurations in Red Hat JBoss are made, to be an easy method to implement to have a bash script pick up if a configuration or optional pre-requisite is required to be installed based on if a particular marker file exists in the main Vagrant directory. For example:

/vagrant/
|
--> setupLinux_1.0.0.rpm
--> mysql.dodeploy
--> oracle.dodeploy


In the above example, I know that I need to install the default pre-requisites for the 1.0.0 release of my software product as well as that it will be using both a MySQL database and an Oracle database and to install and configure one instance of each database platform of the supported verstion for the 1.0.0 release.

This is done in bash via checking if a file exists or not:

#Install MySQL
sudo chmod u+x install_mysql.sh
if [ -f /vagrant/mysql.dodeploy ];then
./install_mysql.sh
fi

#Install Oracle 12c
sudo chmod u+x install_oracle.sh
if [ -f /vagrant/oracle.dodeploy ];then
./install_oracle.sh
fi


## Step 3. Install the Software

After the default and any optional pre-requisites are installed and configured, install the software version you require. Then after that, make any configuration changes required to get this working on the Vagrant VM.

Since this is a controlled, pre-configured environment, the configuration values are most likely to be static or rarely need to be changed. As such, this makes it easier to create pre-configured configuration files or edit files using Linux commands to insert any configurations.

## Step 4. Post-Installation Configurations

If any of your ancillary systems require any configuration based on your software installation, your scripting can now make those configurations.

## Step 5. Launch or Start the Software

At the end, your Vagrant VM should be started and completely configured with either the software and all services up and running for the tester to interact with or possibly ready for the tester to complete a simple initiation script or other method to start up the application for testing.