Changeset - 0388df2571ca
[Not reviewed]
0 2 7
Branko Majic (branko) - 7 years ago 2017-06-07 23:49:57
branko@majic.rs
MAR-21: Implemented tests for the bootstrap role:

- Use plain spaces in sudo configuration to make it easier to test.
- Added testing for both Debian Stretch and Debian Jessie.
- Fixed Ansible liniting for file mode.
- Implemented a couple of basic tests.
9 files changed with 215 insertions and 3 deletions:
0 comments (0 inline, 0 general)
roles/bootstrap/files/ansible_sudo
Show inline comments
 
ansible	ALL=(ALL:ALL) NOPASSWD:ALL
 
ansible ALL=(ALL:ALL) NOPASSWD:ALL
roles/bootstrap/molecule.yml
Show inline comments
 
new file 100644
 
---
 
dependency: {}
 

	
 
driver:
 
  name: vagrant
 

	
 
vagrant:
 

	
 
  platforms:
 
    - name: debian-jessie64
 
      box: debian/contrib-jessie64
 
    - name: debian-stretch64
 
      box: debian/stretch64
 

	
 
  providers:
 
    - name: virtualbox
 
      type: virtualbox
 
      options:
 
        memory: 512
 
        cpus: 1
 

	
 
  instances:
 

	
 
    - name: parameters-mandatory
 
    - name: parameters-optional
 

	
 
verifier:
 
  name: testinfra
roles/bootstrap/playbook.yml
Show inline comments
 
new file 100644
 
---
 

	
 
# Put Ansible key into root's authorized_keys to test its removal, but don't
 
# touch the file if it does not exist in order to properly test for idempotence.
 
- hosts: parameters-mandatory
 
  tasks:
 

	
 
    - name: Check if authorized_keys exists already
 
      stat:
 
        path: "/root/.ssh/authorized_keys"
 
      register: "authorized_keys"
 

	
 
    - name: Deploy authorized_keys to mimic set-up via preseed file
 
      authorized_key:
 
        user: root
 
        key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
 
      when: "not authorized_keys.stat.exists"
 

	
 
# Put Ansible key into root's authorized_keys to test its removal, but don't
 
# touch the file if it does not exist in order to properly test for idempotence.
 
- hosts: parameters-optional
 
  tasks:
 

	
 
    - name: Check if authorized_keys exists already
 
      stat:
 
        path: "/root/.ssh/authorized_keys"
 
      register: "authorized_keys"
 

	
 
    - name: Deploy authorized_keys to mimic set-up via preseed file
 
      authorized_key:
 
        user: root
 
        key: "{{ lookup('file', 'tests/data/ansible_key.pub') }}"
 
      when: "not authorized_keys.stat.exists"
 

	
 
- hosts: parameters-mandatory
 
  roles:
 
    - role: bootstrap
 

	
 
- hosts: parameters-optional
 
  roles:
 
    - role: bootstrap
 
      ansible_key: "{{ lookup('file', 'tests/data/ansible_key.pub') }}"
roles/bootstrap/tasks/main.yml
Show inline comments
 
@@ -13,7 +13,7 @@
 
  authorized_key: user=ansible key="{{ ansible_key }}"
 

	
 
- name: Set-up password-less sudo for the ansible user
 
  copy: src=ansible_sudo dest=/etc/sudoers.d/ansible mode=640 owner=root group=root
 
  copy: src=ansible_sudo dest=/etc/sudoers.d/ansible mode=0640 owner=root group=root
 

	
 
- name: Revoke rights for Ansible user to log-in as root to server via ssh
 
  authorized_key: user=root key="{{ ansible_key }}" state=absent
 
@@ -22,4 +22,4 @@
 
  include: ../handlers/main.yml
 
  when: "handlers | default(False) | bool() == True"
 
  tags:
 
    - handlers
 
\ No newline at end of file
 
    - handlers
roles/bootstrap/tests/data/ansible_key
Show inline comments
 
new file 100644
 
-----BEGIN RSA PRIVATE KEY-----
 
MIIEoAIBAAKCAQEA2Ic132ERipLFnARqpH4gCapohYucphUjX1Syt+O/ledtwdqQ
 
h65RjOip6QXRPewsTIIpFvCNRjc3brkxP/v9ikKA8s7495V9Plv5FeOxDzi0wAkC
 
UHV2Kz2IxZX79/5h6vnot2ujhqXwHeCFMdtyMvl5F2e9O/gtn0ngehEbHlXPSFa2
 
C2Sx5RuhdjM/Y0SxCu4++h0SUCOPzEgGsLXON63aRl8Kctu99QSnEU1GIiJBU+Lq
 
mIRwBjaSlBtY3KcJONbEBq0gHi/OspvtqdFn2pDAz2K8hgt4Z66u4O2TGBnE7CnH
 
JPCz0+0ZC/E5W4ECfjtncYWTMMyRlC1JLRXznwIDAQABAoIBAB+3rqyKC4fA+DFi
 
dDykPQ0taSIm0UuCzSupvIQzWF0dCXbcDU/SuR9q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4oBa73jmfRdmXTRq9WBq5CORENWBN
 
Anf8HVII11gwu8FQGBhWd2mhQ0kvDlKLVRq0taQxL4pjJ3yru1gbkI43yCYc+O3z
 
7rvuYpnhOj2h8pk5/hCoW2EEtk4RSuxI41AJrBb2szrkaebR
 
-----END RSA PRIVATE KEY-----
roles/bootstrap/tests/data/ansible_key.pub
Show inline comments
 
new file 100644
 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDYhzXfYRGKksWcBGqkfiAJqmiFi5ymFSNfVLK347+V523B2pCHrlGM6KnpBdE97CxMgikW8I1GNzduuTE/+/2KQoDyzvj3lX0+W/kV47EPOLTACQJQdXYrPYjFlfv3/mHq+ei3a6OGpfAd4IUx23Iy+XkXZ707+C2fSeB6ERseVc9IVrYLZLHlG6F2Mz9jRLEK7j76HRJQI4/MSAawtc43rdpGXwpy2731BKcRTUYiIkFT4uqYhHAGNpKUG1jcpwk41sQGrSAeL86ym+2p0WfakMDPYryGC3hnrq7g7ZMYGcTsKcck8LPT7RkL8TlbgQJ+O2dxhZMwzJGULUktFfOf ansible-key
roles/bootstrap/tests/test_default.py
Show inline comments
 
new file 100644
 
import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory').get_hosts('all')
 

	
 

	
 
def test_installed_packages(Package):
 
    """
 
    Tests if packages have been installed.
 
    """
 

	
 
    assert Package('sudo').is_installed
 

	
 

	
 
def test_ansible_user(Group, Sudo, User):
 
    """
 
    Tests if Ansible user and group have been set-up correctly.
 
    """
 

	
 
    with Sudo():
 
        group = Group('ansible')
 
        assert group.exists
 
        assert group.gid < 1000
 

	
 
        user = User('ansible')
 
        assert user.exists
 
        assert user.group == 'ansible'
 
        assert user.groups == ['ansible']
 
        assert user.uid < 1000
 
        assert user.shell == '/bin/bash'
 
        assert user.password == '!'
 

	
 

	
 
def test_sudo_configuration(File, Sudo):
 
    """
 
    Tests if sudo has been configured to allow Ansible user to run any command
 
    without password.
 
    """
 

	
 
    with Sudo():
 

	
 
        sudo_config = File('/etc/sudoers.d/ansible')
 

	
 
        assert sudo_config.is_file
 
        assert sudo_config.user == 'root'
 
        assert sudo_config.group == 'root'
 
        assert sudo_config.mode == 0o640
 
        assert sudo_config.content == 'ansible ALL=(ALL:ALL) NOPASSWD:ALL'
roles/bootstrap/tests/test_parameters_mandatory.py
Show inline comments
 
new file 100644
 
import os
 

	
 

	
 
import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory').get_hosts('parameters-mandatory')
 

	
 

	
 
def test_authorized_keys(File, Sudo):
 
    """
 
    Tests if Ansible user authorized_keys has been set-up correctly.
 
    """
 

	
 
    with Sudo():
 

	
 
        ssh_key = open(os.path.expanduser('~/.ssh/id_rsa.pub'), 'read').read().strip()
 
        authorized_keys = File('/home/ansible/.ssh/authorized_keys')
 

	
 
        assert authorized_keys.is_file
 
        assert ssh_key in authorized_keys.content
 

	
 

	
 
def test_root_authorized_keys(File, Sudo):
 
    """
 
    Tests if Ansible key been removed from root's authorized keys.
 
    """
 

	
 
    with Sudo():
 

	
 
        ssh_key = open(os.path.expanduser('~/.ssh/id_rsa.pub'), 'read').read().strip()
 

	
 
        assert ssh_key not in File('/root/.ssh/authorized_keys').content
roles/bootstrap/tests/test_parameters_optional.py
Show inline comments
 
new file 100644
 
import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory').get_hosts('parameters-optional')
 

	
 

	
 
def test_authorized_keys(File, Sudo):
 
    """
 
    Tests if Ansible user authorized_keys has been set-up correctly.
 
    """
 

	
 
    with Sudo():
 

	
 
        ssh_key = open('tests/data/ansible_key.pub', 'read').read().strip()
 
        authorized_keys = File('/home/ansible/.ssh/authorized_keys')
 

	
 
        assert authorized_keys.is_file
 
        assert ssh_key in authorized_keys.content
 

	
 

	
 
def test_root_authorised_keys(File, Sudo):
 
    """
 
    Tests if Ansible key been removed from root's authorized keys.
 
    """
 

	
 
    with Sudo():
 

	
 
        ssh_key = open('tests/data/ansible_key.pub', 'read').read().strip()
 

	
 
        assert ssh_key not in File('/root/.ssh/authorized_keys').content
0 comments (0 inline, 0 general)