def test_exception():
    # do directly to exception:
    with pytest.raises(FailedActivity,
                       match=r"configuration is not complete.*"):
        network_advanced(instance_ids=['CLIENT1', 'CLIENT2'],
                         execution_duration="1",
                         command="loss 5%")
    with pytest.raises(FailedActivity,
                       match=r"configuration is not complete.*"):
        burn_cpu(instance_ids=['CLIENT1'], execution_duration="1")
    with pytest.raises(FailedActivity,
                       match=r"configuration is not complete.*"):
        burn_io(instance_ids=['CLIENT1', 'CLIENT2'], execution_duration="1")
    with pytest.raises(FailedActivity,
                       match=r"configuration is not complete.*"):
        fill_disk(instance_ids=['CLIENT1'], execution_duration="1")
    with pytest.raises(FailedActivity,
                       match=r"configuration is not complete.*"):
        network_corruption(instance_ids=['CLIENT1', 'CLIENT2'],
                           execution_duration="1")
    with pytest.raises(FailedActivity,
                       match=r"configuration is not complete.*"):
        network_loss(instance_ids=['CLIENT1', 'CLIENT2'],
                     execution_duration="1")
    with pytest.raises(FailedActivity,
                       match=r"configuration is not complete.*"):
        network_latency(instance_ids=['CLIENT1'], execution_duration="1")
def test_network_advanced_on_linux(init, open):
    # mock
    client = MagicMock()
    init.return_value = client

    client.get_grains_get.return_value = {'CLIENT1': "Linux"}
    client.async_run_cmd.return_value = "20190830103239148771"
    client.get_async_cmd_result.return_value = {'CLIENT1': "Stressing CLIENT1 1 CPUs for 180 seconds.\nStressing 1 CPUs for 180 seconds. Done\nexperiment strees_cpu <CLIENT1> -> success"}
    client.async_cmd_exit_success.return_value = {'CLIENT1': True}

    # do
    network_advanced(instance_ids=['CLIENT1'], execution_duration="1", command="loss 5%")

    open.assert_called_with(AnyStringWith("network_advanced.sh"))
    client.get_grains_get.assert_called_with(['CLIENT1'], 'kernel')
    client.async_run_cmd.assert_called_with('CLIENT1', 'cmd.run', AnyStringWith('script'))
    client.async_cmd_exit_success.assert_called_with('20190830103239148771')
    client.get_async_cmd_result.assert_called_with('20190830103239148771')
def test_network_advanced_on_linux_two_error_in_execution(init, open):
    # mock
    client = MagicMock()
    init.return_value = client

    client.get_grains_get.return_value = {
        'CLIENT1': "Linux",
        'CLIENT2': "Linux"
    }
    client.async_run_cmd.return_value = "20190830103239148772"
    client.get_async_cmd_result.return_value = {
        'CLIENT1':
        "Stressing CLIENT1 1 CPUs for 180 seconds.\nStressing 1 CPUs for 180 seconds. Done\nexperiment strees_cpu <CLIENT1> -> success",
        'CLIENT2': "experiment strees_cpu <CLIENT2> -> success"
    }
    client.async_cmd_exit_success.return_value = {
        'CLIENT1': True,
        'CLIENT2': False
    }

    # do
    with pytest.raises(FailedActivity,
                       match=r"One of experiments are failed among.*"):
        network_advanced(instance_ids=['CLIENT1', 'CLIENT2'],
                         execution_duration="1",
                         command="loss 5%")
    # assert
    open.assert_called_with(AnyStringWith("network_advanced.sh"))
    client.get_grains_get.assert_called_with(['CLIENT1', 'CLIENT2'], 'kernel')
    assert client.async_run_cmd.mock_calls == [
        call('CLIENT1', 'cmd.run', AnyStringWith('script')),
        call('CLIENT2', 'cmd.run', AnyStringWith('script'))
    ]
    assert client.async_cmd_exit_success.mock_calls == [
        call('20190830103239148772'),
        call('20190830103239148772')
    ]
    assert client.get_async_cmd_result.mock_calls == [
        call('20190830103239148772'),
        call('20190830103239148772')
    ]