PageRenderTime 496ms CodeModel.GetById 151ms app.highlight 91ms RepoModel.GetById 251ms app.codeStats 0ms

/neatx/test/python/neatx.session_test.py

http://neatx.googlecode.com/
Python | 187 lines | 111 code | 45 blank | 31 comment | 4 complexity | ea69242e935f86cd59c5de08f89e3f5b MD5 | raw file
  1#!/usr/bin/python
  2#
  3
  4# Copyright (C) 2009 Google Inc.
  5#
  6# This program is free software; you can redistribute it and/or modify
  7# it under the terms of the GNU General Public License as published by
  8# the Free Software Foundation; either version 2 of the License, or
  9# (at your option) any later version.
 10#
 11# This program is distributed in the hope that it will be useful, but
 12# WITHOUT ANY WARRANTY; without even the implied warranty of
 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14# General Public License for more details.
 15#
 16# You should have received a copy of the GNU General Public License
 17# along with this program; if not, write to the Free Software
 18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 19# 02110-1301, USA.
 20
 21
 22"""Script for unittesting the session module"""
 23
 24
 25import logging
 26import os
 27import os.path
 28import shutil
 29import tempfile
 30import unittest
 31
 32from neatx import constants
 33from neatx import errors
 34from neatx import session
 35from neatx import utils
 36
 37
 38class _SaveableFakeSession(session.SessionBase):
 39  pass
 40
 41
 42class TestNewUniqueId(unittest.TestCase):
 43  """Tests for NewUniqueId"""
 44
 45  def test(self):
 46    wanted = set("ABCDEF0123456789")
 47    found = set()
 48
 49    # NewUniqueId uses a hash algorithm, hence we've to run it
 50    # several times to cover the whole charset.
 51    for i in xrange(1, 5):
 52      sessid = session.NewUniqueId(_data=i)
 53      self.failUnlessEqual(len(sessid), 32)
 54
 55      chars = set(sessid)
 56      self.failUnlessEqual(len(chars - wanted), 0)
 57
 58      # Keep used chars
 59      found |= chars
 60
 61    self.failUnlessEqual(len(found), len(wanted))
 62
 63
 64class TestNxSessionManager(unittest.TestCase):
 65  """Tests for NxSessionManager"""
 66
 67  def setUp(self):
 68    self.tmpdir = tempfile.mkdtemp()
 69    self.mgr = session.NxSessionManager(_path=self.tmpdir)
 70
 71  def tearDown(self):
 72    shutil.rmtree(self.tmpdir)
 73
 74  def _CreateSession(self, host, display, user):
 75    sessid = self.mgr.CreateSessionID()
 76
 77    sesspath = os.path.join(self.tmpdir, sessid)
 78    self.failUnless(os.path.exists(sesspath))
 79
 80    sess = _SaveableFakeSession(sessid, host, display, user)
 81    self.failUnlessEqual(sess.id, sessid)
 82    self.failUnlessEqual(sess.hostname, host)
 83    self.failUnlessEqual(sess.display, display)
 84    self.failUnlessEqual(sess.username, user)
 85    self.failUnlessEqual(sess.state, constants.SESS_STATE_CREATED)
 86    self.failUnless(sess.cookie is not None)
 87
 88    sessdatapath = os.path.join(self.tmpdir, sess.id,
 89                                constants.SESSION_DATA_FILE_NAME)
 90    self.failIf(os.path.exists(sessdatapath))
 91
 92    self.mgr.SaveSession(sess)
 93    self.failUnless(os.path.exists(sessdatapath))
 94
 95    return (sess, sessid, sesspath, sessdatapath)
 96
 97  def testSession(self):
 98    (sess1, sessid1, _, _) = self._CreateSession("localhost", 1, "joedoe")
 99
100    sess1.name = "foobar"
101    self.failUnlessEqual(sess1.name, "foobar")
102
103    self.mgr.SaveSession(sess1)
104
105    self.failUnlessEqual(sess1.id, sessid1)
106    self.failUnlessEqual(sess1.name, "foobar")
107
108  def testFindSessionsWithFilter(self):
109    (sess1, _, _, _) = self._CreateSession("localhost", 1, "user_a")
110    (sess2, _, _, _) = self._CreateSession("localhost", 2, "user_a")
111    (sess3, _, _, _) = self._CreateSession("localhost", 3, "user_b")
112    (sess4, _, _, _) = self._CreateSession("localhost", 4, "user_c")
113
114    # Filter without username and function
115    result = self.mgr.FindSessionsWithFilter(None, None)
116    self.failUnlessEqual(len(result), 4)
117
118    # Filter with username without function
119    result = self.mgr.FindSessionsWithFilter("user_a", None)
120    self.failUnlessEqual(len(result), 2)
121    self.failUnless(isinstance(result[0], session.NxSession))
122    self.failUnless(isinstance(result[1], session.NxSession))
123    self.failIf(set([result[0].id, result[1].id]) - set([sess1.id, sess2.id]))
124
125    # Filter with username and without function
126    result = self.mgr.FindSessionsWithFilter("user_b", None)
127    self.failUnlessEqual(len(result), 1)
128    self.failUnless(isinstance(result[0], session.NxSession))
129    self.failUnlessEqual(result[0].id, sess3.id)
130
131    # Set data for filter function tests
132    sess1.state = constants.SESS_STATE_RUNNING
133    sess2.state = constants.SESS_STATE_TERMINATING
134    sess3.state = constants.SESS_STATE_RUNNING
135    sess4.type = "unix-kde"
136    self.mgr.SaveSession(sess1)
137    self.mgr.SaveSession(sess2)
138    self.mgr.SaveSession(sess3)
139    self.mgr.SaveSession(sess4)
140
141    # Filter with username and function
142    result = self.mgr.FindSessionsWithFilter("user_c", self._FilterTypeUnixKde)
143    self.failUnlessEqual(len(result), 1)
144    self.failUnlessEqual(result[0].id, sess4.id)
145
146    # Filter without username, with function
147    result = self.mgr.FindSessionsWithFilter(None, self._FilterStateRunning)
148    self.failUnlessEqual(len(result), 2)
149    self.failUnlessEqual(result[0].state, constants.SESS_STATE_RUNNING)
150    self.failUnlessEqual(result[1].state, constants.SESS_STATE_RUNNING)
151    self.failIf(set([result[0].id, result[1].id]) - set([sess1.id, sess3.id]))
152
153  @staticmethod
154  def _FilterTypeUnixKde(sess):
155    return sess.type == "unix-kde"
156
157  @staticmethod
158  def _FilterStateRunning(sess):
159    return sess.state == constants.SESS_STATE_RUNNING
160
161  def testLoadSessionForUser(self):
162    (sess1, _, _, _) = self._CreateSession("localhost", 1, "user_a")
163    (sess2, _, _, _) = self._CreateSession("localhost", 2, "user_a")
164    (sess3, _, _, _) = self._CreateSession("localhost", 2, "user_b")
165
166    self.failUnless(self.mgr.LoadSessionForUser(sess1.id, "user_a"))
167    self.failUnless(self.mgr.LoadSessionForUser(sess2.id, "user_a"))
168    self.failUnless(self.mgr.LoadSessionForUser(sess3.id, "user_b"))
169    self.failIf(self.mgr.LoadSessionForUser(sess1.id, "!otheruser!"))
170    self.failIf(self.mgr.LoadSessionForUser(sess2.id, "user_b"))
171
172  def testState(self):
173    (sess, _, _, _) = self._CreateSession("localhost", 1, "joedoe")
174
175    def _SetState(state):
176      sess.state = state
177
178    _SetState(constants.SESS_STATE_STARTING)
179    self.failUnlessRaises(errors.InvalidSessionState, _SetState, "!invalid!")
180    self.failUnlessRaises(errors.InvalidSessionState, _SetState, "!dummy!")
181    self.failUnlessEqual(sess.state, constants.SESS_STATE_STARTING)
182
183
184if __name__ == '__main__':
185  # TODO: Move this to a generic function
186  logging.disable(logging.CRITICAL)
187  unittest.main()