<commit_before># Copyright (c) 2012 OpenStack Foundation.
#
# 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.

import contextlib
import os

import fixtures
import mock
import webob.exc

from neutron.common import constants
from neutron.common.test_lib import test_config
from neutron.common import topics
from neutron import context
from neutron.db import db_base_plugin_v2
from neutron import manager
from neutron.plugins.nec.common import exceptions as nexc
from neutron.plugins.nec.db import api as ndb
from neutron.plugins.nec import nec_plugin
from neutron.tests.unit.nec import fake_ofc_manager
from neutron.tests.unit import test_db_plugin as test_plugin


PLUGIN_NAME = 'neutron.plugins.nec.nec_plugin.NECPluginV2'
OFC_MANAGER = 'neutron.plugins.nec.nec_plugin.ofc_manager.OFCManager'
NOTIFIER = 'neutron.plugins.nec.nec_plugin.NECPluginV2AgentNotifierApi'
NEC_PLUGIN_INI = """
[DEFAULT]
api_extensions_path = neutron/plugins/nec/extensions
[OFC]
driver = neutron.tests.unit.nec.stub_ofc_driver.StubOFCDriver
enable_packet_filter = False
"""


class NecPluginV2TestCaseBase(object):
    _nec_ini = NEC_PLUGIN_INI

    def _set_nec_ini(self):
        self.nec_ini_file = self.useFixture(fixtures.TempDir()).join("nec.ini")
        with open(self.nec_ini_file, 'w') as f:
            f.write(self._nec_ini)
        if 'config_files' in test_config.keys():
            for c in test_config['config_files']:
                if c.rfind("/nec.ini") > -1:
                    test_config['config_files'].remove(c)
            test_config['config_files'].append(self.nec_ini_file)
        else:
            test_config['config_files'] = [self.nec_ini_file]

    def _clean_nec_ini(self):
        test_config['config_files'].remove(self.nec_ini_file)
        os.remove(self.nec_ini_file)
        self.nec_ini_file = None

    def patch_remote_calls(self, use_stop=False):
        self.plugin_notifier_p = mock.patch(NOTIFIER)
        self.ofc_manager_p = mock.patch(OFC_MANAGER)
        self.plugin_notifier_p.start()
        self.ofc_manager_p.start()
        # When using mock.patch.stopall, we need to ensure
        # stop is not used anywhere in a single test.
        # In Neutron several tests use stop for each patched object,
        # so we need to take care of both cases.
        if use_stop:
            self.addCleanup(self.plugin_notifier_p.stop)
            self.addCleanup(self.ofc_manager_p.stop)

    def setup_nec_plugin_base(self, use_stop_all=True,
                              use_stop_each=False):
        # If use_stop_each is set, use_stop_all cannot be set.
        if use_stop_all and not use_stop_each:
            self.addCleanup(mock.patch.stopall)
        self._set_nec_ini()
        self.addCleanup(self._clean_nec_ini)
        self.patch_remote_calls(use_stop_each)


class NecPluginV2TestCase(NecPluginV2TestCaseBase,
                          test_plugin.NeutronDbPluginV2TestCase):

    _plugin_name = PLUGIN_NAME

    def rpcapi_update_ports(self, agent_id='nec-q-agent.fake',
                            datapath_id="0xabc", added=[], removed=[]):
        kwargs = {'topic': topics.AGENT,
                  'agent_id': agent_id,
                  'datapath_id': datapath_id,
                  'port_added': added, 'port_removed': removed}
        self.callback_nec.update_ports(self.context, **kwargs)
<commit_msg>Stop mock patches by default in base test class<commit_after># Copyright (c) 2012 OpenStack Foundation.
#
# 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.

import contextlib
import os

import fixtures
import mock
import webob.exc

from neutron.common import constants
from neutron.common.test_lib import test_config
from neutron.common import topics
from neutron import context
from neutron.db import db_base_plugin_v2
from neutron import manager
from neutron.plugins.nec.common import exceptions as nexc
from neutron.plugins.nec.db import api as ndb
from neutron.plugins.nec import nec_plugin
from neutron.tests.unit.nec import fake_ofc_manager
from neutron.tests.unit import test_db_plugin as test_plugin


PLUGIN_NAME = 'neutron.plugins.nec.nec_plugin.NECPluginV2'
OFC_MANAGER = 'neutron.plugins.nec.nec_plugin.ofc_manager.OFCManager'
NOTIFIER = 'neutron.plugins.nec.nec_plugin.NECPluginV2AgentNotifierApi'
NEC_PLUGIN_INI = """
[DEFAULT]
api_extensions_path = neutron/plugins/nec/extensions
[OFC]
driver = neutron.tests.unit.nec.stub_ofc_driver.StubOFCDriver
enable_packet_filter = False
"""


class NecPluginV2TestCaseBase(object):
    _nec_ini = NEC_PLUGIN_INI

    def _set_nec_ini(self):
        self.nec_ini_file = self.useFixture(fixtures.TempDir()).join("nec.ini")
        with open(self.nec_ini_file, 'w') as f:
            f.write(self._nec_ini)
        if 'config_files' in test_config.keys():
            for c in test_config['config_files']:
                if c.rfind("/nec.ini") > -1:
                    test_config['config_files'].remove(c)
            test_config['config_files'].append(self.nec_ini_file)
        else:
            test_config['config_files'] = [self.nec_ini_file]

    def _clean_nec_ini(self):
        test_config['config_files'].remove(self.nec_ini_file)
        os.remove(self.nec_ini_file)
        self.nec_ini_file = None

    def patch_remote_calls(self, use_stop=False):
        self.plugin_notifier_p = mock.patch(NOTIFIER)
        self.ofc_manager_p = mock.patch(OFC_MANAGER)
        self.plugin_notifier_p.start()
        self.ofc_manager_p.start()
        # When using mock.patch.stopall, we need to ensure
        # stop is not used anywhere in a single test.
        # In Neutron several tests use stop for each patched object,
        # so we need to take care of both cases.
        if use_stop:
            self.addCleanup(self.plugin_notifier_p.stop)
            self.addCleanup(self.ofc_manager_p.stop)

    def setup_nec_plugin_base(self, use_stop_all=True,
                              use_stop_each=False):
        # If use_stop_each is set, use_stop_all cannot be set.
        if use_stop_all and not use_stop_each:
            self.addCleanup(mock.patch.stopall)
        self._set_nec_ini()
        self.addCleanup(self._clean_nec_ini)
        # kevinbenton: stopping patches is now handled in base test class
        self.patch_remote_calls(use_stop=False)


class NecPluginV2TestCase(NecPluginV2TestCaseBase,
                          test_plugin.NeutronDbPluginV2TestCase):

    _plugin_name = PLUGIN_NAME

    def rpcapi_update_ports(self, agent_id='nec-q-agent.fake',
                            datapath_id="0xabc", added=[], removed=[]):
        kwargs = {'topic': topics.AGENT,
                  'agent_id': agent_id,
                  'datapath_id': datapath_id,
                  'port_added': added, 'port_removed': removed}
        self.callback_nec.update_ports(self.context, **kwargs)

{'fraction_non_alphanumeric': 0.08494554772581678, 'fraction_numerical': 0.004356181934657271, 'mean_word_length': 3.174331550802139, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5312895', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2403, 'n_words': 596}