Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

Commit

Permalink
#9: Increase tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
blackandred committed Jul 13, 2020
1 parent 519061f commit 488f888
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 50 deletions.
15 changes: 9 additions & 6 deletions src/harbor/tasks/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,20 @@ def install_and_configure_role(self, force_update: bool = False) -> bool:
return False

self.io().debug('Downloading fresh role...')
subprocess.check_call([
'ansible-galaxy',
'install', '-r', self.ansible_dir + '/requirements.yml',
'-p', self.ansible_dir + '/roles/',
'--force'
])
self.download_roles()

self._write_synced_version(abs_ansible_dir)

return True

def download_roles(self):
self.sh(' '.join([
'ansible-galaxy',
'install', '-r', self.ansible_dir + '/requirements.yml',
'-p', self.ansible_dir + '/roles/',
'--force'
]), capture=False)

def _synchronize_structure_from_template(self, abs_ansible_dir: str, only_jinja_templates: bool = False) -> bool:
"""Synchronizes template structure into .rkd/deployment"""

Expand Down
21 changes: 21 additions & 0 deletions src/harbor/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,27 @@ def fetch_page_content(self, host: str):
return self.exec_in_container(TEST_PROJECT_NAME + '_gateway_1', ['curl', '-s', '-vv', '--header',
'Host: %s' % host, 'http://127.0.0.1'])

def prepare_valid_deployment_yml(self):
"""Internal: Prepares a valid deployment.yml and already synchronized files structure, downloaded role"""

with open(self.get_test_env_subdirectory('') + '/deployment.yml', 'w') as f:
f.write('''deploy_user: vagrant
deploy_group: vagrant
remote_dir: /project
git_url: https://github.com/riotkit-org/empty
git_secret_url: https://github.com/riotkit-org/empty
configure_sudoers: no
nodes:
production:
# example configuration for testing environment based on Vagrant
# to run the environment type: harbor :deployment:vagrant -c "up --provision"
- host: 127.0.0.1
port: 2222
user: docker
password: docker
''')

def assertContainerIsNotRunning(self, service_name: str, driver: ComposeDriver):
container_name_without_instance_num = driver.project_name + '_' + service_name + '_'

Expand Down
63 changes: 19 additions & 44 deletions test/test_deployment_applytask.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from harbor.tasks.deployment import UpdateFilesTask


class DeploymentTaskTask(BaseHarborTestClass):
class DeploymentTaskTest(BaseHarborTestClass):
def test_functional_validates_if_structure_exists(self):
"""Verify that :deployment:apply requires :deployment:files:update to be called first
"""
Expand All @@ -23,26 +23,20 @@ def test_functional_validates_if_structure_exists(self):
self.assertIn('Deployment not configured. Use `harbor :deployment:role:update` first', out)
self.assertIn('TASK_EXIT_RESULT=False', out)

# todo: Move test to other class
def test_functional_validates_deployment_yml_file_not_created(self):
"""Test that deployment.yml/deployment.yaml needs to be created first and the message for the end user is clear
"""

out = self.execute_task(UpdateFilesTask(),
args={
'--ask-vault-pass': False,
'--vault-passwords': ''
},
env={})

self.assertIn('Deployment not configured - missing deployment.yml or deployment.yaml file', out)

def test_functional_passes_structure_validation_after_using_update_command(self):
"""Verify that when :deployment:files:update is called, then :deployment:apply, then the validation
in :deployment:apply is passing - as the files are created by first task
"""

self._prepare_valid_configuration()
# Prepare configuration files first
self.prepare_valid_deployment_yml()
self.execute_task(UpdateFilesTask(),
args={
'--ask-vault-pass': False,
'--vault-passwords': ''
},
env={})

ansible_call = []

deployment_task = DeploymentTask()
Expand All @@ -69,7 +63,15 @@ def test_functional_decrypts_encrypted_deployment_yml_file_on_startup(self):
"""Check that deployment.yml file can be encrypted, and that given vault password in commandline will allow
to decrypt the contents automatically"""

self._prepare_valid_configuration()
# 0) Preparation of configuration files
self.prepare_valid_deployment_yml()
self.execute_task(UpdateFilesTask(),
args={
'--ask-vault-pass': False,
'--vault-passwords': ''
},
env={})

ansible_call = []
passphrase_file_path = self.get_test_env_subdirectory('.rkd') + '/tmp-secret.txt'

Expand Down Expand Up @@ -102,30 +104,3 @@ def test_functional_decrypts_encrypted_deployment_yml_file_on_startup(self):
self.assertIn('.rkd/tmp-secret.txt', ansible_call[0][0])
self.assertIn('TASK_EXIT_RESULT=True', out)

def _prepare_valid_configuration(self):
"""Internal: Prepares a valid deployment.yml and already synchronized files structure, downloaded role"""

with open(self.get_test_env_subdirectory('') + '/deployment.yml', 'w') as f:
f.write('''deploy_user: vagrant
deploy_group: vagrant
remote_dir: /project
git_url: https://github.com/riotkit-org/empty
git_secret_url: https://github.com/riotkit-org/empty
configure_sudoers: no
nodes:
production:
# example configuration for testing environment based on Vagrant
# to run the environment type: harbor :deployment:vagrant -c "up --provision"
- host: 127.0.0.1
port: 2222
user: docker
password: docker
''')

self.execute_task(UpdateFilesTask(),
args={
'--ask-vault-pass': False,
'--vault-passwords': ''
},
env={})
36 changes: 36 additions & 0 deletions test/test_deployment_updatefilestask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
from harbor.test import BaseHarborTestClass
from harbor.tasks.deployment import UpdateFilesTask


class UpdateFilesTaskTest(BaseHarborTestClass):
def test_functional_validates_deployment_yml_file_not_created(self):
"""Test that deployment.yml/deployment.yaml needs to be created first and the message for the end user is clear
"""

out = self.execute_task(UpdateFilesTask(), args={'--ask-vault-pass': False, '--vault-passwords': ''}, env={})
self.assertIn('Deployment not configured - missing deployment.yml or deployment.yaml file', out)

def test_functional_structure_is_copied(self):
"""Verify that the structure is copied, and the files are rendered using JINJA2"""

# prepare configuration
self.prepare_valid_deployment_yml()
task = UpdateFilesTask()

# mock external dependencies: github.com and ansible-galaxy
task.download_roles = lambda *args, **kwargs: None

with self.subTest('Verify overall task exit code'):
out = self.execute_task(task, args={'--ask-vault-pass': False, '--vault-passwords': ''}, env={})
self.assertIn('TASK_EXIT_RESULT=True', out)

with self.subTest('Verify copied files'):
# check a few files, including files that are rendered from JINJA2 like inventory and playbook
for filename in ['ansible.cfg', 'Vagrantfile', 'harbor.inventory.cfg', 'harbor.playbook.yml']:
self.assertTrue(os.path.isfile(self.get_test_env_subdirectory('.rkd/deployment') + '/' + filename))

with self.subTest('Check if JINJA2 templates are rendered'):
with open(self.get_test_env_subdirectory('.rkd/deployment') + '/harbor.inventory.cfg', 'r') as f:
# vagrant is a default value from file created by self.prepare_valid_deployment_yml()
self.assertIn('ansible_ssh_user=docker', f.read())

0 comments on commit 488f888

Please sign in to comment.