# Path: neutron/agent/metadata/agent.py
# LOG = logging.getLogger(__name__)
# MODE_MAP = {
#     config.USER_MODE: 0o644,
#     config.GROUP_MODE: 0o664,
#     config.ALL_MODE: 0o666,
# }
# class MetadataPluginAPI(object):
# class MetadataProxyHandler(object):
# class UnixDomainMetadataProxy(object):
#     def __init__(self, topic):
#     def get_ports(self, context, filters):
#     def __init__(self, conf):
#     def __call__(self, req):
#     def _get_ports_from_server(self, router_id=None, ip_address=None,
#                                networks=None, mac_address=None):
#     def _get_port_filters(self, router_id=None, ip_address=None,
#                           networks=None, mac_address=None):
#     def _get_router_networks(self, router_id, skip_cache=False):
#     def _get_ports_for_remote_address(self, remote_address, networks,
#                                       skip_cache=False,
#                                       remote_mac=None):
#     def _get_ports(self, remote_address, network_id=None, router_id=None,
#                    skip_cache=False, remote_mac=None):
#     def _get_instance_and_tenant_id(self, req, skip_cache=False):
#     def _proxy_request(self, instance_id, tenant_id, req):
#     def _sign_instance_id(self, instance_id):
#     def __init__(self, conf):
#     def _init_state_reporting(self):
#     def _report_state(self):
#     def _get_socket_mode(self):
#     def run(self):


from unittest import mock
from neutron_lib import constants as n_const
from oslo_config import cfg
from oslo_config import fixture as config_fixture
from oslo_utils import fileutils
from oslo_utils import netutils
from neutron.agent.linux import utils as agent_utils
from neutron.agent.metadata import agent
from neutron.agent import metadata_agent
from neutron.common import cache_utils as cache
from neutron.common import utils
from neutron.conf.agent.metadata import config as meta_conf
from neutron.tests import base
import ddt
import netaddr
import testtools
import webob
        agent.UnixDomainMetadataProxy(mock.Mock())
        ensure_dir.assert_called_once_with('/the', mode=0o755)

    def test_init_exists(self):
        with mock.patch('os.path.isdir') as isdir:
            with mock.patch('os.unlink') as unlink:
                isdir.return_value = True
                agent.UnixDomainMetadataProxy(mock.Mock())
                unlink.assert_called_once_with('/the/path')

    def test_init_exists_unlink_no_file(self):
        with mock.patch('os.path.isdir') as isdir:
            with mock.patch('os.unlink') as unlink:
                with mock.patch('os.path.exists') as exists:
                    isdir.return_value = True
                    exists.return_value = False
                    unlink.side_effect = OSError

                    agent.UnixDomainMetadataProxy(mock.Mock())
                    unlink.assert_called_once_with('/the/path')

    def test_init_exists_unlink_fails_file_still_exists(self):
        with mock.patch('os.path.isdir') as isdir:
            with mock.patch('os.unlink') as unlink:
                with mock.patch('os.path.exists') as exists:
                    isdir.return_value = True
                    exists.return_value = True
                    unlink.side_effect = OSError

                    with testtools.ExpectedException(OSError):

# Path: neutron/agent/metadata/agent.py
# LOG = logging.getLogger(__name__)
# MODE_MAP = {
#     config.USER_MODE: 0o644,
#     config.GROUP_MODE: 0o664,
#     config.ALL_MODE: 0o666,
# }
# class MetadataPluginAPI(object):
# class MetadataProxyHandler(object):
# class UnixDomainMetadataProxy(object):
#     def __init__(self, topic):
#     def get_ports(self, context, filters):
#     def __init__(self, conf):
#     def __call__(self, req):
#     def _get_ports_from_server(self, router_id=None, ip_address=None,
#                                networks=None, mac_address=None):
#     def _get_port_filters(self, router_id=None, ip_address=None,
#                           networks=None, mac_address=None):
#     def _get_router_networks(self, router_id, skip_cache=False):
#     def _get_ports_for_remote_address(self, remote_address, networks,
#                                       skip_cache=False,
#                                       remote_mac=None):
#     def _get_ports(self, remote_address, network_id=None, router_id=None,
#                    skip_cache=False, remote_mac=None):
#     def _get_instance_and_tenant_id(self, req, skip_cache=False):
#     def _proxy_request(self, instance_id, tenant_id, req):
#     def _sign_instance_id(self, instance_id):
#     def __init__(self, conf):
#     def _init_state_reporting(self):
#     def _report_state(self):
#     def _get_socket_mode(self):
#     def run(self):


# Path: neutron/tests/unit/agent/metadata/test_agent.py
from unittest import mock
from neutron_lib import constants as n_const
from oslo_config import cfg
from oslo_config import fixture as config_fixture
from oslo_utils import fileutils
from oslo_utils import netutils
from neutron.agent.linux import utils as agent_utils
from neutron.agent.metadata import agent
from neutron.agent import metadata_agent
from neutron.common import cache_utils as cache
from neutron.common import utils
from neutron.conf.agent.metadata import config as meta_conf
from neutron.tests import base
import ddt
import netaddr
import testtools
import webob

        agent.UnixDomainMetadataProxy(mock.Mock())
        ensure_dir.assert_called_once_with('/the', mode=0o755)

    def test_init_exists(self):
        with mock.patch('os.path.isdir') as isdir:
            with mock.patch('os.unlink') as unlink:
                isdir.return_value = True
                agent.UnixDomainMetadataProxy(mock.Mock())
                unlink.assert_called_once_with('/the/path')

    def test_init_exists_unlink_no_file(self):
        with mock.patch('os.path.isdir') as isdir:
            with mock.patch('os.unlink') as unlink:
                with mock.patch('os.path.exists') as exists:
                    isdir.return_value = True
                    exists.return_value = False
                    unlink.side_effect = OSError

                    agent.UnixDomainMetadataProxy(mock.Mock())
                    unlink.assert_called_once_with('/the/path')

    def test_init_exists_unlink_fails_file_still_exists(self):
        with mock.patch('os.path.isdir') as isdir:
            with mock.patch('os.unlink') as unlink:
                with mock.patch('os.path.exists') as exists:
                    isdir.return_value = True
                    exists.return_value = True
                    unlink.side_effect = OSError

                    with testtools.ExpectedException(OSError):

                        agent.UnixDomainMetadataProxy(mock.Mock())