def test_delete_router_direct_failure(self):
        router_info = copy.deepcopy(fake_router_object)
        router_info['router'].update({
            'status': 'ACTIVE',
            'id': fake_router_uuid
        })
        context = mock.Mock(current=fake_router_object)

        with mock.patch.object(ac_rest.RestClient, 'process_request',
                               return_value="Timeout Exceptions") \
                as mock_method:
            with mock.patch.object(L3_HA_NAT_db_mixin,
                                   'delete_router',
                                   return_value=fake_router_db):
                acl3router = HuaweiACL3RouterPlugin()
                acl3router.delete_router(context, fake_router_db['id'])

        tst_url = "http://" + cfg.CONF.huawei_ac_config.host + ":" \
                  + str(cfg.CONF.huawei_ac_config.port) \
                  + ac_const.NW_HW_URL + '/' + \
                  ac_const.NW_HW_NEUTRON_RESOURCES['delete_router']['rsrc'] \
                  + '/' + fake_router_db['id']
        params = jsonutils.dumps({})
        mock_method.\
            assert_called_once_with(ac_const.NW_HW_NEUTRON_RESOURCES
                                    ['delete_router']['method'],
                                    (cfg.CONF.huawei_ac_config.username,
                                     cfg.CONF.huawei_ac_config.password),
                                    tst_url, fake_rest_headers, params)
    def test_remove_router_interface_success(self):
        router_info = copy.deepcopy(fake_router_object)
        router_info['router'].update({
            'status': 'ACTIVE',
            'id': fake_router_uuid
        })
        context = mock.Mock(current=fake_router_object)

        interface_info = {'port_id': fake_port_id}

        router_in = {
            'portId': interface_info['port_id'],
            'id': fake_router_db['id'],
            'serviceName': cfg.CONF.huawei_ac_config.service_name,
            'tenant_id': router_info['router']['tenant_id']
        }

        response = self._create_rest_response(requests.codes.all_good)

        with mock.patch.object(ac_rest.RestClient, 'process_request',
                               return_value=response) \
                as mock_method:
            with mock.patch.object(L3_NAT_db_mixin,
                                   'get_router',
                                   return_value=fake_router_db):
                with mock.patch.object(L3_NAT_with_dvr_db_mixin,
                                       'remove_router_interface',
                                       return_value=interface_info):
                    acl3router = HuaweiACL3RouterPlugin()
                    acl3router.remove_router_interface(context,
                                                       fake_router_db['id'],
                                                       interface_info)

        tst_url = "http://" + cfg.CONF.huawei_ac_config.host + ":" \
                  + str(cfg.CONF.huawei_ac_config.port) \
                  + ac_const.NW_HW_URL + '/' \
                  + ac_const.NW_HW_NEUTRON_RESOURCES['delete_router'
                                                     '_interface']['rsrc'] \
                  + '/' + fake_router_db['id']
        params = jsonutils.dumps(router_in)
        mock_method.\
            assert_called_once_with(ac_const.NW_HW_NEUTRON_RESOURCES
                                    ['delete_router_interface']
                                    ['method'],
                                    (cfg.CONF.huawei_ac_config.username,
                                     cfg.CONF.huawei_ac_config.password),
                                    tst_url,
                                    fake_rest_headers,
                                    params)
 def test_create_router_key_error_exception(self):
     router_info = copy.deepcopy(fake_router_object)
     router_info['router'].update({
         'status': 'ACTIVE',
         'id': fake_router_uuid
     })
     context = mock.Mock(current=fake_router_object)
     del fake_router_db['tenant_id']
     with mock.patch.object(L3_NAT_db_mixin,
                            'create_router',
                            return_value=fake_router_db):
         acl3router = HuaweiACL3RouterPlugin()
         self.assertRaises(KeyError, acl3router.create_router, context,
                           router_info)
     fake_router_db.update({'tenant_id': fake_tenant_id})
    def test_create_router_no_content(self):
        router_info = copy.deepcopy(fake_router_object)
        router_info['router'].update({
            'status': 'ACTIVE',
            'id': fake_router_uuid
        })
        context = mock.Mock(current=fake_router_object)

        router_in = {
            'id': fake_router_db['id'],
            'name': fake_router_db['name'],
            'adminStateUp': fake_router_db['admin_state_up'],
            'tenant_id': fake_router_db['tenant_id'],
            'externalGatewayInfo': fake_router_db['external_gateway_info'],
            'distributed': fake_router_db['distributed'],
            'ha': fake_router_db['ha'],
            'routes': fake_router_db['routes']
        }

        body = {'router': router_in}

        response = self._create_rest_response(requests.codes.no_content)

        with mock.patch.object(ac_rest.RestClient,
                               'process_request',
                               return_value=response) as mock_method:
            with mock.patch.object(L3_NAT_db_mixin,
                                   'create_router',
                                   return_value=fake_router_db):
                acl3router = HuaweiACL3RouterPlugin()
                acl3router.create_router(context, router_info)

        tst_url = "http://" + cfg.CONF.huawei_ac_config.host + ":" \
                  + str(cfg.CONF.huawei_ac_config.port) \
                  + ac_const.NW_HW_URL + '/' \
                  + ac_const.NW_HW_NEUTRON_RESOURCES[
                      'create_router']['rsrc']
        params = jsonutils.dumps(body)
        mock_method.\
            assert_called_once_with(ac_const.NW_HW_NEUTRON_RESOURCES
                                    ['create_router']['method'],
                                    (cfg.CONF.huawei_ac_config.username,
                                     cfg.CONF.huawei_ac_config.password),
                                    tst_url,
                                    fake_rest_headers, params)
    def test_create_router_failure_with_errorcode(self):
        router_info = copy.deepcopy(fake_router_object)
        router_info['router'].update({
            'status': 'ACTIVE',
            'id': fake_router_uuid
        })
        context = mock.Mock(current=fake_router_object)

        response = self._create_rest_response(requests.codes.ok, 'OK', '1')

        with mock.patch.object(ac_rest.RestClient,
                               'process_request',
                               return_value=response):
            with mock.patch.object(L3_NAT_db_mixin,
                                   'create_router',
                                   return_value=fake_router_db):
                acl3router = HuaweiACL3RouterPlugin()
                self.assertRaises(ml2_exc.MechanismDriverError,
                                  acl3router.create_router, context,
                                  router_info)
    def test_remove_router_interface_key_error_exception(self):
        router_info = copy.deepcopy(fake_router_object)
        router_info['router'].update({
            'status': 'ACTIVE',
            'id': fake_router_uuid
        })
        context = mock.Mock(current=fake_router_object)

        interface_info = {'port_id': fake_port_id}

        del interface_info['port_id']

        with mock.patch.object(L3_NAT_db_mixin,
                               'get_router',
                               return_value=fake_router_db):
            with mock.patch.object(L3_NAT_with_dvr_db_mixin,
                                   'remove_router_interface',
                                   return_value=interface_info):
                acl3router = HuaweiACL3RouterPlugin()
                self.assertRaises(KeyError, acl3router.remove_router_interface,
                                  context, fake_router_db['id'],
                                  interface_info)
 def test_rest_request_error_case(self):
     acl3router = HuaweiACL3RouterPlugin()
     self.assertRaises(ml2_exc.MechanismDriverError,
                       acl3router.__rest_request__, None, None,
                       'invalid_operation')
 def test_get_plugin_type(self):
     acl3router = HuaweiACL3RouterPlugin()
     plugin_type = acl3router.get_plugin_type()
     self.assertEqual(plugin_type, constants.L3_ROUTER_NAT)
     plugin_desc = acl3router.get_plugin_description()
     self.assertEqual(plugin_desc, ac_const.NW_HW_L3_DESCRIPTION)