Example #1
0
def test_main_dpkg_hook(mocked_getfqdn, mocked_requests):
    """Calling main() with -g should parse the input for a Dpkg::Pre-Install-Pkgs hook and send the update."""
    args = cli.parse_args(['--config', DEBMONITOR_CLIENT_CONFIG_OK, '-g'])
    input_lines = _get_dpkg_hook_preamble(3) + APT_HOOK_LINES[3][0:2]
    mocked_requests.register_uri('POST',
                                 DEBMONITOR_UPDATE_URL,
                                 status_code=201)
    mocked_apt.cache.Cache().__getitem__.return_value = AptPackage(
        name='package-name',
        is_installed=False,
        installed=None,
        candidate=AptPkgVersion(source_name='package-name', version='1.0.0-1'))

    with mock.patch('{mod}.open'.format(mod=BUILTINS),
                    mock.mock_open(read_data=OS_RELEASE),
                    create=True) as mocked_open:
        exit_code = cli.main(args, input_lines=input_lines)
        assert mock.call(cli.OS_RELEASE_FILE,
                         mode='r') in mocked_open.mock_calls

    mocked_getfqdn.assert_called_once_with()
    assert mocked_requests.called
    assert exit_code == 0
    assert mocked_requests.last_request.json() == _get_payload_with_packages(
        ['-g'])
Example #2
0
def test_main_update_ok(mocked_getfqdn, mocked_requests, caplog):
    """Calling main() whit --update that succeed should update the CLI script."""
    args = cli.parse_args(['-s', DEBMONITOR_SERVER, '--update'])
    mocked_requests.register_uri('POST',
                                 DEBMONITOR_UPDATE_URL,
                                 status_code=201)
    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
                                 })
    _reset_apt_caches()

    with patch('builtins.open', mock_open()) as mocked_open:
        exit_code = cli.main(args)

        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
    mocked_getfqdn.assert_called_once_with()
    assert exit_code == 0
    assert 'Successfully self-updated DebMonitor CLI' in caplog.text
Example #3
0
def test_main_no_packages(mocked_getfqdn):
    """Calling main() if there are no updates should success without sending any update to the DebMonitor server."""
    args = cli.parse_args(['-s', DEBMONITOR_SERVER])
    _reset_apt_caches(empty=True)

    exit_code = cli.main(args)

    mocked_getfqdn.assert_called_once_with()
    assert exit_code == 0
Example #4
0
def test_main_dry_run(mocked_getfqdn, capsys):
    """Calling main() with dry-run parameter should print the updates without sending them to the DebMonitor server."""
    args = cli.parse_args(['-s', DEBMONITOR_SERVER, '-n'])
    _reset_apt_caches()

    exit_code = cli.main(args)

    out, _ = capsys.readouterr()
    mocked_getfqdn.assert_called_once_with()
    assert exit_code == 0
    assert json.loads(out) == _get_payload_with_packages([])
Example #5
0
def test_main_dry_run_image_file(capsys):
    """Calling main() with image_file and dry_run parameters should print the content of the JSON file."""
    args = cli.parse_args(
        ['--config', DEBMONITOR_CLIENT_CONFIG_OK, '-n', '-f', VALID_JSON_FILE])
    _reset_apt_caches()

    exit_code = cli.main(args)

    out, _ = capsys.readouterr()
    assert exit_code == 0
    assert json.loads(out) == {'key': 'value'}
Example #6
0
def test_main(mocked_getfqdn, params, mocked_requests):
    """Calling main() should send the updates to the DebMonitor server with the above parameters."""
    args = cli.parse_args(['-s', DEBMONITOR_SERVER] + params)
    mocked_requests.register_uri('POST',
                                 DEBMONITOR_UPDATE_URL,
                                 status_code=201)
    _reset_apt_caches()

    exit_code = cli.main(args)

    mocked_getfqdn.assert_called_once_with()
    assert mocked_requests.called
    assert exit_code == 0
    assert mocked_requests.last_request.json() == _get_payload_with_packages(
        params)
Example #7
0
def test_main_wrong_http_code(mocked_getfqdn, params, mocked_requests, caplog):
    """Calling main() when the DebMonitor server returns a wrong HTTP code should return 1."""
    args = cli.parse_args(['-s', DEBMONITOR_SERVER] + params)
    mocked_requests.register_uri('POST',
                                 DEBMONITOR_UPDATE_URL,
                                 status_code=400)
    _reset_apt_caches()

    exit_code = cli.main(args)

    mocked_getfqdn.assert_called_once_with()
    assert mocked_requests.called
    assert exit_code == 1
    assert mocked_requests.last_request.json() == _get_payload_with_packages(
        params)
    assert 'Failed to send the update to the DebMonitor server' in caplog.text
Example #8
0
def test_main_dry_run_image_name(capsys):
    """Calling main() with image_name and dry_run parameters should print the updates for an image container."""
    args = cli.parse_args(
        ['--config', DEBMONITOR_CLIENT_CONFIG_OK, '-n', '-i', IMAGENAME])
    _reset_apt_caches()

    with mock.patch('builtins.open',
                    mock.mock_open(read_data=OS_RELEASE),
                    create=True) as mocked_open:
        exit_code = cli.main(args)
        assert mock.call(cli.OS_RELEASE_FILE,
                         mode='r') in mocked_open.mock_calls

    out, _ = capsys.readouterr()
    assert exit_code == 0
    assert json.loads(out) == _get_payload_with_packages(['-i'])
Example #9
0
def test_main_dry_run(mocked_getfqdn, capsys):
    """Calling main() with dry-run parameter should print the updates without sending them to the DebMonitor server."""
    args = cli.parse_args(['--config', DEBMONITOR_CLIENT_CONFIG_OK, '-n'])
    _reset_apt_caches()

    with mock.patch('{mod}.open'.format(mod=BUILTINS),
                    mock.mock_open(read_data=OS_RELEASE),
                    create=True) as mocked_open:
        exit_code = cli.main(args)
        assert mock.call(cli.OS_RELEASE_FILE,
                         mode='r') in mocked_open.mock_calls

    out, _ = capsys.readouterr()
    mocked_getfqdn.assert_called_once_with()
    assert exit_code == 0
    assert json.loads(out) == _get_payload_with_packages([])
Example #10
0
def test_main_update_fail(mocked_getfqdn, mocked_requests, caplog):
    """Calling main() whit --update that fails the update should log the error and continue."""
    args = cli.parse_args(['-s', DEBMONITOR_SERVER, '--update'])
    mocked_requests.register_uri('POST',
                                 DEBMONITOR_UPDATE_URL,
                                 status_code=201)
    mocked_requests.register_uri('HEAD',
                                 DEBMONITOR_CLIENT_URL,
                                 status_code=500)
    _reset_apt_caches()

    exit_code = cli.main(args)

    assert mocked_requests.called
    mocked_getfqdn.assert_called_once_with()
    assert exit_code == 0
    assert 'Unable to self-update this script' in caplog.text
Example #11
0
def test_main_wrong_http_code(params, mocked_getfqdn, mocked_requests, caplog):
    """Calling main() when the DebMonitor server returns a wrong HTTP code should return 1."""
    args = cli.parse_args(['--config', DEBMONITOR_CLIENT_CONFIG_OK] + params)
    mocked_requests.register_uri('POST',
                                 DEBMONITOR_UPDATE_URL,
                                 status_code=400)
    _reset_apt_caches()

    # Explicitely avoiding mocking open() due to a bug in Python 3.4.2 (jessie default version) that make it fail.
    cli.OS_RELEASE_FILE = OS_RELEASE_FILE
    exit_code = cli.main(args)

    mocked_getfqdn.assert_called_once_with()
    assert mocked_requests.called
    assert exit_code == 1
    assert mocked_requests.last_request.json() == _get_payload_with_packages(
        params)
    assert 'Failed to send the update to the DebMonitor server' in caplog.text
Example #12
0
def test_main_image_file(monkeypatch, mocked_getfqdn, mocked_requests):
    """Calling main() with an image file should send the updates to the DebMonitor server."""
    payload = _get_payload_with_packages(['-i'])
    stdin = json.dumps(payload)
    if sys.version_info[0] < 3:
        stdin = stdin.decode()
    monkeypatch.setattr('sys.stdin', io.StringIO(stdin))
    args = cli.parse_args(
        ['--config', '/dev/null', '-s', DEBMONITOR_SERVER, '-f', '-'])
    mocked_requests.register_uri('POST',
                                 DEBMONITOR_IMAGE_UPDATE_URL,
                                 status_code=201)

    exit_code = cli.main(args)

    assert not mocked_getfqdn.called
    assert mocked_requests.called
    assert exit_code == 0
    assert mocked_requests.last_request.json() == payload
Example #13
0
def test_main_dpkg_hook(mocked_getfqdn, mocked_requests):
    """Calling main() with -g should parse the input for a Dpkg::Pre-Install-Pkgs hook and send the update."""
    args = cli.parse_args(['-s', DEBMONITOR_SERVER, '-g'])
    input_lines = _get_dpkg_hook_preamble(3) + APT_HOOK_LINES[3][0:2]
    mocked_requests.register_uri('POST',
                                 DEBMONITOR_UPDATE_URL,
                                 status_code=201)
    mocked_apt.cache.Cache().__getitem__.return_value = AptPackage(
        name='package-name',
        is_installed=False,
        installed=None,
        candidate=AptPkgVersion(source_name='package-name', version='1.0.0-1'))

    exit_code = cli.main(args, input_lines=input_lines)

    mocked_getfqdn.assert_called_once_with()
    assert mocked_requests.called
    assert exit_code == 0
    assert mocked_requests.last_request.json() == _get_payload_with_packages(
        ['-g'])
Example #14
0
def test_main_no_packages(mocked_getfqdn, mocked_requests):
    """Calling main() if there are no updates should success sending an empty update to the DebMonitor server."""
    args = cli.parse_args(['--config', DEBMONITOR_CLIENT_CONFIG_OK, '-u'])
    mocked_requests.register_uri('POST',
                                 DEBMONITOR_UPDATE_URL,
                                 status_code=201)
    _reset_apt_caches(empty=True)

    with mock.patch('{mod}.open'.format(mod=BUILTINS),
                    mock.mock_open(read_data=OS_RELEASE),
                    create=True) as mocked_open:
        exit_code = cli.main(args)
        assert mock.call(cli.OS_RELEASE_FILE,
                         mode='r') in mocked_open.mock_calls

    mocked_getfqdn.assert_called_once_with()
    assert mocked_requests.called
    assert exit_code == 0
    assert mocked_requests.last_request.json() == _get_payload_with_packages(
        ['empty'])
Example #15
0
def test_main_update_ok(mocked_getfqdn, mocked_requests, caplog):
    """Calling main() whit --update that succeed should update the CLI script."""
    caplog.set_level(logging.INFO)
    args = cli.parse_args(
        ['--config', DEBMONITOR_CLIENT_CONFIG_OK, '--update'])
    mocked_requests.register_uri('POST',
                                 DEBMONITOR_UPDATE_URL,
                                 status_code=201)
    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
                                 })
    _reset_apt_caches()

    with mock.patch('{mod}.open'.format(mod=BUILTINS),
                    mock.mock_open(read_data=OS_RELEASE),
                    create=True) as mocked_open:
        exit_code = cli.main(args)
        assert mock.call(cli.OS_RELEASE_FILE,
                         mode='r') in mocked_open.mock_calls
        assert mock.call(os.path.realpath(cli.__file__),
                         mode='w') in mocked_open.mock_calls
        mocked_handler = mocked_open()
        mocked_handler.write.assert_called_once_with(
            tests_deb.CLIENT_BODY_DUMMY_1)

    assert mocked_requests.called
    mocked_getfqdn.assert_called_once_with()
    assert exit_code == 0
    assert 'Successfully self-updated DebMonitor CLI' in caplog.text
Example #16
0
def test_main(params, mocked_getfqdn, mocked_requests):
    """Calling main() should send the updates to the DebMonitor server with the above parameters."""
    args = cli.parse_args(['--config', '/dev/null', '-s', DEBMONITOR_SERVER] +
                          params)
    mocked_requests.register_uri('POST',
                                 DEBMONITOR_UPDATE_URL,
                                 status_code=201)
    _reset_apt_caches()

    with mock.patch('{mod}.open'.format(mod=BUILTINS),
                    mock.mock_open(read_data=OS_RELEASE),
                    create=True) as mocked_open:
        exit_code = cli.main(args)
        assert mock.call(cli.OS_RELEASE_FILE,
                         mode='r') in mocked_open.mock_calls

    mocked_getfqdn.assert_called_once_with()
    assert mocked_requests.called
    assert exit_code == 0
    assert mocked_requests.last_request.json() == _get_payload_with_packages(
        params)
Example #17
0
def test_main_wrong_http_code(params, mocked_getfqdn, mocked_requests, caplog):
    """Calling main() when the DebMonitor server returns a wrong HTTP code should return 1."""
    args = cli.parse_args(['--config', DEBMONITOR_CLIENT_CONFIG_OK] + params)
    mocked_requests.register_uri('POST',
                                 DEBMONITOR_HOST_UPDATE_URL,
                                 status_code=400)
    _reset_apt_caches()

    with mock.patch('builtins.open',
                    mock.mock_open(read_data=OS_RELEASE),
                    create=True) as mocked_open:
        exit_code = cli.main(args)
        assert mock.call(cli.OS_RELEASE_FILE,
                         mode='r') in mocked_open.mock_calls

    mocked_getfqdn.assert_called_once_with()
    assert mocked_requests.called
    assert exit_code == 1
    assert mocked_requests.last_request.json() == _get_payload_with_packages(
        params)
    assert 'Failed to send the update to the DebMonitor server' in caplog.text
Example #18
0
def test_main_image(mocked_getfqdn, mocked_requests):
    """Calling main() with an image should send the updates to the DebMonitor server."""
    args = cli.parse_args(
        ['--config', '/dev/null', '-s', DEBMONITOR_SERVER, '-i', IMAGENAME])
    mocked_requests.register_uri('POST',
                                 DEBMONITOR_IMAGE_UPDATE_URL,
                                 status_code=201)
    _reset_apt_caches()

    with mock.patch('builtins.open',
                    mock.mock_open(read_data=OS_RELEASE),
                    create=True) as mocked_open:
        exit_code = cli.main(args)
        assert mock.call(cli.OS_RELEASE_FILE,
                         mode='r') in mocked_open.mock_calls

    assert not mocked_getfqdn.called
    assert mocked_requests.called
    assert exit_code == 0
    assert mocked_requests.last_request.json() == _get_payload_with_packages(
        ['-i'])
Example #19
0
def test_main_update_fail(mocked_getfqdn, mocked_requests, caplog):
    """Calling main() whit --update that fails the update should log the error and continue."""
    args = cli.parse_args(
        ['--config', DEBMONITOR_CLIENT_CONFIG_OK, '--update'])
    mocked_requests.register_uri('POST',
                                 DEBMONITOR_UPDATE_URL,
                                 status_code=201)
    mocked_requests.register_uri('HEAD',
                                 DEBMONITOR_CLIENT_URL,
                                 status_code=500)
    _reset_apt_caches()

    with mock.patch('{mod}.open'.format(mod=BUILTINS),
                    mock.mock_open(read_data=OS_RELEASE),
                    create=True) as mocked_open:
        exit_code = cli.main(args)
        assert mock.call(cli.OS_RELEASE_FILE,
                         mode='r') in mocked_open.mock_calls

    assert mocked_requests.called
    mocked_getfqdn.assert_called_once_with()
    assert exit_code == 0
    assert 'Unable to self-update this script' in caplog.text
Example #20
0
"""
Author: [email protected] (Sagar Bhat)
Github: @cookienut

This file serves as the entry point for trigerring spammer script through CLI.
Try ``spammer --help`` for more details.
"""

from utils import cli

try:
    cli.main()
except Exception as ex:
    print(f'Error: {ex}')