587
|
1 |
#============================================================================
|
|
2 |
#Name : test_session_provider.py
|
|
3 |
#Part of : Helium
|
|
4 |
|
|
5 |
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
6 |
#All rights reserved.
|
|
7 |
#This component and the accompanying materials are made available
|
|
8 |
#under the terms of the License "Eclipse Public License v1.0"
|
|
9 |
#which accompanies this distribution, and is available
|
|
10 |
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
11 |
#
|
|
12 |
#Initial Contributors:
|
|
13 |
#Nokia Corporation - initial contribution.
|
|
14 |
#
|
|
15 |
#Contributors:
|
|
16 |
#
|
|
17 |
#Description:
|
|
18 |
#===============================================================================
|
|
19 |
|
588
|
20 |
""" Test cases for ccm python toolkit."""
|
587
|
21 |
|
628
|
22 |
# pylint: disable=R0201
|
588
|
23 |
|
587
|
24 |
import unittest
|
|
25 |
import ccm
|
|
26 |
import ccm.extra
|
|
27 |
import os
|
|
28 |
import logging
|
|
29 |
import tempfile
|
|
30 |
|
|
31 |
_logger = logging.getLogger('test.test_session_provider')
|
|
32 |
logging.basicConfig(level=logging.INFO)
|
|
33 |
|
628
|
34 |
|
587
|
35 |
class MockResultSession(ccm.AbstractSession):
|
|
36 |
""" Fake session used to test Result"""
|
628
|
37 |
def __init__(self, behave=None, database="fakedb"):
|
587
|
38 |
ccm.AbstractSession.__init__(self, None, None, None, None)
|
628
|
39 |
if behave == None:
|
|
40 |
behave = {}
|
587
|
41 |
self._behave = behave
|
|
42 |
self._database = database
|
|
43 |
self.dbpath = "/path/to/" + database
|
|
44 |
self._session_addr = "LOCALHOST:127.0.0.1:1234"
|
|
45 |
|
|
46 |
def database(self):
|
|
47 |
"""database"""
|
|
48 |
_logger.info("running database from MockResultSession")
|
|
49 |
return self._database
|
|
50 |
|
|
51 |
def execute(self, cmdline, result=None):
|
|
52 |
"""execute the class"""
|
|
53 |
if result == None:
|
|
54 |
result = ccm.Result(self)
|
|
55 |
if self._behave.has_key(cmdline):
|
|
56 |
result.statuserrors = 0
|
|
57 |
result.output = self._behave[cmdline]
|
|
58 |
else:
|
|
59 |
result.status = -1
|
|
60 |
return result
|
|
61 |
|
628
|
62 |
|
587
|
63 |
class MockOpener(object):
|
|
64 |
""" An Opener which provides a mock session """
|
|
65 |
def __init__(self):
|
|
66 |
self.failOnNewOpen = False
|
|
67 |
|
628
|
68 |
# pylint: disable=W0613
|
587
|
69 |
#need disable msg to prevent pylint warning as this is emulating the real method.
|
|
70 |
def __call__(self, username=None, password=None, engine=None, dbpath=None, database=None, reuse=True):
|
|
71 |
assert self.failOnNewOpen == False, "This method should not be called again."
|
|
72 |
if database != "fakedb":
|
|
73 |
raise ccm.CCMException("Invalid database")
|
|
74 |
return MockResultSession()
|
|
75 |
# pylint: enable-msg=W0613
|
|
76 |
|
628
|
77 |
|
587
|
78 |
class SessionProviderTest(unittest.TestCase):
|
|
79 |
""" Testing Results parsers. """
|
|
80 |
def test_get_valid_db(self):
|
|
81 |
""" Test the opening of a valid database. """
|
|
82 |
prov = ccm.extra.SessionProvider(opener=MockOpener())
|
|
83 |
dbase = prov.get(database="fakedb")
|
|
84 |
assert dbase is not None
|
|
85 |
|
|
86 |
def test_get_invalid_db(self):
|
|
87 |
""" Test the opening of an invalid database. """
|
|
88 |
prov = ccm.extra.SessionProvider(opener=MockOpener())
|
|
89 |
try:
|
588
|
90 |
_ = prov.get(database="invaliddb")
|
587
|
91 |
assert False, "Should raise Exception when giving unexisting dbase.'"
|
628
|
92 |
except ccm.CCMException, exc:
|
587
|
93 |
_logger.info(exc)
|
|
94 |
|
|
95 |
|
|
96 |
class CachedSessionProviderTest(unittest.TestCase):
|
|
97 |
""" Testing Results parsers. """
|
|
98 |
session_cache = os.path.join(tempfile.mkdtemp(), 'session_cache.xml')
|
|
99 |
|
|
100 |
def setUp(self):
|
|
101 |
"""called before any of the test methods are run"""
|
|
102 |
if not os.path.exists(os.path.dirname(self.session_cache)):
|
|
103 |
os.makedirs(os.path.dirname(self.session_cache))
|
|
104 |
if os.path.exists(self.session_cache):
|
|
105 |
os.remove(self.session_cache)
|
|
106 |
|
|
107 |
def tearDown(self):
|
|
108 |
"""called after all the tests are run to tidy up """
|
|
109 |
if os.path.exists(self.session_cache):
|
|
110 |
os.remove(self.session_cache)
|
|
111 |
|
|
112 |
def test_get_valid_db(self):
|
|
113 |
""" Test the opening of a valid database (cached). """
|
|
114 |
prov = ccm.extra.CachedSessionProvider(opener=MockOpener())
|
|
115 |
dbase = prov.get(database="fakedb")
|
|
116 |
assert dbase is not None
|
|
117 |
|
|
118 |
def test_get_invalid_db(self):
|
|
119 |
""" Test the opening of an invalid database (cached). """
|
|
120 |
prov = ccm.extra.CachedSessionProvider(opener=MockOpener())
|
|
121 |
try:
|
588
|
122 |
_ = prov.get(database="invaliddb")
|
587
|
123 |
assert False, "Should raise Exception when giving unexisting dbase.'"
|
628
|
124 |
except ccm.CCMException, exc:
|
587
|
125 |
_logger.info(exc)
|
|
126 |
|
|
127 |
def test_open_session_twice(self):
|
|
128 |
""" Open session then free it then open it again... """
|
|
129 |
opener = MockOpener()
|
|
130 |
prov = ccm.extra.CachedSessionProvider(opener=opener)
|
|
131 |
dbase = prov.get(database="fakedb")
|
|
132 |
assert dbase is not None
|
|
133 |
dbase.close()
|
|
134 |
opener.failOnNewOpen = True
|
|
135 |
db2 = prov.get(database="fakedb")
|
|
136 |
assert db2 is not None
|
|
137 |
|
|
138 |
def test_write_cache(self):
|
|
139 |
"""write to the cache"""
|
|
140 |
prov = ccm.extra.CachedSessionProvider(opener=MockOpener(), cache=self.session_cache)
|
|
141 |
dbase = prov.get(database="fakedb")
|
|
142 |
assert dbase is not None
|
|
143 |
del dbase
|
|
144 |
prov.save()
|
|
145 |
assert os.path.exists(self.session_cache), "Cache file %s is missing." % self.session_cache
|
|
146 |
|
|
147 |
def test_write_and_load_cache(self):
|
|
148 |
"""write to and load the cache"""
|
|
149 |
# patching the default implementation
|
|
150 |
def always_true(_, d_base=None):
|
|
151 |
"""always true return 1"""
|
|
152 |
d_base = True #done to prevent pylint warning
|
|
153 |
return d_base
|
|
154 |
ccm.session_exists = always_true
|
|
155 |
|
|
156 |
prov = ccm.extra.CachedSessionProvider(opener=MockOpener(), cache=self.session_cache)
|
|
157 |
dbase = prov.get(database="fakedb")
|
|
158 |
assert dbase is not None
|
|
159 |
del dbase
|
|
160 |
prov.close()
|
|
161 |
assert os.path.exists(self.session_cache), "Cache file %s is missing." % self.session_cache
|
|
162 |
|
|
163 |
opener = MockOpener()
|
|
164 |
opener.failOnNewOpen = True
|
|
165 |
prov = ccm.extra.CachedSessionProvider(opener=opener, cache=self.session_cache)
|
|
166 |
assert 'fakedb' in prov.cacheFree
|
|
167 |
assert len(prov.cacheFree['fakedb']) == 1
|