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_newrelic_instances(): newrelic_obj = NewRelicInventory('account_number','api_key','/tmp/dummy/path') instances = newrelic_obj.instances() expected_instances = [Instance(name=u'ip-172-16-223-200', address=u'172.16.223.200', source='newrelic'), Instance(name=u'i-04267e627f88362ed-DEV-self-formsvc', address=u'172.16.221.211', source='newrelic'), Instance(name=u'i-0f9a3f0d9399a6c17-PROD-prfsvclmt', address=u'172.16.225.232', source='newrelic')] assert set(instances) == set(expected_instances)
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_inventory_set(mocker): test_dir = os.path.dirname(os.path.abspath(__file__)) cache_dir = os.path.join(test_dir, 'aws_stubs') aws_obj = get_aws_inventory(name='aws') inventorySet = InventorySet() inventorySet.add(aws_obj) inventorySet.add(aws_obj) print(aws_obj.instances()) all_instances = inventorySet.instances() aws_instances = [ Instance(name=u'test-forms', address=u'devbox', aliases=(u'devbox', u'ip-172-31-8-185.us-west-2.compute.internal', u'i-e54cbaeb'), source='aws (aws)', container_id=None, type='VM'), Instance(name=u'devlab-forms', address=u'devbox', aliases=(u'devbox', u'ip-172-31-0-138.us-west-2.compute.internal', u'i-f7d726f9'), source='aws (aws)', container_id=None, type='VM'), Instance(name=u'test-account-svc', address=u'devbox', aliases=(u'devbox', u'ip-172-31-0-139.us-west-2.compute.internal', u'i-f4d726fa'), source='aws (aws)', container_id=None, type='VM'), Instance(name=u'devlab-pubsrv', address=u'devbox', aliases=(u'devbox', u'ip-172-31-0-142.us-west-2.compute.internal', u'i-f5d726fb'), source='aws (aws)', container_id=None, type='VM'), Instance(name=u'devlab-game-svc', address=u'devbox', aliases=(u'devbox', u'ip-172-31-0-140.us-west-2.compute.internal', u'i-f2d726fc'), source='aws (aws)', container_id=None, type='VM'), Instance(name=u'test-game-svc', address=u'devbox', aliases=(u'devbox', u'ip-172-31-0-141.us-west-2.compute.internal', u'i-f3d726fd'), source='aws (aws)', container_id=None, type='VM'), Instance(name=u'test-pubsrv', address=u'devbox', aliases=(u'devbox', u'ip-172-31-2-38.us-west-2.compute.internal', u'i-0f500447384e95942'), source='aws (aws)', container_id=None, type='VM'), Instance(name=u'test-pubsrv', address=u'devbox', aliases=(u'devbox', u'ip-172-31-2-39.us-west-2.compute.internal', u'i-0f500447384e95943'), source='aws (aws)', container_id=None, type='VM') ] expected_instances = aws_instances + aws_instances assert len(all_instances) == len(expected_instances) assert set(all_instances) == set(expected_instances)
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_csv_instances(mocker): mock_open = mocker.patch("%s.open" % builtin_mock, mock.mock_open(read_data=DATA)) mock_csv_reader = mocker.patch("csv.DictReader") # for whatever reason mock_open is not sufficent since the DictReader will return nothing # so mocking the csv reader is necessary ret = [] for line in DATA.split("\n"): ret.append(dict(zip(['name', 'address', 'random'], line.split("|")))) mock_csv_reader.return_value = ret csv_obj = CsvInventory('/tmp/dummy/path', ' name,address, random ', ' | ') instances = csv_obj.instances() print(instances) expected_instances = [ Instance(name='devenv-pubsrv', address='13.14.15.16', source='csv', container_id=None, type='VM'), Instance(name='testenv-pubsrv', address='1.2.3.4', source='csv', container_id=None, type='VM'), Instance(name='devenv-pubsrv', address='9.10.11.12', source='csv', container_id=None, type='VM'), Instance(name='testenv-pubsrv', address='5.6.7.8', source='csv', container_id=None, type='VM'), Instance(name='testenv-formsvc', address='17.18.19.20', source='csv', container_id=None, type='VM') ] assert set(instances) == set(expected_instances)
def test_newrelic_instances(): newrelic_obj = NewRelicInventory('account_number', 'api_key', '/tmp/dummy/path') instances = newrelic_obj.instances() expected_instances = [ Instance(name=u'coolcucumber', address=None, aliases=None, source='acct:account_number (newrelic)', container_id=u'cc3456789098765432', type='ECS'), Instance(name=u'ip-172-16-223-200', address=u'172.16.223.200', aliases=None, source='acct:account_number (newrelic)', container_id=None, type='VM'), Instance(name=u'i-0f9a3f0d9399a6c17-PROD-prfsvclmt', address=u'172.16.225.232', aliases=None, source='acct:account_number (newrelic)', container_id=None, type='VM'), Instance(name=u'awesomecontainer', address=u'672.16.223.200', aliases=None, source='acct:account_number (newrelic)', container_id=u'123456789098765432', type='ECS'), Instance(name=u'ardvark?', address=None, aliases=None, source='acct:account_number (newrelic)', container_id=u'dd3456789098765432', type='ECS'), Instance(name=u'i-04267e627f88362ed-DEV-self-formsvc', address=u'172.16.221.211', aliases=None, source='acct:account_number (newrelic)', container_id=None, type='VM') ] assert set(instances) == set(expected_instances)
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0 udev /dev devtmpfs rw,nosuid,relatime,size=16359216k,nr_inodes=4089804,mode=755 0 0 devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0 tmpfs /run tmpfs rw,nosuid,noexec,relatime,size=3276836k,mode=755 0 0 tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0 tmpfs /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k 0 0 tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,mode=755 0 0 cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd 0 0 pstore /sys/fs/pstore pstore rw,nosuid,nodev,noexec,relatime 0 0 efivarfs /sys/firmware/efi/efivars efivarfs rw,nosuid,nodev,noexec,relatime 0 0 tmpfs /run/user/1000 tmpfs rw,nosuid,nodev,relatime,size=3276836k,mode=700,uid=1000,gid=1000 0 0 gvfsd-fuse /run/user/1000/gvfs fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0 ubuntu@devbox:/tmp /home/dummy/.bridgy/mounts/awesomebox@devbox fuse.sshfs rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0 ubuntu@devbox:/tmp /home/dummy/someotherdir/awesomebox@devbox fuse.sshfs rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0""" instance = Instance('name', 'address.com') whitespace_pattern = re.compile(r'\W+') def assert_command_results(result1, result2): result1 = shlex.split(result1) result2 = shlex.split(result2) assert len(result1) == len(result2) for item1, item2 in zip(result1, result2): item1 = whitespace_pattern.sub(' ', item1) item2 = whitespace_pattern.sub(' ', item2) assert item1 == item2
def test_aws_instances_profile(mocker): test_dir = os.path.dirname(os.path.abspath(__file__)) cache_dir = os.path.join(test_dir, 'aws_stubs') config_dir = os.path.join(test_dir, 'aws_configs') aws_obj = AwsInventory(cache_dir=cache_dir, profile='somewhere', region='region', config_path=config_dir) instances = aws_obj.instances() expected_instances = [ Instance(name='test-forms', address='devbox', aliases=('devbox', 'ip-172-31-8-185.us-west-2.compute.internal', 'i-e54cbaeb'), source='aws', container_id=None, type='VM'), 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='test-account-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-139.us-west-2.compute.internal', 'i-f4d726fa'), 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'), Instance(name='test-game-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-141.us-west-2.compute.internal', 'i-f3d726fd'), source='aws', container_id=None, type='VM'), Instance(name='test-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-2-38.us-west-2.compute.internal', 'i-0f500447384e95942'), source='aws', container_id=None, type='VM'), Instance(name='test-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-2-39.us-west-2.compute.internal', 'i-0f500447384e95943'), source='aws', container_id=None, type='VM') ] assert set(instances) == set(expected_instances) from botocore.exceptions import ProfileNotFound with pytest.raises(ProfileNotFound): aws_obj = AwsInventory(cache_dir=cache_dir, profile='some-unconfigured-profile', region='region', config_path=config_dir)
def test_aws_instances(mocker): test_dir = os.path.dirname(os.path.abspath(__file__)) cache_dir = os.path.join(test_dir, 'aws_stubs') 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') instances = aws_obj.instances() expected_instances = [ Instance(name='test-forms', address='devbox', aliases=('devbox', 'ip-172-31-8-185.us-west-2.compute.internal', 'i-e54cbaeb'), source='aws', container_id=None, type='VM'), 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='test-account-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-139.us-west-2.compute.internal', 'i-f4d726fa'), 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'), Instance(name='test-game-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-141.us-west-2.compute.internal', 'i-f3d726fd'), source='aws', container_id=None, type='VM'), Instance(name='test-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-2-38.us-west-2.compute.internal', 'i-0f500447384e95942'), source='aws', container_id=None, type='VM'), Instance(name='test-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-2-39.us-west-2.compute.internal', 'i-0f500447384e95943'), source='aws', container_id=None, type='VM') ] assert set(instances) == set(expected_instances)