Exemplo n.º 1
0
def test_load_system_paasta_config_file_non_existent_dir():
    fake_path = "/var/dir_of_fake"
    with contextlib.nested(mock.patch("os.path.isdir", return_value=False)) as (isdir_patch,):
        with raises(utils.PaastaNotConfiguredError) as excinfo:
            utils.load_system_paasta_config(fake_path)
        expected = "Could not find system paasta configuration directory: %s" % fake_path
        assert str(excinfo.value) == expected
Exemplo n.º 2
0
def test_load_system_paasta_config_file_non_existent_dir():
    fake_path = '/var/dir_of_fake'
    with contextlib.nested(mock.patch(
            'os.path.isdir', return_value=False), ) as (isdir_patch, ):
        with raises(utils.PaastaNotConfiguredError) as excinfo:
            utils.load_system_paasta_config(fake_path)
        expected = "Could not find system paasta configuration directory: %s" % fake_path
        assert str(excinfo.value) == expected
Exemplo n.º 3
0
def test_load_system_paasta_config_file_dne():
    fake_path = "/var/dir_of_fake"
    with contextlib.nested(
        mock.patch("os.path.isdir", return_value=True),
        mock.patch("os.access", return_value=True),
        mock.patch("utils.open", create=True, side_effect=IOError(2, "a", "b")),
        mock.patch("utils.get_readable_files_in_glob", autospec=True, return_value=[fake_path]),
    ) as (isdir_patch, access_patch, open_patch, mock_get_readable_files_in_glob):
        with raises(utils.PaastaNotConfiguredError) as excinfo:
            utils.load_system_paasta_config(fake_path)
        assert str(excinfo.value) == "Could not load system paasta config file b: a"
Exemplo n.º 4
0
def test_load_system_paasta_config_file_non_readable_dir():
    fake_path = '/var/dir_of_fake'
    with contextlib.nested(
        mock.patch('os.path.isdir', return_value=True),
        mock.patch('os.access', return_value=False),
    ) as (
        isdir_patch,
        access_patch,
    ):
        with raises(utils.PaastaNotConfiguredError) as excinfo:
            utils.load_system_paasta_config(fake_path)
        expected = "Could not read from system paasta configuration directory: %s" % fake_path
        assert str(excinfo.value) == expected
Exemplo n.º 5
0
def test_load_system_paasta_config_merge_lexographically():
    fake_file_a = {'foo': 'this value will be overriden', 'fake': 'fake_data'}
    fake_file_b = {'foo': 'overriding value'}
    expected = utils.SystemPaastaConfig(
        {
            'foo': 'overriding value',
            'fake': 'fake_data'
        }, '/some/fake/dir')
    file_mock = mock.MagicMock(spec=file)
    with contextlib.nested(
            mock.patch('os.path.isdir', return_value=True),
            mock.patch('os.access', return_value=True),
            mock.patch('utils.open', create=True, return_value=file_mock),
            mock.patch('utils.get_readable_files_in_glob',
                       autospec=True,
                       return_value=['a', 'b']),
            mock.patch('utils.json.load',
                       autospec=True,
                       side_effect=[fake_file_a, fake_file_b])) as (
                           os_is_dir_patch,
                           os_access_patch,
                           open_file_patch,
                           mock_get_readable_files_in_glob,
                           json_patch,
                       ):
        actual = utils.load_system_paasta_config()
        assert actual == expected
Exemplo n.º 6
0
def test_load_system_paasta_config():
    json_load_return_value = {'foo': 'bar'}
    expected = utils.SystemPaastaConfig(json_load_return_value,
                                        '/some/fake/dir')
    file_mock = mock.MagicMock(spec=file)
    with contextlib.nested(
            mock.patch('os.path.isdir', return_value=True),
            mock.patch('os.access', return_value=True),
            mock.patch('utils.open', create=True, return_value=file_mock),
            mock.patch('utils.get_readable_files_in_glob',
                       autospec=True,
                       return_value=['/some/fake/dir/some_file.json']),
            mock.patch('utils.json.load',
                       autospec=True,
                       return_value=json_load_return_value)) as (
                           os_is_dir_patch,
                           os_access_patch,
                           open_file_patch,
                           mock_get_readable_files_in_glob,
                           json_patch,
                       ):
        actual = utils.load_system_paasta_config()
        assert actual == expected
        # Kinda weird but without this load_system_paasta_config() can (and
        # did! during development) return a plain dict without the test
        # complaining.
        assert actual.__class__ == expected.__class__
        open_file_patch.assert_any_call('/some/fake/dir/some_file.json')
        json_patch.assert_any_call(file_mock.__enter__())
        assert json_patch.call_count == 1
Exemplo n.º 7
0
def send_event(service, check_name, overrides, status, output, soa_dir, ttl=None):
    """Send an event to sensu via pysensu_yelp with the given information.

    :param service: The service name the event is about
    :param check_name: The name of the check as it appears in Sensu
    :param overrides: A dictionary containing overrides for monitoring options
                      (e.g. notification_email, ticket, page)
    :param status: The status to emit for this event
    :param output: The output to emit for this event
    :param soa_dir: The service directory to read monitoring information from
    """
    # This function assumes the input is a string like "mumble.main"
    team = get_team(overrides, service, soa_dir)
    if not team:
        return
    runbook = overrides.get('runbook', 'http://y/paasta-troubleshooting')
    result_dict = {
        'tip': get_tip(overrides, service, soa_dir),
        'notification_email': get_notification_email(overrides, service, soa_dir),
        'irc_channels': get_irc_channels(overrides, service, soa_dir),
        'ticket': get_ticket(overrides, service, soa_dir),
        'project': get_project(overrides, service, soa_dir),
        'page': get_page(overrides, service, soa_dir),
        'alert_after': overrides.get('alert_after', '5m'),
        'check_every': overrides.get('check_every', '1m'),
        'realert_every': -1,
        'source': 'paasta-%s' % load_system_paasta_config().get_cluster(),
        'ttl': ttl,
    }
    pysensu_yelp.send_event(check_name, runbook, status, output, team, **result_dict)
Exemplo n.º 8
0
def test_load_system_paasta_config():
    json_load_return_value = {'foo': 'bar'}
    expected = utils.SystemPaastaConfig(json_load_return_value, '/some/fake/dir')
    file_mock = mock.MagicMock(spec=file)
    with contextlib.nested(
        mock.patch('os.path.isdir', return_value=True),
        mock.patch('os.access', return_value=True),
        mock.patch('utils.open', create=True, return_value=file_mock),
        mock.patch('utils.get_readable_files_in_glob', autospec=True,
                   return_value=['/some/fake/dir/some_file.json']),
        mock.patch('utils.json.load', autospec=True, return_value=json_load_return_value)
    ) as (
        os_is_dir_patch,
        os_access_patch,
        open_file_patch,
        mock_get_readable_files_in_glob,
        json_patch,
    ):
        actual = utils.load_system_paasta_config()
        assert actual == expected
        # Kinda weird but without this load_system_paasta_config() can (and
        # did! during development) return a plain dict without the test
        # complaining.
        assert actual.__class__ == expected.__class__
        open_file_patch.assert_any_call('/some/fake/dir/some_file.json')
        json_patch.assert_any_call(file_mock.__enter__())
        assert json_patch.call_count == 1
Exemplo n.º 9
0
def send_event(service, check_name, overrides, status, output, soa_dir, ttl=None):
    """Send an event to sensu via pysensu_yelp with the given information.

    :param service: The service name the event is about
    :param check_name: The name of the check as it appears in Sensu
    :param overrides: A dictionary containing overrides for monitoring options
                      (e.g. notification_email, ticket, page)
    :param status: The status to emit for this event
    :param output: The output to emit for this event
    :param soa_dir: The service directory to read monitoring information from
    """
    # This function assumes the input is a string like "mumble.main"
    team = get_team(overrides, service, soa_dir)
    if not team:
        return
    runbook = overrides.get('runbook', 'http://y/paasta-troubleshooting')
    result_dict = {
        'tip': get_tip(overrides, service, soa_dir),
        'notification_email': get_notification_email(overrides, service, soa_dir),
        'irc_channels': get_irc_channels(overrides, service, soa_dir),
        'ticket': get_ticket(overrides, service, soa_dir),
        'project': get_project(overrides, service, soa_dir),
        'page': get_page(overrides, service, soa_dir),
        'alert_after': overrides.get('alert_after', '5m'),
        'check_every': overrides.get('check_every', '1m'),
        'realert_every': -1,
        'source': 'paasta-%s' % load_system_paasta_config().get_cluster(),
        'ttl': ttl,
    }
    pysensu_yelp.send_event(check_name, runbook, status, output, team, **result_dict)
Exemplo n.º 10
0
def send_event(service, check_name, overrides, status, output, soa_dir, ttl=None):
    """Send an event to sensu via pysensu_yelp with the given information.

    :param service: The service name the event is about
    :param check_name: The name of the check as it appears in Sensu
    :param overrides: A dictionary containing overrides for monitoring options
                      (e.g. notification_email, ticket, page)
    :param status: The status to emit for this event
    :param output: The output to emit for this event
    :param soa_dir: The service directory to read monitoring information from
    """
    # This function assumes the input is a string like "mumble.main"
    team = get_team(overrides, service, soa_dir)
    if not team:
        return
    runbook = overrides.get("runbook", "http://y/paasta-troubleshooting")
    result_dict = {
        "tip": get_tip(overrides, service, soa_dir),
        "notification_email": get_notification_email(overrides, service, soa_dir),
        "irc_channels": get_irc_channels(overrides, service, soa_dir),
        "ticket": get_ticket(overrides, service, soa_dir),
        "project": get_project(overrides, service, soa_dir),
        "page": get_page(overrides, service, soa_dir),
        "alert_after": overrides.get("alert_after", "5m"),
        "check_every": overrides.get("check_every", "1m"),
        "realert_every": -1,
        "source": "paasta-%s" % load_system_paasta_config().get_cluster(),
        "ttl": ttl,
    }
    pysensu_yelp.send_event(check_name, runbook, status, output, team, **result_dict)
Exemplo n.º 11
0
def test_load_system_paasta_config_file_dne():
    fake_path = '/var/dir_of_fake'
    with contextlib.nested(
            mock.patch('os.path.isdir', return_value=True),
            mock.patch('os.access', return_value=True),
            mock.patch('utils.open',
                       create=True,
                       side_effect=IOError(2, 'a', 'b')),
            mock.patch('utils.get_readable_files_in_glob',
                       autospec=True,
                       return_value=[fake_path]),
    ) as (
            isdir_patch,
            access_patch,
            open_patch,
            mock_get_readable_files_in_glob,
    ):
        with raises(utils.PaastaNotConfiguredError) as excinfo:
            utils.load_system_paasta_config(fake_path)
        assert str(
            excinfo.value) == "Could not load system paasta config file b: a"
Exemplo n.º 12
0
def test_load_system_paasta_config_merge_lexographically():
    fake_file_a = {"foo": "this value will be overriden", "fake": "fake_data"}
    fake_file_b = {"foo": "overriding value"}
    expected = utils.SystemPaastaConfig({"foo": "overriding value", "fake": "fake_data"}, "/some/fake/dir")
    file_mock = mock.MagicMock(spec=file)
    with contextlib.nested(
        mock.patch("os.path.isdir", return_value=True),
        mock.patch("os.access", return_value=True),
        mock.patch("utils.open", create=True, return_value=file_mock),
        mock.patch("utils.get_readable_files_in_glob", autospec=True, return_value=["a", "b"]),
        mock.patch("utils.json.load", autospec=True, side_effect=[fake_file_a, fake_file_b]),
    ) as (os_is_dir_patch, os_access_patch, open_file_patch, mock_get_readable_files_in_glob, json_patch):
        actual = utils.load_system_paasta_config()
        assert actual == expected
Exemplo n.º 13
0
def create_app_lock():
    """Acquire a lock in zookeeper for creating a marathon app. This is
    due to marathon's extreme lack of resilience with creating multiple
    apps at once, so we use this to not do that and only deploy
    one app at a time."""
    zk = KazooClient(hosts=load_system_paasta_config().get_zk_hosts(), timeout=ZK_LOCK_CONNECT_TIMEOUT_S)
    zk.start()
    lock = zk.Lock('%s/%s' % (ZK_LOCK_PATH, 'create_marathon_app_lock'))
    try:
        lock.acquire(timeout=30)  # timeout=0 throws some other strange exception
        yield
    except LockTimeout:
        raise LockHeldException("Failed to acquire lock for creating marathon app!")
    finally:
        lock.release()
        zk.stop()
Exemplo n.º 14
0
def test_load_system_paasta_config_merge_lexographically():
    fake_file_a = {'foo': 'this value will be overriden', 'fake': 'fake_data'}
    fake_file_b = {'foo': 'overriding value'}
    expected = utils.SystemPaastaConfig({'foo': 'overriding value', 'fake': 'fake_data'}, '/some/fake/dir')
    file_mock = mock.MagicMock(spec=file)
    with contextlib.nested(
        mock.patch('os.path.isdir', return_value=True),
        mock.patch('os.access', return_value=True),
        mock.patch('utils.open', create=True, return_value=file_mock),
        mock.patch('utils.get_readable_files_in_glob', autospec=True,
                   return_value=['a', 'b']),
        mock.patch('utils.json.load', autospec=True, side_effect=[fake_file_a, fake_file_b])
    ) as (
        os_is_dir_patch,
        os_access_patch,
        open_file_patch,
        mock_get_readable_files_in_glob,
        json_patch,
    ):
        actual = utils.load_system_paasta_config()
        assert actual == expected
Exemplo n.º 15
0
def bounce_lock_zookeeper(name):
    """Acquire a bounce lock in zookeeper for the name given. The name should
    generally be the service namespace being bounced.

    This is a contextmanager. Please use it via 'with bounce_lock(name):'.

    :param name: The lock name to acquire"""
    zk = KazooClient(hosts=load_system_paasta_config().get_zk_hosts(), timeout=ZK_LOCK_CONNECT_TIMEOUT_S)
    zk.start()
    lock = zk.Lock('%s/%s' % (ZK_LOCK_PATH, name))
    acquired = False
    try:
        lock.acquire(timeout=1)  # timeout=0 throws some other strange exception
        acquired = True
        yield
    except LockTimeout:
        raise LockHeldException("Service %s is already being bounced!" % name)
    finally:
        if acquired:
            lock.release()
        zk.stop()