def test_detect_config_schema_by_list(): config = Config({ 'config-schema': '1' }) assert config.config_template_path == 'sample_config_1.yml' config = Config({ 'config-schema': '2' }) assert config.config_template_path == 'sample_config_2.yml'
def test_hostfile_gocase(mocker): config = Config({ 'ansible': { 'become_user': '******', 'become_method': 'sudo' }, 'bastion': { 'address': 'bastion.com', 'options': '-C -o ServerAliveInterval=255' } }) instances = [ Instance(name='devenv-pubsrv', address='13.14.15.16'), Instance(name='testenv-pubsrv', address='1.2.3.4'), Instance(name='testenv-formsvc', address='17.18.19.20') ] task = RunAnsiblePlaybook('name', 'playbook', config, instances) contents = task._build_host_file_contents() assert contents.strip() == """\ devenv-pubsrv ansible_host=13.14.15.16 ansible_ssh_common_args="-o ProxyCommand='ssh -C -o ServerAliveInterval=255 -W %h:%p bastion.com' " testenv-pubsrv ansible_host=1.2.3.4 ansible_ssh_common_args="-o ProxyCommand='ssh -C -o ServerAliveInterval=255 -W %h:%p bastion.com' " testenv-formsvc ansible_host=17.18.19.20 ansible_ssh_common_args="-o ProxyCommand='ssh -C -o ServerAliveInterval=255 -W %h:%p bastion.com' "\ """.strip()
def test_ssh_command_bastion_missing_address(): config = Config({ 'bastion': {} }) with pytest.raises(MissingBastionHost): sshObj = Ssh(config, instance) sshObj.command
def test_ssh_command_no_user(): config = Config({ 'ssh': { 'options': '-C -o ServerAliveInterval=255' } }) sshObj = Ssh(config, instance) assert_command_results(sshObj.command, 'ssh -C -o ServerAliveInterval=255 address.com')
def test_ssh_command_user(): config = Config({ 'ssh': { 'user': '******' } }) sshObj = Ssh(config, instance) assert_command_results(sshObj.command, 'ssh [email protected]')
def test_sshfs_command_user(): config = Config({'ssh': {'user': '******'}}) remotedir = '/tmp' sshObj = Sshfs(config, instance, remotedir) mount_arg = '%s/%s@%s' % (config.mount_root_dir, instance.name, instance.address) assert_command_results(sshObj.command, 'sshfs [email protected]:/tmp %s' % mount_arg)
def test_sshfs_command_go_case_no_options(): config = Config({}) remotedir = '/tmp' sshObj = Sshfs(config, instance, remotedir) mount_arg = '%s/%s@%s' % (config.mount_root_dir, instance.name, instance.address) assert_command_results(sshObj.command, 'sshfs address.com:/tmp %s' % mount_arg)
def test_ssh_command_bastion_options(): config = Config({ 'bastion': { 'address': 'bastion.com', 'options': '-C -o ServerAliveInterval=255' } }) sshObj = Ssh(config, instance) assert_command_results(sshObj.command, "ssh -o ProxyCommand='ssh -C -o ServerAliveInterval=255 -W %h:%p bastion.com' address.com")
def test_sshfs_command_no_user(): config = Config({'sshfs': {'options': '-C -o ServerAliveInterval=255'}}) remotedir = '/tmp' sshObj = Sshfs(config, instance, remotedir) mount_arg = '%s/%s@%s' % (config.mount_root_dir, instance.name, instance.address) assert_command_results( sshObj.command, 'sshfs -C -o ServerAliveInterval=255 address.com:/tmp %s' % mount_arg)
def test_ssh_command_bastion_user(): config = Config({ 'bastion': { 'address': 'bastion.com', 'user': '******' } }) sshObj = Ssh(config, instance) assert_command_results(sshObj.command, "ssh -o ProxyCommand='ssh -W %h:%p [email protected]' address.com")
def test_detect_config_schema_indirectly(): config = Config({ 'inventory': { 'source': 'csv' } }) assert config.config_template_path == 'sample_config_1.yml' config = Config({ 'inventory': { 'source': [ {'type': 'csv'}, {'type': 'aws'}, ] } }) assert config.config_template_path == 'sample_config_2.yml'
def test_ssh_command_options(): config = Config({ 'ssh': { 'user': '******', 'options': '-C -o ServerAliveInterval=255' } }) sshObj = Ssh(config, instance) assert_command_results( sshObj.command, 'ssh -C -o ServerAliveInterval=255 -t [email protected]')
def test_inclusion_filtering(mocker): test_dir = os.path.dirname(os.path.abspath(__file__)) cache_dir = os.path.join(test_dir, 'aws_stubs') config = Config({'inventory': {'include_pattern': 'test.*'}}) aws_obj = AwsInventory(cache_dir=cache_dir, access_key_id='access_key_id', secret_access_key='secret_access_key', session_token='session_token', region='region') mock_inventory = mocker.patch.object(bridgy.inventory, 'inventory') mock_inventory.return_value = aws_obj all_instances = instances(config) expected_instances = [ Instance(name='test-forms', address='devbox', aliases=('devbox', 'ip-172-31-8-185.us-west-2.compute.internal', 'i-e54cbaeb'), source='aws'), Instance(name='test-account-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-139.us-west-2.compute.internal', 'i-f4d726fa'), source='aws'), Instance(name='test-game-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-141.us-west-2.compute.internal', 'i-f3d726fd'), source='aws'), Instance(name='test-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-2-38.us-west-2.compute.internal', 'i-0f500447384e95942'), source='aws'), Instance(name='test-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-2-39.us-west-2.compute.internal', 'i-0f500447384e95943'), source='aws') ] assert set(all_instances) == set(expected_instances)
def test_sshfs_command_mountoptions(): config = Config({ 'sshfs': { 'options': '-o auto_cache,reconnect,defer_permissions,noappledouble,nolocalcaches,no_readahead' } }) remotedir = '/tmp' sshObj = Sshfs(config, instance, remotedir) mount_arg = '%s/%s@%s' % (config.mount_root_dir, instance.name, instance.address) assert_command_results( sshObj.command, "sshfs %s address.com:/tmp %s" % (config.dig('sshfs', 'options'), mount_arg))
def test_sshfs_command_bastion_user(): config = Config( {'bastion': { 'address': 'bastion.com', 'user': '******' }}) remotedir = '/tmp' sshObj = Sshfs(config, instance, remotedir) mount_arg = '%s/%s@%s' % (config.mount_root_dir, instance.name, instance.address) assert_command_results( sshObj.command, "sshfs -o ProxyCommand='ssh -W %%h:%%p [email protected]' address.com:/tmp %s" % mount_arg)
def test_sshfs_unmount_fuse_failure(mocker): mock_rmdir = mocker.patch.object(os, 'rmdir') mock_system = mocker.patch.object(os, 'system') mock_exists = mocker.patch.object(os.path, 'exists') mock_exists.return_value = True mock_system.return_value = 1 mock_rmdir.return_value = True config = Config({}) sshfsObj = Sshfs(config, instance, remotedir='/tmp/test') success = sshfsObj.unmount() assert mock_rmdir.call_count == 0 assert success == False
def test_sshfs_command_bastion_options(): config = Config({ 'bastion': { 'address': 'bastion.com', 'options': '-C -o ServerAliveInterval=255' } }) remotedir = '/tmp' sshObj = Sshfs(config, instance, remotedir) mount_arg = '%s/%s@%s' % (config.mount_root_dir, instance.name, instance.address) assert_command_results( sshObj.command, "sshfs -o ProxyCommand='ssh -C -o ServerAliveInterval=255 -W %%h:%%p bastion.com' address.com:/tmp %s" % mount_arg)
def test_exclusion_filtering(mocker): test_dir = os.path.dirname(os.path.abspath(__file__)) cache_dir = os.path.join(test_dir, 'aws_stubs') config = Config({'inventory': {'exclude_pattern': 'test.*'}}) aws_obj = AwsInventory(cache_dir=cache_dir, access_key_id='access_key_id', secret_access_key='secret_access_key', session_token='session_token', region='region') inventorySet = InventorySet() inventorySet.add(aws_obj) mock_inventory = mocker.patch.object(bridgy.inventory, 'inventory') mock_inventory.return_value = inventorySet all_instances = instances(config) expected_instances = [ Instance(name='devlab-forms', address='devbox', aliases=('devbox', 'ip-172-31-0-138.us-west-2.compute.internal', 'i-f7d726f9'), source='aws', container_id=None, type='VM'), Instance(name='devlab-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-0-142.us-west-2.compute.internal', 'i-f5d726fb'), source='aws', container_id=None, type='VM'), Instance(name='devlab-game-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-140.us-west-2.compute.internal', 'i-f2d726fc'), source='aws', container_id=None, type='VM') ] assert set(all_instances) == set(expected_instances)
def test_sshfs_mount_failed(mocker): mock_ls = mocker.patch.object(os, 'listdir') mock_rmdir = mocker.patch('os.rmdir', side_effect=lambda x: True) mock_system = mocker.patch('os.system', side_effect=lambda x: 1) mock_mkdir = mocker.patch('os.mkdir', side_effect=lambda x: True) mock_exists = mocker.patch('os.path.exists', side_effect=lambda x: False) mock_ls.return_value = [ '/home/dummy/.bridgy/mounts/baddir', '/home/dummy/.bridgy/mounts/awesomebox@devbox' ] config = Config({}) sshObj = Sshfs(config, instance, remotedir='/tmp/test') sshObj.mount() assert mock_exists.called assert mock_mkdir.called assert mock_system.called assert mock_rmdir.called
def test_sshfs_options(): config = Config({ 'ssh': { 'user': '******', 'options': '-o ForwardAgent=yes' }, 'sshfs': { 'options': '-C -o ServerAliveInterval=255' } }) remotedir = '/tmp' sshObj = Sshfs(config, instance, remotedir) mount_arg = '%s/%s@%s' % (config.mount_root_dir, instance.name, instance.address) assert_command_results( sshObj.command, 'sshfs -C -o ServerAliveInterval=255 [email protected]:/tmp %s' % mount_arg)
def test_sshfs_mount_remotedir_missing(): config = Config({}) sshObj = Sshfs(config, instance) with pytest.raises(BadRemoteDir): sshObj.mount()
def test_sshfs_command_null_instance(): config = Config({}) remotedir = '/tmp' with pytest.raises(BadInstanceError): sshObj = Sshfs(config, None) sshObj.command
def test_sshfs_command_bastion_missing_address(): config = Config({'bastion': {}}) remotedir = '/tmp' with pytest.raises(MissingBastionHost): sshObj = Sshfs(config, instance, remotedir) sshObj.command
def test_ssh_command_null_instance(): config = Config({}) with pytest.raises(BadInstanceError): sshObj = Ssh(config, None) sshObj.command
def test_ssh_command_go_case(): config = Config({'ssh': {}}) sshObj = Ssh(config, instance) assert_command_results(sshObj.command, 'ssh -t address.com')
def test_ssh_command_go_case_no_options(): config = Config({}) sshObj = Ssh(config, instance) assert_command_results(sshObj.command, 'ssh -t address.com')