Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
 - add project example test
 - add parallel tests
 - add structure tests
  • Loading branch information
janhybs committed Nov 22, 2018
1 parent 0477f8c commit eaa0799
Show file tree
Hide file tree
Showing 15 changed files with 1,014 additions and 4 deletions.
10 changes: 6 additions & 4 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import os


__dir__ = os.path.dirname(__file__)
__root__ = os.path.abspath(os.path.join(__dir__, '..'))
__tests__ = os.path.join(__root__, 'tests')
__src__ = os.path.join(__root__, 'ci-hpc')


def fix_paths(verbose=False):
# append module root directory to sys.path
__dir__ = os.path.dirname(__file__)
__root__ = os.path.abspath(os.path.join(__dir__, '..'))
__tests__ = os.path.join(__root__, 'tests')
__src__ = os.path.join(__root__, 'ci-hpc')
sys.path = [__src__, __tests__] + sys.path
if verbose:
print('\n'.join(sys.path))
Expand Down
13 changes: 13 additions & 0 deletions tests/project-example/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
install:
- name: install-step

foo: <foo>
bar-foo: <bar.foo>

shell: |
echo foo
test:
- name: test-step
shell: |
echo bar
7 changes: 7 additions & 0 deletions tests/project-example/variables-complex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
foo:
- 1
- 2
- 3

foo-bar:
foo: 5
4 changes: 4 additions & 0 deletions tests/project-example/variables-iter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foo:
- 1
- 2
- 3
1 change: 1 addition & 0 deletions tests/project-example/variables-simple.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: 1
File renamed without changes.
71 changes: 71 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/python3
# author: Jan Hybs

import tests

import random
from os.path import join

from unittest import TestCase
from cfg import cfgutil


project_dir = join(tests.__dir__, 'project-example')
config_path = join(project_dir, 'config.yaml')


class TestConfig(TestCase):

def test_configure_file(self):

variables = dict()
project_configs = list(cfgutil.configure_file(config_path, variables))
self.assertEqual(len(project_configs), 1)

variables = cfgutil.load_config(join(project_dir, 'variables-simple.yaml'))
project_configs = list(cfgutil.configure_file(config_path, variables))
self.assertEqual(len(project_configs), 1)

variables = cfgutil.load_config(join(project_dir, 'variables-iter.yaml'))
project_configs = list(cfgutil.configure_file(config_path, variables))
self.assertEqual(len(project_configs), 3)

variables = cfgutil.load_config(join(project_dir, 'variables-complex.yaml'), hostname='foo-bar')
project_configs = list(cfgutil.configure_file(config_path, variables))
self.assertEqual(len(project_configs), 1)

variables = cfgutil.load_config(join(project_dir, 'variables-complex.yaml'), hostname='foo-foo')
project_configs = list(cfgutil.configure_file(config_path, variables))
self.assertEqual(len(project_configs), 3)

def test_configure_string(self):
variables = dict(
foo='foo',
bar=dict(
foo='123',
bar=True,
)
)
self.assertEqual(cfgutil.configure_string('<foo>', variables), 'foo')
self.assertEqual(cfgutil.configure_string('<bar.foo>', variables), '123')
self.assertEqual(cfgutil.configure_string('<bar.foo|s>', variables), '123')
self.assertEqual(cfgutil.configure_string('<bar.foo|i>', variables), 123)
self.assertEqual(cfgutil.configure_string('<bar.foo|f>', variables), 123.0)
self.assertEqual(cfgutil.configure_string('<bar.bar>', variables), 'True')
self.assertEqual(cfgutil.configure_string('<bar.bar|b>', variables), True)

self.assertIsInstance(cfgutil.configure_string('<bar>', variables), str)
self.assertIsInstance(cfgutil.configure_string('<bar.foo>', variables), str)
self.assertIsInstance(cfgutil.configure_string('<bar.foo|i>', variables), int)
self.assertIsInstance(cfgutil.configure_string('<bar.bar|b>', variables), bool)

variables = dict(
foo=random.random(),
bar=dict(
foo=random.random(),
)
)

config = list(cfgutil.configure_file(config_path, variables))[0]
self.assertEqual(config['install'][0]['foo'], variables['foo'])
self.assertEqual(config['install'][0]['bar-foo'], '<bar.foo>') # is not supported
44 changes: 44 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/python3
# author: Jan Hybs

import tests

from os.path import join

from unittest import TestCase
from main import parse_args, main

project_dir = join(tests.__dir__, 'project-example')


class TestProject(TestCase):

def test_parse_args(self):
with self.assertRaises(SystemExit):
parse_args([])

args = [
'--project=foo',
'--log-path=foo.log',
'--watch-commit-policy=commit-per-day',
'--git-commit=foobar',
'--tty',
]
parsed = parse_args(args)
self.assertEqual(parsed.project, 'foo')
self.assertEqual(parsed.log_path, 'foo.log')
self.assertEqual(parsed.tty, True)
self.assertEqual(parsed.watch_commit_policy, 'commit-per-day')
self.assertListEqual(parsed.step, ['install', 'test'])
self.assertListEqual(parsed.git_commit, ['foobar'])

# no valid config
with self.assertRaises(SystemExit):
main(args)

args = [
'--project=foo',
'--log-level=warning',
'--config-dir=%s' % project_dir,
]
main(args)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ def test_is_complex(self):
data = {'no-less-than': 15}
repeat = ProjectStepRepeat(data)
self.assertTrue(repeat.is_complex())

def test_load_stats(self):
repeat = ProjectStepRepeat(None)

with self.assertRaises(NotImplementedError):
repeat.load_stats('foo', 'bar')

# cannot really test the rest without DB and repository
36 changes: 36 additions & 0 deletions tests/test_push_webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/python3
# author: Jan Hybs

import tests

import yaml
from os.path import join
from unittest import TestCase

from cfg.cfgutil import read_file
from vcs.webhooks.push_hook import push_webhook_from_dict, push_webhook_to_dict

webhook_dir = join(tests.__dir__, 'webhooks')
content = read_file(join(webhook_dir, 'example.json'))
yaml_file = yaml.load(content)


class TestPush_webhook(TestCase):

def test_push_webhook_to_dict(self):
webhook = push_webhook_from_dict(yaml_file)
dictionary = push_webhook_to_dict(webhook)

self.assertEqual(dictionary['head_commit']['author']['username'], 'janhybs')
self.assertIsInstance(dictionary['commits'], list)
self.assertEqual(dictionary['commits'][0]['timestamp'], '2018-11-19T08:53:54+01:00')
self.assertEqual(dictionary['after'], 'e87eead22e741dafb58355b9a6cbcd632c8f3704')

def test_push_webhook_from_dict(self):
webhook = push_webhook_from_dict(yaml_file)

self.assertEqual(webhook.head_commit.author.username, 'janhybs')
self.assertIsInstance(webhook.commits, list)
self.assertEqual(webhook.commits[0].message, 'Change L3 array size from 32 to 44')
self.assertEqual(webhook.commits[0].distinct, True)
self.assertEqual(webhook.after, 'e87eead22e741dafb58355b9a6cbcd632c8f3704')
File renamed without changes.
90 changes: 90 additions & 0 deletions tests/test_worker_pool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/python3
# author: Jan Hybs

import tests

import time

import utils.logging
utils.logging.logger.set_level('WARNING')


import multiprocessing
from unittest import TestCase
from processing.multi_processing.thread_pool import (
WorkerPool, Worker, LogStatusFormat
)


cpu_count = multiprocessing.cpu_count()


class TestWorkerPool(TestCase):

def test_start_serial(self):
utils.logging.logger.set_level('WARNING')

items = [dict(foo=123), dict(foo=456)]
processes = 1

counter = dict(cnt=0)

def func(worker: Worker):
counter['cnt'] += 1
return [counter['cnt'], worker.crate['foo']]

pool = WorkerPool(items, processes, func)
pool.start_serial()
self.assertListEqual(pool.result, [[1, 123], [2, 456]])

def test_start_parallel(self):
utils.logging.logger.set_level('WARNING')

class A:
def __init__(self, name):
self.name = name

items = [
A(name='a'), A(name='b'), A(name='c'),
A(name='a'), A(name='b'), A(name='c'),
A(name='a'), A(name='b'), A(name='c'),
]
result = [[i+1, items[i].name] for i in range(len(items))]

def func(worker: Worker):
counter['cnt'] += 1
time.sleep(0.001)
return [counter['cnt'], worker.crate.name]

# this test is not by its nature bulletproof
# TODO make parallel testing more robust
if cpu_count > 1:
with self.assertRaises(AssertionError):
for i in range(25):
counter = dict(cnt=0)

pool = WorkerPool(items, 2, func)
pool.log_statuses(format=LogStatusFormat.ONELINE_GROUPED)
pool.start_parallel()
self.assertListEqual(pool.result, result)

for i in range(10):
counter = dict(cnt=0)
pool = WorkerPool(items, 2, func)
pool.log_statuses(format=LogStatusFormat.COMPLEX)
pool.start_serial()
self.assertListEqual(pool.result, result)

for i in range(10):
counter = dict(cnt=0)
pool = WorkerPool(items, 1, func)
pool.log_statuses(format=LogStatusFormat.SIMPLE)
pool.start_serial()
self.assertListEqual(pool.result, result)

for i in range(10):
counter = dict(cnt=0)
pool = WorkerPool(items, 1, func)
pool.log_statuses(format=LogStatusFormat.ONELINE)
pool.start_parallel()
self.assertListEqual(pool.result, result)

0 comments on commit eaa0799

Please sign in to comment.