def test_destroy(self): LoadBalancer.create('fake', 'my-lb', {'LB_ID': 'xxx'}) db_lb = LoadBalancer.find('my-lb') self.assertEqual(db_lb.id, 'xxx') db_lb.destroy() db_lb = LoadBalancer.find('my-lb') self.assertIsNone(db_lb)
def test_destroy(self): LoadBalancer.create('fake', 'my-lb', {'LB_ID': 'xxx'}) db_lb = LoadBalancer.find('my-lb') self.assertEqual(db_lb.id, 'xxx') db_lb.destroy() db_lb = LoadBalancer.find('my-lb') self.assertIsNone(db_lb)
def test_destroy_ignores_manager_exception(self, log): LoadBalancer.create('fake', 'my-lb', {'LB_ID': 'explode'}) db_lb = LoadBalancer.find('my-lb') self.assertEqual(db_lb.id, 'explode') db_lb.destroy() self.assertEqual(log.call_args, call("Error trying to destroy load balancer name: 'my-lb' " "id: 'explode' in 'fake': failure to destroy")) db_lb = LoadBalancer.find('my-lb') self.assertIsNone(db_lb)
def test_destroy_ignores_manager_exception(self, log): LoadBalancer.create('fake', 'my-lb', {'LB_ID': 'explode'}) db_lb = LoadBalancer.find('my-lb') self.assertEqual(db_lb.id, 'explode') db_lb.destroy() self.assertEqual( log.call_args, call("Error trying to destroy load balancer name: 'my-lb' " "id: 'explode' in 'fake': failure to destroy")) db_lb = LoadBalancer.find('my-lb') self.assertIsNone(db_lb)
def run(self, config, name): self.init_config(config) healthcheck_timeout = int(self._get_conf("RPAAS_HEALTHCHECK_TIMEOUT", 600)) host = Host.create(self.host_manager_name, name, self.config) lb = None try: lb = LoadBalancer.create(self.lb_manager_name, name, self.config) lb.add_host(host) self.nginx_manager.wait_healthcheck(host.dns_name, timeout=healthcheck_timeout) self.hc.create(name) self.hc.add_url(name, host.dns_name) self.storage.remove_task(name) except: exc_info = sys.exc_info() rollback = self._get_conf("RPAAS_ROLLBACK_ON_ERROR", "0") in ("True", "true", "1") if not rollback: raise try: if lb is not None: lb.destroy() except Exception as e: logging.error("Error in rollback trying to destroy load balancer: {}".format(e)) try: host.destroy() except Exception as e: logging.error("Error in rollback trying to destroy host: {}".format(e)) try: self.hc.destroy(name) except Exception as e: logging.error("Error in rollback trying to remove healthcheck: {}".format(e)) raise exc_info[0], exc_info[1], exc_info[2]
def _add_host(self, name, lb=None): healthcheck_timeout = int(self._get_conf("RPAAS_HEALTHCHECK_TIMEOUT", 600)) host = Host.create(self.host_manager_name, name, self.config) created_lb = None try: if not lb: lb = created_lb = LoadBalancer.create(self.lb_manager_name, name, self.config) self.hc.create(name) lb.add_host(host) self.nginx_manager.wait_healthcheck(host.dns_name, timeout=healthcheck_timeout) self.hc.add_url(name, host.dns_name) self.storage.remove_task(name) except: exc_info = sys.exc_info() rollback = self._get_conf("RPAAS_ROLLBACK_ON_ERROR", "0") in ("True", "true", "1") if not rollback: raise try: if created_lb is not None: created_lb.destroy() except Exception as e: logging.error("Error in rollback trying to destroy load balancer: {}".format(e)) try: if created_lb is not None: self._delete_host(name, host) else: self._delete_host(name, host, lb) except Exception as e: logging.error("Error in rollback trying to destroy host: {}".format(e)) try: if lb and len(lb.hosts) == 0: self.hc.destroy(name) except Exception as e: logging.error("Error in rollback trying to remove healthcheck: {}".format(e)) raise exc_info[0], exc_info[1], exc_info[2]
def test_remove_host(self): h1 = Host('x', 'x.me.com') h2 = Host('y', 'y.me.com') lb = LoadBalancer.create('fake', 'my-lb', {'LB_ID': 'explode'}) lb.add_host(h1) lb.add_host(h2) lb.remove_host(h1) self.assertItemsEqual(lb.hosts, [h2]) db_lb = LoadBalancer.find('my-lb') self.assertItemsEqual([h.to_json() for h in db_lb.hosts], [h2.to_json()])
def _add_host(self, name, lb=None): healthcheck_timeout = int( self._get_conf("RPAAS_HEALTHCHECK_TIMEOUT", 600)) created_lb = None try: if not lb: lb = created_lb = LoadBalancer.create(self.lb_manager_name, name, self.config) self.hc.create(name) config = copy.deepcopy(self.config) if hasattr(lb, 'dsr') and lb.dsr: config["HOST_TAGS"] = config[ "HOST_TAGS"] + ",dsr_ip:{}".format(lb.address) host = Host.create(self.host_manager_name, name, config) lb.add_host(host) self.nginx_manager.wait_healthcheck(host.dns_name, timeout=healthcheck_timeout) acls = self.consul_manager.find_acl_network(name) if acls: acl_host = acls.pop() for dst in acl_host['destination']: self.acl_manager.add_acl(name, host.dns_name, dst) self.hc.add_url(name, host.dns_name) except: exc_info = sys.exc_info() rollback = self._get_conf("RPAAS_ROLLBACK_ON_ERROR", "0") in ("True", "true", "1") if not rollback: raise try: if created_lb is not None: created_lb.destroy() except Exception as e: logging.error( "Error in rollback trying to destroy load balancer: {}". format(e)) try: if created_lb is not None: self._delete_host(name, host) else: self._delete_host(name, host, lb) except Exception as e: logging.error( "Error in rollback trying to destroy host: {}".format(e)) try: if lb and len(lb.hosts) == 0: self.hc.destroy(name) except Exception as e: logging.error( "Error in rollback trying to remove healthcheck: {}". format(e)) raise exc_info[0], exc_info[1], exc_info[2] finally: self.storage.remove_task(name)
def test_remove_host(self): h1 = Host('x', 'x.me.com') h2 = Host('y', 'y.me.com') lb = LoadBalancer.create('fake', 'my-lb', {'LB_ID': 'explode'}) lb.add_host(h1) lb.add_host(h2) lb.remove_host(h1) self.assertItemsEqual(lb.hosts, [h2]) db_lb = LoadBalancer.find('my-lb') self.assertItemsEqual([h.to_json() for h in db_lb.hosts], [h2.to_json()])
def test_add_host(self): h1 = Host('x', 'x.me.com') h2 = Host('y', 'y.me.com') conf = {'LB_ID': 'explode'} lb = LoadBalancer.create('fake', 'my-lb', conf) lb.add_host(h1) lb.add_host(h2) self.assertItemsEqual(lb.hosts, [h1, h2]) db_lb = LoadBalancer.find('my-lb', conf) self.assertEqual(db_lb.hosts[0].config, conf) self.assertEqual(db_lb.hosts[1].config, conf) self.assertItemsEqual([h.to_json() for h in db_lb.hosts], [h1.to_json(), h2.to_json()])
def test_add_host(self): h1 = Host('x', 'x.me.com') h2 = Host('y', 'y.me.com') conf = {'LB_ID': 'explode'} lb = LoadBalancer.create('fake', 'my-lb', conf) lb.add_host(h1) lb.add_host(h2) self.assertItemsEqual(lb.hosts, [h1, h2]) db_lb = LoadBalancer.find('my-lb', conf) self.assertEqual(db_lb.hosts[0].config, conf) self.assertEqual(db_lb.hosts[1].config, conf) self.assertItemsEqual([h.to_json() for h in db_lb.hosts], [h1.to_json(), h2.to_json()])
def test_create(self): conf = {'LB_ID': 'xxx'} lb = LoadBalancer.create('fake', 'my-lb', conf=conf) self.assertEqual(lb.id, 'xxx') self.assertEqual(lb.name, 'my-lb') self.assertEqual(lb.manager, 'fake') self.assertEqual(lb.extra, 'something') self.assertEqual(lb.config, conf) db_lb = LoadBalancer.find('my-lb', conf=conf) self.assertEqual(db_lb.id, 'xxx') self.assertEqual(db_lb.name, 'my-lb') self.assertEqual(db_lb.manager, 'fake') self.assertEqual(db_lb.extra, 'something') self.assertEqual(db_lb.config, conf)
def test_create(self): conf = {'LB_ID': 'xxx'} lb = LoadBalancer.create('fake', 'my-lb', conf=conf) self.assertEqual(lb.id, 'xxx') self.assertEqual(lb.name, 'my-lb') self.assertEqual(lb.manager, 'fake') self.assertEqual(lb.extra, 'something') self.assertEqual(lb.config, conf) db_lb = LoadBalancer.find('my-lb', conf=conf) self.assertEqual(db_lb.id, 'xxx') self.assertEqual(db_lb.name, 'my-lb') self.assertEqual(db_lb.manager, 'fake') self.assertEqual(db_lb.extra, 'something') self.assertEqual(db_lb.config, conf)
def _add_host(self, name, lb=None): healthcheck_timeout = int(self._get_conf("RPAAS_HEALTHCHECK_TIMEOUT", 600)) created_lb = None try: if not lb: lb = created_lb = LoadBalancer.create(self.lb_manager_name, name, self.config) self.hc.create(name) config = copy.deepcopy(self.config) if hasattr(lb, 'dsr') and lb.dsr: config["HOST_TAGS"] = config["HOST_TAGS"] + ",dsr_ip:{}".format(lb.address) host = Host.create(self.host_manager_name, name, config) lb.add_host(host) self.nginx_manager.wait_healthcheck(host.dns_name, timeout=healthcheck_timeout) acls = self.consul_manager.find_acl_network(name) if acls: acl_host = acls.pop() for dst in acl_host['destination']: self.acl_manager.add_acl(name, host.dns_name, dst) self.hc.add_url(name, host.dns_name) except: exc_info = sys.exc_info() rollback = self._get_conf("RPAAS_ROLLBACK_ON_ERROR", "0") in ("True", "true", "1") if not rollback: raise try: if created_lb is not None: created_lb.destroy() except Exception as e: logging.error("Error in rollback trying to destroy load balancer: {}".format(e)) try: if created_lb is not None: self._delete_host(name, host) else: self._delete_host(name, host, lb) except Exception as e: logging.error("Error in rollback trying to destroy host: {}".format(e)) try: if lb and len(lb.hosts) == 0: self.hc.destroy(name) except Exception as e: logging.error("Error in rollback trying to remove healthcheck: {}".format(e)) raise exc_info[0], exc_info[1], exc_info[2] finally: self.storage.remove_task(name)
def test_create_duplicated(self): lb = LoadBalancer.create('fake', 'my-lb', {'LB_ID': 'xxx'}) self.assertEqual(lb.id, 'xxx') self.assertEqual(lb.name, 'my-lb') with self.assertRaises(pymongo.errors.DuplicateKeyError): LoadBalancer.create('fake', 'my-lb', {'LB_ID': 'xxx'})
def test_create_duplicated(self): lb = LoadBalancer.create('fake', 'my-lb', {'LB_ID': 'xxx'}) self.assertEqual(lb.id, 'xxx') self.assertEqual(lb.name, 'my-lb') with self.assertRaises(pymongo.errors.DuplicateKeyError): LoadBalancer.create('fake', 'my-lb', {'LB_ID': 'xxx'})