def test_deploy_status(monkeypatch): deploy_id = 'deploy-id-x' response_json = { "response": { "items": [{ 'id': deploy_id, 'status': 'STARTING', 'currentBatch': 13, 'numOfBatches': 20, 'progress': { 'unit': 'percentage', 'value': 65 } }] } } with responses.RequestsMock() as rsps: spotinst_account_data = SpotInstAccountData('act-zwk', 'fake-token') elastigroup_id = 'sig-xfy' rsps.add(rsps.GET, '{}/aws/ec2/group/{}/roll/{}?accountId={}'.format( SPOTINST_API_URL, elastigroup_id, deploy_id, spotinst_account_data.account_id), status=200, json=response_json) deploy_status_response = deploy_status(deploy_id, elastigroup_id, spotinst_account_data)[0] assert deploy_status_response['id'] == deploy_id assert deploy_status_response['numOfBatches'] == 20 assert deploy_status_response['progress']['value'] == 65
def test_update_capacity(monkeypatch): update = { 'response': { 'items': [{ 'id': 'sig-xfy', 'name': 'my-app-1', 'capacity': { 'minimum': 1, 'maximum': 3, 'target': 3, 'unit': 'instance' } }] } } elastigroup_id = 'sig-xfy' spotinst_account_data = SpotInstAccountData('act-zwk', 'fake-token') with responses.RequestsMock() as rsps: rsps.add(rsps.PUT, '{}/aws/ec2/group/{}?accountId={}'.format( SPOTINST_API_URL, elastigroup_id, spotinst_account_data.account_id), status=200, json=update) update_response = update_capacity(1, 3, 3, elastigroup_id, spotinst_account_data)[0] assert update_response['id'] == elastigroup_id assert update_response['name'] == 'my-app-1' assert update_response['capacity']['minimum'] == 1 assert update_response['capacity']['maximum'] == 3 assert update_response['capacity']['target'] == 3
def test_deploy(monkeypatch): response_json = { "response": { "items": [{ 'id': 'deploy-id', 'status': 'STARTING', 'currentBatch': 1, 'numOfBatches': 1, 'progress': { 'unit': 'percentage', 'value': 0 } }] } } with responses.RequestsMock() as rsps: spotinst_account_data = SpotInstAccountData('act-zwk', 'fake-token') elastigroup_id = 'sig-xfy' rsps.add(rsps.PUT, '{}/aws/ec2/group/{}/roll?accountId={}'.format( SPOTINST_API_URL, elastigroup_id, spotinst_account_data.account_id), status=200, json=response_json) deploy_response = deploy( batch_size=35, grace_period=50, elastigroup_id=elastigroup_id, spotinst_account_data=spotinst_account_data)[0] assert deploy_response['id'] == 'deploy-id' assert deploy_response['status'] == 'STARTING' assert deploy_response['numOfBatches'] == 1
def test_patch_user_data_wrong_type_elastigroup(monkeypatch): spotinst_account_data = SpotInstAccountData('act-zwk', 'fake-token') elastigroup_id = 'sig-xfy' properties = {'UserData': "it's a string"} group = { 'compute': { 'launchSpecification': { 'userData': codecs.encode(b'#firstline\nsource: oldsource', 'base64').decode('utf-8'), 'imageId': 'myoldimage' }, 'instanceTypes': { 'ondemand': 'myoldinstancetyoe' } } } with pytest.raises(InvalidUserDataType) as exc_info: patch_elastigroup(group, properties, elastigroup_id, spotinst_account_data) assert str(exc_info.value) == ('Current user data is a map but provided ' 'user data is a string.')
def test_respawn_elastigroup(monkeypatch): elastigroup_id = 'sig-xfy' stack_name = 'my-app-stack' region = 'my-region' batch_size = 35 spotinst_account = SpotInstAccountData('act-zwk', 'fake-token') spotinst_account_mock = MagicMock() spotinst_account_mock.return_value = spotinst_account monkeypatch.setattr( 'senza.spotinst.components.elastigroup_api.get_spotinst_account_data', spotinst_account_mock) deploy_output = [{'id': 'deploy-1'}] deploy_output_mock = MagicMock() deploy_output_mock.return_value = deploy_output monkeypatch.setattr('senza.spotinst.components.elastigroup_api.deploy', deploy_output_mock) execution_data = {'percentage': 0, 'runs': 0, 'status': 'starting'} def deploy_status(*args): execution_data['runs'] += 1 execution_data['percentage'] += 50 if execution_data['percentage'] == 100: execution_data['status'] = 'finished' else: execution_data['status'] = 'in_progress' return [{ 'id': args[0], 'status': execution_data['status'], 'progress': { 'value': execution_data['percentage'] } }] monkeypatch.setattr( 'senza.spotinst.components.elastigroup_api.deploy_status', deploy_status) respawn_elastigroup(elastigroup_id, stack_name, region, batch_size) assert execution_data['runs'] == 2 assert execution_data['percentage'] == 100
def test_patch_elastigroup(monkeypatch): spotinst_account_data = SpotInstAccountData('act-zwk', 'fake-token') elastigroup_id = 'sig-xfy' new_lc = {} def create_lc(properties_to_patch, *args): new_lc.update(properties_to_patch) monkeypatch.setattr( 'senza.spotinst.components.elastigroup_api.patch_elastigroup', create_lc) properties = { 'ImageId': 'mynewimage', 'InstanceType': 'mynewinstancetyoe', 'UserData': { 'source': 'newsource >' } } group = { 'compute': { 'launchSpecification': { 'userData': base64.b64encode('#firstline\nsource: oldsource\n'.encode( 'utf-8')).decode('utf-8'), 'imageId': 'myoldimage' }, 'instanceTypes': { 'ondemand': 'myoldinstancetyoe' } } } changed = patch_elastigroup(group, properties, elastigroup_id, spotinst_account_data) assert changed assert new_lc['ImageId'] == 'mynewimage' assert new_lc['UserData'] == base64.b64encode( '#firstline\nsource: newsource >\n'.encode('utf-8')).decode('utf-8') assert new_lc['InstanceType'] == 'mynewinstancetyoe'
def test_patch_elastigroup(monkeypatch): patch = { 'ImageId': 'image-foo', 'InstanceType': 'm1.micro', 'UserData': 'user-data-value' } update_response = { 'response': { 'items': [{ 'compute': { 'instanceTypes': { 'ondemand': 'm1.micro', 'spot': ['m1.micro'] }, 'launchSpecification': { 'imageId': 'image-foo', 'userData': 'user-data-value' } } }] } } with responses.RequestsMock() as rsps: spotinst_account_data = SpotInstAccountData('act-zwk', 'fake-token') elastigroup_id = 'sig-xfy' rsps.add(rsps.PUT, '{}/aws/ec2/group/{}?accountId={}'.format( SPOTINST_API_URL, elastigroup_id, spotinst_account_data.account_id), status=200, json=update_response) patch_response = patch_elastigroup(patch, elastigroup_id, spotinst_account_data)[0] assert patch_response['compute']['launchSpecification'][ 'imageId'] == 'image-foo' assert patch_response['compute']['instanceTypes'][ 'ondemand'] == 'm1.micro' assert patch_response['compute']['launchSpecification'][ 'userData'] == 'user-data-value'
def test_get_elastigroup(monkeypatch): group = { 'response': { 'items': [{ 'id': 'sig-xfy', 'name': 'my-app-1', 'region': 'eu-central-1', }] } } elastigroup_id = 'sig-xfy' spotinst_account_data = SpotInstAccountData('act-zwk', 'fake-token') with responses.RequestsMock() as rsps: rsps.add(rsps.GET, '{}/aws/ec2/group/{}?accountId={}'.format( SPOTINST_API_URL, elastigroup_id, spotinst_account_data.account_id), status=200, json=group) group = get_elastigroup(elastigroup_id, spotinst_account_data)[0] assert group['id'] == elastigroup_id assert group['name'] == 'my-app-1'