Coverage for test_ec2: 100%

# Coverage for test_ec2 : 100%

## 31 statements 31 run 0 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | # -*- coding: utf-8 -*- import boto3 import pytest from moto import mock_ec2 from jungle import cli @pytest.yield_fixture(scope='function') def ec2(): """EC2 mock service""" mock = mock_ec2() mock.start() ec2 = boto3.resource('ec2') server = ec2.create_instances(ImageId='ami-xxxxx', MinCount=1, MaxCount=1) servers = ec2.create_instances(ImageId='ami-xxxxx', MinCount=2, MaxCount=2) for i, s in enumerate(servers): ec2.create_tags( Resources=[s.id], Tags=[{'Key': 'Name', 'Value': 'server{:0>2d}'.format(i)}]) yield {'ec2': ec2, 'servers': servers, 'server': server[0]} mock.stop() def test_ec2_up(runner, ec2): """jungle ec2 up test""" result = runner.invoke(cli.cli, ['ec2', 'up', '-i', ec2['server'].id]) assert result.exit_code == 0 def test_ec2_down(runner, ec2): """jungle ec2 up test""" result = runner.invoke(cli.cli, ['ec2', 'down', '-i', ec2['server'].id]) assert result.exit_code == 0 @pytest.mark.parametrize('arg, expected_server_names', [ ('*', ['server00', 'server01']), ('server01', ['server01']), ('fake-server', []), ]) def test_ec2_ls(runner, ec2, arg, expected_server_names): """jungle ec2 ls test""" result = runner.invoke(cli.cli, ['ec2', 'ls', arg]) assert result.exit_code == 0 assert expected_server_names == [x for x in expected_server_names if x in result.output] @pytest.mark.parametrize('opt, arg, expected_server_names', [ ('-l', '*', ['server00', 'server01']), ('-l', 'server01', ['server01']), ('-l', 'fake-server', []), ]) def test_ec2_ls_formatted(runner, ec2, opt, arg, expected_server_names): """jungle ec2 ls test""" result = runner.invoke(cli.cli, ['ec2', 'ls', opt, arg]) assert result.exit_code == 0 assert expected_server_names == [x for x in expected_server_names if x in result.output] @pytest.mark.parametrize('tags, key, expected', [ ([{'Key': 'Name', 'Value': 'server01'}, {'Key': 'env', 'Value': 'prod'}], 'Name', 'server01'), ([{'Key': 'Name', 'Value': 'server01'}, {'Key': 'env', 'Value': 'prod'}], 'env', 'prod'), ([{'Key': 'Name', 'Value': 'server01'}, {'Key': 'env', 'Value': 'prod'}], 'dummy', ''), ([], 'dummy', ''), (None, 'dummy', ''), ]) def test_get_tag_value(tags, key, expected): """get_tag_value utility test""" from jungle.ec2 import get_tag_value assert get_tag_value(tags, key) == expected