<commit_before>import logging
import shutil

import os
from bzt import TaurusException
from tests import BZTestCase, __dir__

from bzt.cli import CLI, ConfigOverrider, get_option_parser
from bzt.engine import Configuration
from tests.mocks import EngineEmul, ModuleMock


class TestCLI(BZTestCase):
    def setUp(self):
        super(TestCLI, self).setUp()
        self.log = os.path.join(os.path.dirname(__file__), "..", "build", "bzt.log")
        self.verbose = False
        self.quiet = False
        self.no_system_configs = True
        self.option = []
        self.datadir = os.path.join(os.path.dirname(__file__), "..", "build", "acli")
        self.obj = CLI(self)
        self.aliases = []
        self.obj.engine = EngineEmul()

    def test_perform_normal(self):
        ret = self.obj.perform([__dir__() + "/json/mock_normal.json"])
        self.assertEquals(0, ret)

    def test_perform_aliases(self):
        self.aliases = ['test']
        ret = self.obj.perform([__dir__() + "/json/mock_normal.json"])
        self.assertEquals(0, ret)
        self.assertTrue(self.obj.engine.config['marker'])

    def test_perform_prepare_exc(self):
        self.obj.engine.prepare_exc = TaurusException()
        ret = self.obj.perform([__dir__() + "/json/mock_normal.json"])
        self.assertEquals(1, ret)

    def test_perform_overrides(self):
        self.option.append("test.subkey5.-1=value")
        self.option.append("modules.mock=" + ModuleMock.__module__ + "." + ModuleMock.__name__)
        self.option.append("provisioning=mock")
        self.option.append("settings.artifacts-dir=build/test/%Y-%m-%d_%H-%M-%S.%f")
        self.option.append("test.subkey2.0.sskey=value")
        self.option.append("test.subkey.0=value")
        self.option.append("execution.-1.option=value")
        ret = self.obj.perform([])
        self.assertEquals(0, ret)

    def test_perform_overrides_fail(self):
        self.option.append("test.subkey2.0.sskey=value")
        self.option.append("test.subkey.0=value")
        ret = self.obj.perform([__dir__() + "/json/mock_normal.json"])
        self.assertEquals(1, ret)

    def test_perform_prepare_err(self):
        ret = self.obj.perform([__dir__() + "/json/mock_prepare_err.json"])
        self.assertEquals(1, ret)

        prov = self.obj.engine.provisioning

        self.assertTrue(prov.was_prepare)
        self.assertFalse(prov.was_startup)
        self.assertFalse(prov.was_check)
        self.assertFalse(prov.was_shutdown)
        self.assertTrue(prov.was_postproc)

    def test_perform_start_err(self):
        conf = __dir__() + "/json/mock_start_err.json"
        self.assertEquals(1, self.obj.perform([conf]))

        prov = self.obj.engine.provisioning
        self.assertTrue(prov.was_prepare)
        self.assertTrue(prov.was_startup)
        self.assertFalse(prov.was_check)
        self.assertTrue(prov.was_shutdown)
        self.assertTrue(prov.was_postproc)

    def test_perform_wait_err(self):
        conf = __dir__() + "/json/mock_wait_err.json"
        self.assertEquals(1, self.obj.perform([conf]))

        prov = self.obj.engine.provisioning
        self.assertTrue(prov.was_prepare)
        self.assertTrue(prov.was_startup)
        self.assertTrue(prov.was_check)
        self.assertTrue(prov.was_shutdown)
        self.assertTrue(prov.was_postproc)

    def test_perform_end_err(self):
        conf = __dir__() + "/json/mock_end_err.json"
        self.assertEquals(1, self.obj.perform([conf]))

        prov = self.obj.engine.provisioning
        self.assertTrue(prov.was_prepare)
        self.assertTrue(prov.was_startup)
        self.assertTrue(prov.was_check)
        self.assertTrue(prov.was_shutdown)
        self.assertTrue(prov.was_postproc)

    def test_perform_postproc_err(self):
        conf = __dir__() + "/json/mock_postproc_err.json"
        self.assertEquals(3, self.obj.perform([conf]))

        prov = self.obj.engine.provisioning
        self.assertTrue(prov.was_prepare)
        self.assertTrue(prov.was_startup)
        self.assertTrue(prov.was_check)
        self.assertTrue(prov.was_shutdown)
        self.assertTrue(prov.was_postproc)

    def test_jmx_shorthand(self):
        ret = self.obj.perform([
            __dir__() + "/json/mock_normal.json",
            __dir__() + "/jmx/dummy.jmx",
            __dir__() + "/jmx/dummy.jmx",
        ])
        self.assertEquals(0, ret)

    def test_override_artifacts_dir(self):
        # because EngineEmul sets up its own artifacts_dir
        self.obj.engine.artifacts_dir = None
        artifacts_dir = "build/tmp-test-artifacts"

        self.option.append("modules.mock=" + ModuleMock.__module__ + "." + ModuleMock.__name__)
        self.option.append("provisioning=mock")
        self.option.append("settings.artifacts-dir=%s" % artifacts_dir)
        try:
            ret = self.obj.perform([])
            self.assertEquals(0, ret)
            self.assertTrue(os.path.exists(artifacts_dir))
        finally:
            # cleanup artifacts dir
            if os.path.exists(artifacts_dir):
                shutil.rmtree(artifacts_dir)

    def test_logging_verbosity_adjustment(self):
        self.verbose = False
        ret = self.obj.perform([
            __dir__() + "/json/mock_normal.json",
        ])
        self.assertEquals(0, ret)
        log_lines = open(os.path.join(self.obj.engine.artifacts_dir, "bzt.log")).readlines()
        checking = False
        found_line = False
        for line in log_lines:
            if "Leveling down" in line:
                found_line = True
                checking = True
            elif "Leveled up" in line:
                checking = False
            else:
                if checking:
                    self.assertNotIn("DEBUG", line)
        self.assertTrue(found_line)

    def test_cover_option_parser(self):
        parser = get_option_parser()
        parsed_options, parsed_configs = parser.parse_args()
        self.assertEquals([], parsed_options.aliases)


class TestConfigOverrider(BZTestCase):
    def setUp(self):
        self.obj = ConfigOverrider(logging.getLogger())
        self.config = Configuration()

    def test_numbers(self):
        self.obj.apply_overrides(["int=11", "float=3.14"], self.config)
        self.assertEqual(self.config.get("int"), int(11))
        self.assertEqual(self.config.get("float"), float(3.14))

    def test_booleans(self):
        self.obj.apply_overrides(["yes=true", "no=false"], self.config)
        self.assertEqual(self.config.get("yes"), bool(True))
        self.assertEqual(self.config.get("no"), bool(False))

    def test_strings(self):
        self.obj.apply_overrides(["plain=ima plain string",
                                  """quoted='"ima quoted string"'""",
                                  """empty-quoted='""'""",
                                  '''escaped="a "b" 'c' d"''',
                                  '''escaped-quoted="a "b" 'c' d"'''], self.config)
        self.assertEqual(self.config.get("plain"), str("ima plain string"))
        self.assertEqual(self.config.get("quoted"), str('''"ima quoted string"'''))
        self.assertEqual(self.config.get("empty-quoted"), str('""'))<commit_msg>Fix jenkins problems<commit_after>import logging
import shutil
import traceback

import os
from bzt import TaurusException
from tests import BZTestCase, __dir__

from bzt.cli import CLI, ConfigOverrider, get_option_parser
from bzt.engine import Configuration
from tests.mocks import EngineEmul, ModuleMock


class TestCLI(BZTestCase):
    def setUp(self):
        super(TestCLI, self).setUp()
        self.log = os.path.join(os.path.dirname(__file__), "..", "build", "bzt.log")
        self.verbose = False
        self.quiet = False
        self.no_system_configs = True
        self.option = []
        self.datadir = os.path.join(os.path.dirname(__file__), "..", "build", "acli")
        self.obj = CLI(self)
        self.aliases = []
        self.obj.engine = EngineEmul()

    def test_perform_normal(self):
        ret = self.obj.perform([__dir__() + "/json/mock_normal.json"])
        self.assertEquals(0, ret)

    def test_perform_aliases(self):
        self.aliases = ['test']
        ret = self.obj.perform([__dir__() + "/json/mock_normal.json"])
        self.assertEquals(0, ret)
        self.assertTrue(self.obj.engine.config['marker'])

    def test_perform_prepare_exc(self):
        self.obj.engine.prepare_exc = TaurusException()
        ret = self.obj.perform([__dir__() + "/json/mock_normal.json"])
        self.assertEquals(1, ret)

    def test_perform_overrides(self):
        self.option.append("test.subkey5.-1=value")
        self.option.append("modules.mock=" + ModuleMock.__module__ + "." + ModuleMock.__name__)
        self.option.append("provisioning=mock")
        self.option.append("settings.artifacts-dir=build/test/%Y-%m-%d_%H-%M-%S.%f")
        self.option.append("test.subkey2.0.sskey=value")
        self.option.append("test.subkey.0=value")
        self.option.append("execution.-1.option=value")
        ret = self.obj.perform([])
        self.assertEquals(0, ret)

    def test_perform_overrides_fail(self):
        self.option.append("test.subkey2.0.sskey=value")
        self.option.append("test.subkey.0=value")
        ret = self.obj.perform([__dir__() + "/json/mock_normal.json"])
        self.assertEquals(1, ret)

    def test_perform_prepare_err(self):
        ret = self.obj.perform([__dir__() + "/json/mock_prepare_err.json"])
        self.assertEquals(1, ret)

        prov = self.obj.engine.provisioning

        self.assertTrue(prov.was_prepare)
        self.assertFalse(prov.was_startup)
        self.assertFalse(prov.was_check)
        self.assertFalse(prov.was_shutdown)
        self.assertTrue(prov.was_postproc)

    def test_perform_start_err(self):
        conf = __dir__() + "/json/mock_start_err.json"
        self.assertEquals(1, self.obj.perform([conf]))

        prov = self.obj.engine.provisioning
        self.assertTrue(prov.was_prepare)
        self.assertTrue(prov.was_startup)
        self.assertFalse(prov.was_check)
        self.assertTrue(prov.was_shutdown)
        self.assertTrue(prov.was_postproc)

    def test_perform_wait_err(self):
        conf = __dir__() + "/json/mock_wait_err.json"
        self.assertEquals(1, self.obj.perform([conf]))

        prov = self.obj.engine.provisioning
        self.assertTrue(prov.was_prepare)
        self.assertTrue(prov.was_startup)
        self.assertTrue(prov.was_check)
        self.assertTrue(prov.was_shutdown)
        self.assertTrue(prov.was_postproc)

    def test_perform_end_err(self):
        conf = __dir__() + "/json/mock_end_err.json"
        self.assertEquals(1, self.obj.perform([conf]))

        prov = self.obj.engine.provisioning
        self.assertTrue(prov.was_prepare)
        self.assertTrue(prov.was_startup)
        self.assertTrue(prov.was_check)
        self.assertTrue(prov.was_shutdown)
        self.assertTrue(prov.was_postproc)

    def test_perform_postproc_err(self):
        conf = __dir__() + "/json/mock_postproc_err.json"
        self.assertEquals(3, self.obj.perform([conf]))

        prov = self.obj.engine.provisioning
        self.assertTrue(prov.was_prepare)
        self.assertTrue(prov.was_startup)
        self.assertTrue(prov.was_check)
        self.assertTrue(prov.was_shutdown)
        self.assertTrue(prov.was_postproc)

    def test_jmx_shorthand(self):
        ret = self.obj.perform([
            __dir__() + "/json/mock_normal.json",
            __dir__() + "/jmx/dummy.jmx",
            __dir__() + "/jmx/dummy.jmx",
        ])
        self.assertEquals(0, ret)

    def test_override_artifacts_dir(self):
        # because EngineEmul sets up its own artifacts_dir
        self.obj.engine.artifacts_dir = None
        artifacts_dir = "build/tmp-test-artifacts"

        self.option.append("modules.mock=" + ModuleMock.__module__ + "." + ModuleMock.__name__)
        self.option.append("provisioning=mock")
        self.option.append("settings.artifacts-dir=%s" % artifacts_dir)
        try:
            ret = self.obj.perform([])
            self.assertEquals(0, ret)
            self.assertTrue(os.path.exists(artifacts_dir))
        finally:
            # cleanup artifacts dir
            if os.path.exists(artifacts_dir):
                shutil.rmtree(artifacts_dir)

    def test_logging_verbosity_adjustment(self):
        self.verbose = False
        ret = self.obj.perform([
            __dir__() + "/json/mock_normal.json",
        ])
        self.assertEquals(0, ret)
        log_lines = open(os.path.join(self.obj.engine.artifacts_dir, "bzt.log")).readlines()
        checking = False
        found_line = False
        for line in log_lines:
            if "Leveling down" in line:
                found_line = True
                checking = True
            elif "Leveled up" in line:
                checking = False
            else:
                if checking:
                    self.assertNotIn("DEBUG", line)
        self.assertTrue(found_line)

    def test_cover_option_parser(self):
        parser = get_option_parser()
        parser.print_usage()


class TestConfigOverrider(BZTestCase):
    def setUp(self):
        self.obj = ConfigOverrider(logging.getLogger())
        self.config = Configuration()

    def test_numbers(self):
        self.obj.apply_overrides(["int=11", "float=3.14"], self.config)
        self.assertEqual(self.config.get("int"), int(11))
        self.assertEqual(self.config.get("float"), float(3.14))

    def test_booleans(self):
        self.obj.apply_overrides(["yes=true", "no=false"], self.config)
        self.assertEqual(self.config.get("yes"), bool(True))
        self.assertEqual(self.config.get("no"), bool(False))

    def test_strings(self):
        self.obj.apply_overrides(["plain=ima plain string",
                                  """quoted='"ima quoted string"'""",
                                  """empty-quoted='""'""",
                                  '''escaped="a "b" 'c' d"''',
                                  '''escaped-quoted="a "b" 'c' d"'''], self.config)
        self.assertEqual(self.config.get("plain"), str("ima plain string"))
        self.assertEqual(self.config.get("quoted"), str('''"ima quoted string"'''))
        self.assertEqual(self.config.get("empty-quoted"), str('""'))
{'fraction_non_alphanumeric': 0.1344625679631953, 'fraction_numerical': 0.004461173846368326, 'mean_word_length': 2.526794493608653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6465849', 'n_tokens_mistral': 4484, 'n_tokens_neox': 4432, 'n_words': 651}