Esempio n. 1
0
def test_self_update_has_update_ok(mocked_requests):
    """Calling self_update() should self-update the CLI script."""
    mocked_requests.register_uri(
        'HEAD',
        DEBMONITOR_CLIENT_URL,
        status_code=200,
        headers={cli.CLIENT_VERSION_HEADER: DEBMONITOR_CLIENT_VERSION})
    mocked_requests.register_uri('GET',
                                 DEBMONITOR_CLIENT_URL,
                                 status_code=200,
                                 text='data',
                                 headers={
                                     cli.CLIENT_VERSION_HEADER:
                                     DEBMONITOR_CLIENT_VERSION,
                                     cli.CLIENT_CHECKSUM_HEADER:
                                     DEBMONITOR_CLIENT_CHECKSUM
                                 })

    with patch('builtins.open', mock_open()) as mocked_open:
        cli.self_update(DEBMONITOR_BASE_URL, None)

        mocked_open.assert_called_once_with(os.path.realpath(cli.__file__),
                                            mode='w')
        mocked_handler = mocked_open()
        mocked_handler.write.assert_called_once_with('data')

    assert mocked_requests.called
Esempio n. 2
0
def test_self_update_has_update_ok(mocked_requests):
    """Calling self_update() should self-update the CLI script."""
    mocked_requests.register_uri(
        'HEAD',
        DEBMONITOR_CLIENT_URL,
        status_code=200,
        headers={cli.CLIENT_VERSION_HEADER: tests_deb.CLIENT_VERSION})
    mocked_requests.register_uri('GET',
                                 DEBMONITOR_CLIENT_URL,
                                 status_code=200,
                                 text=tests_deb.CLIENT_BODY_DUMMY_1,
                                 headers={
                                     cli.CLIENT_VERSION_HEADER:
                                     tests_deb.CLIENT_VERSION,
                                     cli.CLIENT_CHECKSUM_HEADER:
                                     tests_deb.CLIENT_CHECKSUM_DUMMY_1
                                 })

    with mock.patch('{mod}.open'.format(mod=BUILTINS),
                    mock.mock_open(),
                    create=True) as mocked_open:
        cli.self_update(DEBMONITOR_BASE_URL, None, True)

        mocked_open.assert_called_once_with(os.path.realpath(cli.__file__),
                                            mode='w')
        mocked_handler = mocked_open()
        mocked_handler.write.assert_called_once_with(
            tests_deb.CLIENT_BODY_DUMMY_1)

    assert mocked_requests.called
Esempio n. 3
0
def test_self_update_head_fail(mocked_requests):
    """Calling self_update() when the HEAD request to DebMonitor fail should raise RuntimeError."""
    mocked_requests.register_uri('HEAD',
                                 DEBMONITOR_CLIENT_URL,
                                 status_code=500)
    with pytest.raises(RuntimeError,
                       match='Unable to check remote script version'):
        cli.self_update(DEBMONITOR_BASE_URL, None, True)

    assert mocked_requests.called
Esempio n. 4
0
def test_self_update_head_no_header(mocked_requests):
    """Calling self_update() when the HEAD request is missing the expected header should raise RuntimeError."""
    mocked_requests.register_uri('HEAD',
                                 DEBMONITOR_CLIENT_URL,
                                 status_code=200)
    with pytest.raises(RuntimeError,
                       match='No header {header} value found'.format(
                           header=cli.CLIENT_VERSION_HEADER)):
        cli.self_update(DEBMONITOR_BASE_URL, None, True)

    assert mocked_requests.called
Esempio n. 5
0
def test_self_update_has_update_fail(mocked_requests):
    """Calling self_update() when the GET request to DebMonitor fail should raise RuntimeError."""
    mocked_requests.register_uri(
        'HEAD',
        DEBMONITOR_CLIENT_URL,
        status_code=200,
        headers={cli.CLIENT_VERSION_HEADER: tests_deb.CLIENT_VERSION})
    mocked_requests.register_uri('GET', DEBMONITOR_CLIENT_URL, status_code=500)

    with pytest.raises(RuntimeError, match='Unable to download remote script'):
        cli.self_update(DEBMONITOR_BASE_URL, None, True)

    assert mocked_requests.called
Esempio n. 6
0
def test_self_update_head_same_version(mocked_requests):
    """Calling self_update() when client on DebMonitor is at the same version should return without doing anything."""
    mocked_requests.register_uri(
        'HEAD',
        DEBMONITOR_CLIENT_URL,
        status_code=200,
        headers={cli.CLIENT_VERSION_HEADER: cli.__version__})

    ret = cli.self_update(DEBMONITOR_BASE_URL, None, True)

    assert ret is None
    assert mocked_requests.called
Esempio n. 7
0
def test_self_update_has_update_wrong_hash(mocked_requests):
    """Calling self_update() when the checksum mismatch should raise RuntimeError."""
    mocked_requests.register_uri(
        'HEAD',
        DEBMONITOR_CLIENT_URL,
        status_code=200,
        headers={cli.CLIENT_VERSION_HEADER: tests_deb.CLIENT_VERSION})
    mocked_requests.register_uri('GET',
                                 DEBMONITOR_CLIENT_URL,
                                 status_code=200,
                                 text=tests_deb.CLIENT_BODY_DUMMY_1,
                                 headers={
                                     cli.CLIENT_VERSION_HEADER:
                                     tests_deb.CLIENT_VERSION,
                                     cli.CLIENT_CHECKSUM_HEADER: 'invalid'
                                 })

    with pytest.raises(
            RuntimeError,
            match='The checksum of the script do not match the HTTP header'):
        cli.self_update(DEBMONITOR_BASE_URL, None, True)

    assert mocked_requests.called