def test_used_resources(self): """ Tests retrieving dictionary of resources used by a cluster user """ owner = ClusterUser(name='owner') owner.save() c = Cluster(hostname='testing') c.save() vm0 = VirtualMachine(hostname='one', owner=owner, cluster=c) vm1 = VirtualMachine(hostname='two', ram=1, virtual_cpus=3, disk_size=5, owner=owner, cluster=c) vm2 = VirtualMachine(hostname='three', ram=2, virtual_cpus=4, disk_size=6, owner=owner, cluster=c) vm3 = VirtualMachine(hostname='four', ram=3, virtual_cpus=5, disk_size=7, cluster=c) vm0.save() vm1.save() vm2.save() vm3.save() used = owner.used_resources self.assertEqual(1+2, used['ram']) self.assertEqual(3+4, used['virtual_cpus']) self.assertEqual(5+6, used['disk'])
def setUp(self): self.cluster = Cluster(hostname='ganeti.osuosl.test') self.cluster.save()
class TestRapiCache(TestCase): def setUp(self): self.cluster = Cluster(hostname='ganeti.osuosl.test') self.cluster.save() def tearDown(self): clear_rapi_cache() Cluster.objects.all().delete() def test_get_with_cluster(self): """ Test getting a new rapi for a cluster Verifies: * rapi is returned """ cluster = self.cluster rapi = get_rapi(cluster.hash, cluster) self.assert_(rapi) self.assert_(isinstance(rapi, (client.GanetiRapiClient,))) def test_get_with_id(self): """ Test getting a new rapi for a cluster by cluster ID Verifies: * rapi is returned """ cluster = self.cluster rapi = get_rapi(cluster.hash, cluster.id) self.assert_(rapi) self.assert_(isinstance(rapi, (client.GanetiRapiClient,))) def test_get_cached_client(self): """ Test getting a cached rapi Verifies: * rapi returned is the same as the cached rapi """ cluster = self.cluster rapi = get_rapi(cluster.hash, cluster.id) self.assert_(rapi) self.assert_(isinstance(rapi, (client.GanetiRapiClient,))) cached_rapi = get_rapi(cluster.hash, cluster) self.assertEqual(rapi, cached_rapi) cached_rapi = get_rapi(cluster.hash, cluster.id) self.assertEqual(rapi, cached_rapi) def test_get_changed_hash(self): """ Test getting rapi after hash has changed Verifies: * a new rapi is created and returned * old rapi is removed from cache * reverse cache is now pointing to new hash """ cluster = self.cluster old_hash = cluster.hash rapi = get_rapi(cluster.hash, cluster) cluster.hostname = 'a.different.hostname' cluster.save() self.assertNotEqual(old_hash, cluster.hash, "new hash was not created") new_rapi = get_rapi(cluster.hash, cluster) self.assert_(rapi) self.assert_(isinstance(rapi, (client.GanetiRapiClient,))) self.assertNotEqual(rapi, new_rapi) self.assertFalse(old_hash in RAPI_CACHE, "old rapi client was not removed") def test_stale_hash(self): """ Tests an object with a stale hash Verifies: * a rapi is created and stored using the current credentials """ cluster = self.cluster stale_cluster = Cluster.objects.get(id=cluster.id) cluster.hostname = 'a.different.hostname' cluster.save() clear_rapi_cache() stale_rapi = get_rapi(stale_cluster.hash, stale_cluster) self.assert_(stale_rapi) self.assert_(isinstance(stale_rapi, (client.GanetiRapiClient,))) fresh_rapi = get_rapi(cluster.hash, cluster) self.assertEqual(stale_rapi, fresh_rapi) def test_stale_hash_new_already_created(self): """ Tests an object with a stale hash, but the new client was already created Verifies: * Existing client, with current hash, is returned """ cluster = self.cluster stale_cluster = Cluster.objects.get(id=cluster.id) cluster.hostname = 'a.different.hostname' cluster.save() clear_rapi_cache() fresh_rapi = get_rapi(cluster.hash, cluster) stale_rapi = get_rapi(stale_cluster.hash, stale_cluster) self.assert_(stale_rapi) self.assert_(isinstance(stale_rapi, (client.GanetiRapiClient,))) self.assertEqual(stale_rapi, fresh_rapi)