def test_stress_cpu_on_win(init, fetch, open):
    # arrange mocks
    client = MagicMock()
    init.return_value = client
    resource = __get_resource(os_type='Windows')
    resource_list = [resource]
    fetch.return_value = resource_list
    # run command mocks
    poller = MagicMock()
    client.virtual_machines.run_command.return_value = poller
    result = MagicMock(spec=RunCommandResult)
    poller.result.return_value = result
    result.value = [InstanceViewStatus()]

    # act
    stress_cpu("where name=='some_windows_machine'", 60)

    # assert
    fetch.assert_called_with("where name=='some_windows_machine'",
                             "Microsoft.Compute/virtualMachines", None, None)
    open.assert_called_with(AnyStringWith("cpu_stress_test.ps1"))
    client.virtual_machines.run_command.assert_called_with(
        resource['resourceGroup'], resource['name'], {
            'command_id': 'RunPowerShellScript',
            'script': ['script'],
            'parameters': [{
                'name': 'duration',
                'value': 60
            }]
        })
def test_stress_cpu_on_lnx(init, fetch, open):
    # arrange mocks
    client = MagicMock()
    init.return_value = client
    resource = __get_resource(os_type='Linux')
    resource_list = [resource]
    fetch.return_value = resource_list
    # run command mocks
    poller = MagicMock()
    client.virtual_machines.run_command.return_value = poller
    result = MagicMock(spec=RunCommandResult)
    poller.result.return_value = result
    result.value = [InstanceViewStatus()]

    # act
    stress_cpu(filter="where name=='some_linux_machine'",
               duration=60,
               configuration=CONFIG,
               secrets=SECRETS)

    # assert
    fetch.assert_called_with("where name=='some_linux_machine'",
                             "Microsoft.Compute/virtualMachines", SECRETS,
                             CONFIG)
    open.assert_called_with(AnyStringWith("cpu_stress_test.sh"))
    client.virtual_machines.run_command.assert_called_with(
        resource['resourceGroup'], resource['name'], {
            'command_id': 'RunShellScript',
            'script': ['script'],
            'parameters': [{
                'name': 'duration',
                'value': 60
            }]
        })
def test_stress_cpu(mocked_command_run, mocked_command_prepare, fetch):
    # arrange mocks
    mocked_command_prepare.return_value = 'RunShellScript', 'cpu_stress_test.sh'

    machine = machine_provider.provide_machine()
    machines = [machine]
    fetch.return_value = machines

    config = config_provider.provide_default_config()
    secrets = secrets_provider.provide_secrets_via_service_principal()

    # act
    stress_cpu(filter="where name=='some_linux_machine'",
               duration=60,
               timeout=60,
               configuration=config,
               secrets=secrets)

    # assert
    fetch.assert_called_with("where name=='some_linux_machine'", RES_TYPE_VM,
                             secrets, config)
    mocked_command_prepare.assert_called_with(machine, 'cpu_stress_test')
    mocked_command_run.assert_called_with(
        machine['resourceGroup'], machine, 120, {
            'command_id': 'RunShellScript',
            'script': ['cpu_stress_test.sh'],
            'parameters': [
                {
                    'name': "duration",
                    'value': 60
                },
            ]
        }, secrets, config)
def test_stress_cpu_invalid_resource(init, fetch, open):
    # arrange mocks
    client = MagicMock()
    init.return_value = client
    resource = __get_resource(os_type='Invalid')
    resource_list = [resource]
    fetch.return_value = resource_list

    # act
    with pytest.raises(Exception) as ex:
        stress_cpu("where name=='some_machine'", 60)
    assert str(ex.value) == 'Unknown OS Type: invalid'
Exemple #5
0
def test_stress_cpu_timeout(init, fetch, open):
    # arrange mocks
    client = MagicMock()
    init.return_value = client
    resource = __get_resource(os_type='Windows')
    resource_list = [resource]
    fetch.return_value = resource_list
    # run command mocks
    poller = MagicMock()
    client.virtual_machines.run_command.return_value = poller
    poller.result.return_value = None

    # act & assert
    with pytest.raises(FailedActivity, match=r'stress_cpu operation did not '
                                             r'finish on time'):
        stress_cpu("where name=='some_windows_machine'", 60)