From 0a0db3e07526c055183d70432d822bacf01d9de6 Mon Sep 17 00:00:00 2001 From: Atul Moghe Date: Fri, 21 Aug 2015 08:01:47 -0700 Subject: [PATCH] fix APIs to increase test coverage for cfg_types, add_mgmt and instance_ip, ip_alloc Also added generated files in omit section for test coverage. Closes-Bug:#1487514 Change-Id: Ic2e7341ccb4611f517c2b312a4e8f4b1d1ab18d1 (cherry picked from commit bdde6efab7c9fbfa1b98874f76bdd4801cebb10a) --- src/config/api-server/.coveragerc | 3 ++ src/config/api-server/tests/test_ip_alloc.py | 44 ++++++++++++++---- src/config/api-server/vnc_addr_mgmt.py | 47 +++++--------------- src/config/api-server/vnc_cfg_api_server.py | 2 +- src/config/api-server/vnc_cfg_ifmap.py | 9 ++++ src/config/api-server/vnc_cfg_types.py | 28 ++++++------ src/config/common/zkclient.py | 4 ++ 7 files changed, 77 insertions(+), 60 deletions(-) create mode 100644 src/config/api-server/.coveragerc diff --git a/src/config/api-server/.coveragerc b/src/config/api-server/.coveragerc new file mode 100644 index 00000000000..dd3e8614a66 --- /dev/null +++ b/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 diff --git a/src/config/api-server/tests/test_ip_alloc.py b/src/config/api-server/tests/test_ip_alloc.py index 967040fa902..a392c378e62 100644 --- a/src/config/api-server/tests/test_ip_alloc.py +++ b/src/config/api-server/tests/test_ip_alloc.py @@ -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', @@ -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 diff --git a/src/config/api-server/vnc_addr_mgmt.py b/src/config/api-server/vnc_addr_mgmt.py index c1379d068a4..e06cfaf50e2 100644 --- a/src/config/api-server/vnc_addr_mgmt.py +++ b/src/config/api-server/vnc_addr_mgmt.py @@ -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) @@ -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, @@ -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], diff --git a/src/config/api-server/vnc_cfg_api_server.py b/src/config/api-server/vnc_cfg_api_server.py index c3faf8f13d8..5c814fe033c 100644 --- a/src/config/api-server/vnc_cfg_api_server.py +++ b/src/config/api-server/vnc_cfg_api_server.py @@ -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 diff --git a/src/config/api-server/vnc_cfg_ifmap.py b/src/config/api-server/vnc_cfg_ifmap.py index 31e103c6565..545d770672d 100644 --- a/src/config/api-server/vnc_cfg_ifmap.py +++ b/src/config/api-server/vnc_cfg_ifmap.py @@ -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: @@ -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 diff --git a/src/config/api-server/vnc_cfg_types.py b/src/config/api-server/vnc_cfg_types.py index acb703f20e2..906b7d02acd 100644 --- a/src/config/api-server/vnc_cfg_types.py +++ b/src/config/api-server/vnc_cfg_types.py @@ -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 @@ -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 @@ -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 diff --git a/src/config/common/zkclient.py b/src/config/common/zkclient.py index d881cbd9365..95335929e66 100644 --- a/src/config/common/zkclient.py +++ b/src/config/common/zkclient.py @@ -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():