def test_new_instance_with_plan(self, nginx): manager = Manager(self.config) manager.consul_manager = mock.Mock() manager.consul_manager.generate_token.return_value = "abc-123" manager.new_instance("x", plan_name="small") host = self.Host.create.return_value config = copy.deepcopy(self.config) config.update(self.plan["config"]) config[ "HOST_TAGS"] = "rpaas_service:test-suite-rpaas,rpaas_instance:x,consul_token:abc-123" lb = self.LoadBalancer.create.return_value self.Host.create.assert_called_with("my-host-manager", "x", config) self.LoadBalancer.create.assert_called_with("my-lb-manager", "x", config) lb.add_host.assert_called_with(host) self.assertEquals(manager.storage.find_task("x").count(), 0) nginx.Nginx.assert_called_once_with(config) nginx_manager = nginx.Nginx.return_value nginx_manager.wait_healthcheck.assert_called_once_with(host.dns_name, timeout=600) metadata = manager.storage.find_instance_metadata("x") self.assertEqual( { "_id": "x", "plan_name": "small", "consul_token": "abc-123" }, metadata)
def test_new_instance(self, nginx): manager = Manager(self.config) manager.new_instance('x') host = self.Host.create.return_value lb = self.LoadBalancer.create.return_value self.Host.create.assert_called_with('my-host-manager', 'x', self.config) self.LoadBalancer.create.assert_called_with('my-lb-manager', 'x', self.config) lb.add_host.assert_called_with(host) self.assertIsNone(self.storage.find_task('x')) nginx.NginxDAV.assert_called_once_with(self.config) nginx_manager = nginx.NginxDAV.return_value nginx_manager.wait_healthcheck.assert_called_once_with(host.dns_name, timeout=600)
def test_new_instance_host_create_fail_and_raises(self, nginx): manager = Manager(self.config) manager.consul_manager = mock.Mock() manager.consul_manager.generate_token.return_value = "abc-123" lb = self.LoadBalancer.create.return_value self.Host.create.side_effect = Exception("Host create failure") host = self.Host.create.return_value manager.new_instance("x") lb.add_host.assert_not_called() lb.destroy.assert_not_called() host.destroy.assert_not_called() self.assertEqual(self.storage.find_task("x").count(), 0)
def test_new_instance_lb_create_fail_and_rollback(self, nginx): config = copy.deepcopy(self.config) config["RPAAS_ROLLBACK_ON_ERROR"] = "1" manager = Manager(config) manager.consul_manager = mock.Mock() manager.consul_manager.generate_token.return_value = "abc-123" self.LoadBalancer.create.side_effect = Exception("LB create failure") lb = self.LoadBalancer.create.return_value host = self.Host.create.return_value manager.new_instance("x") lb.add_host.assert_not_called() lb.destroy.assert_not_called() host.destroy.assert_called_once() self.assertEqual(self.storage.find_task("x").count(), 0)
def test_new_instance(self, nginx): manager = Manager(self.config) manager.new_instance("x") host = self.Host.create.return_value lb = self.LoadBalancer.create.return_value config = copy.deepcopy(self.config) config["HOST_TAGS"] = "rpaas_service:test-suite-rpaas,rpaas_instance:x" self.Host.create.assert_called_with("my-host-manager", "x", config) self.LoadBalancer.create.assert_called_with("my-lb-manager", "x", config) lb.add_host.assert_called_with(host) self.assertIsNone(self.storage.find_task("x")) nginx.NginxDAV.assert_called_once_with(config) nginx_manager = nginx.NginxDAV.return_value nginx_manager.wait_healthcheck.assert_called_once_with(host.dns_name, timeout=600)
def test_new_instance(self, nginx): manager = Manager(self.config) manager.consul_manager = mock.Mock() manager.consul_manager.generate_token.return_value = "abc-123" manager.new_instance("x") host = self.Host.create.return_value lb = self.LoadBalancer.create.return_value config = copy.deepcopy(self.config) config["HOST_TAGS"] = "rpaas_service:test-suite-rpaas,rpaas_instance:x,consul_token:abc-123" self.Host.create.assert_called_with("my-host-manager", "x", config) self.LoadBalancer.create.assert_called_with("my-lb-manager", "x", config) lb.add_host.assert_called_with(host) self.assertEquals(self.storage.find_task("x").count(), 0) nginx.Nginx.assert_called_once_with(config) nginx_manager = nginx.Nginx.return_value nginx_manager.wait_healthcheck.assert_called_once_with(host.dns_name, timeout=600) manager.consul_manager.write_healthcheck.assert_called_once_with("x")
def test_remove_instance_decrement_quota(self, nginx): manager = Manager(self.config) for name in ['a', 'b', 'c', 'd', 'e']: manager.new_instance(name) with self.assertRaises(QuotaExceededError): manager.new_instance('f') manager.remove_instance('e') manager.new_instance('f') manager.remove_instance('e') with self.assertRaises(QuotaExceededError): manager.new_instance('g')
def test_remove_instance_decrement_quota(self, nginx): manager = Manager(self.config) for name in ["a", "b", "c", "d", "e"]: manager.new_instance(name) with self.assertRaises(QuotaExceededError): manager.new_instance("f") manager.remove_instance("e") manager.new_instance("f") manager.remove_instance("e") with self.assertRaises(QuotaExceededError): manager.new_instance("g")
def test_new_instance_nginx_wait_healthcheck_fail_and_rollback( self, nginx, hc): config = copy.deepcopy(self.config) config["RPAAS_ROLLBACK_ON_ERROR"] = "1" manager = Manager(config) manager.consul_manager = mock.Mock() manager.consul_manager.generate_token.return_value = "abc-123" lb = self.LoadBalancer.create.return_value host = self.Host.create.return_value dumb_hc = hc.return_value nginx_manager = nginx.Nginx.return_value nginx_manager.wait_healthcheck.side_effect = Exception("Nginx timeout") manager.new_instance("x") self.LoadBalancer.create.assert_called_once() lb.add_host.assert_called_once() dumb_hc.add_url.assert_not_called() lb.destroy.assert_called_once() host.destroy.assert_called_once() lb.remove_host.assert_not_called() dumb_hc.destroy.assert_called_once() self.assertEqual(self.storage.find_task("x").count(), 0)
def test_new_instance_with_extra_tags(self, nginx): config = copy.deepcopy(self.config) config["INSTANCE_EXTRA_TAGS"] = "enable_monitoring:1" manager = Manager(config) manager.consul_manager = mock.Mock() manager.consul_manager.generate_token.return_value = "abc-123" manager.new_instance("x") host = self.Host.create.return_value config["HOST_TAGS"] = ( "rpaas_service:test-suite-rpaas,rpaas_instance:x," "consul_token:abc-123,enable_monitoring:1") del config["INSTANCE_EXTRA_TAGS"] lb = self.LoadBalancer.create.return_value self.Host.create.assert_called_with("my-host-manager", "x", config) self.LoadBalancer.create.assert_called_with("my-lb-manager", "x", config) lb.add_host.assert_called_with(host) self.assertEquals(manager.storage.find_task("x").count(), 0) nginx.Nginx.assert_called_once_with(config) nginx_manager = nginx.Nginx.return_value nginx_manager.wait_healthcheck.assert_called_once_with(host.dns_name, timeout=600)
def test_new_instance_over_quota(self, nginx): manager = Manager(self.config) for name in ["a", "b", "c", "d", "e"]: manager.new_instance(name, "myteam") with self.assertRaises(QuotaExceededError) as cm: manager.new_instance("f", "myteam") self.assertEqual(str(cm.exception), "quota execeeded 5/5 used") manager.new_instance("f", "otherteam")
def test_new_instance_over_quota(self, nginx): manager = Manager(self.config) for name in ['a', 'b', 'c', 'd', 'e']: manager.new_instance(name, 'myteam') with self.assertRaises(QuotaExceededError) as cm: manager.new_instance('f', 'myteam') self.assertEqual(str(cm.exception), "quota execeeded 5/5 used") manager.new_instance('f', 'otherteam')
def test_new_instance_error_already_running(self): self.storage.store_task("x") manager = Manager(self.config) with self.assertRaises(storage.DuplicateError): manager.new_instance("x")
def test_new_instance_error_already_exists(self, LoadBalancer): LoadBalancer.find.return_value = "something" manager = Manager(self.config) with self.assertRaises(storage.DuplicateError): manager.new_instance("x") LoadBalancer.find.assert_called_once_with("x")
def test_new_instance_plan_not_found(self): manager = Manager(self.config) with self.assertRaises(storage.PlanNotFoundError): manager.new_instance("x", plan_name="supersmall")