summaryrefslogtreecommitdiff
path: root/oslo_messaging/tests/drivers/test_matchmaker_redis.py
blob: 77604040c72599e33d91ce867ad5843baa38ebe5 (plain)
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
# 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 oslo_utils import importutils
import testtools

from oslo_messaging.tests import utils as test_utils

redis = importutils.try_import('redis')
matchmaker_redis = (
    importutils.try_import('oslo_messaging._drivers.matchmaker_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


@testtools.skipIf(not matchmaker_redis, "matchmaker/eventlet unavailable")
@testtools.skipIf(not redis_available(), "redis unavailable")
class RedisMatchMakerTest(test_utils.BaseTestCase):

    def setUp(self):
        super(RedisMatchMakerTest, self).setUp()
        self.ring_data = {
            "conductor": ["controller1", "node1", "node2", "node3"],
            "scheduler": ["controller1", "node1", "node2", "node3"],
            "network": ["controller1", "node1", "node2", "node3"],
            "cert": ["controller1"],
            "console": ["controller1"],
            "l3_agent.node1": ["node1"],
            "consoleauth": ["controller1"]}
        self.matcher = matchmaker_redis.MatchMakerRedis()
        self.populate()

    def tearDown(self):
        super(RedisMatchMakerTest, self).tearDown()
        c = redis.StrictRedis()
        c.flushdb()

    def populate(self):
        for k, hosts in self.ring_data.items():
            for h in hosts:
                self.matcher.register(k, h)

    def test_direct(self):
        self.assertEqual(
            self.matcher.queues('cert.controller1'),
            [('cert.controller1', 'controller1')])

    def test_register(self):
        self.matcher.register('cert', 'keymaster')
        self.assertEqual(
            sorted(self.matcher.redis.smembers('cert')),
            ['cert.controller1', 'cert.keymaster'])
        self.matcher.register('l3_agent.node1', 'node1')
        self.assertEqual(
            sorted(self.matcher.redis.smembers('l3_agent.node1')),
            ['l3_agent.node1.node1'])

    def test_unregister(self):
        self.matcher.unregister('conductor', 'controller1')
        self.assertEqual(
            sorted(self.matcher.redis.smembers('conductor')),
            ['conductor.node1', 'conductor.node2', 'conductor.node3'])

    def test_ack_alive(self):
        self.matcher.ack_alive('ack_alive', 'controller1')
        self.assertEqual(
            sorted(self.matcher.redis.smembers('ack_alive')),
            ['ack_alive.controller1'])

    def test_is_alive(self):
        self.assertEqual(
            self.matcher.is_alive('conductor', 'conductor.controller1'),
            True)
        self.assertEqual(
            self.matcher.is_alive('conductor', 'conductor.controller2'),
            False)