Beispiel #1
0
    def test_non_additive_requires_tags(self):
        """create_instances always_expand=False throws if tags aren't given"""

        # local imports of code-under-test ensure moto has mocks
        # registered before any possible calls out to AWS
        from awstools.awstools import launch_instances, run_block_device_dict

        # launch_instances requires vpc setup as done by firesim/scripts/setup_firesim.py
        from awstools.aws_setup import aws_setup
        aws_setup()

        type = 'f1.2xlarge'

        with pytest.raises(ValueError):
            launch_instances(type,
                             1,
                             instancemarket="ondemand",
                             spotinterruptionbehavior=None,
                             spotmaxprice=None,
                             blockdevices=run_block_device_dict(),
                             always_expand=False)
Beispiel #2
0
    def test_non_additive_instance_creation(self):
        """create_instances always_expand=False checks for existing instances when tags!=None"""

        # local imports of code-under-test ensure moto has mocks
        # registered before any possible calls out to AWS
        from awstools.awstools import launch_instances, run_block_device_dict

        # launch_instances requires vpc setup as done by firesim/scripts/setup_firesim.py
        from awstools.aws_setup import aws_setup
        aws_setup()

        type = 'f1.2xlarge'

        instances = launch_instances(type,
                                     1,
                                     instancemarket="ondemand",
                                     spotinterruptionbehavior=None,
                                     spotmaxprice=None,
                                     blockdevices=run_block_device_dict(),
                                     tags={'fsimcluster': 'testcluster'},
                                     always_expand=False)
        instances.should.have.length_of(1)

        instances = launch_instances(type,
                                     1,
                                     instancemarket="ondemand",
                                     spotinterruptionbehavior=None,
                                     spotmaxprice=None,
                                     blockdevices=run_block_device_dict(),
                                     tags={'fsimcluster': 'testcluster'},
                                     always_expand=False)
        instances.should.have.length_of(1)

        # There should be one instance total now, across one reservation
        ec2_client = boto3.client('ec2')
        desc = ec2_client.describe_instances()
        desc['ResponseMetadata']['HTTPStatusCode'].should.equal(200)
        desc['Reservations'].should.have.length_of(1)
        [i for r in desc['Reservations']
         for i in r['Instances']].should.have.length_of(1)
Beispiel #3
0
    def test_can_create_multiple_instance_tags(self):
        """Can pass multiple tags to launch_instances"""

        # local imports of code-under-test ensure moto has mocks
        # registered before any possible calls out to AWS
        from awstools.awstools import launch_instances, run_block_device_dict

        # launch_instances requires vpc setup as done by firesim/scripts/setup_firesim.py
        from awstools.aws_setup import aws_setup
        aws_setup()

        instances = launch_instances('f1.2xlarge',
                                     1,
                                     instancemarket="ondemand",
                                     spotinterruptionbehavior=None,
                                     spotmaxprice=None,
                                     blockdevices=run_block_device_dict(),
                                     tags={
                                         'fsimcluster': 'testcluster',
                                         'secondtag': 'secondvalue'
                                     })
        instances.shouldnt.be.empty

        ids = [i.id for i in instances]
        ids.shouldnt.be.empty

        ec2_client = boto3.client('ec2')
        desc = ec2_client.describe_instances(InstanceIds=ids)
        desc['ResponseMetadata']['HTTPStatusCode'].should.equal(200)
        tags = {
            t['Key']: t['Value']
            for t in desc['Reservations'][0]['Instances'][0]['Tags']
        }
        tags.should.have.key('fsimcluster')
        tags['fsimcluster'].should.equal('testcluster')
        tags.should.have.key('secondtag')
        tags['secondtag'].should.equal('secondvalue')
Beispiel #4
0
    def test_can_query_multiple_instance_tags(self):
        """get_instances_by_tag_type returns only instances matching all tags"""

        # local imports of code-under-test ensure moto has mocks
        # registered before any possible calls out to AWS
        from awstools.awstools import launch_instances, run_block_device_dict, get_instances_by_tag_type

        # launch_instances requires vpc setup as done by firesim/scripts/setup_firesim.py
        from awstools.aws_setup import aws_setup
        aws_setup()

        tag1 = {'fsimcluster': 'testcluster'}
        type = 'f1.2xlarge'

        # create an instance with only a single tag
        instances = launch_instances(type,
                                     1,
                                     instancemarket="ondemand",
                                     spotinterruptionbehavior=None,
                                     spotmaxprice=None,
                                     blockdevices=run_block_device_dict(),
                                     tags=tag1)
        instances.should.have.length_of(1)

        tag2 = {'secondtag': 'secondvalue'}
        # create an instance with additional tag
        instances = launch_instances(type,
                                     1,
                                     instancemarket="ondemand",
                                     spotinterruptionbehavior=None,
                                     spotmaxprice=None,
                                     blockdevices=run_block_device_dict(),
                                     tags={
                                         **tag1,
                                         **tag2
                                     })
        instances.shouldnt.be.empty

        # There should be two instances total now, across two reservations
        ec2_client = boto3.client('ec2')
        desc = ec2_client.describe_instances()
        desc['ResponseMetadata']['HTTPStatusCode'].should.equal(200)
        desc['Reservations'].should.have.length_of(2)
        [i for r in desc['Reservations']
         for i in r['Instances']].should.have.length_of(2)

        # get_instances_by_tag_type with both tags should only return one instance
        instances = get_instances_by_tag_type({**tag1, **tag2}, type)
        list(instances).should.have.length_of(1)

        # and that instance should be the one with both tags
        ids = [i.id for i in instances]
        ids.shouldnt.be.empty

        desc = ec2_client.describe_instances(InstanceIds=ids)
        desc['ResponseMetadata']['HTTPStatusCode'].should.equal(200)
        tags = {
            t['Key']: t['Value']
            for t in desc['Reservations'][0]['Instances'][0]['Tags']
        }
        tags.should.equal({**tag1, **tag2})

        # get_instances_by_tag_type with only the original tag should return both instances
        instances = get_instances_by_tag_type(tag1, type)
        list(instances).should.have.length_of(2)