<commit_before>import logging
from operator import eq
from nose.plugins.attrib import attr

from fuel_health import config
from fuel_health.common.amqp_client import RabbitClient
from fuel_health.common.ssh import Client as SSHClient
from fuel_health.common.utils.data_utils import rand_name
from fuel_health.test import BaseTestCase

LOG = logging.getLogger(__name__)


class RabbitSmokeTest(BaseTestCase):
    """
    TestClass contains tests check RabbitMQ.
    Special requirements:
            1. A controllers' IPs should be specified in
                controller_nodes parameter of the config file.
            2. The controllers' domain names should be specified in
                controller_nodes_name parameter of the config file.
            3. SSH user credentials should be specified in
                controller_node_ssh_user/password parameters
                of the config file.
            4. List of services are expected to be run should be specified in
                enabled_services parameter of the config file.
            5. SSH user should have root permissions on controllers
    """

    @classmethod
    def setUpClass(cls):
        cls.config = config.FuelConfig()
        cls._controllers = cls.config.compute.controller_nodes
        cls._usr = cls.config.compute.controller_node_ssh_user
        cls._pwd = cls.config.compute.controller_node_ssh_password
        cls._key = cls.config.compute.path_to_private_key
        cls._ssh_timeout = cls.config.compute.ssh_timeout
        cls._rabbit_user = 'nova'
        cls._rabbit_password = '<PASSWORD>'
        cls.amqp_clients = [RabbitClient(cnt,
                                         cls._usr,
                                         cls._pwd, cls._key,
                                         cls._ssh_timeout,
                                         cls._rabbit_user,
                                         cls._rabbit_password)
                            for cnt in cls._controllers]

    @attr(type=['fuel', 'ha', 'non-destructive'])
    def test_001_rabbitmqctl_status(self):
        """RabbitMQ cluster availability

        Scenario:
          1. Retrieve cluster status for each the controller.
          2. Check number of clusters are equal to number of controllers.
          3. Check cluster list is the same for each the controller.
        Duration: 100 s.
        """
        if not self._controllers:
            self.fail('Step 1 failed: There are no controller nodes.')
        cmd = 'sudo rabbitmqctl cluster_status'
        nodes = []
        for node in self._controllers:
            output = ''
            error = ''
            ssh_client = SSHClient(host=node, username=self._usr,
                                   password=self._pwd,
                                   pkey=self._key,
                                   timeout=self._ssh_timeout)
            output = self._format_output(self.verify(20,
                                                     ssh_client.exec_command,
                                                     1,
                                                     "Cannot get cluster "
                                                     "status for %s node." %
                                                     node,
                                                     "Retrieve cluster status "
                                                     "for each the controller",
                                                     cmd))
            nodes.append({'controller': node, 'clusters': output})
            #for HA configuration number of controllers and
        #number of clusters should be the same
        _num_of_controllers = len(self._controllers)
        _aux_node_clusters = sorted(nodes[0]['clusters'])
        for node in nodes[1:]:
            self.assertEqual(_num_of_controllers, len(node["clusters"]),
                             'Step 2 failed: The number of clusters for '
                             'node %s is not equal to number of controllers' %
                             node['controller'])

            self.assertTrue(map(eq, sorted(node['clusters']),
                                _aux_node_clusters), "Step 3 failed: Cluster "
                                                     "lists on nodes %s"
                                                     " and %s are different" %
                                                     (nodes[0]["controller"],
                                                      node["controller"]))

    @attr(type=['fuel', 'ha', 'non-destructive'])
    def test_002_rabbit_queues(self):
        """RabbitMQ queues availability
        Scenario:
          1. Retrieve list of RabbitMQ queues for each controller
          2. Check the same queue list is present on each node
        Duration: 100 s.
        """
        if not self._controllers:
            self.fail('Step 1 failed: There are no controller nodes.')
        cmd = 'sudo rabbitmqctl list_queues'
        temp_set = set()
        get_name = lambda x: x.split('\t')[0]
        for node in self._controllers:
            ssh_client = SSHClient(host=node,
                                   username=self._usr,
                                   password=self._<PASSWORD>,
                                   pkey=self._key,
                                   timeout=self._ssh_timeout)
            output = self.verify(20, ssh_client.exec_command, 1,
                                 "Cannot get queue list for %s node. " % node,
                                 "Retrieve queue list",
                                 cmd)
            output = set([get_name(x) for x in (output.splitlines())[1:-1]])
            if not temp_set:
                #this means it is the first node,
                #this case we check there are queues only
                temp_set = output
                if not output:
                    self.fail("Step 2 failed: Queue list for %s"
                              " controller is empty" % node)
                continue
                #check all the queues are present on all the nodes
            if output.symmetric_difference(temp_set):
                self.fail("Step 2 failed: Queue lists are different for %s "
                          "and %s controllers" %
                          (self._controllers[0], node))

    @attr(type=['fuel', 'ha', 'non-destructive'])
    def test_003_rabbit_messages(self):
        """RabbitMQ messages availability
        Scenario:
          1. Create a queue on a controller
          2. Create an exchange on the controller
          3. Create a binding for the queue and exchange
          4. Publish messages to the queue.
          5. Check the messages are available on other controllers
          6. Delete the exchange
          7. Delete the queue (with binding)
        Duration: 100 s.
        """
        if not self._controllers:
            self.fail('Step 1 failed: There are no controller nodes.')

        new_queue = rand_name(name='ostf1-test-queue-')<commit_msg>test_002_rabbit_queues test was refactored to use new AMQP client<commit_after>import logging
from operator import eq
from nose.plugins.attrib import attr

from fuel_health import config
from fuel_health.common.amqp_client import RabbitClient
from fuel_health.common.ssh import Client as SSHClient
from fuel_health.common.utils.data_utils import rand_name
from fuel_health.test import BaseTestCase

LOG = logging.getLogger(__name__)


class RabbitSmokeTest(BaseTestCase):
    """
    TestClass contains tests check RabbitMQ.
    Special requirements:
            1. A controllers' IPs should be specified in
                controller_nodes parameter of the config file.
            2. The controllers' domain names should be specified in
                controller_nodes_name parameter of the config file.
            3. SSH user credentials should be specified in
                controller_node_ssh_user/password parameters
                of the config file.
            4. List of services are expected to be run should be specified in
                enabled_services parameter of the config file.
            5. SSH user should have root permissions on controllers
    """

    @classmethod
    def setUpClass(cls):
        cls.config = config.FuelConfig()
        cls._controllers = cls.config.compute.controller_nodes
        cls._usr = cls.config.compute.controller_node_ssh_user
        cls._pwd = cls.config.compute.controller_node_ssh_password
        cls._key = cls.config.compute.path_to_private_key
        cls._ssh_timeout = cls.config.compute.ssh_timeout
        cls._rabbit_user = 'nova'
        cls._rabbit_password = '<PASSWORD>'
        cls.amqp_clients = [RabbitClient(cnt,
                                         cls._usr,
                                         cls._pwd, cls._key,
                                         cls._ssh_timeout,
                                         cls._rabbit_user,
                                         cls._rabbit_password)
                            for cnt in cls._controllers]

    @attr(type=['fuel', 'ha', 'non-destructive'])
    def test_001_rabbitmqctl_status(self):
        """RabbitMQ cluster availability

        Scenario:
          1. Retrieve cluster status for each the controller.
          2. Check number of clusters are equal to number of controllers.
          3. Check cluster list is the same for each the controller.
        Duration: 100 s.
        """
        if not self._controllers:
            self.fail('Step 1 failed: There are no controller nodes.')
        cmd = 'sudo rabbitmqctl cluster_status'
        nodes = []
        for node in self._controllers:
            output = ''
            error = ''
            ssh_client = SSHClient(host=node, username=self._usr,
                                   password=self._pwd,
                                   pkey=self._key,
                                   timeout=self._ssh_timeout)
            output = self._format_output(self.verify(20,
                                                     ssh_client.exec_command,
                                                     1,
                                                     "Cannot get cluster "
                                                     "status for %s node." %
                                                     node,
                                                     "Retrieve cluster status "
                                                     "for each the controller",
                                                     cmd))
            nodes.append({'controller': node, 'clusters': output})
            #for HA configuration number of controllers and
        #number of clusters should be the same
        _num_of_controllers = len(self._controllers)
        _aux_node_clusters = sorted(nodes[0]['clusters'])
        for node in nodes[1:]:
            self.assertEqual(_num_of_controllers, len(node["clusters"]),
                             'Step 2 failed: The number of clusters for '
                             'node %s is not equal to number of controllers' %
                             node['controller'])

            self.assertTrue(map(eq, sorted(node['clusters']),
                                _aux_node_clusters), "Step 3 failed: Cluster "
                                                     "lists on nodes %s"
                                                     " and %s are different" %
                                                     (nodes[0]["controller"],
                                                      node["controller"]))

    @attr(type=['fuel', 'ha', 'non-destructive'])
    def test_002_rabbit_queues(self):
        """RabbitMQ queues availability
        Scenario:
          1. Retrieve list of RabbitMQ queues for each controller
          2. Check the same queue list is present on each node
        Duration: 100 s.
        """
        if not self.amqp_clients:
            self.fail('Step 1 failed: There are no controller nodes.')
        first_list = self.amqp_clients[0].list_queues()
        for client in self.amqp_clients[1:]:
            list = client.list_queues()
            if not list:
                self.fail('Step 1 failed: Cannot retrieve queues list for '
                          '{ctlr} controller.'.format(ctlr=client.host))
            if list != first_list:
                self.fail('Step 2 failed: Queue lists for controllers {ctlr1}'
                          ' and {ctlr2} are different.'.format(
                    ctlr1=client.host,
                    ctlr2=self.amqp_clients[0].host)
                )


    @attr(type=['fuel', 'ha', 'non-destructive'])
    def test_003_rabbit_messages(self):
        """RabbitMQ messages availability
        Scenario:
          1. Create a queue on a controller
          2. Create an exchange on the controller
          3. Create a binding for the queue and exchange
          4. Publish messages to the queue.
          5. Check the messages are available on other controllers
          6. Delete the exchange
          7. Delete the queue (with binding)
        Duration: 100 s.
        """
        if not self._controllers:
            self.fail('Step 1 failed: There are no controller nodes.')

        new_queue = rand_name(name='ostf1-test-queue-')
{'fraction_non_alphanumeric': 0.06970649895178196, 'fraction_numerical': 0.008610362383947289, 'mean_word_length': 1.369522795813376, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 83, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '955507', 'n_tokens_mistral': 3194, 'n_tokens_neox': 3001, 'n_words': 980}