[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Ginger-dev-list] [PATCH 1/2] Unit tests for user management
From: |
dhbarboza82 |
Subject: |
[Ginger-dev-list] [PATCH 1/2] Unit tests for user management |
Date: |
Fri, 24 Jul 2015 10:53:18 -0300 |
From: Daniel Henrique Barboza <address@hidden>
This patch creates a basic testing framework for Ginger models,
starting with some tests that cover the user management
backend
Signed-off-by: Daniel Henrique Barboza <address@hidden>
---
Makefile.am | 2 +-
configure.ac | 6 +++
tests/Makefile.am | 35 +++++++++++++++++
tests/run_tests.sh | 45 +++++++++++++++++++++
tests/run_tests.sh.in | 45 +++++++++++++++++++++
tests/test_libuser.py | 100 +++++++++++++++++++++++++++++++++++++++++++++++
tests/test_user_model.py | 79 +++++++++++++++++++++++++++++++++++++
7 files changed, 311 insertions(+), 1 deletion(-)
create mode 100644 tests/Makefile.am
create mode 100755 tests/run_tests.sh
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 5a52d95..4a13143 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -17,7 +17,7 @@
# 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
diff --git a/configure.ac b/configure.ac
index 26b78c0..f5de8e3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -42,6 +42,11 @@ 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 +74,7 @@ AC_CONFIG_FILES([
contrib/Makefile
contrib/DEBIAN/Makefile
contrib/DEBIAN/control
+ tests/Makefile
],[
chmod +x po/gen-pot
])
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 b/tests/run_tests.sh
new file mode 100755
index 0000000..55991db
--- /dev/null
+++ b/tests/run_tests.sh
@@ -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
+
+HAVE_UNITTEST=yes
+PYTHON_VER=2.7
+
+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/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..59e3c45
--- /dev/null
+++ b/tests/test_libuser.py
@@ -0,0 +1,100 @@
+#
+# 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 models.users as users
+
+from kimchi.exception import OperationFailed
+from kimchi.rollbackcontext import RollbackContext
+
+class UserAdmTests(unittest.TestCase):
+
+ def test_get_users_list(self):
+ common_users = users.get_users()
+ self.assertGreaterEqual(len(common_users), 0)
+
+ all_users = users.get_users(exclude_system_users=False)
+ self.assertGreaterEqual(len(all_users), 1)
+
+ self.assertGreaterEqual(all_users, common_users)
+
+ def test_create_user(self):
+ user = 'unit_test_fake_user'
+ passwd = 'fakepass'
+
+ common_users = users.get_users()
+ with RollbackContext() as rollback:
+ users.create_user(user,passwd)
+ rollback.prependDefer(users.delete_user, user)
+
+ new_users = users.get_users()
+ self.assertEqual(len(new_users), len(common_users) +1)
+
+ enc_passwd = spwd.getspnam(user)[1]
+ invalid_passwd = [None, "NP", "!", "!!", "", "LK", "*"]
+ self.assertNotIn(enc_passwd, invalid_passwd)
+
+ self.assertEqual(crypt.crypt(passwd, enc_passwd), enc_passwd)
+
+ def test_create_existing_user_fails(self):
+ user = 'unit_test_fake_user'
+ passwd = 'fakepass'
+
+ with RollbackContext() as rollback:
+ users.create_user(user,passwd)
+ rollback.prependDefer(users.delete_user, user)
+
+ with self.assertRaises(OperationFailed):
+ users.create_user(user,passwd)
+
+ def test_list_existing_groups(self):
+ groups = users.get_groups()
+ self.assertGreaterEqual(len(groups), 1)
+
+ def test_create_group(self):
+ groupname = 'unit_test_fake_group'
+ groups = users.get_groups()
+
+ with RollbackContext() as rollback:
+ users.create_group(groupname)
+ rollback.prependDefer(users.delete_group, groupname)
+
+ new_groups = users.get_groups()
+ self.assertEqual(len(new_groups), len(groups) + 1)
+
+ def test_add_user_to_primary_group(self):
+ user = 'unit_test_fake_user'
+ passwd = 'fakepass'
+ group = 'unit_test_fake_group'
+
+ with RollbackContext() as rollback:
+ users.create_group(group)
+ rollback.prependDefer(users.delete_group, group)
+
+ users.create_user(user,passwd)
+ rollback.prependDefer(users.delete_user, user)
+
+ users.add_user_to_primary_group(user, group)
+
+ users_group = users.get_users_from_group(group)
+ self.assertEqual(len(users_group), 1)
+ self.assertIn(user, users_group)
diff --git a/tests/test_user_model.py b/tests/test_user_model.py
new file mode 100644
index 0000000..efec45d
--- /dev/null
+++ b/tests/test_user_model.py
@@ -0,0 +1,79 @@
+#
+# 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 kimchi.exception import OperationFailed
+from kimchi.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'
+
+ common_users = users_model.get_list()
+ params = {'name': user, 'password': 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)
+
+ enc_passwd = spwd.getspnam(user)[1]
+ invalid_passwd = [None, "NP", "!", "!!", "", "LK", "*"]
+ self.assertNotIn(enc_passwd, invalid_passwd)
+
+ self.assertEqual(crypt.crypt(passwd, enc_passwd), enc_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.4.3