Changeset - d6a8b9523eb6
[Not reviewed]
0 3 0
Branko Majic (branko) - 6 years ago 2017-11-19 18:21:04
branko@majic.rs
MAR-127: Added tests for time synchronisation (NTP) implementation:

- Updated test playbook.
- Added tests related to time synchronisation.
3 files changed with 76 insertions and 0 deletions:
0 comments (0 inline, 0 general)
roles/common/playbook.yml
Show inline comments
 
@@ -68,6 +68,12 @@
 
      pipreqcheck_gid: 2500
 
      prompt_colour: cyan
 
      prompt_id: test
 
      # Purposefully set this to 3 servers to make sure we are
 
      # overriding the default configuration.
 
      ntp_servers:
 
        - "0.debian.pool.ntp.org"
 
        - "1.debian.pool.ntp.org"
 
        - "2.debian.pool.ntp.org"
 
      # From backup_client role meta dependency.
 
      backup_encryption_key: "{{ lookup('file', 'tests/data/gnupg/backup_encryption_key') }}"
 
      backup_server: backup-server
roles/common/tests/test_parameters_mandatory.py
Show inline comments
 
@@ -122,3 +122,24 @@ def test_backup_configuration_absent(File, Sudo):
 

	
 
    with Sudo():
 
        assert not File('/etc/duply/main/patterns/common').exists
 

	
 

	
 
def test_ntp_software_not_installed(Package):
 
    """
 
    Tests if NTP packages are absent.
 
    """
 

	
 
    # @TODO: This throws an exception. It seems version of Testinfra
 
    # used cannot properly check for absence of package.
 
    # assert not Package('ntp').is_installed
 
    # assert not Package('ntpdate').is_installed
 

	
 
    pass
 

	
 

	
 
def test_ntp_listening_interfaces(Socket):
 
    """
 
    Tests if NTP server is not listening.
 
    """
 

	
 
    assert not Socket('udp://:::123').is_listening
roles/common/tests/test_parameters_optional.py
Show inline comments
 
import os
 
import re
 
import socket
 

	
 
import paramiko
 
@@ -283,3 +284,51 @@ def test_backup_configuration(File, Sudo):
 
        assert common_extra.is_file
 
        assert "/home/user1" in common_extra.content.split("\n")
 
        assert "/home/user2" in common_extra.content.split("\n")
 

	
 

	
 
def test_ntp_software_installed(Package):
 
    """
 
    Tests if NTP packages are installed.
 
    """
 

	
 
    assert Package('ntp').is_installed
 
    assert Package('ntpdate').is_installed
 

	
 

	
 
def test_ntp_server_configuration(File, Sudo):
 
    """
 
    Tests if NTP server has been correctly configured.
 
    """
 

	
 
    with Sudo():
 

	
 
        # Read the configuration file.
 
        configuration = File("/etc/ntp.conf").content.split("\n")
 

	
 
        # Extract only the relevant sections of files (exculde empty
 
        # lines and comments).
 
        configuration = [c.strip() for c in configuration if re.match('^\s*(|#.*)$', c) is None]
 

	
 
        # Ensure correct servers have been configured in the pool.
 
        servers = [c for c in configuration if c.startswith('server')]
 

	
 
        expected_servers = ["server 0.debian.pool.ntp.org iburst",
 
                            "server 1.debian.pool.ntp.org iburst",
 
                            "server 2.debian.pool.ntp.org iburst"]
 

	
 
        assert sorted(servers) == sorted(expected_servers)
 

	
 
        # Ensure querying of server is disable for untrusted clients.
 
        restrictions = [c for c in configuration if c.startswith('restrict')]
 
        expected_restrictions = ["restrict -4 default kod notrap nomodify nopeer noquery notrust",
 
                                 "restrict -6 default kod notrap nomodify nopeer noquery notrust"]
 

	
 
        assert sorted(restrictions) == sorted(expected_restrictions)
 

	
 

	
 
def test_ntp_listening_interfaces(Socket):
 
    """
 
    Tests if NTP server is listening on correct ports.
 
    """
 

	
 
    assert Socket('udp://:::123').is_listening
0 comments (0 inline, 0 general)