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,
19 from django
.test
import TestCase
21 from ganeti_web
.models
import (get_rapi
, clear_rapi_cache
, RAPI_CACHE
,
23 from util
import client
25 __all__
= ('TestRapiCache',)
28 class TestRapiCache(TestCase
):
32 self
.cluster
= Cluster(hostname
='ganeti.osuosl.test')
37 Cluster
.objects
.all().delete()
39 def test_get_with_cluster(self
):
41 Test getting a new rapi for a cluster
46 cluster
= self
.cluster
47 rapi
= get_rapi(cluster
.hash, cluster
)
49 self
.assert_(isinstance(rapi
, (client
.GanetiRapiClient
,)))
51 def test_get_with_id(self
):
53 Test getting a new rapi for a cluster by cluster ID
57 cluster
= self
.cluster
58 rapi
= get_rapi(cluster
.hash, cluster
.id)
60 self
.assert_(isinstance(rapi
, (client
.GanetiRapiClient
,)))
62 def test_get_cached_client(self
):
64 Test getting a cached rapi
67 * rapi returned is the same as the cached rapi
69 cluster
= self
.cluster
70 rapi
= get_rapi(cluster
.hash, cluster
.id)
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
):
82 Test getting rapi after hash has changed
85 * a new rapi is created and returned
86 * old rapi is removed from cache
87 * reverse cache is now pointing to new hash
89 cluster
= self
.cluster
90 old_hash
= cluster
.hash
91 rapi
= get_rapi(cluster
.hash, cluster
)
93 cluster
.hostname
= 'a.different.hostname'
95 self
.assertNotEqual(old_hash
, cluster
.hash, "new hash was not created")
96 new_rapi
= get_rapi(cluster
.hash, cluster
)
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
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'
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
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'
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
)