Ticket #6231 - Overview: resources used does not show when user has a quota but no...
[ganeti_webmgr.git] / ganeti_web / tests / rapi_cache.py
blobed7180a047b14dd849c4b41e750d0bb0cc4bc3c3
1 # Copyright (C) 2010 Oregon State University et al.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
16 # USA.
19 from django.test import TestCase
21 from ganeti_web.models import (get_rapi, clear_rapi_cache, RAPI_CACHE,
22 Cluster)
23 from util import client
25 __all__ = ('TestRapiCache',)
28 class TestRapiCache(TestCase):
30 def setUp(self):
31 self.tearDown()
32 self.cluster = Cluster(hostname='ganeti.osuosl.test')
33 self.cluster.save()
35 def tearDown(self):
36 clear_rapi_cache()
37 Cluster.objects.all().delete()
39 def test_get_with_cluster(self):
40 """
41 Test getting a new rapi for a cluster
43 Verifies:
44 * rapi is returned
45 """
46 cluster = self.cluster
47 rapi = get_rapi(cluster.hash, cluster)
48 self.assert_(rapi)
49 self.assert_(isinstance(rapi, (client.GanetiRapiClient,)))
51 def test_get_with_id(self):
52 """
53 Test getting a new rapi for a cluster by cluster ID
54 Verifies:
55 * rapi is returned
56 """
57 cluster = self.cluster
58 rapi = get_rapi(cluster.hash, cluster.id)
59 self.assert_(rapi)
60 self.assert_(isinstance(rapi, (client.GanetiRapiClient,)))
62 def test_get_cached_client(self):
63 """
64 Test getting a cached rapi
66 Verifies:
67 * rapi returned is the same as the cached rapi
68 """
69 cluster = self.cluster
70 rapi = get_rapi(cluster.hash, cluster.id)
71 self.assert_(rapi)
72 self.assert_(isinstance(rapi, (client.GanetiRapiClient,)))
74 cached_rapi = get_rapi(cluster.hash, cluster)
75 self.assertEqual(rapi, cached_rapi)
77 cached_rapi = get_rapi(cluster.hash, cluster.id)
78 self.assertEqual(rapi, cached_rapi)
80 def test_get_changed_hash(self):
81 """
82 Test getting rapi after hash has changed
84 Verifies:
85 * a new rapi is created and returned
86 * old rapi is removed from cache
87 * reverse cache is now pointing to new hash
88 """
89 cluster = self.cluster
90 old_hash = cluster.hash
91 rapi = get_rapi(cluster.hash, cluster)
93 cluster.hostname = 'a.different.hostname'
94 cluster.save()
95 self.assertNotEqual(old_hash, cluster.hash, "new hash was not created")
96 new_rapi = get_rapi(cluster.hash, cluster)
97 self.assert_(rapi)
98 self.assert_(isinstance(rapi, (client.GanetiRapiClient,)))
99 self.assertNotEqual(rapi, new_rapi)
100 self.assertFalse(old_hash in RAPI_CACHE, "old rapi client was not removed")
102 def test_stale_hash(self):
104 Tests an object with a stale hash
106 Verifies:
107 * a rapi is created and stored using the current credentials
109 cluster = self.cluster
110 stale_cluster = Cluster.objects.get(id=cluster.id)
111 cluster.hostname = 'a.different.hostname'
112 cluster.save()
113 clear_rapi_cache()
114 stale_rapi = get_rapi(stale_cluster.hash, stale_cluster)
115 self.assert_(stale_rapi)
116 self.assert_(isinstance(stale_rapi, (client.GanetiRapiClient,)))
118 fresh_rapi = get_rapi(cluster.hash, cluster)
119 self.assertEqual(stale_rapi, fresh_rapi)
121 def test_stale_hash_new_already_created(self):
123 Tests an object with a stale hash, but the new client was already
124 created
126 Verifies:
127 * Existing client, with current hash, is returned
129 cluster = self.cluster
130 stale_cluster = Cluster.objects.get(id=cluster.id)
131 cluster.hostname = 'a.different.hostname'
132 cluster.save()
133 clear_rapi_cache()
134 fresh_rapi = get_rapi(cluster.hash, cluster)
135 stale_rapi = get_rapi(stale_cluster.hash, stale_cluster)
136 self.assert_(stale_rapi)
137 self.assert_(isinstance(stale_rapi, (client.GanetiRapiClient,)))
138 self.assertEqual(stale_rapi, fresh_rapi)