def test_detach_real_with_project_id(self, CloudStack): self.conf.update({'CLOUDSTACK_VIP_NETWORK_INDEX': '0'}) vms = { "virtualmachine": [{ "id": "abc123", "nic": [{ "id": "def456", "networkid": "netid1" }] }] } CloudStack.return_value = cloudstack_client = mock.Mock() cloudstack_client.listVirtualMachines.return_value = vms manager = networkapi_cloudstack.NetworkApiCloudstackLB(self.conf) lb = load_balancer.LoadBalancer("500", "myapp", "192.168.1.1", ip_id="303", project_id="pid-xxx") h = host.Host('abc123', 'name.host') manager.detach_real(lb, h) list_data = {"id": "abc123", "projectid": "pid-xxx"} cloudstack_client.listVirtualMachines.assert_called_with(list_data) assoc_data = { "nicid": "def456", "vipid": "500", "projectid": "pid-xxx" } cloudstack_client.disassociateGloboNetworkRealFromVip.assert_called_with( assoc_data) CloudStack.assert_called_with(self.conf["CLOUDSTACK_API_URL"], self.conf["CLOUDSTACK_API_KEY"], self.conf["CLOUDSTACK_SECRET_KEY"])
def create_load_balancer(self, name): ip_id = self._associate_ip() try: lb_id, address = self._create_lb_rule(ip_id, name) self._assign_lb_additional_networks(lb_id) try: self._create_lb_hc(lb_id) except: exc_info = sys.exc_info() try: self._delete_lb_rule(lb_id, self.project_id) except: log.exception('error in rollback trying to delete lb rule') raise exc_info[0], exc_info[1], exc_info[2] return load_balancer.LoadBalancer(lb_id, name, address, project_id=self.project_id, ip_id=ip_id) except: exc_info = sys.exc_info() try: self._dissociate_ip(ip_id, self.project_id) except: log.exception('error in rollback trying to dissociate ip') raise exc_info[0], exc_info[1], exc_info[2]
def test_destroy_load_balancer(self, Ip, Vip, CloudStack): client_vip = mock.Mock() Vip.return_value = client_vip client_ip = mock.Mock() Ip.return_value = client_ip cloudstack_client = CloudStack.return_value lb = load_balancer.LoadBalancer("404", "myapp", "192.168.1.1", ip_id="303", project_id="project_id-x") manager = networkapi_cloudstack.NetworkApiCloudstackLB(self.conf) manager.destroy_load_balancer(lb) Vip.assert_called_with(self.conf["NETWORKAPI_ENDPOINT"], self.conf["NETWORKAPI_USER"], self.conf["NETWORKAPI_PASSWORD"]) client_vip.remove_script.assert_called_with("404") client_vip.remover.assert_called_with("404") Ip.assert_called_with(self.conf["NETWORKAPI_ENDPOINT"], self.conf["NETWORKAPI_USER"], self.conf["NETWORKAPI_PASSWORD"]) client_ip.delete_ip4.assert_called_with("303") data = {"projectid": "project_id-x", "vipid": "404"} cloudstack_client.removeGloboNetworkVip.assert_called_with(data)
def create_load_balancer(self, name): vip_id, ip_id, address = self._create_vip(name, self.vip_config) return load_balancer.LoadBalancer(vip_id, name, address, ip_id=ip_id, project_id=self.create_project_id)
def test_detach_real(self, cs_mock): cs_instance = cs_mock.return_value cs_instance.removeFromLoadBalancerRule.return_value = {'jobid': 'j1'} lb = load_balancer.LoadBalancer('lbid', 'lbname', 'lbaddr', ip_id='ip_id', project_id='projid') h = host.Host('hostid', 'hostaddr') manager = cloudstack.CloudstackLB(self.conf) manager.detach_real(lb, h) cs_instance.removeFromLoadBalancerRule.assert_called_once_with({ 'id': 'lbid', 'projectid': 'projid', 'virtualmachineids': 'hostid', }) cs_instance.wait_for_job.assert_called_with('j1', 100)
def test_attach_real(self, cs_mock): cs_instance = cs_mock.return_value cs_instance.make_request.return_value = {'jobid': 'j1'} cs_instance.assignToLoadBalancerRule.return_value = {'jobid': 'j2'} cs_instance.wait_for_job.return_value = {'jobresult': True} vms = { "virtualmachine": [{ "id": "abc123", "nic": [{ "id": "def456", "networkid": "netid1" }] }] } cs_instance.listVirtualMachines.return_value = vms lb = load_balancer.LoadBalancer('lbid', 'lbname', 'lbaddr', ip_id='ip_id', project_id='projid') h = host.Host('hostid', 'hostaddr') manager = cloudstack.CloudstackLB(self.conf) manager.attach_real(lb, h) cs_instance.make_request.assert_called_once_with( 'assignNetwork', { 'id': 'lbid', 'networkids': 'netid1', 'projectid': 'projid', }) cs_instance.assignToLoadBalancerRule.assert_called_once_with({ 'id': 'lbid', 'projectid': 'projid', 'virtualmachineids': 'hostid', })
def test_remove_vip_ip_not_found(self, Ip, Vip, CloudStack): client_vip = mock.Mock() Vip.return_value = client_vip client_ip = mock.Mock() client_ip.delete_ip4.side_effect = IpNaoExisteError("303") Ip.return_value = client_ip lb = load_balancer.LoadBalancer("404", "myapp", "192.168.1.1", ip_id="303", project_id="project_id-x") manager = networkapi_cloudstack.NetworkApiCloudstackLB(self.conf) manager.destroy_load_balancer(lb) Vip.assert_called_with(self.conf["NETWORKAPI_ENDPOINT"], self.conf["NETWORKAPI_USER"], self.conf["NETWORKAPI_PASSWORD"]) client_vip.remove_script.assert_called_with("404") client_vip.remover.assert_called_with("404") Ip.assert_called_with(self.conf["NETWORKAPI_ENDPOINT"], self.conf["NETWORKAPI_USER"], self.conf["NETWORKAPI_PASSWORD"]) client_ip.delete_ip4.assert_called_with("303")
def test_attach_real_with_network_index(self, CloudStack): vms = { "virtualmachine": [{ "id": "abc123", "nic": [{ "id": "def456", "networkid": "netid1" }, { "id": "second-id", "networkid": "netid2" }] }] } CloudStack.return_value = cloudstack_client = mock.Mock() cloudstack_client.listVirtualMachines.return_value = vms manager = networkapi_cloudstack.NetworkApiCloudstackLB(self.conf) lb = load_balancer.LoadBalancer( "500", "myapp", "192.168.1.1", ip_id="303", project_id=None, ) h = host.Host('abc123', 'name.host') manager.attach_real(lb, h) list_data = {"id": "abc123"} cloudstack_client.listVirtualMachines.assert_called_with(list_data) net_data = {"networkid": "netid2", "vipid": "500"} cloudstack_client.addGloboNetworkVipToAccount.assert_called_with( net_data) assoc_data = {"nicid": "second-id", "vipid": "500"} cloudstack_client.associateGloboNetworkRealToVip.assert_called_with( assoc_data) CloudStack.assert_called_with(self.conf["CLOUDSTACK_API_URL"], self.conf["CLOUDSTACK_API_KEY"], self.conf["CLOUDSTACK_SECRET_KEY"])
def test_destroy_load_balancer(self, cs_mock): cs_instance = cs_mock.return_value cs_instance.deleteLoadBalancerRule.return_value = {'jobid': 'j1'} cs_instance.make_request.return_value = {'jobid': 'j2'} lb = load_balancer.LoadBalancer('lbid', 'lbname', 'lbaddr', ip_id='ipid', project_id='projid') manager = cloudstack.CloudstackLB(self.conf) manager.destroy_load_balancer(lb) cs_instance.deleteLoadBalancerRule.assert_called_once_with({ 'id': 'lbid', 'projectid': 'projid', }) cs_instance.make_request.assert_called_once_with( 'disassociateIpAddress', { 'id': 'ipid', 'projectid': 'projid', }) cs_instance.wait_for_job.assert_any_call('j1', 100) cs_instance.wait_for_job.assert_any_call('j2', 100)