def test_render_definition(monkeypatch, popen): senza = Senza('region') senza.logger = MagicMock() mock_named_tempfile = MagicMock() mock_tempfile = MagicMock() mock_tempfile.name = 'lizzy.yaml' mock_named_tempfile.__enter__.return_value = mock_tempfile mock_named_tempfile.return_value = mock_named_tempfile monkeypatch.setattr('tempfile.NamedTemporaryFile', mock_named_tempfile) senza.render_definition('yaml content', 'version42', 'imgversion22', ['Param1=app', 'SecondParam=3']) cmd = 'senza print --region region -o json --force lizzy.yaml version42 ' \ 'imgversion22 Param1=app SecondParam=3' popen.assert_called_with(cmd.split(" "), stdout=-1, stderr=-1) assert not senza.logger.error.called senza.render_definition('yaml content', None, 'imgversion22', ['Param1=app', 'SecondParam=3']) assert not senza.logger.error.called # test error case popen.side_effect = ExecutionError('', '') with pytest.raises(SenzaRenderError): senza.render_definition('yaml content', 'version42', 'imgversion22', ['Param1=app', 'SecondParam=3'])
def test_patch(monkeypatch, app, mock_senza): data = {'new_traffic': 50} # Only changes the traffic request = app.patch('/api/stacks/stack-1', headers=GOOD_HEADERS, data=json.dumps(data)) assert request.status_code == 202 mock_senza.traffic.assert_called_once_with(percentage=50, stack_name='stack', stack_version='1') assert request.headers['X-Lizzy-Version'] == CURRENT_VERSION assert request.headers['X-Senza-Version'] == SENZA_VERSION # Should return 500 when not possible to change the traffic # while running the one of the senza commands an error occurs # the error is exposed to the client mock_senza.traffic.side_effect = SenzaDomainsError('', '') request = app.patch('/api/stacks/stack-1', headers=GOOD_HEADERS, data=json.dumps(data)) assert request.status_code == 500 mock_senza.traffic.reset() # Does not change anything request = app.patch('/api/stacks/stack-1', headers=GOOD_HEADERS, data=json.dumps({})) assert request.status_code == 202 assert request.headers['X-Lizzy-Version'] == CURRENT_VERSION assert request.headers['X-Senza-Version'] == SENZA_VERSION # Run in a different region mock_senza.traffic.reset() data_for_another_region = {'new_traffic': 50, 'region': 'ee-foobar-8'} request = app.patch('/api/stacks/stack-1', headers=GOOD_HEADERS, data=json.dumps(data_for_another_region)) mock_senza.assert_called_with('ee-foobar-8') update_image = {'new_ami_image': 'ami-2323'} # Should change the AMI image used by the stack and respawn the instances # using the new AMI image. request = app.patch('/api/stacks/stack-1', headers=GOOD_HEADERS, data=json.dumps(update_image)) assert request.status_code == 202 assert request.headers['X-Lizzy-Version'] == CURRENT_VERSION assert request.headers['X-Senza-Version'] == SENZA_VERSION mock_senza.patch.assert_called_once_with('stack', '1', 'ami-2323') mock_senza.respawn_instances.assert_called_once_with('stack', '1') # Should return 500 when not possible to change the AMI image mock_senza.patch.side_effect = ExecutionError(1, 'fake error') request = app.patch('/api/stacks/stack-1', headers=GOOD_HEADERS, data=json.dumps(update_image)) assert request.status_code == 500
def test_application_status_endpoint_when_nok(app, mock_senza): os.environ['DEPLOYER_SCOPE'] = 'can_deploy' os.environ['TOKEN_URL'] = 'https://tokenservice.example.com' mock_senza.list = MagicMock(side_effect=ExecutionError('test', 'Error')) response = app.get('/api/status', headers=GOOD_HEADERS) assert response.status_code == 200 payload = json.loads(response.data.decode()) assert payload['status'] == 'NOK'
def test_delete_error(app, mock_senza, dry_run, force): data = {'dry_run': dry_run, 'force': force} mock_senza.remove.side_effect = ExecutionError('test', 'Error msg') request = app.delete('/api/stacks/stack-1', data=json.dumps(data), headers=GOOD_HEADERS) assert request.status_code == 500 # TODO test message problem = json.loads(request.data.decode()) assert problem['detail'] == "Error msg" mock_senza.remove.assert_called_once_with('stack-1', dry_run=dry_run, force=force)
def test_new_stack_execution_error(monkeypatch, app, mock_senza): mock_senza.render_definition.return_value = GOOD_CF_DEFINITION mock_senza.create.side_effect = ExecutionError(2, "error") data = {'keep_stacks': 0, 'new_traffic': 100, 'stack_version': '43', 'senza_yaml': 'SenzaInfo:\n StackName: abc', 'parameters': ['MintBucket=bk-bucket', 'ImageVersion=28'], 'dry_run': True} request = app.post('/api/stacks', headers=GOOD_HEADERS, data=json.dumps(data)) # type: flask.Response assert request.status_code == 500 error_data = json.loads(request.data.decode()) assert error_data['detail'] == 'error'
def test_domain(monkeypatch, popen): senza = Senza('region') senza.logger = MagicMock() domains = senza.domains() cmd = 'senza domains --region region -o json' senza.logger.debug.assert_called_with('Executing %s.', 'senza', extra={'command': cmd}) assert not senza.logger.error.called assert not senza.logger.exception.called popen.assert_called_with( ['senza', 'domains', '--region', 'region', '-o', 'json'], stdout=-1, stderr=-1) assert domains == {'stream': 'stdout'} senza.logger.reset_mock() popen.reset_mock() popen.communicate.return_value = b'{"test": "domain2"}', b'stderr' domains = senza.domains('lizzy') cmd = 'senza domains --region region -o json lizzy' senza.logger.debug.assert_called_with('Executing %s.', 'senza', extra={'command': cmd}) assert not senza.logger.error.called assert not senza.logger.exception.called popen.assert_called_with( ['senza', 'domains', '--region', 'region', '-o', 'json', 'lizzy'], stdout=-1, stderr=-1) assert domains == {'test': 'domain2'} # test error case popen.side_effect = ExecutionError('', '') with pytest.raises(SenzaDomainsError): senza.domains()
def test_traffic(monkeypatch, popen): senza = Senza('region') senza.logger = MagicMock() traffic = senza.traffic('lizzy', 'version42', 25) cmd = 'senza traffic --region region -o json lizzy version42 25' senza.logger.debug.assert_called_with('Executing %s.', 'senza', extra={'command': cmd}) assert not senza.logger.error.called assert not senza.logger.exception.called popen.assert_called_with([ 'senza', 'traffic', '--region', 'region', '-o', 'json', 'lizzy', 'version42', '25' ], stdout=-1, stderr=-1) assert traffic == {'stream': 'stdout'} # test error case popen.side_effect = ExecutionError('', '') with pytest.raises(SenzaTrafficError): senza.traffic('lizzy', 'version42', 25) # traffic listing popen.side_effect = None traffic = senza.traffic('lizzy', 'version42') popen.assert_called_with([ 'senza', 'traffic', '--region', 'region', '-o', 'json', 'lizzy', 'version42' ], stdout=-1, stderr=-1) # returns the output result assert traffic == {'stream': 'stdout'}
def test_scale(monkeypatch, popen): senza = Senza('region') senza.logger = MagicMock() senza.scale('lizzy', 'version42', 0) cmd = 'senza scale --region region lizzy version42 0 --force' senza.logger.debug.assert_called_with('Executing %s.', 'senza', extra={'command': cmd}) assert not senza.logger.error.called assert not senza.logger.exception.called popen.assert_called_with([ 'senza', 'scale', '--region', 'region', 'lizzy', 'version42', '0', '--force' ], stdout=-1, stderr=-2) # test error case popen.side_effect = ExecutionError('', '') with pytest.raises(SenzaScaleError): senza.scale('lizzy', 'version42', 0)
def test_patch(monkeypatch, popen): senza = Senza('region') senza.logger = MagicMock() senza.patch('lizzy', 'version42', 'latest') cmd = 'senza patch --region region -o json lizzy version42 --image=latest' senza.logger.debug.assert_called_with('Executing %s.', 'senza', extra={'command': cmd}) assert not senza.logger.error.called assert not senza.logger.exception.called popen.assert_called_with([ 'senza', 'patch', '--region', 'region', '-o', 'json', 'lizzy', 'version42', '--image=latest' ], stdout=-1, stderr=-1) # test error case popen.side_effect = ExecutionError('', '') with pytest.raises(SenzaPatchError): senza.patch('lizzy', 'version42', 'latest')
def test_respawn_instances(monkeypatch, popen): senza = Senza('region') senza.logger = MagicMock() senza.respawn_instances('lizzy', 'version42') cmd = 'senza respawn-instances --region region -o json lizzy version42' senza.logger.debug.assert_called_with('Executing %s.', 'senza', extra={'command': cmd}) assert not senza.logger.error.called assert not senza.logger.exception.called popen.assert_called_with([ 'senza', 'respawn-instances', '--region', 'region', '-o', 'json', 'lizzy', 'version42' ], stdout=-1, stderr=-1) # test error case popen.side_effect = ExecutionError('', '') with pytest.raises(SenzaRespawnInstancesError): senza.respawn_instances('lizzy', 'version42')
def test_exception(): try: raise ExecutionError(20, ' Output ') except ExecutionError as e: assert str(e) == '(20): Output'
def test_health_check_failing(app, mock_senza): mock_senza.list = MagicMock(side_effect=ExecutionError(2, "error")) response = app.get('/health') assert response.status_code == 500