Skip to content

Commit

Permalink
Merge "fix APIs to increase test coverage for cfg_types, add_mgmt and…
Browse files Browse the repository at this point in the history
… instance_ip, ip_alloc Also added generated files in omit section for test coverage. Closes-Bug:#1487514" into R2.22-dev
  • Loading branch information
Zuul authored and opencontrail-ci-admin committed Sep 16, 2015
2 parents 5f6f2fc + 0a0db3e commit 6fbba98
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 60 deletions.
3 changes: 3 additions & 0 deletions src/config/api-server/.coveragerc
@@ -0,0 +1,3 @@
[run]
branch = True
omit = vnc_cfg_api_server/gen/vnc_api_server_gen.py,vnc_cfg_api_server/gen/vnc_cassandra_client_gen.py,vnc_cfg_api_server/gen/vnc_api_test_gen.py,vnc_cfg_api_server/gen/vnc_api_extension_gen.py,vnc_cfg_api_server/gen/vnc_ifmap_client_gen.py,vnc_cfg_api_server/gen/resource_test.py,vnc_cfg_api_server/gen/resource_client.py
44 changes: 35 additions & 9 deletions src/config/api-server/tests/test_ip_alloc.py
Expand Up @@ -339,7 +339,8 @@ def test_bulk_ip_alloc_free(self):
# request to allocate 10 ip address using bulk allocation api
data = {"subnet" : "11.1.1.0/24", "count" : 10}
url = '/virtual-network/%s/ip-alloc' %(vn.uuid)
rv_json = self._vnc_lib._request_server(rest.OP_POST, url, json.dumps(data))
rv_json = self._vnc_lib._request_server(rest.OP_POST, url,
json.dumps(data))
ret_data = json.loads(rv_json)
ret_ip_addr = ret_data['ip_addr']
expected_ip_addr = ['11.1.1.252', '11.1.1.251', '11.1.1.250',
Expand All @@ -351,24 +352,49 @@ def test_bulk_ip_alloc_free(self):
for idx in range(len(expected_ip_addr)):
self.assertEqual(expected_ip_addr[idx], ret_ip_addr[idx])

print 'Verified bulk ip address allocation'
print 'Verify bulk ip address allocation'
# Find out number of allocated ips from given VN/subnet
# We should not get 13 ip allocated from this subnet
# 10 user request + 3 reserved ips (first, last and gw).
data = {"subnet_list" : ["11.1.1.0/24"]}
url = '/virtual-network/%s/subnet-ip-count' %(vn.uuid)
rv_json = self._vnc_lib._request_server(rest.OP_POST, url,
json.dumps(data))
ret_ip_count = json.loads(rv_json)['ip_count_list'][0]
allocated_ip = ret_ip_count - 3
self.assertEqual(allocated_ip, 10)

#free allocated ip addresses from vn
#free 5 allocated ip addresses from vn
data = {"subnet" : "11.1.1.0/24",
"ip_addr" : ['11.1.1.252', '11.1.1.251', '11.1.1.250',
'11.1.1.249', '11.1.1.248', '11.1.1.247',
'11.1.1.246', '11.1.1.245', '11.1.1.244',
'11.1.1.243']}
'11.1.1.249', '11.1.1.248']}
url = '/virtual-network/%s/ip-free' %(vn.uuid)
self._vnc_lib._request_server(rest.OP_POST, url, json.dumps(data))

# Find out number of allocated ips from given VN/subnet
# We should not get any ip allocated from this subnet
# We should get 5+3 ip allocated from this subnet
data = {"subnet_list" : ["11.1.1.0/24"]}
url = '/virtual-network/%s/subnet-ip-count' %(vn.uuid)
rv_json = self._vnc_lib._request_server(rest.OP_POST, url,
json.dumps(data))
ret_ip_count = json.loads(rv_json)['ip_count_list'][0]
allocated_ip = ret_ip_count - 3
self.assertEqual(allocated_ip, 5)

#free remaining 5 allocated ip addresses from vn
data = {"subnet" : "11.1.1.0/24",
"ip_addr": ['11.1.1.247', '11.1.1.246', '11.1.1.245',
'11.1.1.244', '11.1.1.243']}
url = '/virtual-network/%s/ip-free' %(vn.uuid)
self._vnc_lib._request_server(rest.OP_POST, url, json.dumps(data))

data = {"subnet_list" : ["11.1.1.0/24"]}
url = '/virtual-network/%s/subnet-ip-count' %(vn.uuid)
rv_json = self._vnc_lib._request_server(rest.OP_POST, url, json.dumps(data))
rv_json = self._vnc_lib._request_server(rest.OP_POST, url,
json.dumps(data))
ret_ip_count = json.loads(rv_json)['ip_count_list'][0]
self.assertEqual(ret_ip_count, 0)
allocated_ip = ret_ip_count - 3
self.assertEqual(allocated_ip, 0)
print 'Verified bulk ip free'

# cleanup
Expand Down
47 changes: 12 additions & 35 deletions src/config/api-server/vnc_addr_mgmt.py
Expand Up @@ -315,6 +315,10 @@ def ip_reserve(self, ipaddr, value):
return None
# end ip_reserve

def ip_count(self):
return self._db_conn.subnet_alloc_count(self._name)
# end ip_count

def ip_alloc(self, ipaddr=None, value=None):
if ipaddr:
return self.ip_reserve(ipaddr, value)
Expand Down Expand Up @@ -835,6 +839,14 @@ def net_check_subnet_delete(self, db_vn_dict, req_vn_dict):
return True, ""
# end net_check_subnet_delete

# return number of ip address currently allocated for a subnet
# count will also include reserved ips
def ip_count_req(self, vn_fq_name, subnet_name=None):
vn_fq_name_str = ':'.join(vn_fq_name)
subnet_obj = self._subnet_objs[vn_fq_name_str][subnet_name]
return subnet_obj.ip_count()
# end ip_count_req

# allocate an IP address for given virtual network
# we use the first available subnet unless provided
def ip_alloc_req(self, vn_fq_name, vn_dict=None, sub=None, asked_ip_addr=None,
Expand Down Expand Up @@ -1026,41 +1038,6 @@ def ip_free_notify(self, ip_addr, vn_fq_name):
break
# end ip_free_notify

# Given IP address count on given virtual network, subnet/List of subnet
def ip_count(self, obj_dict, subnet=None):
db_conn = self._get_db_conn()
addr_num = 0
if not subnet:
return addr_num

instip_refs = obj_dict.get('instance_ip_back_refs', None)
if instip_refs:
for ref in instip_refs:
uuid = ref['uuid']
try:
(ok, result) = db_conn.dbe_read(
'instance-ip', {'uuid': uuid})
inst_ip = result.get('instance_ip_address')
if not inst_ip:
self.config_log(
"Error in ip_count, ip null: %s" %(uuid),
level=SandeshLevel.SYS_ERR)
continue
if IPAddress(inst_ip) in IPNetwork(subnet):
addr_num += 1
except cfgm_common.exceptions.NoIdError:
continue
except Exception as e:
ok = False
result = cfgm_common.utils.detailed_traceback()
if not ok:
self.config_log(result, level=SandeshLevel.SYS_ERR)
continue


return addr_num
# end ip_count

def mac_alloc(self, obj_dict):
uid = obj_dict['uuid']
return '02:%s:%s:%s:%s:%s' % (uid[0:2], uid[2:4], uid[4:6],
Expand Down
2 changes: 1 addition & 1 deletion src/config/api-server/vnc_cfg_api_server.py
Expand Up @@ -2277,7 +2277,7 @@ def vn_subnet_ip_count_http_post(self, id):
subnet_list = req_dict[
'subnet_list'] if 'subnet_list' in req_dict else []
result = vnc_cfg_types.VirtualNetworkServer.subnet_ip_count(
obj_dict, subnet_list)
vn_fq_name, subnet_list)
return result
# end vn_subnet_ip_count_http_post

Expand Down
9 changes: 9 additions & 0 deletions src/config/api-server/vnc_cfg_ifmap.py
Expand Up @@ -1212,6 +1212,11 @@ def subnet_reserve_req(self, subnet, addr, value):
return allocator.reserve(addr, value)
# end subnet_reserve_req

def subnet_alloc_count(self, subnet):
allocator = self._get_subnet_allocator(subnet)
return allocator.get_alloc_count()
# end subnet_alloc_count

def subnet_alloc_req(self, subnet, value=None):
allocator = self._get_subnet_allocator(subnet)
try:
Expand Down Expand Up @@ -1706,6 +1711,10 @@ def subnet_reset_in_use(self, subnet, addr):
return self._zk_db.subnet_reset_in_use(subnet, addr)
#end subnet_reset_in_use

def subnet_alloc_count(self, subnet):
return self._zk_db.subnet_alloc_count(subnet)
# end subnet_alloc_count

def subnet_alloc_req(self, subnet, value=None):
return self._zk_db.subnet_alloc_req(subnet, value)
# end subnet_alloc_req
Expand Down
28 changes: 13 additions & 15 deletions src/config/api-server/vnc_cfg_types.py
Expand Up @@ -621,10 +621,10 @@ def ip_free(cls, vn_fq_name, subnet_name, ip_list):
# end ip_free

@classmethod
def subnet_ip_count(cls, obj_dict, subnet_list):
def subnet_ip_count(cls, vn_fq_name, subnet_list):
ip_count_list = []
for item in subnet_list:
ip_count_list.append(cls.addr_mgmt.ip_count(obj_dict, item))
ip_count_list.append(cls.addr_mgmt.ip_count_req(vn_fq_name, item))
return {'ip_count_list': ip_count_list}
# end subnet_ip_count

Expand Down Expand Up @@ -665,10 +665,9 @@ def http_put(cls, id, fq_name, obj_dict, db_conn):
return True, ""
old_dns_method = old_ipam_mgmt.get('ipam_dns_method')
new_dns_method = new_ipam_mgmt.get('ipam_dns_method')
if not cls.is_change_allowed(old_dns_method, new_dns_method, obj_dict,
db_conn):
return (False, (409, "Cannot change DNS Method from " +
old_dns_method + " to " + new_dns_method +
if not cls.is_change_allowed(old_dns_method, new_dns_method,
read_result, db_conn):
return (False, (409, "Cannot change DNS Method " +
" with active VMs referring to the IPAM"))
return True, ""
# end http_put
Expand All @@ -681,16 +680,15 @@ def http_put_fail(cls, id, fq_name, obj_dict, db_conn):

@classmethod
def is_change_allowed(cls, old, new, obj_dict, db_conn):
if (old == "default-dns-server" or old == "virtual-dns-server"):
if ((new == "tenant-dns-server" or new == "none") and
cls.is_active_vm_present(obj_dict, db_conn)):
active_vm_present = cls.is_active_vm_present(obj_dict, db_conn)
if active_vm_present:
if (old == "default-dns-server" or old == "virtual-dns-server"):
if (new == "tenant-dns-server" or new == None):
return False
if (old == "tenant-dns-server" and new != old):
return False
if (old == None and new != old):
return False
if (old == "tenant-dns-server" and new != old and
cls.is_active_vm_present(obj_dict, db_conn)):
return False
if (old == "none" and new != old and
cls.is_active_vm_present(obj_dict, db_conn)):
return False
return True
# end is_change_allowed

Expand Down
4 changes: 4 additions & 0 deletions src/config/common/zkclient.py
Expand Up @@ -134,6 +134,10 @@ def reset_in_use(self, idx):
self._reset_in_use(bit_idx)
# end reset_in_use

def get_alloc_count(self):
return self._in_use.count()
# end get_alloc_count

def alloc(self, value=None):
# Allocates a index from the allocation list
if self._in_use.all():
Expand Down

0 comments on commit 6fbba98

Please sign in to comment.