Fix zookeeper tests

Change-Id: I90a609992a41a2802245a9b80b95c9eb16203bb4
This commit is contained in:
Georgy Dyuldin 2017-04-26 17:55:53 +03:00
parent eb1dbe1873
commit 7bb3233061
2 changed files with 36 additions and 9 deletions

View File

@ -16,16 +16,20 @@ import pytest
@pytest.fixture
def znodes_list(nodes_ips):
hosts_list = ""
def zoo_client(nodes_ips):
hosts_list = []
contrail_controllers_fqdns = settings.CONTRAIL_ROLES_DISTRIBUTION[
settings.ROLE_CONTRAIL_CONTROLLER]
for name in nodes_ips:
if name in contrail_controllers_fqdns:
hosts_list += "{}:{},".format(nodes_ips[name][0],
settings.ZOOKEEPER_PORT)
hosts_list = hosts_list[:-1]
zk = client.KazooClient(hosts=hosts_list)
hosts_list.append("{}:{}".format(nodes_ips[name][0],
settings.ZOOKEEPER_PORT))
return client.KazooClient(hosts=','.join(hosts_list))
@pytest.fixture
def znodes_list(zoo_client):
zk = zoo_client
zk.start()
znodes_list_ = zk.get_children("/")
zk.stop()

View File

@ -10,8 +10,12 @@
# License for the specific language governing permissions and limitations
# under the License.
import collections
import re
from hamcrest import (assert_that, has_item, has_entry, is_not, empty,
has_property, contains_inanyorder) # noqa: H301
has_property, contains_inanyorder, has_length,
greater_than) # noqa: H301
import jmespath
import pycontrail.types as types
import pytest
@ -173,5 +177,24 @@ def test_contrail_alarms_is_empty(client_contrail_analytics):
def test_zookeeper_status(znodes_list):
expected_znodes_list = settings.ZOOKEEPER_NODES
assert_that(znodes_list, contains_inanyorder(*expected_znodes_list))
expected_znodes_list = set(settings.ZOOKEEPER_NODES)
znodes_list = set(znodes_list)
min_intersection = len(znodes_list) // 2
assert_that(znodes_list & expected_znodes_list,
has_length(greater_than(min_intersection)))
def test_zookeeper_leader_followers(zoo_client):
"""Check that only one zookeeper host has leader mode."""
hosts = ['{}:{}'.format(ip, port) for (ip, port) in zoo_client.hosts]
pattern = re.compile('Mode: (follower|leader)')
modes = collections.defaultdict(set)
for host in hosts:
zoo_client.set_hosts(host)
zoo_client.start()
result = zoo_client.command('stat')
mode = pattern.search(result).group(1)
modes[mode].add(host)
zoo_client.stop()
zoo_client.close()
assert_that(modes['leader'], has_length(1))