<commit_before># Copyright (c) 2014 Red Hat, Inc.
# All Rights Reserved.
#
#    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_config import cfg
from oslo_log import log as logging

from neutron.agent.linux import external_process
from neutron.agent.linux import keepalived
from neutron.agent.linux import utils
from neutron.tests import base
from neutron.tests.unit.agent.linux import test_keepalived


LOG = logging.getLogger(__name__)


class KeepalivedManagerTestCase(base.BaseTestCase,
                                test_keepalived.KeepalivedConfBaseMixin):

    def setUp(self):
        super(KeepalivedManagerTestCase, self).setUp()
        cfg.CONF.set_override('check_child_processes_interval', 1, 'AGENT')

        self.expected_config = self._get_config()
        self.process_monitor = external_process.ProcessMonitor(cfg.CONF,
                                                               'router')
        self.manager = keepalived.KeepalivedManager(
            'router1', self.expected_config, self.process_monitor,
            conf_path=cfg.CONF.state_path)
        self.addCleanup(self.manager.disable)

    def test_keepalived_spawn(self):
        self.manager.spawn()
        process = external_process.ProcessManager(
            cfg.CONF,
            'router1',
            namespace=None,
            pids_path=cfg.CONF.state_path)
        self.assertTrue(process.active)

        self.assertEqual(self.expected_config.get_config_str(),
                         self.manager.get_conf_on_disk())

    def test_keepalived_respawns(self):<commit_msg>Skip keepalived_respawns test<commit_after># Copyright (c) 2014 Red Hat, Inc.
# All Rights Reserved.
#
#    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 distutils import version
import re

from oslo_config import cfg
from oslo_log import log as logging
import testtools

from neutron.agent.linux import external_process
from neutron.agent.linux import keepalived
from neutron.agent.linux import utils
from neutron.tests import base
from neutron.tests.unit.agent.linux import test_keepalived


LOG = logging.getLogger(__name__)


def keepalived_version_not_supported():
    try:
        cmd = ['keepalived', '--version']
        out = utils.execute(cmd, return_stderr=True)
        ver = re.search(r"Keepalived v(\d+\.\d+\.\d+)", out[1]).group(1)
        return version.LooseVersion(ver) >= version.LooseVersion("1.2.11")
    except (OSError, RuntimeError, IndexError, ValueError) as e:
        LOG.debug("Exception while checking keepalived version. "
                  "Exception: %s", e)
        return False


class KeepalivedManagerTestCase(base.BaseTestCase,
                                test_keepalived.KeepalivedConfBaseMixin):

    def setUp(self):
        super(KeepalivedManagerTestCase, self).setUp()
        cfg.CONF.set_override('check_child_processes_interval', 1, 'AGENT')

        self.expected_config = self._get_config()
        self.process_monitor = external_process.ProcessMonitor(cfg.CONF,
                                                               'router')
        self.manager = keepalived.KeepalivedManager(
            'router1', self.expected_config, self.process_monitor,
            conf_path=cfg.CONF.state_path)
        self.addCleanup(self.manager.disable)

    def test_keepalived_spawn(self):
        self.manager.spawn()
        process = external_process.ProcessManager(
            cfg.CONF,
            'router1',
            namespace=None,
            pids_path=cfg.CONF.state_path)
        self.assertTrue(process.active)

        self.assertEqual(self.expected_config.get_config_str(),
                         self.manager.get_conf_on_disk())

    @testtools.skipIf(keepalived_version_not_supported(), 'bug/1511311')
    def test_keepalived_respawns(self):
{'fraction_non_alphanumeric': 0.08328166976648067, 'fraction_numerical': 0.007232899359371771, 'mean_word_length': 2.817034700315457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2556494', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1311, 'n_words': 356}