Esempio n. 1
0
def test_api_publish_event(datetime_mock):
    # arrange
    context = {'context': 'context_value'}
    state = {'state': 'state_value'}
    datetime_mock.utcnow.return_value = datetime(2020, 6, 19, 9, 12, 49,
                                                 663318)
    settings = data.provide_settings()
    session = Mock(Session)

    # act
    publish_event('test_event', context, state, settings, session)

    # assert
    expected_data = json.dumps({
        'name':
        "test_event",
        'state':
        state,
        'context':
        context,
        'timestamp':
        datetime(2020, 6, 19, 9, 12, 49, 663318).isoformat()
    })
    session.post.assert_called_once_with('/v1/executions/1234/events',
                                         data=expected_data,
                                         timeout=30)
Esempio n. 2
0
def test_action_cancel_all_attack(get_loaded_settings, app_api, session):
    # arrange
    get_loaded_settings.return_value = data.provide_settings()

    # act
    actions.cancel_attack()

    app_api.get_applications.assert_not_called()
    app_api.cancel_attack.assert_called_once_with([], ANY)
Esempio n. 3
0
def test_action_start_attack_no_application(get_loaded_settings, app_api,
                                            session):
    # arrange
    get_loaded_settings.return_value = data.provide_settings()
    app_api.get_applications.return_value = []

    # act
    with pytest.raises(ActivityFailed) as e:
        actions.start_attack('application_1', 'delay', '10')
    assert str(e.value) == 'Application application_1 not found.'
Esempio n. 4
0
def test_action_cancel_attack(get_loaded_settings, app_api, session):
    # arrange
    get_loaded_settings.return_value = data.provide_settings()
    app_api.get_applications.return_value = [{'id': '1234'}]

    # act
    result = actions.cancel_attack('application_1')

    assert len(result) == 1
    assert result[0].get('id') == '1234'
    app_api.cancel_attack.assert_called_once_with(['1234'], ANY)
Esempio n. 5
0
def test_probe_was_alert_fired_no_alerts(get_loaded_settings, get_alerts,
                                         get_alert_rule, session):
    # arrange
    get_alert_rule.return_value = {"latest_alert": {"status": "resolved"}}
    get_alerts.return_value = []
    get_loaded_settings.return_value = data.provide_settings()

    # act
    result = probes.was_alert_fired('rule_1')

    assert result is False
    get_alert_rule.assert_called_once_with('rule_1', ANY)
    get_alerts.assert_called_once_with('rule_1', 'fired', ANY, ANY, ANY)
Esempio n. 6
0
def test_before_experiment_control(push_execution, session):
    # arrange
    experiment = data.provide_experiment()
    settings = data.provide_settings()
    push_execution.return_value = {"id": "12345"}

    # act
    controls.before_experiment_control(context=experiment,
                                       configuration={},
                                       secrets={},
                                       settings=settings)

    assert settings.get('run_context').get('execution').get('id') == "12345"
    push_execution.assert_called_once_with(settings, ANY)
Esempio n. 7
0
def test_before_method_control(publish_event, session):
    # arrange
    experiment = data.provide_experiment()
    settings = data.provide_settings()

    # act
    controls.before_method_control(context=experiment,
                                   state=None,
                                   configuration={},
                                   secrets={},
                                   settings=settings)

    publish_event.assert_called_once_with("before-method", experiment, None,
                                          settings, ANY)
Esempio n. 8
0
def test_before_hypothesis_control(publish_event, session):
    # arrange
    settings = data.provide_settings()
    hypothesis = {"hypothesis": "hippo"}

    # act
    controls.before_hypothesis_control(context=hypothesis,
                                       state=None,
                                       configuration={},
                                       secrets={},
                                       settings=settings)

    publish_event.assert_called_once_with("before-hypothesis", hypothesis,
                                          None, settings, ANY)
Esempio n. 9
0
def test_before_activity_control(publish_event, session):
    # arrange
    settings = data.provide_settings()
    activity = {"activity": "my_activity"}

    # act
    controls.before_activity_control(context=activity,
                                     state=None,
                                     configuration={},
                                     secrets={},
                                     settings=settings)

    publish_event.assert_called_once_with("before-activity", activity, None,
                                          settings, ANY)
Esempio n. 10
0
def test_after_hypothesis_control(publish_event, session):
    # arrange
    settings = data.provide_settings()
    hypothesis = {"hypothesis": "hippo"}
    hypothesis_outcome = {"result": "maybe"}

    # act
    controls.after_hypothesis_control(context=hypothesis,
                                      state=hypothesis_outcome,
                                      configuration={},
                                      secrets={},
                                      settings=settings)

    publish_event.assert_called_once_with("after-hypothesis", hypothesis,
                                          hypothesis_outcome, settings, ANY)
Esempio n. 11
0
def test_after_experiment_control(publish_event, session):
    # arrange
    experiment = data.provide_experiment()
    settings = data.provide_settings()
    journal = data.provide_journal()

    # act
    controls.after_experiment_control(context=experiment,
                                      state=journal,
                                      configuration={},
                                      secrets={},
                                      settings=settings)

    publish_event.assert_called_once_with("after-experiment", None, journal,
                                          settings, ANY)
Esempio n. 12
0
def test_after_activity_control(publish_event, session):
    # arrange
    settings = data.provide_settings()
    activity = {"activity": "my_activity"}
    activity_outcome = {"result": "maybe"}

    # act
    controls.after_activity_control(context=activity,
                                    state=activity_outcome,
                                    configuration={},
                                    secrets={},
                                    settings=settings)

    publish_event.assert_called_once_with("after-activity", activity,
                                          activity_outcome, settings, ANY)
Esempio n. 13
0
def test_after_rollback_control(publish_event, session):
    # arrange
    experiment = data.provide_experiment()
    settings = data.provide_settings()
    method_outcome = [{"result": "maybe"}]

    # act
    controls.after_rollback_control(context=experiment,
                                    state=method_outcome,
                                    configuration={},
                                    secrets={},
                                    settings=settings)

    publish_event.assert_called_once_with("after-rollback", experiment,
                                          method_outcome, settings, ANY)
Esempio n. 14
0
def test_probe_was_alert_fired_fired_during_experiment(get_loaded_settings,
                                                       get_alerts,
                                                       get_alert_rule,
                                                       session):
    # arrange
    get_alert_rule.return_value = {"latest_alert": {"status": "resolved"}}

    get_alerts.return_value = [{"alert_time": "2020-07-23T19:49:59Z"}]
    get_loaded_settings.return_value = data.provide_settings()

    # act
    result = probes.was_alert_fired('rule_1')

    assert result is True
    get_alert_rule.assert_called_once_with('rule_1', ANY)