import types from pspec import describe from pspec.groups.base import BaseGroup from fixtures import random_spec with describe('BaseGroup'): with describe('add_child'): def it_adds_a_group_to_its_children_and_sets_parent(): group_a = BaseGroup() group_b = BaseGroup() group_a.add_child(group_b) assert group_a.children == [group_b] assert group_b.parent == group_a with describe('get_collecting_group'): def it_returns_none_by_default(): assert BaseGroup().get_collecting_group() is None def it_returns_own_group_if_it_is_collecting_and_has_no_children(): group = BaseGroup() group.is_collecting = True assert group.get_collecting_group() == group def it_returns_a_collecting_child_before_itself(): group = BaseGroup() group.add_child(BaseGroup()) group.is_collecting = True group.children[0].is_collecting = True assert group.get_collecting_group() == group.children[0]
from pspec import describe from loom.decorators import requires_puppet, REQUIRES_GEM from mock import patch from fabric.api import env with describe('loom.decorations.requires_puppet'): @patch('loom.decorators.has_librarian_installed') @patch('loom.decorators.has_puppet_installed') def it_does_not_do_anything_if_puppet_and_librarian_are_installed( puppet_mock, librarian_mock): env.host_string = 'app.example.com' puppet_mock.return_value = True librarian_mock.return_value = True requires_puppet(lambda: 1)() assert puppet_mock.called assert librarian_mock.called @patch('loom.decorators.has_librarian_installed') @patch('loom.decorators.has_puppet_installed') @patch('loom.decorators.abort') def it_aborts_if_puppet_is_not_installed(abort_mock, puppet_mock, librarian_mock): env.host_string = 'app.example.com' puppet_mock.return_value = False librarian_mock.return_value = True requires_puppet(lambda: 1)()
from pspec import describe from attest import assert_hook from fabric.api import env from loom.config import host_roles, current_roles with describe('host_roles'): def it_returns_the_role_for_a_host_with_a_single_role(): env.roledefs = {'app': 'app.example.com'} assert host_roles('app.example.com') == ['app'] def it_returns_the_roles_for_a_host_with_multiple_roles(): env.roledefs = { 'app': 'server.example.com', 'db': 'server.example.com', } assert host_roles('server.example.com') == ['app', 'db'] with describe('current_roles'): def it_returns_the_roles_for_the_current_host(): env.roledefs = {'app': 'app.example.com'} env.host_string = 'app.example.com' assert current_roles() == ['app']
from fabric.api import env from pspec import describe from loom.tasks import all, ssh, upgrade from mock import patch, call with describe("loom.tasks.all"): def it_sets_env_hsots_to_contain_all_hosts_in_roledefs(): env.roledefs = {"app1": ["app1.com", "app2.com"], "app2": ["app1.com", "app2.com"], "db": ["db.com"]} all() assert set(["app1.com", "app2.com", "db.com"]) == set(env.hosts) with describe("loom.tasks.ssh"): @patch("loom.tasks.subprocess") def it_calls_ssh(mock): env.user = "******" env.host_string = "example.com" env.key_filename = None ssh() expected = [call("ssh -A -t [email protected]".split())] assert mock.call.call_args_list == expected @patch("loom.tasks.subprocess") def it_calls_ssh_with_a_key_filename(mock): env.user = "******" env.host_string = "example.com"
from pspec import describe from attest import assert_hook from fabric.api import env from loom.config import host_roles, current_roles with describe('loom.config.host_roles'): def it_returns_the_role_for_a_host_with_a_single_role(): env.roledefs = {'app': 'app.example.com'} assert host_roles('app.example.com') == ['app'] def it_returns_the_roles_for_a_host_with_multiple_roles(): env.roledefs = { 'app': 'server.example.com', 'db': 'server.example.com', } assert host_roles('server.example.com') == ['app', 'db'] def it_returns_the_role_for_multiple_hosts_with_a_single_role(): env.roledefs = {'app': ['app1.example.com', 'app2.example.com']} assert host_roles('app1.example.com') == ['app'] assert host_roles('app2.example.com') == ['app'] def it_returns_the_role_for_multiple_hosts_with_multiple_roles(): env.roledefs = { 'app': ['app1.example.com', 'app2.example.com'], 'db': ['app1.example.com', 'app2.example.com'] } assert host_roles('app1.example.com') == ['app', 'db'] assert host_roles('app2.example.com') == ['app', 'db']
from pspec import describe, run from pspec.groups.contextmanager import ContextManagerGroup from fixtures import random_spec, single_level_spec, multiple_level_spec with describe('ContextManagerGroup'): def it_creates_a_spec_on_the_module(): assert hasattr(single_level_spec, '_pspec_group') def it_creates_a_group_with_a_subject(): assert isinstance(single_level_spec._pspec_group.children[0], ContextManagerGroup) assert single_level_spec._pspec_group.children[0].subject == 'random' def it_creates_tests_from_functions_within_context_manager(): spec = single_level_spec._pspec_group assert len(spec.tests) == 0 assert len(spec.children[0].tests) == 1 assert spec.children[0].tests[0].__name__ == 'it_chooses' def it_creates_children_with_a_subject(): parent = multiple_level_spec._pspec_group.children[0] assert len(parent.children) == 1 assert isinstance(parent.children[0], ContextManagerGroup) assert parent.children[0].subject == 'choice' def it_creates_tests_from_functions_as_a_child(): parent = multiple_level_spec._pspec_group.children[0] assert len(parent.children[0].tests) == 1 def it_does_not_collect_tests_that_belong_to_child_groups(): parent = multiple_level_spec._pspec_group.children[0]
from pspec import describe, assert_raises import random with describe('random'): with describe('shuffle'): def it_does_not_lose_any_elements(): seq = range(10) random.shuffle(seq) seq.sort() assert seq == range(10) def it_raises_an_exception_for_an_immutable_sequence(): with assert_raises(TypeError): random.shuffle((1, 2, 3)) with describe('choice'): def it_picks_an_element_that_is_in_the_sequence(): seq = range(10) assert random.choice(seq) in seq with describe('sample'): def it_raises_an_exception_for_a_sample_size_larger_than_the_sequence(): with assert_raises(ValueError): random.sample(range(10), 20) def it_chooses_elements_that_are_in_the_sequence(): seq = range(10) for element in random.sample(seq, 5): assert element in seq
from pspec import describe from attest import assert_hook from fabric.api import env from loom.config import host_roles, current_roles with describe("loom.config.host_roles"): def it_returns_the_role_for_a_host_with_a_single_role(): env.roledefs = {"app": "app.example.com"} assert host_roles("app.example.com") == ["app"] def it_returns_the_roles_for_a_host_with_multiple_roles(): env.roledefs = {"app": "server.example.com", "db": "server.example.com"} assert host_roles("server.example.com") == ["app", "db"] def it_returns_the_role_for_multiple_hosts_with_a_single_role(): env.roledefs = {"app": ["app1.example.com", "app2.example.com"]} assert host_roles("app1.example.com") == ["app"] assert host_roles("app2.example.com") == ["app"] def it_returns_the_role_for_multiple_hosts_with_multiple_roles(): env.roledefs = {"app": ["app1.example.com", "app2.example.com"], "db": ["app1.example.com", "app2.example.com"]} assert host_roles("app1.example.com") == ["app", "db"] assert host_roles("app2.example.com") == ["app", "db"] with describe("loom.config.current_roles"): def it_returns_the_roles_for_the_current_host(): env.roledefs = {"app": "app.example.com"} env.host_string = "app.example.com"
from pspec import describe from attest import assert_hook from fabric.api import env from loom.puppet import generate_site_pp, get_puppetmaster_host, _gem_install, generate_site_pp from mock import patch with describe('loom.puppet.get_puppetmaster_host'): def it_returns_env_puppetmaster_host_when_it_is_defined(): newenv = {'puppetmaster_host': 'master.example.com'} with patch.dict('fabric.api.env', newenv): assert 'master.example.com' == get_puppetmaster_host() def it_returns_the_host_in_the_puppetmaster_role(): newenv = {'roledefs': {'puppetmaster': ['master.example.com']}} with patch.dict('fabric.api.env', newenv): assert 'master.example.com' == get_puppetmaster_host() def it_returns_the_first_puppetmaster_host_when_multiple_are_defined(): newenv = { 'roledefs': { 'puppetmaster': ['master.example.com', 'master2.example.com'] } } with patch.dict('fabric.api.env', newenv): assert 'master.example.com' == get_puppetmaster_host() def it_returns_none_when_no_puppetmaster_is_defined(): newenv = {'roledefs': {'puppetmaster': []}} with patch.dict('fabric.api.env', newenv): assert None == get_puppetmaster_host()
from pspec import describe from attest import assert_hook from fabric.api import env from loom.puppet import generate_site_pp, get_puppetmaster_host, _gem_install, generate_site_pp from mock import patch with describe('loom.puppet.get_puppetmaster_host'): def it_returns_env_puppetmaster_host_when_it_is_defined(): newenv = {'puppetmaster_host': 'master.example.com'} with patch.dict('fabric.api.env', newenv): assert 'master.example.com' == get_puppetmaster_host() def it_returns_the_host_in_the_puppetmaster_role(): newenv = {'roledefs': {'puppetmaster': ['master.example.com']} } with patch.dict('fabric.api.env', newenv): assert 'master.example.com' == get_puppetmaster_host() def it_returns_the_first_puppetmaster_host_when_multiple_are_defined(): newenv = {'roledefs': {'puppetmaster': ['master.example.com', 'master2.example.com']} } with patch.dict('fabric.api.env', newenv): assert 'master.example.com' == get_puppetmaster_host() def it_returns_none_when_no_puppetmaster_is_defined(): newenv = {'roledefs': {'puppetmaster': []} } with patch.dict('fabric.api.env', newenv):
from pspec import describe with describe('this'): def it_should_not_be_here(): assert True
from fabric.api import env from pspec import describe from loom.tasks import all, ssh, upgrade from mock import patch, call with describe('loom.tasks.all'): def it_sets_env_hsots_to_contain_all_hosts_in_roledefs(): env.roledefs = { 'app1': ['app1.com', 'app2.com'], 'app2': ['app1.com', 'app2.com'], 'db': ['db.com'] } all() assert set(['app1.com', 'app2.com', 'db.com']) == set(env.hosts) with describe('loom.tasks.ssh'): @patch('loom.tasks.subprocess') def it_calls_ssh(mock): env.user = '******' env.host_string = 'example.com' env.key_filename = None ssh() expected = [call('ssh -A -t [email protected]'.split())] assert mock.call.call_args_list == expected
delete=True, extra_opts=' '.join(extra_opts), ssh_opts='-oStrictHostKeyChecking=no' ) ########NEW FILE######## __FILENAME__ = config_spec from pspec import describe from attest import assert_hook from fabric.api import env from loom.config import host_roles, current_roles with describe('loom.config.host_roles'): def it_returns_the_role_for_a_host_with_a_single_role(): env.roledefs = {'app': 'app.example.com'} assert host_roles('app.example.com') == ['app'] def it_returns_the_roles_for_a_host_with_multiple_roles(): env.roledefs = { 'app': 'server.example.com', 'db': 'server.example.com', } assert host_roles('server.example.com') == ['app', 'db'] def it_returns_the_role_for_multiple_hosts_with_a_single_role(): env.roledefs = {'app': ['app1.example.com', 'app2.example.com']} assert host_roles('app1.example.com') == ['app'] assert host_roles('app2.example.com') == ['app']
from pspec import describe import random with describe('random'): with describe('choice'): def it_chooses(): seq = range(10) assert random.choice(seq) in seq
extra_opts.append('--rsync-path="sudo rsync"') rsync_project(local_dir=src, remote_dir=dest, delete=True, extra_opts=' '.join(extra_opts), ssh_opts='-oStrictHostKeyChecking=no') ########NEW FILE######## __FILENAME__ = config_spec from pspec import describe from attest import assert_hook from fabric.api import env from loom.config import host_roles, current_roles with describe('loom.config.host_roles'): def it_returns_the_role_for_a_host_with_a_single_role(): env.roledefs = {'app': 'app.example.com'} assert host_roles('app.example.com') == ['app'] def it_returns_the_roles_for_a_host_with_multiple_roles(): env.roledefs = { 'app': 'server.example.com', 'db': 'server.example.com', } assert host_roles('server.example.com') == ['app', 'db'] def it_returns_the_role_for_multiple_hosts_with_a_single_role(): env.roledefs = {'app': ['app1.example.com', 'app2.example.com']} assert host_roles('app1.example.com') == ['app']
from pspec import describe import random with describe('random'): def it_chooses(): seq = range(10) assert random.choice(seq) in seq
from pspec import describe from loom.decorators import requires_puppet, REQUIRES_GEM from mock import patch from fabric.api import env with describe('loom.decorations.requires_puppet'): @patch('loom.decorators.has_librarian_installed') @patch('loom.decorators.has_puppet_installed') def it_does_not_do_anything_if_puppet_and_librarian_are_installed(puppet_mock, librarian_mock): env.host_string = 'app.example.com' puppet_mock.return_value = True librarian_mock.return_value = True requires_puppet(lambda: 1)() assert puppet_mock.called assert librarian_mock.called @patch('loom.decorators.has_librarian_installed') @patch('loom.decorators.has_puppet_installed') @patch('loom.decorators.abort') def it_aborts_if_puppet_is_not_installed(abort_mock, puppet_mock, librarian_mock): env.host_string = 'app.example.com' puppet_mock.return_value = False librarian_mock.return_value = True requires_puppet(lambda: 1)() assert puppet_mock.called assert REQUIRES_GEM.format(host=env.host_string, gem='puppet') == abort_mock.call_args[0][0]
from pspec import describe with describe('nothing'): def it_does_nothing(): pass
from pspec import describe with describe('something'): def it_does_something(): assert False
from pspec import describe from attest import assert_hook from fabric.api import env from loom.puppet import generate_site_pp with describe('generate_site_pp'): def it_creates_an_include_statement_for_each_role(): env.roledefs = { 'app': 'server.example.com', 'db': 'server.example.com', } env.host_string = 'server.example.com' site_pp = generate_site_pp() assert 'include "roles::app"' in site_pp assert 'include "roles::db"' in site_pp