示例#1
0
 def test_retrieve_instance_check_liveness(self):
     instance = storage.Instance(name="what", state="removed")
     self.storage.store_instance(instance)
     self.addCleanup(self.client.feaas_test.instances.remove,
                     {"name": instance.name})
     with self.assertRaises(storage.InstanceNotFoundError):
         self.storage.retrieve_instance(name="what", check_liveness=True)
示例#2
0
 def test_retrieve_instance_by_state(self):
     instance1 = storage.Instance(name="where", state="creating")
     self.storage.store_instance(instance1)
     self.addCleanup(self.client.feaas_test.instances.remove,
                     {"name": instance1.name})
     instance2 = storage.Instance(name="when", state="creating")
     self.storage.store_instance(instance2)
     self.addCleanup(self.client.feaas_test.instances.remove,
                     {"name": instance2.name})
     instance3 = storage.Instance(name="who", state="starting")
     self.storage.store_instance(instance3)
     self.addCleanup(self.client.feaas_test.instances.remove,
                     {"name": instance3.name})
     instance = self.storage.retrieve_instance(state="creating")
     self.assertEqual(instance1.name, instance.name)
     self.assertEqual(instance1.state, instance.state)
示例#3
0
 def test_start_instance_no_network_id(self, uuid):
     self.set_api_envs()
     self.addCleanup(self.del_api_envs)
     self.set_vm_envs(project_id="proj-123")
     self.addCleanup(self.del_vm_envs)
     uuid.return_value = "uuid_val"
     instance = storage.Instance(name="some_instance", units=[])
     strg_mock = mock.Mock()
     strg_mock.retrieve_instance.return_value = instance
     client_mock = mock.Mock()
     client_mock.deployVirtualMachine.return_value = {"id": "abc123",
                                                      "jobid": "qwe321"}
     client_mock.queryAsyncJobResult.return_value = {"jobstatus": 1}
     vm = {"id": "abc123", "nic": []}
     client_mock.listVirtualMachines.return_value = {"virtualmachine": [vm]}
     client_mock.encode_user_data.return_value = user_data = mock.Mock()
     manager = cloudstack.CloudStackManager(storage=strg_mock)
     manager.client = client_mock
     got_instance = manager.start_instance("some_instance")
     self.assertEqual(instance, got_instance)
     self.assertEqual(1, len(instance.units))
     unit = instance.units[0]
     self.assertEqual("abc123", unit.id)
     self.assertEqual("uuid_val", unit.secret)
     self.assertEqual(instance, unit.instance)
     self.assertEqual("", unit.dns_name)
     self.assertEqual("creating", unit.state)
     strg_mock.retrieve_instance.assert_called_with(name="some_instance")
     create_data = {"group": "feaas", "templateid": self.template_id,
                    "zoneid": self.zone_id,
                    "serviceofferingid": self.service_offering_id,
                    "userdata": user_data, "projectid": self.project_id}
     client_mock.deployVirtualMachine.assert_called_with(create_data)
     actual_user_data = manager.get_user_data("uuid_val")
     client_mock.encode_user_data.assert_called_with(actual_user_data)
示例#4
0
 def test_physical_scale_up(self, uuid):
     self.set_api_envs()
     self.addCleanup(self.del_api_envs)
     self.set_vm_envs(project_id="project-123", network_ids="net-123")
     self.addCleanup(self.del_vm_envs)
     uuid.return_value = "uuid_val"
     instance = storage.Instance(name="some_instance",
                                 units=[storage.Unit(id="123")])
     strg_mock = mock.Mock()
     client_mock = mock.Mock()
     client_mock.deployVirtualMachine.return_value = {"id": "abc123",
                                                      "jobid": "qwe321"}
     client_mock.queryAsyncJobResult.return_value = {"jobstatus": 1}
     vm = {"id": "qwe123", "nic": [{"ipaddress": "10.0.0.5"}]}
     client_mock.listVirtualMachines.return_value = {"virtualmachine": [vm]}
     client_mock.encode_user_data.return_value = user_data = mock.Mock()
     manager = cloudstack.CloudStackManager(storage=strg_mock)
     manager.client = client_mock
     units = manager.physical_scale(instance, 2)
     self.assertEqual(2, len(instance.units))
     self.assertEqual(1, len(units))
     unit = instance.units[1]
     self.assertEqual("qwe123", unit.id)
     self.assertEqual("uuid_val", unit.secret)
     self.assertEqual(instance, unit.instance)
     self.assertEqual("10.0.0.5", unit.dns_name)
     self.assertEqual("creating", unit.state)
     create_data = {"group": "feaas", "templateid": self.template_id,
                    "zoneid": self.zone_id,
                    "serviceofferingid": self.service_offering_id,
                    "userdata": user_data, "networkids": self.network_ids,
                    "projectid": self.project_id}
     client_mock.deployVirtualMachine.assert_called_with(create_data)
     actual_user_data = manager.get_user_data("uuid_val")
     client_mock.encode_user_data.assert_called_with(actual_user_data)
示例#5
0
 def test_update_bind(self):
     instance = storage.Instance(name="great")
     bind = storage.Bind("wat.g1.cloud.tsuru.io", instance)
     self.storage.store_bind(bind)
     self.addCleanup(self.storage.remove_bind, bind)
     self.storage.update_bind(bind, state="created")
     bind = self.storage.retrieve_binds(instance_name="great")[0]
     self.assertEqual("created", bind.state)
示例#6
0
 def test_remove_bind(self):
     instance = storage.Instance(name="years")
     bind = storage.Bind(app_host="something.where.com", instance=instance)
     self.storage.store_bind(bind)
     self.addCleanup(self.client.feaas_test.binds.remove,
                     {"instance_name": "years"})
     self.storage.remove_bind(bind)
     self.assertEqual([],
                      self.storage.retrieve_binds(instance_name="years"))
示例#7
0
 def test_status(self):
     instance = api_storage.Instance(name="secret", state="started",
                                     units=[api_storage.Unit(dns_name="secret.cloud.tsuru.io",
                                                             id="i-0800")])
     storage = mock.Mock()
     storage.retrieve_instance.return_value = instance
     manager = managers.BaseManager(storage)
     status = manager.status("secret")
     self.assertEqual("started", status)
示例#8
0
 def test_remove_unit(self):
     unit1 = storage.Unit(dns_name="instance1.cloud.tsuru.io", id="i-0800")
     unit2 = storage.Unit(dns_name="instance2.cloud.tsuru.io", id="i-0801")
     instance = storage.Instance()
     instance.add_unit(unit1)
     instance.add_unit(unit2)
     self.assertEqual([unit1, unit2], instance.units)
     instance.remove_unit(unit1)
     self.assertEqual([unit2], instance.units)
示例#9
0
 def test_start_instance(self):
     instance = api_storage.Instance(name="myapp")
     storage = mock.Mock()
     storage.retrieve_instance.return_value = instance
     manager = ec2.EC2Manager(storage)
     manager._add_units = mock.Mock()
     created_instance = manager.start_instance("myapp")
     self.assertEqual(instance, created_instance)
     manager._add_units.assert_called_with(instance, 1)
示例#10
0
 def test_remove_instance(self):
     instance = api_storage.Instance(name="secret")
     storage = mock.Mock()
     storage.retrieve_instance.return_value = instance
     manager = ec2.EC2Manager(storage)
     manager.remove_instance("secret")
     self.assertEqual("removed", instance.state)
     storage.retrieve_instance.assert_called_with(name="secret")
     storage.store_instance.assert_called_with(instance)
示例#11
0
 def test_scale_instance_already_scaling(self):
     instance = api_storage.Instance(name="secret", state="scaling")
     storage = mock.Mock()
     storage.retrieve_instance.return_value = instance
     manager = managers.BaseManager(storage)
     with self.assertRaises(ValueError) as cm:
         manager.scale_instance("secret", 2)
     exc = cm.exception
     self.assertEqual(("instance is already scaling",), exc.args)
示例#12
0
 def test_scale_instance(self):
     instance = api_storage.Instance(name="secret", state="started")
     storage = mock.Mock()
     storage.retrieve_instance.return_value = instance
     manager = managers.BaseManager(storage)
     manager.scale_instance("secret", 2)
     storage.store_scale_job.assert_called_with({"instance": "secret",
                                                 "quantity": 2,
                                                 "state": "pending"})
示例#13
0
 def test_run_binds(self):
     units = [
         storage.Unit(id="i-0800",
                      dns_name="unit1.cloud.tsuru.io",
                      secret="abc123",
                      state="started"),
         storage.Unit(id="i-8001",
                      dns_name="unit2.cloud.tsuru.io",
                      secret="abc321",
                      state="started")
     ]
     instance1 = storage.Instance(name="wat", units=units)
     instance2 = storage.Instance(name="wet", units=units)
     binds = [
         storage.Bind(instance=instance1, app_host="cool",
                      state="creating"),
         storage.Bind(instance=instance2, app_host="bool", state="creating")
     ]
     strg = mock.Mock()
     strg.retrieve_units.return_value = units
     strg.retrieve_binds.return_value = binds
     manager = mock.Mock(storage=strg)
     writer = vcl_writer.VCLWriter(manager, max_items=3)
     writer.locker = mock.Mock()
     writer.run_binds()
     writer.locker.lock.assert_called_with(vcl_writer.BINDS_LOCKER)
     writer.locker.unlock.assert_called_with(vcl_writer.BINDS_LOCKER)
     strg.retrieve_units.assert_called_once_with(
         state="started", instance_name={"$in": ["wat", "wet"]})
     strg.retrieve_binds.assert_called_once_with(state="creating", limit=3)
     expected_write_vcl_calls = [
         mock.call("unit1.cloud.tsuru.io", "abc123", "cool"),
         mock.call("unit2.cloud.tsuru.io", "abc321", "cool"),
         mock.call("unit1.cloud.tsuru.io", "abc123", "bool"),
         mock.call("unit2.cloud.tsuru.io", "abc321", "bool")
     ]
     self.assertEqual(expected_write_vcl_calls,
                      manager.write_vcl.call_args_list)
     expected_update_bind_calls = [
         mock.call(binds[0], state="created"),
         mock.call(binds[1], state="created")
     ]
     self.assertEqual(expected_update_bind_calls,
                      strg.update_bind.call_args_list)
示例#14
0
 def test_to_dict(self):
     instance = storage.Instance(name="myinstance")
     bind = storage.Bind("wat.g1.cloud.tsuru.io", instance)
     expected = {
         "app_host": "wat.g1.cloud.tsuru.io",
         "instance_name": "myinstance",
         "created_at": bind.created_at,
         "state": bind.state
     }
     self.assertEqual(expected, bind.to_dict())
示例#15
0
 def test_info(self):
     instance = api_storage.Instance(name="secret",
                                     units=[api_storage.Unit(dns_name="secret.cloud.tsuru.io",
                                                             id="i-0800")])
     storage = mock.Mock()
     storage.retrieve_instance.return_value = instance
     manager = managers.BaseManager(storage)
     expected = [{"label": "Address", "value": "secret.cloud.tsuru.io"}]
     self.assertEqual(expected, manager.info("secret"))
     storage.retrieve_instance.assert_called_with(name="secret")
示例#16
0
 def test_run(self):
     instance = storage.Instance(name="something")
     manager = mock.Mock(storage=mock.Mock())
     get_instance = mock.Mock()
     get_instance.return_value = instance
     starter = instance_starter.InstanceStarter(manager, interval=3)
     starter.get_instance = get_instance
     starter.start_instance = mock.Mock()
     starter.run()
     starter.get_instance.assert_called_once()
     starter.start_instance.assert_called_with(instance)
示例#17
0
 def test_store_bind(self):
     instance = storage.Instance(name="years")
     bind = storage.Bind(app_host="something.where.com", instance=instance)
     self.storage.store_bind(bind)
     self.addCleanup(self.client.feaas_test.binds.remove,
                     {"instance_name": "years"})
     got = self.client.feaas_test.binds.find_one({"instance_name": "years"})
     expected = bind.to_dict()
     expected["_id"] = got["_id"]
     expected["created_at"] = got["created_at"]
     self.assertEqual(expected, got)
示例#18
0
 def test_retrieve_binds_query(self):
     instance = storage.Instance(name="years")
     bind1 = storage.Bind(app_host="something.where.com", instance=instance)
     self.storage.store_bind(bind1)
     bind2 = storage.Bind(app_host="belong.where.com", instance=instance)
     self.storage.store_bind(bind2)
     self.addCleanup(self.client.feaas_test.binds.remove,
                     {"instance_name": "years"})
     binds = self.storage.retrieve_binds(app_host="belong.where.com")
     binds = [b.to_dict() for b in binds]
     self.assertEqual([bind2.to_dict()], binds)
示例#19
0
 def test_terminate_instance(self):
     instance = storage.Instance(name="something")
     strg = mock.Mock()
     manager = mock.Mock(storage=strg)
     terminator = instance_terminator.InstanceTerminator(manager,
                                                         interval=3)
     terminator.locker = mock.Mock()
     terminator.terminate_instance(instance)
     terminator.locker.lock.assert_called_with(terminator.lock_name)
     manager.terminate_instance.assert_called_with(instance.name)
     terminator.locker.unlock.assert_called_with(terminator.lock_name)
     strg.remove_instance.assert_called_with(instance.name)
示例#20
0
 def test_run(self):
     instance = storage.Instance(name="something")
     manager = mock.Mock(storage=mock.Mock())
     get_instance = mock.Mock()
     get_instance.return_value = instance
     terminator = instance_terminator.InstanceTerminator(manager,
                                                         interval=3)
     terminator.get_instance = get_instance
     terminator.terminate_instance = mock.Mock()
     terminator.run()
     terminator.get_instance.assert_called_once()
     terminator.terminate_instance.assert_called_with(instance)
示例#21
0
 def test_terminate_instance(self):
     conn = mock.Mock()
     storage = mock.Mock()
     unit = api_storage.Unit(id="i-0800")
     instance = api_storage.Instance(name="secret", units=[unit])
     storage.retrieve_instance.return_value = instance
     manager = ec2.EC2Manager(storage)
     manager._connection = conn
     got_instance = manager.terminate_instance("secret")
     conn.terminate_instances.assert_called_with(instance_ids=["i-0800"])
     storage.retrieve_instance.assert_called_with(name="secret")
     self.assertEqual(instance, got_instance)
示例#22
0
 def test_remove_instance_with_units(self):
     units = [
         storage.Unit(dns_name="instance1.cloud.tsuru.io", id="i-0800"),
         storage.Unit(dns_name="instance2.cloud.tsuru.io", id="i-0801"),
         storage.Unit(dns_name="instance3.cloud.tsuru.io", id="i-0802")
     ]
     instance = storage.Instance(name="secret", units=units)
     self.storage.store_instance(instance)
     self.storage.remove_instance(instance.name)
     self.assertIsNone(
         self.client.feaas_test.units.find_one(
             {"instance_name": instance.name}))
示例#23
0
 def test_start_instance(self):
     instance = storage.Instance(name="something")
     strg = mock.Mock()
     manager = mock.Mock(storage=strg)
     starter = instance_starter.InstanceStarter(manager, interval=3)
     starter.locker = mock.Mock()
     starter.start_instance(instance)
     self.assertEqual("started", instance.state)
     starter.locker.lock.assert_called_with(starter.lock_name)
     manager.start_instance.assert_called_with(instance.name)
     starter.locker.unlock.assert_called_with(starter.lock_name)
     strg.store_instance.assert_called_with(instance, save_units=False)
示例#24
0
 def test_bind_units(self):
     instance1 = storage.Instance(name="myinstance")
     instance2 = storage.Instance(name="yourinstance")
     units = [
         storage.Unit(dns_name="instance1-1.cloud.tsuru.io",
                      id="i-0800",
                      instance=instance1,
                      secret="abc123"),
         storage.Unit(dns_name="instance1-2.cloud.tsuru.io",
                      id="i-0801",
                      instance=instance1,
                      secret="abc321"),
         storage.Unit(dns_name="instance2-1.cloud.tsuru.io",
                      id="i-0802",
                      instance=instance2,
                      secret="abc456")
     ]
     strg = mock.Mock()
     strg.retrieve_units.return_value = units
     strg.retrieve_binds.return_value = [
         storage.Bind("myapp.cloud.tsuru.io", instance1)
     ]
     manager = mock.Mock(storage=strg)
     writer = vcl_writer.VCLWriter(manager, max_items=3)
     writer.bind_units(units)
     expected_calls = [
         mock.call(instance_name="myinstance", state="created"),
         mock.call(instance_name="yourinstance", state="created")
     ]
     self.assertEqual(expected_calls, strg.retrieve_binds.call_args_list)
     expected_calls = [
         mock.call("instance1-1.cloud.tsuru.io", "abc123",
                   "myapp.cloud.tsuru.io"),
         mock.call("instance1-2.cloud.tsuru.io", "abc321",
                   "myapp.cloud.tsuru.io"),
         mock.call("instance2-1.cloud.tsuru.io", "abc456",
                   "myapp.cloud.tsuru.io")
     ]
     self.assertEqual(expected_calls, manager.write_vcl.call_args_list)
示例#25
0
 def test_bind_instance(self, Bind):
     Bind.return_value = "abacaxi"
     instance = api_storage.Instance(name="myinstance",
                                     units=[api_storage.Unit(secret="abc-123",
                                                             dns_name="10.1.1.2",
                                                             id="i-0800")])
     storage = mock.Mock()
     storage.retrieve_instance.return_value = instance
     manager = managers.BaseManager(storage)
     manager.bind("someapp", "myapp.cloud.tsuru.io")
     storage.retrieve_instance.assert_called_with(name="someapp")
     storage.store_bind.assert_called_with("abacaxi")
     Bind.assert_called_with("myapp.cloud.tsuru.io", instance)
示例#26
0
 def test_terminate_instance_ec2_failure(self, stderr_mock):
     conn = mock.Mock()
     conn.terminate_instances.side_effect = ValueError(
         "Something went wrong")
     unit = api_storage.Unit(id="i-0800")
     storage = mock.Mock()
     storage.retrieve_instance.return_value = api_storage.Instance(
         name="secret", units=[unit])
     manager = ec2.EC2Manager(storage)
     manager._connection = conn
     manager.terminate_instance("someapp")
     msg = "[ERROR] Failed to terminate EC2 instance: Something went wrong"
     stderr_mock.write.assert_called_with(msg)
示例#27
0
 def test_start_instance_error(self, stderr):
     instance = storage.Instance(name="something")
     strg = mock.Mock()
     manager = mock.Mock(storage=strg)
     manager.start_instance.side_effect = ValueError("something went wrong")
     starter = instance_starter.InstanceStarter(manager, interval=3)
     starter.locker = mock.Mock()
     starter.start_instance(instance)
     self.assertEqual("error", instance.state)
     starter.locker.lock.assert_called_with(starter.lock_name)
     starter.locker.unlock.assert_called_with(starter.lock_name)
     strg.store_instance.assert_called_with(instance, save_units=False)
     stderr.write.assert_called_with("[ERROR] failed to start instance: something went wrong\n")
示例#28
0
 def test_store_instance(self):
     instance = storage.Instance(name="secret")
     self.storage.store_instance(instance)
     self.addCleanup(self.client.feaas_test.instances.remove,
                     {"name": "secret"})
     instance = self.client.feaas_test.instances.find_one(
         {"name": "secret"})
     expected = {
         "name": "secret",
         "_id": instance["_id"],
         "state": "creating"
     }
     self.assertEqual(expected, instance)
示例#29
0
 def test_scale_instance(self):
     instance = storage.Instance(name="something", state="started")
     strg = mock.Mock()
     manager = mock.Mock(storage=strg)
     scalator = instance_scalator.InstanceScalator(manager, interval=3)
     scalator.locker = mock.Mock()
     scalator.scale_instance(instance, 2)
     self.assertEqual("started", instance.state)
     lock_name = "%s/something" % scalator.lock_name
     scalator.locker.init.assert_called_with(lock_name)
     scalator.locker.lock.assert_called_with(lock_name)
     manager.physical_scale.assert_called_with(instance, 2)
     strg.store_instance.assert_called_with(instance, save_units=False)
     scalator.locker.unlock.assert_called_with(lock_name)
示例#30
0
 def test_get_instance(self):
     instance = storage.Instance(name="something")
     strg = mock.Mock()
     strg.retrieve_instance.return_value = instance
     manager = mock.Mock(storage=strg)
     starter = instance_starter.InstanceStarter(manager, interval=3)
     starter.locker = mock.Mock()
     got_instance = starter.get_instance()
     self.assertEqual(instance, got_instance)
     self.assertEqual("starting", got_instance.state)
     strg.retrieve_instance.assert_called_with(state="creating")
     strg.store_instance.assert_called_with(instance)
     starter.locker.lock.assert_called_with(starter.lock_name)
     starter.locker.unlock.assert_called_with(starter.lock_name)