# Testing

Testing an image creation is composed by different stages; in order to create an image, it is necessary to spin up a machine and run provisioning against it; this is the first test that must be done, before testing the image creation process.

Hereby the ordered list of tests that can be executed

1. Local test, using VirtualBox and Kitchen
2. (if creating an AMI) EC2 test using kitchen-ec2 driver
3. Packer Common run

This page will guide you through the installation of software and configuration to allow running image creation tests.

## Prerequisites

> NOTE! On Windows 7 64bit - when running kitchen commands - make sure that you can connect to the boxes:
> ```
> ssh -i ~/.vagrant.d/insecure_private_key vagrant@127.0.0.1 -p 2222
> ```
> Password is `vagrant`; if the connection hangs, investigate how to make it working (check Windows firewall, wireshark, ...)

On Windows, you need to run the following commands:
```
gem install net-ssh
eval `ssh-agent -s`
ssh-add ~/.ssh/packer-build.pem
```

- 1. Install [git commandline](git-scm.com) and make sure your `~/.ssh/id_rsa` SSH key is in place; for Windows workstation, install [OpenSSH](http://www.mls-software.com/opensshd.html) and [create an ssh key](http://guides.beanstalkapp.com/version-control/git-on-windows.html), then [register it on your github account](https://github.com/settings/ssh)
- 2. Install [VirtualBox](https://www.virtualbox.org/)
- 3. Install [Vagrant 1.7+](https://www.vagrantup.com/downloads.html)
- 4. Install [ChefDK](https://downloads.chef.io/chef-dk/); edit `~/.bashrc` or `~/.zshrc` or `/etc/profile` (whatever is loaded when a shell is open) and add: `export PATH=$HOME/.chefdk/gem/ruby/2.1.0/bin:/opt/chefdk/bin:/opt/chefdk/embedded/bin:$PATH` (not needed on Windows); close and reopen the shell to apply changes
- 5. Install [Packer](https://www.packer.io/); you just need to download and unpack an archive to a local folder and add it to the PATH var, as in step #4:  `export PATH=/opt/packer:$PATH`
ie. --> 'export PATH=$HOME/.chefdk/gem/ruby/2.1.0/bin:/opt/chefdk/bin:/opt/chefdk/embedded/bin:/opt/packer:$PATH'
- 6. Export the following environment variables (replace `export` with a proper command, if running on Windows)
```
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export KEYPAIR_NAME=
export AWS_KEY_PATH=
```
- 7. Make sure that the following commands point to chefdk/packer/vagrant installation:
```
which ruby
which gem
which kitchen
which packer
which vagrant
```

Now you can checkout and cd the Chef cookbook project:
```
git clone git@github.com:Alfresco/chalf-img-basic.git
cd chalf-img-basic
```

## Local test

Simply run `kitchen converge` from the root of your chalf project; make sure you follow the project-specific documentation, some provisioning processes need access to VPN servers

## EC2 test

1. Install kitchen-ec2 driver: `gem install kitchen-ec2` ;
2. Download packer-build.pem (ask maoo or fab for it) in your ~/.ssh (keep the same filename!) and `chmod 400` (without it, kitchen won't be able to connect to the server)
3. Make sure that [`packer_ssh_in` Security Group](https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#SecurityGroups:search=packer_ssh_in;sort=groupDescription) grants inbound SSH connections from your IP (by default London and Maidenhead IPs are allowed) ;
4. Export your AWS id/secret/path settings (you can use those of [bamboo user in QA account](https://github.com/Alfresco/pcb-data_bags/blob/master/data_bags/aws/bamboo_qa.json)) : `export AWS_ACCESS_KEY_ID=AK3456... ; export AWS_SECRET_ACCESS_KEY=DFERT455... ; export KEYPAIR_NAME=packer-build; export AWS_KEY_PATH=/Users/mau/.ssh/packer-build.pem`. These can be added to ~/.bash_profile , ~/.bashrc , /etc/profile or /etc/bashrc as single lines. For `packer-build.pem` file, ask Devops team
5. `rm Berksfile.lock` - This will force Berkshelf run (`berks vendor`, invoked by Kitchen and Packer Common) to fetch latest cookbook versions when running Kitchen ;
6. `KITCHEN_AWS=true kitchen converge` - Kitchen will create an m3.medium EC2 instance and run provisioning processes; you can re-run the same command all times you want; everytime kitchen will upload Chef cookbook contents and run Chef converge

## Packer Common run

For instructions on how to create images, please follow [IMAGES.md](IMAGES.md)

## Other Kitchen commands

You can now run
- `kitchen converge` - Creates a VM and provision chef-cherokee inside; before running, make sure to delete Berksfile.lock in order to apply changes to imported Chef cookbooks ;
- `kitchen verify` - Runs integration testing - defined in `test/integration/default/serverspec` - against the VM
- `kitchen login` - Log into the provisioned VM ("sudo su -" gives root privileges) ;
- `kitchen destroy` - Destroy the provisioned VM (doesn't work on Windows, unless you can execute `ssh` from commandline; as alternative, use Virtualbox GUI to connect to the VM) ;
- `kitchen test` - Same as `kitchen destroy ; kitchen converge ; kitchen verify` ;
- `berks vendor` - Creates a local `berks-cookbooks` folder containing all Chef cookbooks used by Kitchen; this will help debugging any issue with Berkshelf ;
- `rspec` - Runs unit testing defined in `spec` folder (not working on Windows host machines; use `kitchen test` only) ;
- `invoke-packer.sh` - Generates a Packer template and runs it.

## Unit testing (deprecated)
```
bundle update
bundle exec rake
```

For a end-to-end testing, run `kitchen test`

## Troubleshooting
> As stated by the message, the file `.kitchen.yml` could not be parsed; most of the times, the error is included in the Chef `attributes` part, maybe a missing comma or wrong bracketing.

```
>>>>>> ------Exception-------
>>>>>> Class: Kitchen::UserError
>>>>>> Message: Error parsing C:/Users/<user>/Documents/GitHub/chef-cherokee/.kitchen.yml as YAML.
Please run `kitchen diagnose --no-instances --loader' to help debug your issue.
>>>>>> ----------------------
>>>>>> Please see .kitchen/logs/kitchen.log for more details
>>>>>> Also try running `kitchen diagnose --all` for configuration
```

> Sometimes Virtualbox fails and the VM aborts unexpectedly; in this case, run `kitchen create` and the VM will be booted again; you cannot reboot with Virtualbox, since Kithen port and folder mapping configurations won't be preserved.

> Randomly the following error is displayed. So far there is no clear solution an usually a new (or multiple) "kitchen converge" can bypass the issue:

```
Failed to mount folders in Linux guest. This is usually because
       the "vboxsf" file system is not available. Please verify that
       the guest additions are properly installed in the guest and
       can work properly. The command attempted was:

       mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant
       mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant

       The error output from the last command was:

       mount: unknown filesystem type 'vboxsf'

------Exception-------
Class: Kitchen::ActionFailed
Message: Failed to complete #create action: [Expected process to exit with [0], but received '1'
---- Begin output of vagrant up --no-provision --provider=virtualbox ----
STDOUT: Bringing machine 'default' up with 'virtualbox' provider...
```
More info on [https://github.com/mitchellh/vagrant/issues/3341](https://github.com/mitchellh/vagrant/issues/3341)
