Exemple #1
0
 def test_is_multi_host_instance_has_no_fixed_ip(self):
     with mock.patch.object(self.network_api.db, 'fixed_ip_get_by_instance',
         side_effect=exception.FixedIpNotFoundForInstance(
             instance_uuid=FAKE_UUID)):
         instance = objects.Instance(uuid=FAKE_UUID)
         result, floats = (
             self.network_api._get_multi_addresses(self.context, instance))
         self.assertFalse(result)
Exemple #2
0
 def fake_fixed_ip_get_by_instance(ctxt, uuid):
     raise exception.FixedIpNotFoundForInstance(instance_uuid=uuid)
class FixedIpTestV21(test.NoDBTestCase):
    controller_class = multinic_v21
    validation_error = exception.ValidationError

    def setUp(self):
        super(FixedIpTestV21, self).setUp()
        fakes.stub_out_networking(self)
        self.stub_out('nova.compute.api.API.add_fixed_ip',
                      compute_api_add_fixed_ip)
        self.stub_out('nova.compute.api.API.remove_fixed_ip',
                      compute_api_remove_fixed_ip)
        self.stub_out('nova.compute.api.API.get', compute_api_get)
        self.controller = self.controller_class.MultinicController()
        self.fake_req = fakes.HTTPRequest.blank('')
        self.mock_get = self.useFixture(
            fixtures.MockPatch('nova.api.openstack.common.get_instance')).mock
        self.mock_get.return_value = fake_instance.fake_instance_obj(
            self.fake_req.environ['nova.context'], uuid=UUID,
            project_id=self.fake_req.environ['nova.context'].project_id)

    def test_add_fixed_ip(self):
        global last_add_fixed_ip
        last_add_fixed_ip = (None, None)

        body = dict(addFixedIp=dict(networkId='test_net'))
        self.controller._add_fixed_ip(self.fake_req, UUID, body=body)
        self.assertEqual(last_add_fixed_ip, (UUID, 'test_net'))

    def _test_add_fixed_ip_bad_request(self, body):
        self.assertRaises(self.validation_error,
                          self.controller._add_fixed_ip,
                          self.fake_req,
                          UUID, body=body)

    def test_add_fixed_ip_empty_network_id(self):
        body = {'addFixedIp': {'network_id': ''}}
        self._test_add_fixed_ip_bad_request(body)

    def test_add_fixed_ip_network_id_bigger_than_36(self):
        body = {'addFixedIp': {'network_id': 'a' * 37}}
        self._test_add_fixed_ip_bad_request(body)

    def test_add_fixed_ip_no_network(self):
        global last_add_fixed_ip
        last_add_fixed_ip = (None, None)

        body = dict(addFixedIp=dict())
        self._test_add_fixed_ip_bad_request(body)
        self.assertEqual(last_add_fixed_ip, (None, None))

    @mock.patch.object(compute.api.API, 'add_fixed_ip')
    def test_add_fixed_ip_no_more_ips_available(self, mock_add_fixed_ip):
        mock_add_fixed_ip.side_effect = exception.NoMoreFixedIps(net='netid')

        body = dict(addFixedIp=dict(networkId='test_net'))
        self.assertRaises(webob.exc.HTTPBadRequest,
                          self.controller._add_fixed_ip,
                          self.fake_req,
                          UUID, body=body)

    def test_remove_fixed_ip(self):
        global last_remove_fixed_ip
        last_remove_fixed_ip = (None, None)

        body = dict(removeFixedIp=dict(address='10.10.10.1'))
        self.controller._remove_fixed_ip(self.fake_req, UUID, body=body)
        self.assertEqual(last_remove_fixed_ip, (UUID, '10.10.10.1'))

    def test_remove_fixed_ip_no_address(self):
        global last_remove_fixed_ip
        last_remove_fixed_ip = (None, None)

        body = dict(removeFixedIp=dict())
        self.assertRaises(self.validation_error,
                          self.controller._remove_fixed_ip,
                          self.fake_req,
                          UUID, body=body)
        self.assertEqual(last_remove_fixed_ip, (None, None))

    def test_remove_fixed_ip_invalid_address(self):
        body = {'removeFixedIp': {'address': ''}}
        self.assertRaises(self.validation_error,
                          self.controller._remove_fixed_ip,
                          self.fake_req,
                          UUID, body=body)

    @mock.patch.object(compute.api.API, 'remove_fixed_ip',
        side_effect=exception.FixedIpNotFoundForInstance(
            instance_uuid=UUID, ip='10.10.10.1'))
    def test_remove_fixed_ip_not_found(self, _remove_fixed_ip):

        body = {'removeFixedIp': {'address': '10.10.10.1'}}
        self.assertRaises(webob.exc.HTTPBadRequest,
                          self.controller._remove_fixed_ip,
                          self.fake_req,
                          UUID, body=body)