def test_can_give_current_user_access_to_share(self, mocked_call): lxd_container = unittest.mock.Mock() host = Host(lxd_container) host.give_current_user_access_to_share('.') assert mocked_call.call_count == 1 assert mocked_call.call_args[0][0] == 'setfacl -Rdm u:{}:rwX .'.format( os.getuid())
def test_uidgid(self, mock_getuid, mock_getgid): mock_getuid.return_value = 10000 mock_getgid.return_value = 10001 host = Host() assert host.uidgid(), (10000, 10001)
def test_can_give_mapped_user_access_to_share(self, mocked_stat, mocked_call): class MockedContainer(object): name = 'test' mocked_stat.return_value = unittest.mock.MagicMock(st_uid='19958953') host = Host() host.give_mapped_user_access_to_share(FakeContainer(), '.', '.') assert mocked_call.call_count == 1 assert mocked_call.call_args[0] == ( 'setfacl -Rm user:lxd:rwx,default:user:lxd:rwx,user:19958953:rwx,default:user:19958953' ':rwx .',)
def test_can_set_module_path(self, mock_run, mock_copy_dir): class DummyGuest(Guest): name = 'dummy' host = Host() guest = DummyGuest(unittest.mock.Mock()) mock_run.return_value = 0 provisioner = PuppetProvisioner( './', host, [guest], { 'manifest_file': 'mani.pp', 'manifests_path': 'test_manifests', 'module_path': 'test-puppet-modules' }) provisioner.provision() assert mock_copy_dir.call_count == 2 assert (Path('test-puppet-modules'), PurePosixPath(provisioner._guest_module_path)) in { mock_copy_dir.call_args_list[0][0], mock_copy_dir.call_args_list[1][0] } assert mock_run.call_count == 2 assert mock_run.call_args_list[1][0][0] == [ 'sh', '-c', "puppet apply --modulepath {}:{} --detailed-exitcodes --manifestdir {} {}" .format( PurePosixPath(provisioner._guest_module_path), PurePosixPath(provisioner._guest_default_module_path), PurePosixPath(provisioner._guest_manifests_path), PurePosixPath(provisioner._guest_manifests_path) / 'mani.pp') ]
def test_can_run_puppet_manifest_mode(self, mock_run, mock_copy_dir): class DummyGuest(Guest): name = 'dummy' host = Host() guest = DummyGuest(unittest.mock.Mock()) mock_run.return_value = 0 provisioner = PuppetProvisioner('./', host, [guest], { 'manifest_file': 'test_site.pp', 'manifests_path': 'test_manifests' }) provisioner.provision() assert mock_copy_dir.call_count == 1 assert mock_copy_dir.call_args_list[0][0][0] == Path('test_manifests') assert mock_copy_dir.call_args_list[0][0][1] == PurePosixPath( provisioner._guest_manifests_path) assert mock_run.call_count == 2 assert mock_run.call_args_list[0][0][0] == ['which', 'puppet'] assert mock_run.call_args_list[1][0][0] == [ 'sh', '-c', 'puppet apply --detailed-exitcodes --manifestdir {} {}'.format( PurePosixPath(provisioner._guest_manifests_path), PurePosixPath(provisioner._guest_manifests_path) / 'test_site.pp') ]
def test_can_run_puppet_environment_mode(self, mock_run, mock_copy_dir): class DummyGuest(Guest): name = 'dummy' host = Host() guest = DummyGuest(unittest.mock.Mock()) mock_run.return_value = 0 provisioner = PuppetProvisioner( './', host, [guest], { 'environment': 'test_production', 'environment_path': 'test_environments' }) provisioner.provision() assert mock_copy_dir.call_count == 1 assert mock_copy_dir.call_args_list[0][0][0] == Path( 'test_environments') assert mock_copy_dir.call_args_list[0][0][1] == PurePosixPath( provisioner._guest_environment_path) assert mock_run.call_count == 2 assert mock_run.call_args_list[0][0][0] == ['which', 'puppet'] assert mock_run.call_args_list[1][0][0] == [ 'sh', '-c', 'puppet apply --detailed-exitcodes ' '--environmentpath {} --environment {}'.format( PurePosixPath(provisioner._guest_environment_path), 'test_production') ]
def test_can_set_hiera_config_path(self, mock_run, mock_copy_dir, mock_copy_file): class DummyGuest(Guest): name = 'dummy' host = Host() guest = DummyGuest(unittest.mock.Mock()) mock_run.return_value = 0 provisioner = PuppetProvisioner( './', host, [guest], { 'manifest_file': 'site.pp', 'manifests_path': 'test_manifests', 'hiera_config_path': 'hiera.yaml' }) provisioner.provision() assert mock_copy_file.call_count == 1 assert mock_copy_file.call_args_list[0][0][0] == Path('hiera.yaml') assert mock_copy_file.call_args_list[0][0][1] == PurePosixPath( provisioner._guest_hiera_file) assert mock_run.call_count == 2 assert mock_run.call_args_list[1][0][0] == [ 'sh', '-c', "puppet apply --hiera_config={} --detailed-exitcodes --manifestdir {} {}" .format( PurePosixPath(provisioner._guest_hiera_file), PurePosixPath(provisioner._guest_manifests_path), PurePosixPath(provisioner._guest_manifests_path) / 'site.pp') ]
def test_inventory_with_lxd_transport(self): c = FakeContainer(name='c1') provisioner = AnsibleProvisioner('./', Host(), [DebianGuest(c)], { 'playbook': 'deploy.yml', 'lxd_transport': True }) inv = provisioner.get_inventory() assert 'ansible_host={}'.format(c.lxd_name) in inv
def test_can_run_commands_on_the_host_side(self, mock_popen): host = Host(unittest.mock.Mock()) guest = DebianGuest(unittest.mock.Mock()) provisioner = ShellProvisioner('./', host, guest, { 'inline': 'touch f', 'side': 'host', }) provisioner.provision() assert mock_popen.call_args[0] == ('touch f', )
def test_can_run_a_script_on_the_host_side(self, mock_popen): host = Host(unittest.mock.Mock()) guest = DebianGuest(unittest.mock.Mock()) provisioner = ShellProvisioner('./', host, guest, { 'script': 'test.sh', 'side': 'host', }) provisioner.provision() assert mock_popen.call_args[0] == ('./test.sh', )
def test_can_run_commands_on_the_guest_side(self): lxd_container = unittest.mock.Mock() lxd_container.execute.return_value = ('ok', 'ok', '') host = Host(unittest.mock.Mock()) guest = DebianGuest(lxd_container) provisioner = ShellProvisioner('./', host, guest, {'inline': 'echo TEST'}) provisioner.provision() assert lxd_container.execute.call_count == 1 assert lxd_container.execute.call_args_list[0][0] == (['echo', 'TEST'], )
def test_can_run_commands_on_the_guest_side(self): container = FakeContainer() lxd_container = container._container host = Host() guest = DebianGuest(container) cmd = """touch f && echo "Here's the PATH" $PATH >> /tmp/test.txt""" provisioner = ShellProvisioner('./', host, [guest], {'inline': cmd}) provisioner.provision() assert lxd_container.execute.call_count == 1 assert lxd_container.execute.call_args_list[0][0] == ([ 'sh', '-c', cmd ], )
def test_can_run_ansible_playbooks(self, mock_popen): host = Host(unittest.mock.Mock()) guest = DebianGuest(unittest.mock.Mock()) lxd_state = unittest.mock.Mock() lxd_state.network.__getitem__ = unittest.mock.MagicMock( return_value={'addresses': [{'family': 'init', 'address': '0.0.0.0', }, ]}) guest.lxd_container.state.return_value = lxd_state provisioner = AnsibleProvisioner('./', host, guest, {'playbook': 'deploy.yml'}) provisioner.provision() assert re.match( 'ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook --inventory-file /[/\w]+ ' './deploy.yml', mock_popen.call_args[0][0])
def test_can_properly_setup_ssh_for_alpine_guests(self): lxd_container = unittest.mock.Mock() lxd_container.execute.return_value = ('ok', 'ok', '') host = Host(unittest.mock.Mock()) guest = AlpineGuest(lxd_container) provisioner = AnsibleProvisioner('./', host, guest, {'playbook': 'deploy.yml'}) provisioner.setup() assert lxd_container.execute.call_count == 4 assert lxd_container.execute.call_args_list[0][0] == (['apk', 'update'], ) assert lxd_container.execute.call_args_list[1][0] == \ (['apk', 'add'] + AnsibleProvisioner.guest_required_packages_alpine, ) assert lxd_container.execute.call_args_list[2][0] == (['rc-update', 'add', 'sshd'], ) assert lxd_container.execute.call_args_list[3][0] == (['/etc/init.d/sshd', 'start'], )
def test_can_run_commands_on_the_host_side(self, mock_popen): host = Host() guest = DebianGuest(FakeContainer()) provisioner = ShellProvisioner( './', host, [guest], { 'inline': """touch f && echo "Here's the PATH" $PATH >> /tmp/test.txt""", 'side': 'host', }) provisioner.provision() assert mock_popen.call_args[0] == ( """sh -c 'touch f && echo "Here'"'"'s the PATH" $PATH >> /tmp/test.txt'""", )
def test_raise_error_if_puppet_is_not_found(self, mock_run, mock_copy_file): class DummyGuest(Guest): name = 'dummy' host = Host() guest = DummyGuest(unittest.mock.Mock()) mock_run.return_value = 1 # Mock the error provisioner = PuppetProvisioner('./', host, [guest], { 'manifest_file': 'test_site.pp', 'manifests_path': 'test_manifests' }) with pytest.raises(ProvisionFailed): provisioner.provision() assert mock_run.call_count == 1 assert mock_run.call_args[0] == (['which', 'puppet'], ) assert mock_copy_file.call_count == 0
def test_can_run_a_script_on_the_guest_side(self, mock_open): container = FakeContainer() lxd_container = container._container host = Host() guest = DebianGuest(container) provisioner = ShellProvisioner('./', host, [guest], { 'script': 'test.sh', }) provisioner.provision() assert lxd_container.execute.call_count == 2 assert lxd_container.execute.call_args_list[0][0] == ([ 'chmod', '+x', '/tmp/test.sh', ], ) assert lxd_container.execute.call_args_list[1][0] == ([ '/tmp/test.sh', ], )
def test_can_run_a_script_on_the_guest_side(self, mock_open): lxd_container = unittest.mock.Mock() lxd_container.execute.return_value = ('ok', 'ok', '') host = Host(unittest.mock.Mock()) guest = DebianGuest(lxd_container) provisioner = ShellProvisioner('./', host, guest, { 'script': 'test.sh', }) provisioner.provision() assert lxd_container.execute.call_count == 2 assert lxd_container.execute.call_args_list[0][0] == ([ 'chmod', '+x', '/tmp/test.sh', ], ) assert lxd_container.execute.call_args_list[1][0] == ([ '/tmp/test.sh', ], )
def test_can_properly_setup_ssh_for_alpine_guests(self): container = FakeContainer() lxd_container = container._container lxd_container.execute.return_value = ('ok', 'ok', '') host = Host() guest = AlpineGuest(container) provisioner = AnsibleProvisioner('./', host, [guest], {'playbook': 'deploy.yml'}) provisioner.setup() EXPECTED = [ ['apk', 'update'], ['apk', 'add'] + AnsibleProvisioner.guest_required_packages_alpine, ['apk', 'update'], ['apk', 'add', 'openssh'], ['rc-update', 'add', 'sshd'], ['/etc/init.d/sshd', 'start'], ] calls = [tup[0][0] for tup in lxd_container.execute.call_args_list] assert calls == EXPECTED
def test_inventory_contains_groups(self): c1 = FakeContainer(name='c1') c2 = FakeContainer(name='c2') # c3 is deliberately not part of our guests list. This is to test that it doesn't end up # in the inventory and result in spurious unreachable hosts. These situations can happen # in two ways: errors in the config file, or guest filtering in the command line ("lxdock # provision c1, c2" for example). provisioner = AnsibleProvisioner( './', Host(), [DebianGuest(c1), DebianGuest(c2)], { 'playbook': 'deploy.yml', 'groups': { 'g1': ['c1', 'c2'], 'g2': ['c1', 'c3'] } }) inv = provisioner.get_inventory() # group order is not guaranteed. our tests have to be written with that in mind. groups = re.findall(r'\[(g1|g2)\]([^[]+)', inv, re.MULTILINE) # we sort so that g1 will be first all the time groups = sorted([(gname, hosts.strip()) for gname, hosts in groups]) assert sorted(groups) == [('g1', 'c1\nc2'), ('g2', 'c1')]
def test_can_set_options(self, mock_run, mock_copy_dir): class DummyGuest(Guest): name = 'dummy' host = Host() guest = DummyGuest(unittest.mock.Mock()) mock_run.return_value = 0 provisioner = PuppetProvisioner( './', host, [guest], { 'manifest_file': 'site.pp', 'manifests_path': 'test_manifests', 'options': '--a --c="test space" --b' }) provisioner.provision() assert mock_run.call_count == 2 assert mock_run.call_args_list[1][0][0] == [ 'sh', '-c', """puppet apply --a '--c=test space' --b """ """--detailed-exitcodes --manifestdir {} {}""".format( PurePosixPath(provisioner._guest_manifests_path), PurePosixPath(provisioner._guest_manifests_path) / 'site.pp') ]
def test_can_run_ansible_playbooks(self, mock_popen, options, expected_cmdargs): host = Host() guest = DebianGuest(FakeContainer()) lxd_state = unittest.mock.Mock() lxd_state.network.__getitem__ = unittest.mock.MagicMock(return_value={ 'addresses': [ { 'family': 'init', 'address': '0.0.0.0', }, ] }) guest.lxd_container.state.return_value = lxd_state options['playbook'] = 'deploy.yml' provisioner = AnsibleProvisioner('./', host, [guest], options) provisioner.provision() m = re.match( r'ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook --inventory-file /[/\w]+ ' r'(.*)\s?./deploy.yml', mock_popen.call_args[0][0]) assert m assert m.group(1).strip() == expected_cmdargs
def test_can_set_environment_variables(self, mock_run, mock_copy_dir): class DummyGuest(Guest): name = 'dummy' host = Host() guest = DummyGuest(unittest.mock.Mock()) mock_run.return_value = 0 provisioner = PuppetProvisioner( './', host, [guest], { 'manifest_file': 'site.pp', 'manifests_path': 'test_manifests', 'environment_variables': { 'FOO': 'bah', 'BAR': 'baz baz' } }) provisioner.provision() assert mock_run.call_count == 2 assert mock_run.call_args_list[1][0][0] == [ 'sh', '-c', "BAR='baz baz' FOO=bah " "puppet apply --detailed-exitcodes --manifestdir {} {}".format( PurePosixPath(provisioner._guest_manifests_path), PurePosixPath(provisioner._guest_manifests_path) / 'site.pp') ]
def test_can_return_ssh_pubkey(self, mock_open): host = Host() assert host.get_ssh_pubkey() assert mock_open.call_count == 1
def test_can_return_ssh_pubkey(self, mock_open): lxd_container = unittest.mock.Mock() host = Host(lxd_container) assert host.get_ssh_pubkey() assert mock_open.call_count == 1