from squadron import state from tempfile import mktemp import pytest import random import os import jsonschema from helper import get_test_path test_path = os.path.join(get_test_path(), 'statetests') def test_basic(tmpdir): tmpfile = os.path.join(str(tmpdir),'basic') handler = state.StateHandler(test_path) num = random.randint(0, 100) item = {'tmpfile': tmpfile, 'num' : num} failed = handler.apply('test1', [item], True) assert len(failed) == 1 assert failed[0] == item failed = handler.apply('test1', [item]) assert len(failed) == 0 with open(tmpfile, 'r') as testfile: assert str(num) == testfile.read() def test_schema_validation_error(tmpdir): tmpfile = os.path.join(str(tmpdir),'basic') handler = state.StateHandler(test_path)
from __future__ import print_function from squadron import template from squadron.template import FileConfig, get_config, apply_config from squadron.exthandlers.extutils import get_filename from squadron.log import log, setup_log from tempfile import mkdtemp from shutil import rmtree import pytest from helper import are_dir_trees_equal, get_test_path import os import stat setup_log('DEBUG', console=True) test_path = os.path.join(get_test_path(), 'template_tests') def test_template_basic(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender(os.path.join(test_path, 'test1')) test.render(dirname, {'name':'user'}, {}) assert are_dir_trees_equal(dirname, os.path.join(test_path, 'test1result')) def test_template_chown_problem(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender(os.path.join(test_path, 'test1-notpermitted')) with pytest.raises(OSError) as ex: test.render(dirname, {'name':'user'}, {}, False) test.render(dirname, {'name':'user'}, {}, True)
from squadron import resources, log from helper import get_test_path import os log.setup_log('DEBUG', console=True) test_path = os.path.join(get_test_path(), 'resources_tests') def test_basic(): squadron_dir = os.path.join(test_path, 'basic') result = resources.load_resources(squadron_dir) assert len(result) == 3 assert 'basic.txt' in result assert 'basic' in result['basic.txt']() assert 'dir/second' in result assert 'second' in result['dir/second']() assert 'deeper/dir/here.sh' in result assert 'here' in result['deeper/dir/here.sh']() def test_empty(): squadron_dir = os.path.join(test_path, 'empty') result = resources.load_resources(squadron_dir) assert len(result) == 0
from squadron.nodes import get_node_info, _descend from helper import get_test_path import os test_path = os.path.join(get_test_path(), 'nodes_tests') def test_descend(): results = _descend(test_path, 'dev-a1.api.example.com') assert len(results) == 4 assert os.path.join(test_path, '%') in results assert os.path.join(test_path, 'dev%') in results assert os.path.join(test_path, 'base', '%.example.com') in results assert os.path.join(test_path, 'dev%.api.example.com') in results def test_get_node_info(): result = get_node_info(test_path, 'dev-a1.api.example.com') assert result == {'env':'dev', 'services':['api'], 'top-level-setting':True} result = get_node_info(test_path, 'www.example.com') assert result == {'top-level-setting':True} result = get_node_info(test_path, 'dev-database') assert result == {'env':'dev', 'top-level-setting':True}
import pytest import jsonschema from helper import are_dir_trees_equal, get_test_path import os import shutil import json from squadron.fileio.dirio import makedirsp def checkfile(filename, compare): with open(filename) as ofile: assert compare == ofile.read() test_path = os.path.join(get_test_path(), 'applytests') @pytest.mark.parametrize("source_dir,dest_dir,do_commit,do_copy", [ ("applytest1", "applytest1result", False, False), ("applytest2", "applytest2result", True, False), ("applytest1", "applytest1result", False, True), ]) def test_apply(tmpdir, source_dir, dest_dir, do_commit, do_copy): tmpdir = str(tmpdir) commit_tmp = os.path.join(tmpdir, 'tmp') makedirsp(commit_tmp) apply_dir = os.path.join(tmpdir, 'apply') # apply_dir must not yet exist
from squadron import commit from squadron.fileio.dirio import makedirsp import pytest import jsonschema from helper import are_dir_trees_equal, get_test_path import os import shutil import json from squadron.fileio.dirio import makedirsp def checkfile(filename, compare): with open(filename) as ofile: assert compare == ofile.read() test_path = os.path.join(get_test_path(), 'applytests') @pytest.mark.parametrize("source_dir,dest_dir,do_commit,do_copy",[ ("applytest1","applytest1result",False, False), ("applytest2","applytest2result",True, False), ("applytest1","applytest1result",False, True), ]) def test_apply(tmpdir, source_dir, dest_dir, do_commit, do_copy): tmpdir = str(tmpdir) commit_tmp = os.path.join(tmpdir, 'tmp') makedirsp(commit_tmp) apply_dir = os.path.join(tmpdir, 'apply') # apply_dir must not yet exist shutil.copytree(os.path.join(test_path, source_dir), apply_dir)
from squadron.tests import get_tests, run_tests from helper import get_test_path import os test_path = os.path.join(get_test_path(), 'tests_test') def test_recurse_executable(): result = get_tests('', '', test_path) assert len(result) == 3 assert os.path.join(test_path, 'tests', 'test.sh') in result assert os.path.join(test_path, 'tests', 'deep','folder','test.sh') in result assert os.path.join(test_path, 'tests', 'deep','anothertest') in result def test_execute_tests(): prepend_test = lambda xl: [os.path.join(test_path, 'tests', x) for x in xl] successful_tests = prepend_test(['test.sh', os.path.join('deep','anothertest')]) failure_tests = prepend_test([os.path.join('deep','folder','test.sh')]) failed_tests = run_tests(successful_tests, {}) assert len(failed_tests) == 0 failed_tests = run_tests(failure_tests, {}) assert len(failure_tests) == 1 assert failure_tests[0] in failed_tests assert failed_tests[failure_tests[0]] == 1 def test_execute_input(): input_test = os.path.join(test_path, 'tests', 'test.sh')
def create_blank_infojson(statedir): open(os.path.join(statedir,'info.json'),'w+').close() def remove_lock_file(d, lockfile='.lock'): try: os.remove(os.path.join(d, lockfile)) except OSError: pass def teardown_function(function): try: shutil.rmtree('/tmp/main2test/') except: pass test_path = os.path.join(get_test_path(), 'main_tests') def test_main_basic(tmpdir): tmpdir = str(tmpdir) squadron_state_dir = os.path.join(tmpdir, 'state') makedirsp(squadron_state_dir) create_blank_infojson(squadron_state_dir) makedirsp('/tmp/applytest1/') squadron_dir = os.path.join(test_path, 'main1') main.go(squadron_dir, squadron_state_dir, os.path.join(test_path, 'main1.config'), 'dev', None, False, False, False)
from squadron.tests import get_tests, run_tests from helper import get_test_path import os test_path = os.path.join(get_test_path(), 'tests_test') def test_recurse_executable(): result = get_tests('', '', test_path) assert len(result) == 3 assert os.path.join(test_path, 'tests', 'test.sh') in result assert os.path.join(test_path, 'tests', 'deep', 'folder', 'test.sh') in result assert os.path.join(test_path, 'tests', 'deep', 'anothertest') in result def test_execute_tests(): prepend_test = lambda xl: [os.path.join(test_path, 'tests', x) for x in xl] successful_tests = prepend_test( ['test.sh', os.path.join('deep', 'anothertest')]) failure_tests = prepend_test([os.path.join('deep', 'folder', 'test.sh')]) failed_tests = run_tests(successful_tests, {}) assert len(failed_tests) == 0 failed_tests = run_tests(failure_tests, {}) assert len(failure_tests) == 1 assert failure_tests[0] in failed_tests
from squadron.service import get_service_actions, get_reactions, react, _checkfiles import glob import os from squadron.fileio.dirio import makedirsp import shutil from helper import get_test_path import pytest test_path = os.path.join(get_test_path(), 'service_tests') def test_get_service_actions(): actions = get_service_actions(test_path, 'service1', '1.0.1', {'TEST':'test'}) print "actions {}".format(actions) assert len(actions) == 3 assert 'service1.start' in actions assert 'service1.reload' in actions assert 'service1.restart' in actions assert len(actions['service1.start']['commands']) > 0 assert len(actions['service1.reload']['commands']) > 0 assert len(actions['service1.restart']['commands']) > 0 assert len(actions['service1.reload']['not_after']) == 2 assert len(actions['service1.restart']['not_after']) == 1 assert 'chdir' in actions['service1.start'] assert 'service1.start' in actions['service1.reload']['not_after'] assert 'service1.restart' in actions['service1.reload']['not_after']
from squadron.nodes import get_node_info, _descend from helper import get_test_path import os test_path = os.path.join(get_test_path(), 'nodes_tests') def test_descend(): results = _descend(test_path, 'dev-a1.api.example.com') assert len(results) == 4 assert os.path.join(test_path, '%') in results assert os.path.join(test_path, 'dev%') in results assert os.path.join(test_path, 'base', '%.example.com') in results assert os.path.join(test_path, 'dev%.api.example.com') in results def test_get_node_info(): result = get_node_info(test_path, 'dev-a1.api.example.com') assert result == { 'env': 'dev', 'services': ['api'], 'top-level-setting': True } result = get_node_info(test_path, 'www.example.com') assert result == {'top-level-setting': True} result = get_node_info(test_path, 'dev-database') assert result == {'env': 'dev', 'top-level-setting': True}
import mock from squadron import daemon from helper import get_test_path import os import threading import pytest from squadron import log log.setup_log('DEBUG', console=True) test_path = os.path.join(get_test_path(), 'daemon_tests') @pytest.mark.parametrize("webhooks,config_file",[ (False,"simple"), (True,"webhooks"), ]) def test_basic(tmpdir, webhooks, config_file): tmpdir = str(tmpdir) config_file = os.path.join(test_path, config_file) with mock.patch('squadron.main.get_squadron_dir') as dirmock: dirmock.return_value = tmpdir # It's a little odd where this needs to be patched, see: # http://www.voidspace.org.uk/python/mock/patch.html#id1 with mock.patch('squadron.daemon.Repo') as gitmock: # The raw git interface object git = mock.MagicMock() git.checkout.return_value = 0 git.pull.return_value = 0
from squadron.service import get_service_actions, get_reactions, react, _checkfiles import glob import os from squadron.fileio.dirio import makedirsp import shutil from helper import get_test_path import pytest test_path = os.path.join(get_test_path(), 'service_tests') def test_get_service_actions(): actions = get_service_actions(test_path, 'service1', '1.0.1', {'TEST': 'test'}) print "actions {}".format(actions) assert len(actions) == 3 assert 'service1.start' in actions assert 'service1.reload' in actions assert 'service1.restart' in actions assert len(actions['service1.start']['commands']) > 0 assert len(actions['service1.reload']['commands']) > 0 assert len(actions['service1.restart']['commands']) > 0 assert len(actions['service1.reload']['not_after']) == 2 assert len(actions['service1.restart']['not_after']) == 1 assert 'chdir' in actions['service1.start'] assert 'service1.start' in actions['service1.reload']['not_after']
import mock from squadron import daemon from helper import get_test_path import os import threading import pytest from squadron import log log.setup_log('DEBUG', console=True) test_path = os.path.join(get_test_path(), 'daemon_tests') @pytest.mark.parametrize("webhooks,config_file", [ (False, "simple"), (True, "webhooks"), ]) def test_basic(tmpdir, webhooks, config_file): tmpdir = str(tmpdir) config_file = os.path.join(test_path, config_file) with mock.patch('squadron.main.get_squadron_dir') as dirmock: dirmock.return_value = tmpdir # It's a little odd where this needs to be patched, see: # http://www.voidspace.org.uk/python/mock/patch.html#id1 with mock.patch('squadron.daemon.Repo') as gitmock: # The raw git interface object git = mock.MagicMock() git.checkout.return_value = 0 git.pull.return_value = 0
from __future__ import print_function from squadron import template from squadron.template import FileConfig, get_config, apply_config from squadron.exthandlers.extutils import get_filename from squadron.log import log, setup_log from tempfile import mkdtemp from shutil import rmtree import pytest from helper import are_dir_trees_equal, get_test_path import os import stat setup_log('DEBUG', console=True) test_path = os.path.join(get_test_path(), 'template_tests') def test_template_basic(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender(os.path.join(test_path, 'test1')) test.render(dirname, {'name': 'user', 'variable': 'test3'}, {}) assert are_dir_trees_equal(dirname, os.path.join(test_path, 'test1result')) def test_template_chown_problem(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender( os.path.join(test_path, 'test1-notpermitted')) with pytest.raises(OSError) as ex:
def remove_lock_file(d, lockfile='.lock'): try: os.remove(os.path.join(d, lockfile)) except OSError: pass def teardown_function(function): try: shutil.rmtree('/tmp/main2test/') except: pass test_path = os.path.join(get_test_path(), 'main_tests') def test_main_basic(tmpdir): tmpdir = str(tmpdir) squadron_state_dir = os.path.join(tmpdir, 'state') makedirsp(squadron_state_dir) create_blank_infojson(squadron_state_dir) makedirsp('/tmp/applytest1/') squadron_dir = os.path.join(test_path, 'main1') main.go(squadron_dir, squadron_state_dir, os.path.join(test_path, 'main1.config'), 'dev', None, False, False, False)