This file is indexed.

/usr/lib/python2.7/dist-packages/oslo_messaging/tests/drivers/zmq/matchmaker/test_impl_matchmaker.py is in python-oslo.messaging 4.6.1-2ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
# Copyright 2014 Canonical, Ltd.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from stevedore import driver
import testscenarios
import testtools

import retrying

import oslo_messaging
from oslo_messaging.tests import utils as test_utils
from oslo_utils import importutils

redis = importutils.try_import('redis')


def redis_available():
    '''Helper to see if local redis server is running'''
    if not redis:
        return False
    try:
        c = redis.StrictRedis(socket_timeout=1)
        c.ping()
        return True
    except redis.exceptions.ConnectionError:
        return False


load_tests = testscenarios.load_tests_apply_scenarios


@testtools.skipIf(not redis_available(), "redis unavailable")
class TestImplMatchmaker(test_utils.BaseTestCase):

    scenarios = [
        ("dummy", {"rpc_zmq_matchmaker": "dummy"}),
        ("redis", {"rpc_zmq_matchmaker": "redis"}),
    ]

    def setUp(self):
        super(TestImplMatchmaker, self).setUp()

        self.test_matcher = driver.DriverManager(
            'oslo.messaging.zmq.matchmaker',
            self.rpc_zmq_matchmaker,
        ).driver(self.conf)

        if self.rpc_zmq_matchmaker == "redis":
            self.addCleanup(self.test_matcher._redis.flushdb)

        self.target = oslo_messaging.Target(topic="test_topic")
        self.host1 = b"test_host1"
        self.host2 = b"test_host2"

    def test_register(self):
        self.test_matcher.register(self.target, self.host1, "test")

        self.assertEqual(self.test_matcher.get_hosts(self.target, "test"),
                         [self.host1])

    def test_register_two_hosts(self):
        self.test_matcher.register(self.target, self.host1, "test")
        self.test_matcher.register(self.target, self.host2, "test")

        self.assertItemsEqual(self.test_matcher.get_hosts(self.target, "test"),
                              [self.host1, self.host2])

    def test_register_unsibscribe(self):
        self.test_matcher.register(self.target, self.host1, "test")
        self.test_matcher.register(self.target, self.host2, "test")

        self.test_matcher.unregister(self.target, self.host2, "test")

        self.assertItemsEqual(self.test_matcher.get_hosts(self.target, "test"),
                              [self.host1])

    def test_register_two_same_hosts(self):
        self.test_matcher.register(self.target, self.host1, "test")
        self.test_matcher.register(self.target, self.host1, "test")

        self.assertEqual(self.test_matcher.get_hosts(self.target, "test"),
                         [self.host1])

    def test_get_hosts_wrong_topic(self):
        target = oslo_messaging.Target(topic="no_such_topic")
        hosts = []
        try:
            hosts = self.test_matcher.get_hosts(target, "test")
        except retrying.RetryError:
            pass
        self.assertEqual(hosts, [])