예제 #1
0
 def test_get_subnetpool_name(self):
     fake_subnet_cidr = "10.0.0.0/16"
     generated_neutron_subnetpool_name = utils.get_neutron_subnetpool_name(
         fake_subnet_cidr)
     name_prefix = cfg.CONF.subnetpool_name_prefix
     self.assertIn(name_prefix, generated_neutron_subnetpool_name)
     self.assertIn(fake_subnet_cidr, generated_neutron_subnetpool_name)
예제 #2
0
    def test_request_pool_create_failures(self, GivenException):
        pool_name = utils.get_neutron_subnetpool_name("10.0.0.0/16")
        new_subnetpool = {
            'name': pool_name,
            'default_prefixlen': 16,
            'prefixes': ['10.0.0.0/16']}

        self.mox.StubOutWithMock(app.neutron, 'list_subnetpools')
        fake_name = pool_name
        app.neutron.list_subnetpools(name=fake_name).AndReturn(
            {'subnetpools': []})

        self.mox.StubOutWithMock(app.neutron, 'create_subnetpool')
        app.neutron.create_subnetpool(
            {'subnetpool': new_subnetpool}).AndRaise(GivenException)

        self.mox.ReplayAll()

        pool = '10.0.0.0/16'
        response = self._invoke_create_request(pool)

        self.assertEqual(GivenException.status_code, response.status_code)
        decoded_json = jsonutils.loads(response.data)
        self.assertIn('Err', decoded_json)
        self.assertEqual(
            {'Err': GivenException.message}, decoded_json)
예제 #3
0
    def test_request_pool_list_subnetpool_failure(self):
        self.mox.StubOutWithMock(app.neutron, 'list_subnetpools')
        pool_name = utils.get_neutron_subnetpool_name("10.0.0.0/16")
        fake_name = pool_name
        ex = exceptions.Unauthorized
        app.neutron.list_subnetpools(name=fake_name).AndRaise(ex)

        self.mox.ReplayAll()

        pool = '10.0.0.0/16'
        response = self._invoke_create_request(pool)

        self.assertEqual(ex.status_code, response.status_code)
예제 #4
0
    def test_ipam_driver_request_address_for_different_gateway(self):
        # faking list_subnetpools
        self.mox.StubOutWithMock(app.neutron, 'list_subnetpools')
        fake_kuryr_subnetpool_id = str(uuid.uuid4())
        fake_name = utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
        kuryr_subnetpools = self._get_fake_v4_subnetpools(
            fake_kuryr_subnetpool_id, prefixes=[FAKE_IP4_CIDR],
            name=fake_name)
        app.neutron.list_subnetpools(id=fake_kuryr_subnetpool_id).AndReturn(
            kuryr_subnetpools)

        # faking list_subnets
        docker_endpoint_id = utils.get_hash()
        neutron_network_id = str(uuid.uuid4())
        subnet_v4_id = str(uuid.uuid4())
        fake_v4_subnet = self._get_fake_v4_subnet(
            neutron_network_id, docker_endpoint_id, subnet_v4_id,
            subnetpool_id=fake_kuryr_subnetpool_id,
            cidr=FAKE_IP4_CIDR)
        fake_v4_subnet['subnet'].update(gateway_ip='10.0.0.1')
        fake_subnet_response = {
            'subnets': [
                fake_v4_subnet['subnet']
            ]
        }
        self.mox.StubOutWithMock(app.neutron, 'list_subnets')
        app.neutron.list_subnets(cidr=FAKE_IP4_CIDR).AndReturn(
            fake_subnet_response)

        # Apply mocks
        self.mox.ReplayAll()

        # Testing container ip allocation
        fake_request = {
            'PoolID': fake_kuryr_subnetpool_id,
            'Address': '10.0.0.5',  # Different with existed gw ip
            'Options': {
                const.REQUEST_ADDRESS_TYPE: const.NETWORK_GATEWAY_OPTIONS
            }
        }
        response = self.app.post('/IpamDriver.RequestAddress',
                                content_type='application/json',
                                data=jsonutils.dumps(fake_request))

        self.assertEqual(500, response.status_code)
        decoded_json = jsonutils.loads(response.data)
        self.assertIn('Err', decoded_json)
        err_message = ("Requested gateway {0} does not match with "
                       "gateway {1} in existed network.").format(
                       '10.0.0.5', '10.0.0.1')
        self.assertEqual({'Err': err_message}, decoded_json)
예제 #5
0
    def test_ipam_driver_request_pool_with_user_pool(self):
        pool_name = utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
        new_subnetpool = {
            'name': pool_name,
            'default_prefixlen': 16,
            'prefixes': [FAKE_IP4_CIDR]
        }

        self.mox.StubOutWithMock(app.neutron, 'list_subnetpools')
        fake_kuryr_subnetpool_id = str(uuid.uuid4())
        fake_name = pool_name
        kuryr_subnetpools = self._get_fake_v4_subnetpools(
            fake_kuryr_subnetpool_id, prefixes=[FAKE_IP4_CIDR], name=fake_name)
        app.neutron.list_subnetpools(name=fake_name).AndReturn(
            {'subnetpools': []})
        fake_subnetpool_response = {
            'subnetpool': kuryr_subnetpools['subnetpools'][0]
        }

        self.mox.StubOutWithMock(app.neutron, 'create_subnetpool')
        app.neutron.create_subnetpool({
            'subnetpool': new_subnetpool
        }).AndReturn(fake_subnetpool_response)

        self.mox.ReplayAll()

        fake_request = {
            'AddressSpace': '',
            'Pool': FAKE_IP4_CIDR,
            'SubPool': '',  # In the case --ip-range is not given
            'Options': {},
            'V6': False
        }
        response = self.app.post('/IpamDriver.RequestPool',
                                 content_type='application/json',
                                 data=jsonutils.dumps(fake_request))

        self.assertEqual(200, response.status_code)
        decoded_json = jsonutils.loads(response.data)
        self.assertEqual(fake_kuryr_subnetpool_id, decoded_json['PoolID'])
예제 #6
0
    def test_ipam_driver_request_pool_with_user_pool(self):
        pool_name = utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
        new_subnetpool = {
            'name': pool_name,
            'default_prefixlen': 16,
            'prefixes': [FAKE_IP4_CIDR]}

        self.mox.StubOutWithMock(app.neutron, 'list_subnetpools')
        fake_kuryr_subnetpool_id = str(uuid.uuid4())
        fake_name = pool_name
        kuryr_subnetpools = self._get_fake_v4_subnetpools(
            fake_kuryr_subnetpool_id, prefixes=[FAKE_IP4_CIDR],
            name=fake_name)
        app.neutron.list_subnetpools(name=fake_name).AndReturn(
            {'subnetpools': []})
        fake_subnetpool_response = {
            'subnetpool': kuryr_subnetpools['subnetpools'][0]
        }

        self.mox.StubOutWithMock(app.neutron, 'create_subnetpool')
        app.neutron.create_subnetpool(
            {'subnetpool': new_subnetpool}).AndReturn(fake_subnetpool_response)

        self.mox.ReplayAll()

        fake_request = {
            'AddressSpace': '',
            'Pool': FAKE_IP4_CIDR,
            'SubPool': '',  # In the case --ip-range is not given
            'Options': {},
            'V6': False
        }
        response = self.app.post('/IpamDriver.RequestPool',
                                content_type='application/json',
                                data=jsonutils.dumps(fake_request))

        self.assertEqual(200, response.status_code)
        decoded_json = jsonutils.loads(response.data)
        self.assertEqual(fake_kuryr_subnetpool_id, decoded_json['PoolID'])
예제 #7
0
    def test_ipam_driver_request_address(self):
        # faking list_subnetpools
        self.mox.StubOutWithMock(app.neutron, 'list_subnetpools')
        fake_kuryr_subnetpool_id = str(uuid.uuid4())
        fake_name = utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
        kuryr_subnetpools = self._get_fake_v4_subnetpools(
            fake_kuryr_subnetpool_id, prefixes=[FAKE_IP4_CIDR],
            name=fake_name)
        app.neutron.list_subnetpools(id=fake_kuryr_subnetpool_id).AndReturn(
            kuryr_subnetpools)

        # faking list_subnets
        docker_endpoint_id = hashlib.sha256(
            utils.getrandbits(256)).hexdigest()
        neutron_network_id = str(uuid.uuid4())
        subnet_v4_id = str(uuid.uuid4())
        fake_v4_subnet = self._get_fake_v4_subnet(
            neutron_network_id, docker_endpoint_id, subnet_v4_id,
            subnetpool_id=fake_kuryr_subnetpool_id,
            cidr=FAKE_IP4_CIDR)
        fake_subnet_response = {
            'subnets': [
                fake_v4_subnet['subnet']
            ]
        }
        self.mox.StubOutWithMock(app.neutron, 'list_subnets')
        app.neutron.list_subnets(cidr=FAKE_IP4_CIDR).AndReturn(
            fake_subnet_response)

        # faking create_port
        fake_neutron_port_id = str(uuid.uuid4())
        fake_port = base.TestKuryrBase._get_fake_port(
            docker_endpoint_id, neutron_network_id,
            fake_neutron_port_id,
            subnet_v4_id,
            neutron_subnet_v4_address="10.0.0.5")
        port_request = {
            'name': 'kuryr-unbound-port',
            'admin_state_up': True,
            'network_id': neutron_network_id,
            'binding:host_id': utils.get_hostname(),
        }
        fixed_ips = port_request['fixed_ips'] = []
        fixed_ip = {'subnet_id': subnet_v4_id}
        fixed_ips.append(fixed_ip)
        self.mox.StubOutWithMock(app.neutron, 'create_port')
        app.neutron.create_port({'port': port_request}).AndReturn(fake_port)

        # Apply mocks
        self.mox.ReplayAll()

        # Testing container ip allocation
        fake_request = {
            'PoolID': fake_kuryr_subnetpool_id,
            'Address': '',  # Querying for container address
            'Options': {}
        }
        response = self.app.post('/IpamDriver.RequestAddress',
                                content_type='application/json',
                                data=jsonutils.dumps(fake_request))

        self.assertEqual(200, response.status_code)
        decoded_json = jsonutils.loads(response.data)
        self.assertEqual('10.0.0.5/16', decoded_json['Address'])
예제 #8
0
파일: controllers.py 프로젝트: fkautz/kuryr
def ipam_request_pool():
    """Creates a new Neutron subnetpool from the given request.

    This funciton takes the following JSON data and delegates the subnetpool
    creation to the Neutron client. ::

        {
            "AddressSpace": string
            "Pool":         string
            "SubPool":      string
            "Options":      map[string]string
            "V6":           bool
        }

    Then the following JSON response is returned. ::

        {
            "PoolID": string
            "Pool":   string
            "Data":   map[string]string
        }

    See the following link for more details about the spec:

      https://github.com/docker/libnetwork/blob/master/docs/ipam.md#requestpool  # noqa
    """
    json_data = flask.request.get_json(force=True)
    app.logger.debug(
        "Received JSON data {0} for /IpamDriver.RequestPool".format(json_data))
    jsonschema.validate(json_data, schemata.REQUEST_POOL_SCHEMA)
    requested_pool = json_data['Pool']
    requested_subpool = json_data['SubPool']
    v6 = json_data['V6']
    pool_id = ''
    subnet_cidr = ''
    if requested_pool:
        app.logger.info(_LI("Creating subnetpool with the given pool CIDR"))
        if requested_subpool:
            cidr = netaddr.IPNetwork(requested_subpool)
        else:
            cidr = netaddr.IPNetwork(requested_pool)
        subnet_cidr = _get_subnet_cidr_using_cidr(cidr)
        pool_name = utils.get_neutron_subnetpool_name(subnet_cidr)
        # Check if requested pool already exist
        pools = _get_subnetpools_by_attrs(name=pool_name)
        if pools:
            pool_id = pools[0]['id']
        if not pools:
            new_subnetpool = {
                'name': pool_name,
                'default_prefixlen': cidr.prefixlen,
                'prefixes': [subnet_cidr]
            }
            created_subnetpool_response = app.neutron.create_subnetpool(
                {'subnetpool': new_subnetpool})
            pool = created_subnetpool_response['subnetpool']
            pool_id = pool['id']
    else:
        if v6:
            default_pool_list = SUBNET_POOLS_V6
        else:
            default_pool_list = SUBNET_POOLS_V4
        pool_name = default_pool_list[0]
        pools = _get_subnetpools_by_attrs(name=pool_name)
        if pools:
            pool = pools[0]
            pool_id = pool['id']
            prefixes = pool['prefixes']
            if len(prefixes) > 1:
                app.logger.warning(
                    _LW("More than one prefixes present. "
                        "Picking first one."))
            cidr = netaddr.IPNetwork(prefixes[0])
            subnet_cidr = _get_subnet_cidr_using_cidr(cidr)
        else:
            app.logger.error(_LE("Default neutron pools not found."))
    req_pool_res = {'PoolID': pool_id, 'Pool': subnet_cidr}
    return flask.jsonify(req_pool_res)
예제 #9
0
    def test_ipam_driver_request_address(self):
        # faking list_subnetpools
        self.mox.StubOutWithMock(app.neutron, 'list_subnetpools')
        fake_kuryr_subnetpool_id = str(uuid.uuid4())
        fake_name = utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
        kuryr_subnetpools = self._get_fake_v4_subnetpools(
            fake_kuryr_subnetpool_id, prefixes=[FAKE_IP4_CIDR], name=fake_name)
        app.neutron.list_subnetpools(
            id=fake_kuryr_subnetpool_id).AndReturn(kuryr_subnetpools)

        # faking list_subnets
        docker_endpoint_id = hashlib.sha256(utils.getrandbits(256)).hexdigest()
        neutron_network_id = str(uuid.uuid4())
        subnet_v4_id = str(uuid.uuid4())
        fake_v4_subnet = self._get_fake_v4_subnet(
            neutron_network_id,
            docker_endpoint_id,
            subnet_v4_id,
            subnetpool_id=fake_kuryr_subnetpool_id,
            cidr=FAKE_IP4_CIDR)
        fake_subnet_response = {'subnets': [fake_v4_subnet['subnet']]}
        self.mox.StubOutWithMock(app.neutron, 'list_subnets')
        app.neutron.list_subnets(
            cidr=FAKE_IP4_CIDR).AndReturn(fake_subnet_response)

        # faking create_port
        fake_neutron_port_id = str(uuid.uuid4())
        fake_port = base.TestKuryrBase._get_fake_port(
            docker_endpoint_id,
            neutron_network_id,
            fake_neutron_port_id,
            subnet_v4_id,
            neutron_subnet_v4_address="10.0.0.5")
        port_request = {
            'name': 'kuryr-unbound-port',
            'admin_state_up': True,
            'network_id': neutron_network_id,
            'binding:host_id': utils.get_hostname(),
        }
        fixed_ips = port_request['fixed_ips'] = []
        fixed_ip = {'subnet_id': subnet_v4_id}
        fixed_ips.append(fixed_ip)
        self.mox.StubOutWithMock(app.neutron, 'create_port')
        app.neutron.create_port({'port': port_request}).AndReturn(fake_port)

        # Apply mocks
        self.mox.ReplayAll()

        # Testing container ip allocation
        fake_request = {
            'PoolID': fake_kuryr_subnetpool_id,
            'Address': '',  # Querying for container address
            'Options': {}
        }
        response = self.app.post('/IpamDriver.RequestAddress',
                                 content_type='application/json',
                                 data=jsonutils.dumps(fake_request))

        self.assertEqual(200, response.status_code)
        decoded_json = jsonutils.loads(response.data)
        self.assertEqual('10.0.0.5/16', decoded_json['Address'])
예제 #10
0
파일: controllers.py 프로젝트: zbx/kuryr
def ipam_request_pool():
    """Creates a new Neutron subnetpool from the given request.

    This funciton takes the following JSON data and delegates the subnetpool
    creation to the Neutron client. ::

        {
            "AddressSpace": string
            "Pool":         string
            "SubPool":      string
            "Options":      map[string]string
            "V6":           bool
        }

    Then the following JSON response is returned. ::

        {
            "PoolID": string
            "Pool":   string
            "Data":   map[string]string
        }

    See the following link for more details about the spec:

      https://github.com/docker/libnetwork/blob/master/docs/ipam.md#requestpool  # noqa
    """
    json_data = flask.request.get_json(force=True)
    app.logger.debug("Received JSON data {0} for /IpamDriver.RequestPool"
                     .format(json_data))
    jsonschema.validate(json_data, schemata.REQUEST_POOL_SCHEMA)
    requested_pool = json_data['Pool']
    requested_subpool = json_data['SubPool']
    v6 = json_data['V6']
    pool_id = ''
    subnet_cidr = ''
    if requested_pool:
        app.logger.info(_LI("Creating subnetpool with the given pool CIDR"))
        if requested_subpool:
            cidr = netaddr.IPNetwork(requested_subpool)
        else:
            cidr = netaddr.IPNetwork(requested_pool)
        subnet_cidr = _get_subnet_cidr_using_cidr(cidr)
        pool_name = utils.get_neutron_subnetpool_name(subnet_cidr)
        # Check if requested pool already exist
        pools = _get_subnetpools_by_attrs(name=pool_name)
        if pools:
            pool_id = pools[0]['id']
        if not pools:
            new_subnetpool = {
                'name': pool_name,
                'default_prefixlen': cidr.prefixlen,
                'prefixes': [subnet_cidr]}
            created_subnetpool_response = app.neutron.create_subnetpool(
                {'subnetpool': new_subnetpool})
            pool = created_subnetpool_response['subnetpool']
            pool_id = pool['id']
    else:
        if v6:
            default_pool_list = SUBNET_POOLS_V6
        else:
            default_pool_list = SUBNET_POOLS_V4
        pool_name = default_pool_list[0]
        pools = _get_subnetpools_by_attrs(name=pool_name)
        if pools:
            pool = pools[0]
            pool_id = pool['id']
            prefixes = pool['prefixes']
            if len(prefixes) > 1:
                app.logger.warning(_LW("More than one prefixes present. "
                                     "Picking first one."))
            cidr = netaddr.IPNetwork(prefixes[0])
            subnet_cidr = _get_subnet_cidr_using_cidr(cidr)
        else:
            app.logger.error(_LE("Default neutron pools not found."))
    req_pool_res = {'PoolID': pool_id,
                    'Pool': subnet_cidr}
    return flask.jsonify(req_pool_res)