ginger-dev-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Ginger-dev-list] [PATCH 1/1] patch for mock tests for ginger-WOK branch


From: pkulkark
Subject: [Ginger-dev-list] [PATCH 1/1] patch for mock tests for ginger-WOK branch Added mock tests for ginger-WOK branch similar to master branch
Date: Thu, 13 Aug 2015 15:38:37 +0530

From: Pooja <address@hidden>

Signed-off-by: Pooja <address@hidden>
---
 Makefile.am               |   3 +-
 configure.ac              |   5 ++
 models/backup.py          |   6 +--
 models/firmware.py        |   2 +-
 models/ibm_sep.py         |  12 ++---
 models/powermanagement.py |   2 +-
 models/sensors.py         |   4 +-
 models/users.py           |   2 +-
 tests/Makefile.am         |  35 ++++++++++++++
 tests/run_tests.sh.in     |  45 ++++++++++++++++++
 tests/test_libuser.py     | 114 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/test_user_model.py  |  81 ++++++++++++++++++++++++++++++++
 12 files changed, 296 insertions(+), 15 deletions(-)
 create mode 100644 tests/Makefile.am
 create mode 100644 tests/run_tests.sh.in
 create mode 100644 tests/test_libuser.py
 create mode 100644 tests/test_user_model.py

diff --git a/Makefile.am b/Makefile.am
index 62fca19..63a7017 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -17,13 +17,14 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 
-SUBDIRS= po controls models ui contrib
+SUBDIRS= po controls models ui contrib tests
 
 ACLOCAL_AMFLAGS = --install -I m4
 
 PEP8_WHITELIST = \
        controls \
        models \
+        tests\
        $(NULL)
 
 ginger_PYTHON = \
diff --git a/configure.ac b/configure.ac
index 7f33e21..1402bb3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -42,6 +42,10 @@ AM_GNU_GETTEXT([external])
 AM_GNU_GETTEXT_VERSION([0.18])
 AC_PATH_PROG([CHEETAH], [cheetah], [/usr/bin/cheetah])
 
+AC_PYTHON_MODULE([unittest])
+AC_SUBST([HAVE_PYMOD_UNITTEST])
+AC_SUBST([PYTHON_VERSION])
+
 # Checking for pyflakes
 AC_PATH_PROG([PYFLAKES], [pyflakes])
 if test "x$PYFLAKES" = "x"; then
@@ -69,6 +73,7 @@ AC_CONFIG_FILES([
     contrib/Makefile
     contrib/DEBIAN/Makefile
     contrib/DEBIAN/control
+    tests/Makefile
 ],[
     chmod +x po/gen-pot
 ])
diff --git a/models/backup.py b/models/backup.py
index b283068..0108804 100644
--- a/models/backup.py
+++ b/models/backup.py
@@ -113,7 +113,7 @@ class ArchivesModel(object):
             # It's OK if archive_dir already exists
             if e.errno != errno.EEXIST:
                 wok_log.error('Error creating archive dir %s: %s',
-                                 cls._archive_dir, e)
+                              cls._archive_dir, e)
                 raise OperationFailed('GINHBK0003E',
                                       {'dir': cls._archive_dir})
 
@@ -157,14 +157,14 @@ class ArchivesModel(object):
                                    ignore_missing=True)
             except Exception as e_session:
                 wok_log.error('Error cleaning archive meta data %s. '
-                                 'Error: %s', params['identity'], e_session)
+                              'Error: %s', params['identity'], e_session)
 
             if params['file'] != '':
                 try:
                     os.unlink(params['file'])
                 except Exception as e_file:
                     wok_log.error('Error cleaning archive file %s. '
-                                     'Error: %s', params['file'], e_file)
+                                  'Error: %s', params['file'], e_file)
 
             raise OperationFailed(reason, {'identity': params['identity']})
 
diff --git a/models/firmware.py b/models/firmware.py
index 1e0da53..1bfbdd4 100644
--- a/models/firmware.py
+++ b/models/firmware.py
@@ -73,7 +73,7 @@ class FirmwareModel(object):
         image_file, ext = os.path.splitext(os.path.basename(fw_path))
         if image_file is None:
             wok_log.error('FW update failed: '
-                             'No image file found in the package file.')
+                          'No image file found in the package file.')
             raise OperationFailed('GINFW0003E')
         command = ['update_flash', '-f',
                    os.path.join('/tmp/fwupdate', '%s.img' % image_file)]
diff --git a/models/ibm_sep.py b/models/ibm_sep.py
index 51b2c2b..83c921a 100644
--- a/models/ibm_sep.py
+++ b/models/ibm_sep.py
@@ -39,7 +39,7 @@ def addSEP(params):
     output, error, rc = run_command(cmd)
     if rc != 0:
         wok_log.error('SEP execution error: %s - %s - %s' % (cmd, rc,
-                         error))
+                      error))
         raise OperationFailed('GINSEP0010E', {'error': error})
 
     return params['hostname']
@@ -65,7 +65,7 @@ class SepModel(object):
         output, error, rc = run_command(cmd)
         if rc != 0:
             wok_log.error('SEP service initialization error: %s - %s - %s'
-                             % (cmd, rc, error))
+                          % (cmd, rc, error))
             raise OperationFailed('GINSEP0008E', {'error': error})
 
     def stop(self, params=None):
@@ -73,7 +73,7 @@ class SepModel(object):
         output, error, rc = run_command(cmd)
         if rc != 0:
             wok_log.error('Error stopping SEP service: %s - %s - %s' % (cmd,
-                             rc, error))
+                          rc, error))
             raise OperationFailed('GINSEP0009E', {'error': error})
 
     def is_feature_available(self):
@@ -101,7 +101,7 @@ class SubscribersModel(object):
         # error: report
         if rc != 0:
             wok_log.error('SEP execution error: %s - %s - %s' % (cmd, rc,
-                             error))
+                          error))
             raise OperationFailed('GINSEP0007E')
 
         if len(output) > 1:
@@ -139,7 +139,7 @@ class SubscriptionModel(object):
         # error: report
         if rc != 0:
             wok_log.error('SEP execution error: %s - %s - %s' % (cmd, rc,
-                             error))
+                          error))
             raise OperationFailed('GINSEP0005E', {'error': error})
 
         if len(output) > 1:
@@ -185,5 +185,5 @@ class SubscriptionModel(object):
 
         if rc != 0:
             wok_log.error('SEP execution error: %s - %s - %s' % (cmd, rc,
-                                                                    error))
+                          error))
             raise OperationFailed('GINSEP0011E', {'error': error})
diff --git a/models/powermanagement.py b/models/powermanagement.py
index 9cd98d6..0d6d798 100644
--- a/models/powermanagement.py
+++ b/models/powermanagement.py
@@ -108,7 +108,7 @@ class PowerProfileModel(object):
             output, error, returncode = run_command(tuned_cmd)
             if returncode != 0:
                 wok_log.error('Could not activate power profile %s, '
-                                 'error: %s', powerprofile, error)
+                              'error: %s', powerprofile, error)
                 raise OperationFailed("GINPOWER004E",
                                       {'profile': powerprofile})
         return powerprofile
diff --git a/models/sensors.py b/models/sensors.py
index 937e747..d0520be 100644
--- a/models/sensors.py
+++ b/models/sensors.py
@@ -60,7 +60,7 @@ class SensorsModel(object):
             sens_out, error, rc = run_command(command)
             if rc:
                 wok_log.error("Error retrieving sensors data: %s: %s." %
-                                 (error, rc))
+                              (error, rc))
 
             devices = OrderedDict()
             for section in sens_out.split('\n\n'):
@@ -145,7 +145,7 @@ class SensorsModel(object):
             out, error, rc = run_command(['hddtemp'])
             if rc:
                 wok_log.error("Error retrieving HD temperatures: rc %s."
-                                 "output: %s" % (rc, error))
+                              "output: %s" % (rc, error))
                 return None
 
             hdds = OrderedDict()
diff --git a/models/users.py b/models/users.py
index a792bbe..2779004 100644
--- a/models/users.py
+++ b/models/users.py
@@ -85,7 +85,7 @@ class UsersModel(object):
         except Exception as e:
             UserModel().delete(user)
             wok_log.error('Could not add user %s to sudoers: %s',
-                             user, e.message)
+                          user, e.message)
             raise OperationFailed('GINUSER0007E', {'user': user})
 
     def _add_user_to_kvm_group(self, adm, user):
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644
index 0000000..d890ba8
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1,35 @@
+#
+# Project Ginger
+#
+# Copyright IBM Corp, 2015
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  
USA
+
+EXTRA_DIST = \
+        Makefile.am \
+       run_tests.sh.in \
+       $(NULL)
+
+do_substitution = \
+       sed -e 's,address@hidden@],$(HAVE_PYMOD_UNITTEST),g' \
+       -e 's,address@hidden@],$(PYTHON_VERSION),g'
+
+run_tests.sh: run_tests.sh.in Makefile
+       $(do_substitution) < $(srcdir)/run_tests.sh.in > run_tests.sh
+       chmod +x run_tests.sh
+
+BUILT_SOURCES = run_tests.sh
+
+CLEANFILES = run_tests.sh
diff --git a/tests/run_tests.sh.in b/tests/run_tests.sh.in
new file mode 100644
index 0000000..a39254c
--- /dev/null
+++ b/tests/run_tests.sh.in
@@ -0,0 +1,45 @@
+#!/bin/bash
+#
+# Project Ginger
+#
+# Copyright IBM, Corp. 2015
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  
USA
+
address@hidden@
address@hidden@
+
+if [ "$1" = "-v" ]; then
+    OPTS="-v"
+    shift
+else
+    OPTS=""
+fi
+
+if [ $# -ne 0 ]; then
+    ARGS="$@"
+else
+    ARGS="discover"
+fi
+
+if [ "$HAVE_UNITTEST" != "yes" -o "$PYTHON_VER" == "2.6" ]; then
+    CMD="unit2"
+else
+    CMD="python -m unittest"
+fi
+
+LIST=($ARGS)
+
+PYTHONPATH=../../../src:../ $CMD $ARGS $OPTS
diff --git a/tests/test_libuser.py b/tests/test_libuser.py
new file mode 100644
index 0000000..bc111f5
--- /dev/null
+++ b/tests/test_libuser.py
@@ -0,0 +1,114 @@
+#
+# Project Ginger
+#
+# Copyright IBM, Corp. 2015
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+
+import crypt
+import spwd
+import unittest
+import libuser
+
+from models.users import UserModel, UsersModel
+
+from wok.exception import OperationFailed
+from wok.rollbackcontext import RollbackContext
+from wok.utils import wok_log
+
+
+class UserAdmTests(unittest.TestCase):
+
+    def test_get_users_list(self):
+        model = UsersModel()
+        common_users = model.get_list()
+        self.assertGreaterEqual(len(common_users), 0)
+
+    def test_create_user(self):
+        user = 'unit_test_fake_user'
+        passwd = 'fakepass'
+        group = 'unit_test_fake_group'
+        profile = 'unit_test_fake_profile'
+        enc_passwd = crypt.crypt(passwd)
+        params = {'name': user, 'password': enc_passwd,
+                  'group': group, 'profile': profile}
+        model = UsersModel()
+        common_users = model.get_list()
+        users = UserModel()
+        with RollbackContext() as rollback:
+
+            model.create(params)
+            rollback.prependDefer(users.delete, user)
+
+            model = UsersModel()
+            new_users = model.get_list()
+            self.assertEqual(len(new_users), len(common_users) + 1)
+            encr_passwd = spwd.getspnam(user)[1]
+            invalid_passwd = [None, "NP", "!", "!!",  "", "LK", "*"]
+            self.assertNotIn(encr_passwd, invalid_passwd)
+            self.assertEqual(crypt.crypt(passwd, encr_passwd), encr_passwd)
+
+    def test_creating_existing_user_fails(self):
+        users_model = UsersModel()
+        user_model = UserModel()
+
+        user = 'unit_test_fake_user'
+        passwd = 'fakepass'
+        group = 'unit_test_fake_group'
+        profile = 'unit_test_fake_profile'
+
+        params = {'name': user, 'password': passwd,
+                  'group': group, 'profile': profile}
+
+        with RollbackContext() as rollback:
+            users_model.create(params)
+            rollback.prependDefer(user_model.delete, user)
+
+            with self.assertRaises(OperationFailed):
+                users_model.create(params)
+
+    def test_list_existing_groups(self):
+        adm = libuser.admin()
+        groups = adm.enumerateGroups()
+        self.assertGreaterEqual(len(groups), 1)
+
+    def test_create_group(self):
+        groupname = 'unit_test_fake_group'
+        adm = libuser.admin()
+        groups = adm.enumerateGroups()
+
+        with RollbackContext() as rollback:
+            new_group = adm.initGroup(groupname)
+            # gid = new_group[libuser.GIDNUMBER]
+            adm.addGroup(new_group)
+            rollback.prependDefer(self.delete_group, groupname)
+
+            new_groups = adm.enumerateGroups()
+            self.assertEqual(len(new_groups), len(groups) + 1)
+
+    def delete_group(self, group):
+        adm = libuser.admin()
+        group_obj = \
+            adm.lookupGroupById(adm.lookupGroupByName(group).get('pw_gid')[0])
+        if group_obj is None:
+            wok_log.error('Could not delete group "%s"', group)
+            raise OperationFailed('GINUSER0012E', {'group': group})
+
+        if not adm.enumerateUsersByGroup(group):
+            try:
+                adm.deleteGroup(group_obj)
+            except Exception as e:
+                wok_log.error('Could not delete group "%s": %s', group, e)
+                raise OperationFailed('GINUSER0012E', {'group': group})
diff --git a/tests/test_user_model.py b/tests/test_user_model.py
new file mode 100644
index 0000000..5b35a22
--- /dev/null
+++ b/tests/test_user_model.py
@@ -0,0 +1,81 @@
+#
+# Project Ginger
+#
+# Copyright IBM, Corp. 2015
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+
+import crypt
+import spwd
+import unittest
+
+from models.users import UserModel, UsersModel
+
+from wok.exception import OperationFailed
+from wok.rollbackcontext import RollbackContext
+
+
+class UserModelTests(unittest.TestCase):
+
+    def test_get_users_list(self):
+        model = UsersModel()
+        users = model.get_list()
+        self.assertGreaterEqual(len(users), 0)
+
+    def test_create_user(self):
+        users_model = UsersModel()
+        user_model = UserModel()
+
+        user = 'unit_test_fake_user'
+        passwd = 'fakepass'
+        group = 'unit_test_fake_group'
+        profile = 'unit_test_fake_profile'
+        enc_passwd = crypt.crypt(passwd)
+
+        common_users = users_model.get_list()
+        params = {'name': user, 'password': enc_passwd,
+                  'group': group, 'profile': profile}
+        with RollbackContext() as rollback:
+
+            users_model.create(params)
+            rollback.prependDefer(user_model.delete, user)
+
+            new_users = users_model.get_list()
+            self.assertEqual(len(new_users), len(common_users) + 1)
+
+            encr_passwd = spwd.getspnam(user)[1]
+            invalid_passwd = [None, "NP", "!", "!!",  "", "LK", "*"]
+            self.assertNotIn(encr_passwd, invalid_passwd)
+
+            self.assertEqual(crypt.crypt(passwd, encr_passwd), encr_passwd)
+
+    def test_creating_existing_user_fails(self):
+        users_model = UsersModel()
+        user_model = UserModel()
+
+        user = 'unit_test_fake_user'
+        passwd = 'fakepass'
+        group = 'unit_test_fake_group'
+        profile = 'unit_test_fake_profile'
+
+        params = {'name': user, 'password': passwd,
+                  'group': group, 'profile': profile}
+
+        with RollbackContext() as rollback:
+            users_model.create(params)
+            rollback.prependDefer(user_model.delete, user)
+
+            with self.assertRaises(OperationFailed):
+                users_model.create(params)
-- 
2.1.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]