def test_open_a8_run_script(scp, run, run_on_frontend):
    """Test run script on A8 nodes."""
    config_ssh = {
        'user': '******',
        'exp_id': 123,
    }
    screen = '{user}-{exp_id}'.format(**config_ssh)
    script = '/tmp/script.sh'
    remote_script = os.path.join('~/A8/.iotlabsshcli',
                                 os.path.basename(script))
    script_data = {'screen': screen, 'path': remote_script}
    return_value = {'0': 'test'}
    run.return_value = return_value

    ret = run_script(config_ssh,
                     _ROOT_NODES,
                     script,
                     run_on_frontend=run_on_frontend)

    assert ret == {'run-script': return_value}
    scp.assert_called_once_with(script, remote_script)
    assert run.call_count == 4

    run.mock_calls[0].assert_called_with(_MKDIR_DST_CMD.format(
        os.path.dirname(remote_script)),
                                         with_proxy=False)
    run.mock_calls[1].assert_called_with(_MAKE_EXECUTABLE_CMD.format(
        os.path.dirname(remote_script)),
                                         with_proxy=False)
    run.mock_calls[2].assert_called_with(
        _QUIT_SCRIPT_CMD.format(**script_data), with_proxy=not run_on_frontend)
    run.mock_calls[3].assert_called_with(_RUN_SCRIPT_CMD.format(**script_data),
                                         use_pty=False,
                                         with_proxy=not run_on_frontend)

    # Raise an exception
    run.side_effect = OpenA8SshAuthenticationException('test')
    ret = run_script(config_ssh, _ROOT_NODES, script)
    assert ret == {'run-script': {'1': _ROOT_NODES}}
def test_open_a8_flash_m3(scp, run):
    """Test flashing an M3."""
    config_ssh = {
        'user': '******',
        'exp_id': 123,
    }
    firmware = '/tmp/firmware.elf'
    remote_fw = os.path.join('~/A8/.iotlabsshcli', os.path.basename(firmware))
    return_value = {'0': 'test'}
    run.return_value = return_value

    ret = flash_m3(config_ssh, _ROOT_NODES, firmware)

    assert ret == {'flash-m3': return_value}
    scp.assert_called_once_with(firmware, remote_fw)
    assert run.call_count == 2
    run.mock_calls[0].assert_called_with(
        _MKDIR_DST_CMD.format(os.path.dirname(remote_fw)))
    run.mock_calls[1].assert_called_with(_UPDATE_M3_CMD.format(remote_fw))

    # Raise an exception
    run.side_effect = OpenA8SshAuthenticationException('test')
    ret = flash_m3(config_ssh, _ROOT_NODES, firmware)
    assert ret == {'flash-m3': {'1': _ROOT_NODES}}
def test_open_a8_copy_file(scp, run):
    """Test copy file on the SSH frontend."""
    config_ssh = {
        'user': '******',
        'exp_id': 123,
    }
    file_path = '/tmp/script.sh'
    remote_file = os.path.join('~/A8/.iotlabsshcli',
                               os.path.basename(file_path))
    return_value = {'0': 'test'}
    scp.return_value = return_value

    ret = copy_file(config_ssh, _ROOT_NODES, file_path)
    assert ret == {'copy-file': return_value}

    scp.assert_called_once_with(file_path, remote_file)
    run.assert_called_once_with(_MKDIR_DST_CMD.format(
        os.path.dirname(remote_file)),
                                with_proxy=False)

    # Raise an exception
    run.side_effect = OpenA8SshAuthenticationException('test')
    ret = copy_file(config_ssh, _ROOT_NODES, file_path)
    assert ret == {'copy-file': {'1': _ROOT_NODES}}