Skip to content

Commit

Permalink
update the Vagrant testing
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomc committed Jun 6, 2018
1 parent ce12bec commit 7f1b8b0
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 63 deletions.
19 changes: 0 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,6 @@ To be stored in an Ansible Vault. It's very high-sensitivity Information.
## Testing
See README file in the `tests` directory

### Requirements
create the file tests/test_variables.yml that must include your private jumpcloud_x_connect_key as follow:
```
---
enc_jumpcloud_x_connect_key: "xxxxxxxxxxxxxyyyyyyyyyyyyyyzzzzzzzzzz"
...
```

Make sure that on you JumpCloud account you have the following System Groups:
```
ansible_test_1
ansible_test_2
```

Run
```
cd tests/
ansible-playbook main.yml
```

#### [`jumpcloud_x_connect_key`][jumpcloud-x-connect-key]
Default: none
Expand Down
2 changes: 1 addition & 1 deletion tasks/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
- name: saving the position of the `/etc/issue` backup
set_fact:
issue_backup_file: "{{ line_infile_result.backup}}"
when: line_infile_result is defined
when: ansible_distribution == "Ubuntu" and "12" in ansible_distribution_version and line_infile_result is defined

- name: Install JumpCloud Agent
shell: "{{ jumpcloud_kickstart_script }}"
Expand Down
42 changes: 39 additions & 3 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,49 @@ Use Vagrant and some VirtualBox boxes that I build to follow the latest releases

The project is extremely simple, and simply requires [Vagrant](https://www.vagrantup.com/), [VirtualBox](https://www.virtualbox.org/), and [Ansible](http://docs.ansible.com/intro_installation.html) to be installed on your host machine.

## Requirements

set local Environment Variables that will be read by Ansible
```
JUMPCLOUD_X_CONNECT_KE=yyyyyyyyyyyyyyzzzzzzzzzzxxxxxxxxxxxxx
JUMPCLOUD_API_KEY=xxxxxxxxxxxxxyyyyyyyyyyyyyyzzzzzzzzzz
```

Make sure that on you JumpCloud account you have the following System Groups:
```
ansible_test_1
ansible_test_2
```

## Testing a Role
The testing process works as follows:
There are an Ansible Playbook and Inventory configured to spin a bunch of VirtualBox's virtual machines via Vagrant.
Vagrant takes care of all the vms setup and and creation of the required rsa-keys to allow interaction with them.
Ansible will install JumpCloud's agent in the VMs.

At the end of the provisioning Ansible will run a few test-tasks that will verify if the JumpCloud agent has been istalled and if the hosts have been regitered again JC portal.

This is the command to start the testing process the use of the `-l <inventory_group_name>|<inventory_hostname>` parameter is optional and is useful to run the test on a specific vm instead of all of them.

```
cd ./tests
ansible-playbook -i inventory [ -l centos,ubuntu | centos6,centos7,ubuntu1202,ubuntu1402,ubuntu1602 ] playbook.yml
```

This command is to to run a playbook which will instruct Vagrant to destroy the testing vms.
```
cd ./tests
ansible-playbook -i inventory [ -l centos,ubuntu | centos6,centos7,ubuntu1202,ubuntu1402,ubuntu1602 ] playbook_delete_vms.yml
To test a role, the role must be installed on your host machine (you can install galaxy roles via `$ ansible-galaxy install [rolename]`, but this project is more focused on testing roles you'd be working on locally). Just add the role to `playbook.yml` and run `vagrant up`.
```

It should take a few minutes to download each of the base boxes the first time, but after that, it takes about a minute to boot each VM, then run the playbook with your role(s).
### Travis CI Testing
for the testing to work set up in the Travis CI project's settings the following `Environment Variables` that will be read by Anbsible

After testing a role, you can destroy the four VMs with `vagrant destroy -f`. You can also just build one particular VM with `vagrant up ubuntu1204` (as an example), or re-run the ansible playbook with `vagrant provision ubuntu1204`.
```
JUMPCLOUD_X_CONNECT_KE=yyyyyyyyyyyyyyzzzzzzzzzzxxxxxxxxxxxxx
JUMPCLOUD_API_KEY=xxxxxxxxxxxxxyyyyyyyyyyyyyyzzzzzzzzzz
```

## License

Expand Down
46 changes: 9 additions & 37 deletions tests/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ LOCAL_BOX_DIRECTORY = "file://~/Downloads/"
# Uncomment when explicitly testing VirtualBox.
PROVIDER_UNDER_TEST = "virtualbox"
NETWORK_PRIVATE_IP_PREFIX = "172.16.3."
PROJECT_NAME="ansible-jumpcloud"

# Uncomment when explicitly testing VMWare.
# PROVIDER_UNDER_TEST = "vmware"
# PROVIDER_UNDER_TEST + PROJECT_NAME = "vmware"
# NETWORK_PRIVATE_IP_PREFIX = "192.168.3."

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.ssh.insert_key = false
config.ssh.insert_key = true

# VirtualBox.
config.vm.provider :virtualbox do |v|
Expand All @@ -38,14 +39,10 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
if not TEST_MODE
ubuntu1604.vm.box = "geerlingguy/ubuntu1604"
else
ubuntu1604.vm.box = LOCAL_BOX_DIRECTORY + PROVIDER_UNDER_TEST + "-ubuntu1604.box"
ubuntu1604.vm.box = LOCAL_BOX_DIRECTORY + PROVIDER_UNDER_TEST + PROJECT_NAME + "-ubuntu1604.box"
end
ubuntu1604.vm.network :private_network, ip: NETWORK_PRIVATE_IP_PREFIX + "2"

# Ansible.
ubuntu1604.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end

# Ubuntu 14.04 - Trusty Tahr
Expand All @@ -54,14 +51,9 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
if not TEST_MODE
ubuntu1404.vm.box = "geerlingguy/ubuntu1404"
else
ubuntu1404.vm.box = LOCAL_BOX_DIRECTORY + PROVIDER_UNDER_TEST + "-ubuntu1404.box"
ubuntu1404.vm.box = LOCAL_BOX_DIRECTORY + PROVIDER_UNDER_TEST + PROJECT_NAME + "-ubuntu1404.box"
end
ubuntu1404.vm.network :private_network, ip: NETWORK_PRIVATE_IP_PREFIX + "3"

# Ansible.
ubuntu1404.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end

# Ubuntu 12.04 - Precise Pangolin
Expand All @@ -70,14 +62,9 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
if not TEST_MODE
ubuntu1204.vm.box = "geerlingguy/ubuntu1204"
else
ubuntu1204.vm.box = LOCAL_BOX_DIRECTORY + PROVIDER_UNDER_TEST + "-ubuntu1204.box"
ubuntu1204.vm.box = LOCAL_BOX_DIRECTORY + PROVIDER_UNDER_TEST + PROJECT_NAME + "-ubuntu1204.box"
end
ubuntu1204.vm.network :private_network, ip: NETWORK_PRIVATE_IP_PREFIX + "4"

# Ansible.
ubuntu1204.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end

# CentOS 7
Expand All @@ -86,14 +73,9 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
if not TEST_MODE
centos7.vm.box = "geerlingguy/centos7"
else
centos7.vm.box = LOCAL_BOX_DIRECTORY + PROVIDER_UNDER_TEST + "-centos7.box"
centos7.vm.box = LOCAL_BOX_DIRECTORY + PROVIDER_UNDER_TEST + PROJECT_NAME + "-centos7.box"
end
centos7.vm.network :private_network, ip: NETWORK_PRIVATE_IP_PREFIX + "5"

# Ansible.
centos7.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end

# CentOS 6
Expand All @@ -102,28 +84,18 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
if not TEST_MODE
centos6.vm.box = "geerlingguy/centos6"
else
centos6.vm.box = LOCAL_BOX_DIRECTORY + PROVIDER_UNDER_TEST + "-centos6.box"
centos6.vm.box = LOCAL_BOX_DIRECTORY + PROVIDER_UNDER_TEST + PROJECT_NAME + "-centos6.box"
end
centos6.vm.network :private_network, ip: NETWORK_PRIVATE_IP_PREFIX + "6"

# Ansible.
centos6.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end
# CentOS 6
config.vm.define "centos6-without-system-groups" do |centos6|
centos6.vm.hostname = "centos6test-without-system-groups"
if not TEST_MODE
centos6.vm.box = "geerlingguy/centos6"
else
centos6.vm.box = LOCAL_BOX_DIRECTORY + PROVIDER_UNDER_TEST + "-centos6.box"
centos6.vm.box = LOCAL_BOX_DIRECTORY + PROVIDER_UNDER_TEST + PROJECT_NAME + "-centos6.box"
end
centos6.vm.network :private_network, ip: NETWORK_PRIVATE_IP_PREFIX + "6"

# Ansible.
centos6.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook_without-system-groups.yml"
end
end
end
9 changes: 9 additions & 0 deletions tests/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[ubuntu]
ubuntu1604 ansible_ssh_host=172.16.3.2
ubuntu1404 ansible_ssh_host=172.16.3.3
ubuntu1204 ansible_ssh_host=172.16.3.4


[centos]
centos7 ansible_ssh_host=172.16.3.5
centos6 ansible_ssh_host=172.16.3.6
26 changes: 23 additions & 3 deletions tests/playbook.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
---
- hosts: all
- hosts: localhost
gather_facts: no
tasks:
# - include_tasks: tasks/run_vagrant.yml
- include_tasks: tasks/reset_ssh_agent.yml


- hosts: all:!localhost
gather_facts: no
vars:
ansible_ssh_private_key_file: '.vagrant/machines/{{ inventory_hostname}}/virtualbox/private_key'
vagrant_ssh_key: "{{ ansible_ssh_private_key_file }}"
tasks:
- include_tasks: tasks/run_vagrant.yml
- pause: seconds=3
- include_tasks: tasks/load_vagrant_ssh_key.yml

- hosts: all:!localhost
gather_facts: yes
become: yes

vars_files:
- ./test_variables.yml
vars:
enc_jumpcloud_x_connect_key: "{{ lookup('env','JUMPCLOUD_X_CONNECT_KEY') }}"
jumpcloud_api_key: "{{ lookup('env','JUMPCLOUD_API_KEY') }}"
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
ansible_ssh_user: 'vagrant'
ansible_ssh_port: 22
jumpcloud_system_groups:
- 'ansible_test_1'
- 'ansible_test_2'
Expand Down
14 changes: 14 additions & 0 deletions tests/playbook_delete_vms.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---

- hosts: all
gather_facts: false
tasks:
- name: delete test box
local_action: shell vagrant destroy -f {{ inventory_hostname }}
ignore_errors: true

- hosts: localhost
gather_facts: false
tasks:
- include_tasks: tasks/reset_ssh_agent.yml
...
16 changes: 16 additions & 0 deletions tests/tasks/load_vagrant_ssh_key.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---

- name: add 'vagrant_ssh_key' to ssh-agent
local_action: shell ssh-add {{vagrant_ssh_key}}
register: ssh_add_output
ignore_errors: True

- name: Check if `vagrant_ssh_key` has been added
set_fact:
vagrant_ssh_key_added: "{{ not 'not found' in ssh_add_output.stdout }}"

- debug:
msg: "Warning ==> vagrant_ssh_key wasn't added to ssh-agent"
when: not vagrant_ssh_key_added

...
21 changes: 21 additions & 0 deletions tests/tasks/reset_ssh_agent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---

- name: Get `ssh-agent` status
local_action: shell type ssh-agent
register: ssh_agent_status
ignore_errors: true

- name: Check if `ssh-agent` is installed
set_fact:
ssh_agent_is_installed: "{{ not 'not found' in ssh_agent_status.stdout }}"

- fail:
msg: "Warning ==> ssh-agent is not installed"
when: not ssh_agent_is_installed

- name: stop 'ssh-agent' to reset the loaded keys
local_action: shell killall -9 ssh-agent
when: ssh_agent_is_installed
ignore_errors: true

...
26 changes: 26 additions & 0 deletions tests/tasks/run_vagrant.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---

- name: Get `vagrant` status
local_action: shell type vagrant
register: vagrant_status
ignore_errors: true
run_once: true

- name: Check if `vagrant` is installed
set_fact:
vagrant_is_installed: "{{ not 'not found' in vagrant_status.stdout }}"
run_once: true

- debug:
msg: "Warning ==> vagrant is not installed"
when: not vagrant_is_installed
run_once: true

- name: run 'vagrant up'
local_action: shell vagrant up {{ inventory_hostname }}
when: vagrant_is_installed
register: vagrant_output
ignore_errors: True

- debug: var=vagrant_output.stdout_lines
...

0 comments on commit 7f1b8b0

Please sign in to comment.