From 7f1b8b0caf9e1d137359aeca9b75cd9bdb30f1b0 Mon Sep 17 00:00:00 2001 From: Marco Massari Calderone Date: Tue, 5 Jun 2018 20:21:14 -0500 Subject: [PATCH] update the Vagrant testing --- README.md | 19 ------------ tasks/install.yml | 2 +- tests/README.md | 42 +++++++++++++++++++++++-- tests/Vagrantfile | 46 ++++++---------------------- tests/inventory | 9 ++++++ tests/playbook.yml | 26 ++++++++++++++-- tests/playbook_delete_vms.yml | 14 +++++++++ tests/tasks/load_vagrant_ssh_key.yml | 16 ++++++++++ tests/tasks/reset_ssh_agent.yml | 21 +++++++++++++ tests/tasks/run_vagrant.yml | 26 ++++++++++++++++ 10 files changed, 158 insertions(+), 63 deletions(-) create mode 100644 tests/inventory create mode 100644 tests/playbook_delete_vms.yml create mode 100644 tests/tasks/load_vagrant_ssh_key.yml create mode 100644 tests/tasks/reset_ssh_agent.yml create mode 100644 tests/tasks/run_vagrant.yml diff --git a/README.md b/README.md index fed9ca3..354b335 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/tasks/install.yml b/tasks/install.yml index 656b88a..48e06b6 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -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 }}" diff --git a/tests/README.md b/tests/README.md index 646084b..c4ad532 100644 --- a/tests/README.md +++ b/tests/README.md @@ -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 |` 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 diff --git a/tests/Vagrantfile b/tests/Vagrantfile index 239fba5..dbb0349 100644 --- a/tests/Vagrantfile +++ b/tests/Vagrantfile @@ -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| @@ -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 @@ -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 @@ -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 @@ -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 @@ -102,14 +84,9 @@ 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| @@ -117,13 +94,8 @@ 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_without-system-groups.yml" - end end end diff --git a/tests/inventory b/tests/inventory new file mode 100644 index 0000000..9853fb2 --- /dev/null +++ b/tests/inventory @@ -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 diff --git a/tests/playbook.yml b/tests/playbook.yml index 59cc0f0..3a12b79 100644 --- a/tests/playbook.yml +++ b/tests/playbook.yml @@ -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' diff --git a/tests/playbook_delete_vms.yml b/tests/playbook_delete_vms.yml new file mode 100644 index 0000000..f3e92e4 --- /dev/null +++ b/tests/playbook_delete_vms.yml @@ -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 +... diff --git a/tests/tasks/load_vagrant_ssh_key.yml b/tests/tasks/load_vagrant_ssh_key.yml new file mode 100644 index 0000000..6271c75 --- /dev/null +++ b/tests/tasks/load_vagrant_ssh_key.yml @@ -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 + +... diff --git a/tests/tasks/reset_ssh_agent.yml b/tests/tasks/reset_ssh_agent.yml new file mode 100644 index 0000000..cd003af --- /dev/null +++ b/tests/tasks/reset_ssh_agent.yml @@ -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 + +... diff --git a/tests/tasks/run_vagrant.yml b/tests/tasks/run_vagrant.yml new file mode 100644 index 0000000..ca62efc --- /dev/null +++ b/tests/tasks/run_vagrant.yml @@ -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 +...