<commit_before>"""Unit tests to test infoservices configuration class"""
# This has been copied from test_cemon.py and has had the names changed.
# Further changes will be needed to reduce duplication between the two
# sets of tests; changes will need to be made to the configure modules
# to reduce duplication there as well.

# pylint: disable=W0703
# pylint: disable=R0904

import os
import sys
import unittest
import ConfigParser
import logging

# setup system library path
pathname = os.path.realpath('../')
sys.path.insert(0, pathname)

from osg_configure.modules import exceptions
from osg_configure.configure_modules import infoservices
from  osg_configure.modules import utilities
from osg_configure.modules.utilities import get_test_config

global_logger = logging.getLogger(__name__)
if sys.version_info[0] >= 2 and sys.version_info[1] > 6:
    global_logger.addHandler(logging.NullHandler())
else:
    class NullHandler(logging.Handler):
        def emit(self, record):
            pass

    global_logger.addHandler(NullHandler())


class TestInfoServices(unittest.TestCase):
    """
    Unit test class to test InfoServicesConfiguration class
    """

    def testParsing1(self):
        """
        Test infoservices parsing
        """

        config_file = get_test_config("infoservices/infoservices.ini")
        configuration = ConfigParser.SafeConfigParser()
        configuration.read(config_file)

        settings = infoservices.InfoServicesConfiguration(logger=global_logger)
        try:
            settings.parse_configuration(configuration)
        except Exception, e:
            self.fail("Received exception while parsing configuration: %s" % e)

        options = settings.options
        variables = {'bdii_servers': 'http://is1.grid.iu.edu:14001[RAW], http://is2.grid.iu.edu:14001[RAW]'}

        for var in variables:
            self.assertTrue(options.has_key(var),
                            "Option %s missing" % var)
            self.assertEqual(options[var].value,
                             variables[var],
                             "Wrong value obtained for %s, got %s but " \
                             "expected %s" % (var,
                                              options[var].value,
                                              variables[var]))

    def testParsingITBDefaults(self):
        """
        Test infoservices parsing to make sure it picks up ITB defaults
        """

        # need to be on a CE to get CE defaults
        if not utilities.ce_installed():
            return
        config_file = get_test_config("infoservices/itb_defaults.ini")
        configuration = ConfigParser.SafeConfigParser()
        configuration.read(config_file)

        settings = infoservices.InfoServicesConfiguration(logger=global_logger)
        try:
            settings.parse_configuration(configuration)
        except Exception, e:
            self.fail("Received exception while parsing configuration: %s" % e)

        options = settings.options
        variables = {'bdii_servers': 'http://is1.grid.iu.edu:14001[RAW],'
                                     'http://is2.grid.iu.edu:14001[RAW]'}

        for var in variables:
            self.assertTrue(options.has_key(var),
                            "Option %s missing" % var)
            self.assertEqual(options[var].value,
                             variables[var],
                             "Wrong value obtained for %s, got %s but " \
                             "expected %s" % (var,
                                              options[var].value,
                                              variables[var]))

    def testParsingProductionDefaults(self):
        """
        Test infoservices parsing to make sure it picks up production defaults
        """

        # need to be on a CE to get CE defaults
        if not utilities.ce_installed():
            return
        config_file = get_test_config("infoservices/prod_defaults.ini")
        configuration = ConfigParser.SafeConfigParser()
        configuration.read(config_file)

        settings = infoservices.InfoServicesConfiguration(logger=global_logger)
        try:
            settings.parse_configuration(configuration)
        except Exception, e:
            self.fail("Received exception while parsing configuration: %s" % e)

        options = settings.options
        variables = {'bdii_servers': 'http://is1.grid.iu.edu:14001[RAW],'
                                     'http://is2.grid.iu.edu:14001[RAW]'}

        for var in variables:
            self.assertTrue(options.has_key(var),
                            "Option %s missing" % var)
            self.assertEqual(options[var].value,
                             variables[var],
                             "Wrong value obtained for %s, got %s but " \
                             "expected %s" % (var,
                                              options[var].value,
                                              variables[var]))

    def testParsingMissingITBDefaults(self):
        """
        Test infoservices parsing to make sure it picks up ITB defaults
        when the infoservices section is missing
        """

        # need to be on a CE to get CE defaults
        if not utilities.ce_installed():
            return
        config_file = get_test_config("infoservices/itb_defaults2.ini")
        configuration = ConfigParser.SafeConfigParser()
        configuration.read(config_file)

        settings = infoservices.InfoServicesConfiguration(logger=global_logger)
        try:
            settings.parse_configuration(configuration)
        except Exception, e:
            self.fail("Received exception while parsing configuration: %s" % e)

        options = settings.options
        variables = {'bdii_servers': 'http://is1.grid.iu.edu:14001[RAW],'
                                     'http://is2.grid.iu.edu:14001[RAW]'}

        for var in variables:
            self.assertTrue(options.has_key(var),
                            "Option %s missing" % var)
            self.assertEqual(options[var].value,
                             variables[var],
                             "Wrong value obtained for %s, got %s but " \
                             "expected %s" % (var,
                                              options[var].value,
                                              variables[var]))

    def testParsingMissingProductionDefaults(self):
        """
        Test infoservices parsing to make sure it picks up production defaults
        when the infoservices section is missing
        """

        # need to be on a CE to get CE defaults
        if not utilities.ce_installed():
            return
        config_file = get_test_config("infoservices/prod_defaults2.ini")<commit_msg>Skip failing info services tests if CE is installed but osg-info-services isn't (SOFTWARE-2655)<commit_after>"""Unit tests to test infoservices configuration class"""
# This has been copied from test_cemon.py and has had the names changed.
# Further changes will be needed to reduce duplication between the two
# sets of tests; changes will need to be made to the configure modules
# to reduce duplication there as well.

# pylint: disable=W0703
# pylint: disable=R0904

import os
import sys
import unittest
import ConfigParser
import logging

# setup system library path
pathname = os.path.realpath('../')
sys.path.insert(0, pathname)

from osg_configure.modules import exceptions
from osg_configure.configure_modules import infoservices
from  osg_configure.modules import utilities
from osg_configure.modules.utilities import get_test_config

global_logger = logging.getLogger(__name__)
if sys.version_info[0] >= 2 and sys.version_info[1] > 6:
    global_logger.addHandler(logging.NullHandler())
else:
    class NullHandler(logging.Handler):
        def emit(self, record):
            pass

    global_logger.addHandler(NullHandler())


class TestInfoServices(unittest.TestCase):
    """
    Unit test class to test InfoServicesConfiguration class
    """

    def testParsing1(self):
        """
        Test infoservices parsing
        """

        config_file = get_test_config("infoservices/infoservices.ini")
        configuration = ConfigParser.SafeConfigParser()
        configuration.read(config_file)

        settings = infoservices.InfoServicesConfiguration(logger=global_logger)
        try:
            settings.parse_configuration(configuration)
        except Exception, e:
            self.fail("Received exception while parsing configuration: %s" % e)

        options = settings.options
        variables = {'bdii_servers': 'http://is1.grid.iu.edu:14001[RAW], http://is2.grid.iu.edu:14001[RAW]'}

        for var in variables:
            self.assertTrue(options.has_key(var),
                            "Option %s missing" % var)
            self.assertEqual(options[var].value,
                             variables[var],
                             "Wrong value obtained for %s, got %s but " \
                             "expected %s" % (var,
                                              options[var].value,
                                              variables[var]))

    def testParsingITBDefaults(self):
        """
        Test infoservices parsing to make sure it picks up ITB defaults
        """

        # need to be on a CE to get CE defaults
        if not utilities.ce_installed():
            return
        config_file = get_test_config("infoservices/itb_defaults.ini")
        configuration = ConfigParser.SafeConfigParser()
        configuration.read(config_file)

        settings = infoservices.InfoServicesConfiguration(logger=global_logger)
        try:
            settings.parse_configuration(configuration)
        except Exception, e:
            self.fail("Received exception while parsing configuration: %s" % e)

        options = settings.options
        variables = {'bdii_servers': 'http://is1.grid.iu.edu:14001[RAW],'
                                     'http://is2.grid.iu.edu:14001[RAW]'}

        for var in variables:
            self.assertTrue(options.has_key(var),
                            "Option %s missing" % var)
            self.assertEqual(options[var].value,
                             variables[var],
                             "Wrong value obtained for %s, got %s but " \
                             "expected %s" % (var,
                                              options[var].value,
                                              variables[var]))

    def testParsingProductionDefaults(self):
        """
        Test infoservices parsing to make sure it picks up production defaults
        """

        # need to be on a CE to get CE defaults
        if not utilities.ce_installed():
            return
        config_file = get_test_config("infoservices/prod_defaults.ini")
        configuration = ConfigParser.SafeConfigParser()
        configuration.read(config_file)

        settings = infoservices.InfoServicesConfiguration(logger=global_logger)
        try:
            settings.parse_configuration(configuration)
        except Exception, e:
            self.fail("Received exception while parsing configuration: %s" % e)

        options = settings.options
        variables = {'bdii_servers': 'http://is1.grid.iu.edu:14001[RAW],'
                                     'http://is2.grid.iu.edu:14001[RAW]'}

        for var in variables:
            self.assertTrue(options.has_key(var),
                            "Option %s missing" % var)
            self.assertEqual(options[var].value,
                             variables[var],
                             "Wrong value obtained for %s, got %s but " \
                             "expected %s" % (var,
                                              options[var].value,
                                              variables[var]))

    def testParsingMissingITBDefaults(self):
        """
        Test infoservices parsing to make sure it picks up ITB defaults
        when the infoservices section is missing
        """

        # need to be on a CE to get CE defaults
        if not (utilities.ce_installed() and utilities.rpm_installed(['gip', 'osg-info-services'])):
            return
        config_file = get_test_config("infoservices/itb_defaults2.ini")
        configuration = ConfigParser.SafeConfigParser()
        configuration.read(config_file)

        settings = infoservices.InfoServicesConfiguration(logger=global_logger)
        try:
            settings.parse_configuration(configuration)
        except Exception, e:
            self.fail("Received exception while parsing configuration: %s" % e)

        options = settings.options
        variables = {'bdii_servers': 'http://is1.grid.iu.edu:14001[RAW],'
                                     'http://is2.grid.iu.edu:14001[RAW]'}

        for var in variables:
            self.assertTrue(options.has_key(var),
                            "Option %s missing" % var)
            self.assertEqual(options[var].value,
                             variables[var],
                             "Wrong value obtained for %s, got %s but " \
                             "expected %s" % (var,
                                              options[var].value,
                                              variables[var]))

    def testParsingMissingProductionDefaults(self):
        """
        Test infoservices parsing to make sure it picks up production defaults
        when the infoservices section is missing
        """

        # need to be on a CE to get CE defaults
        if not (utilities.ce_installed() and utilities.rpm_installed(['gip', 'osg-info-services'])):
            return
        config_file = get_test_config("infoservices/prod_defaults2.ini")
{'fraction_non_alphanumeric': 0.08631945451907733, 'fraction_numerical': 0.009574931089511098, 'mean_word_length': 1.8580016583747927, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6584347', 'n_tokens_mistral': 3500, 'n_tokens_neox': 3359, 'n_words': 889}