summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRoman Rakus <rrakus@redhat.com>2013-01-14 18:22:04 +0100
committerRoman Rakus <rrakus@redhat.com>2013-01-14 18:23:58 +0100
commite2f7cec35373782817e161ec988922a577dd2da6 (patch)
treee0f1db679bd0410a75e6a523bb05f782ca7b99f2 /src
parentdf6847b72e4ba18d34c9309847f1f8c061102dd0 (diff)
downloadopenlmi-providers-e2f7cec35373782817e161ec988922a577dd2da6.tar.gz
openlmi-providers-e2f7cec35373782817e161ec988922a577dd2da6.tar.xz
openlmi-providers-e2f7cec35373782817e161ec988922a577dd2da6.zip
Account: tests
Signed-off-by: Roman Rakus <rrakus@redhat.com>
Diffstat (limited to 'src')
-rw-r--r--src/account/test/TestAccount.py100
-rw-r--r--src/account/test/TestGroup.py72
-rw-r--r--src/account/test/TestMemberOfGroup.py84
-rw-r--r--src/account/test/TestService.py114
-rw-r--r--src/account/test/__init__.py0
-rw-r--r--src/account/test/common.py46
-rwxr-xr-xsrc/account/test/localtest.sh1
-rw-r--r--src/account/test/methods.py80
8 files changed, 497 insertions, 0 deletions
diff --git a/src/account/test/TestAccount.py b/src/account/test/TestAccount.py
new file mode 100644
index 0000000..7157b52
--- /dev/null
+++ b/src/account/test/TestAccount.py
@@ -0,0 +1,100 @@
+# -*- encoding: utf-8 -*-
+# Copyright (C) 2012 Red Hat, Inc. All rights reserved.
+#
+# 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
+#
+# Authors: Roman Rakus <rrakus@redhat.com
+#
+
+from common import AccountBase
+from methods import *
+import subprocess
+
+class TestAccount(AccountBase):
+ """
+ Class for testing LMI_Account class
+ """
+ def test_account_properties(self):
+ """
+ Test if there are key and main properties in LMI_Account
+ """
+ slct = "select * from LMI_Account"
+ instances = self.wbemconnection.ExecQuery('WQL', slct)
+ self.assertTrue(len(instances) > 0)
+ # check if it provides key properties
+ for attr in ["CreationClassName", "Name", "SystemCreationClassName",
+ "SystemName"]:
+ self.assertIsNotNone(instances[0][attr])
+
+ # check if it provides other properties, which should be set
+ for attr in ["host", "UserID", "UserPassword", "UserPasswordEncoding"]:
+ self.assertIsNotNone(instances[0][attr])
+
+ def test_create_account(self):
+ """
+ Test to create user account
+ """
+ # make sure the account will not exist
+ clean_account(self.user_name)
+ computer_system = self.wbemconnection.ExecQuery('WQL',
+ 'select * from Linux_ComputerSystem')[0]
+ lams = self.wbemconnection.ExecQuery('WQL',
+ 'select * from LMI_AccountManagementService')[0]
+ self.wbemconnection.InvokeMethod("CreateAccount", lams.path,
+ Name=self.user_name, System=computer_system.path)
+ # The user now should be created, check it
+ subprocess.check_call(["id", self.user_name])
+ # now delete that user
+ clean_account(self.user_name)
+
+ def test_delete_account(self):
+ """
+ Test to delete account
+ """
+ # make sure the account will exist
+ create_account(self.user_name)
+ i = self.wbemconnection.ExecQuery('WQL',
+ 'select * from LMI_Account where Name = "%s"' % self.user_name)[0]
+ self.wbemconnection.DeleteInstance(i.path)
+ # check if it was really deleted
+ c = subprocess.Popen(["id", self.user_name])
+ c.communicate()
+ self.assertEqual(c.returncode, 1)
+ clean_account(self.user_name)
+
+ def test_modify_account(self):
+ """
+ Test several modifications
+ """
+ create_account(self.user_name)
+ i = self.wbemconnection.ExecQuery('WQL',
+ 'select * from LMI_Account where Name = "%s"' % self.user_name)[0]
+ # gecos
+ print i["ElementName"]
+ i["ElementName"] = "GECOS"
+ print i["ElementName"]
+ self.wbemconnection.ModifyInstance(i)
+ self.assertEqual(field_in_passwd(self.user_name, 4), "GECOS")
+ # login shell
+ i["LoginShell"] = "/modified"
+ self.wbemconnection.ModifyInstance(i)
+ self.assertEqual(field_in_passwd(self.user_name, 6), "/modified")
+ # password
+ i["UserPassword"][0] = '$6$9Ky8vI6f$ipRcdc7rgMrtDh.sWOaRSoBck2cLz4eUom8Eze.NaY2DoMmNimuFBrXpJjlPCjMoeFTYC.FdZwj488JZcohyw1'
+ self.wbemconnection.ModifyInstance(i)
+ self.assertEqual(field_in_shadow(self.user_name, 1),
+ '$6$9Ky8vI6f$ipRcdc7rgMrtDh.sWOaRSoBck2cLz4eUom8Eze.NaY2DoMmNimuFBrXpJjlPCjMoeFTYC.FdZwj488JZcohyw1')
+ clean_account(self.user_name)
+
diff --git a/src/account/test/TestGroup.py b/src/account/test/TestGroup.py
new file mode 100644
index 0000000..2cf5228
--- /dev/null
+++ b/src/account/test/TestGroup.py
@@ -0,0 +1,72 @@
+# -*- encoding: utf-8 -*-
+# Copyright (C) 2012 Red Hat, Inc. All rights reserved.
+#
+# 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
+#
+# Authors: Roman Rakus <rrakus@redhat.com
+#
+
+from common import AccountBase
+from methods import *
+
+class TestGroup(AccountBase):
+ """
+ Class for testing LMI_Group class
+ """
+ def test_group_properties(self):
+ """
+ Test if there are key and main properties in LMI_Group
+ """
+ slct = "select * from LMI_Group"
+ instances = self.wbemconnection.ExecQuery('WQL', slct)
+ self.assertTrue(len(instances) > 0)
+ # check if it provides key properties
+ for attr in ["CreationClassName", "Name"]:
+ self.assertIsNotNone(instances[0][attr])
+
+ # check if it provides other properties, which should be set
+ for attr in ["ElementName", "InstanceID"]:
+ self.assertIsNotNone(instances[0][attr])
+
+ def test_create_group(self):
+ """
+ Test to create group
+ """
+ # make sure the group will not exist
+ clean_group(self.group_name)
+ computer_system = self.wbemconnection.ExecQuery('WQL',
+ 'select * from Linux_ComputerSystem')[0]
+ lams = self.wbemconnection.ExecQuery('WQL',
+ 'select * from LMI_AccountManagementService')[0]
+ self.wbemconnection.InvokeMethod("CreateGroup", lams.path,
+ Name=self.group_name, System=computer_system.path)
+ # The group now should be created, check it
+ self.assertEqual(field_in_group(self.group_name, 0), self.group_name)
+ # now delete that group
+ clean_group(self.group_name)
+
+ def test_delete_group(self):
+ """
+ Test to delete group
+ """
+ # make sure the group will exist
+ create_group(self.group_name)
+ i = self.wbemconnection.ExecQuery('WQL',
+ 'select * from LMI_Group where Name = "%s"' % self.group_name)[0]
+ self.wbemconnection.DeleteInstance(i.path)
+ # check if it was really deleted
+ self.assertIsNone(field_in_group(self.group_name, 0))
+ clean_group(self.group_name)
+
diff --git a/src/account/test/TestMemberOfGroup.py b/src/account/test/TestMemberOfGroup.py
new file mode 100644
index 0000000..19071ef
--- /dev/null
+++ b/src/account/test/TestMemberOfGroup.py
@@ -0,0 +1,84 @@
+# -*- encoding: utf-8 -*-
+# Copyright (C) 2012 Red Hat, Inc. All rights reserved.
+#
+# 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
+#
+# Authors: Roman Rakus <rrakus@redhat.com
+#
+
+from common import AccountBase
+from methods import *
+import pywbem
+
+class TestMemberOfGroup(AccountBase):
+ """
+ Class for testing LMI_MemberOfGroup class
+ """
+ def test_add_user_to_group(self):
+ """
+ Test to add user to group
+ """
+ create_account(self.user_name)
+ create_group(self.group_name)
+ user = self.wbemconnection.ExecQuery('WQL',
+ 'select * from LMI_Account where Name = "%s"' %self.user_name)[0]
+ group = self.wbemconnection.ExecQuery('WQL',
+ 'select * from LMI_Group where Name = "%s"' %self.group_name)[0]
+ tocreate = pywbem.CIMInstance('LMI_MemberOfGroup',
+ {'Collection' : group.path,
+ 'Member' : user.path})
+ self.wbemconnection.CreateInstance(tocreate)
+ clean_account(self.user_name)
+ clean_group(self.group_name)
+
+ def test_remove_user_from_group(self):
+ """
+ Test remove user from group
+ """
+ # make sure the account will exist
+ create_account(self.user_name)
+ i = self.wbemconnection.ExecQuery('WQL',
+ 'select * from LMI_Account where Name = "%s"' % self.user_name)[0]
+ self.wbemconnection.DeleteInstance(i.path)
+ # check if it was really deleted
+ c = subprocess.Popen(["id", self.user_name])
+ c.communicate()
+ self.assertEqual(c.returncode, 1)
+ clean_account(self.user_name)
+
+ def test_user_in_groups(self):
+ """
+ Test correct list of groups for user
+ """
+ create_account(self.user_name)
+ create_group(self.group_name)
+ add_user_to_group(self.user_name, self.group_name)
+ user = self.wbemconnection.ExecQuery('WQL',
+ 'select * from LMI_Account where Name = "%s"' %self.user_name)[0]
+ ident = self.wbemconnection.ExecQuery('WQL',
+ 'select * from LMI_Identity where InstanceID = "LMI:UID:%s"' % user["UserID"])[0]
+ insts = self.wbemconnection.Associators(ident.path, AssocClass = 'LMI_MemberOfGroup')
+ self.assertEqual(len(insts), 2)
+ found_user = False
+ found_group = False
+ for inst in insts:
+ if inst["Name"] == self.user_name:
+ found_user = True
+ elif inst["Name"] == self.group_name:
+ found_group = True
+ self.assertTrue(found_user)
+ self.assertTrue(found_group)
+ clean_account(self.user_name)
+ clean_group(self.group_name)
diff --git a/src/account/test/TestService.py b/src/account/test/TestService.py
new file mode 100644
index 0000000..08a47ee
--- /dev/null
+++ b/src/account/test/TestService.py
@@ -0,0 +1,114 @@
+# -*- encoding: utf-8 -*-
+# Copyright (C) 2012 Red Hat, Inc. All rights reserved.
+#
+# 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
+#
+# Authors: Roman Rakus <rrakus@redhat.com
+#
+
+from common import AccountBase
+from methods import *
+import pywbem
+
+class TestService(AccountBase):
+ """
+ Class for testing LMI_AccountManagementService
+ """
+
+ def test_create_account(self):
+ """
+ Test create account parameters
+ """
+ # make sure the account will not exist
+ clean_account(self.user_name)
+ computer_system = self.wbemconnection.ExecQuery('WQL',
+ 'select * from Linux_ComputerSystem')[0]
+ lams = self.wbemconnection.ExecQuery('WQL',
+ 'select * from LMI_AccountManagementService')[0]
+
+ # create account and test all parameters
+ shell = "testshell"
+ system_account = True
+ dont_create_home = True
+ dont_create_group = True
+ gid = 0
+ gecos = "test create account gecos"
+ home_dir = "/test/home"
+ password = '$6$9Ky8vI6f$ipRcdc7rgMrtDh.sWOaRSoBck2cLz4eUom8Eze.NaY2DoMmNimuFBrXpJjlPCjMoeFTYC.FdZwj488JZcohyw1'
+ uid = 777
+ (rc, out) = self.wbemconnection.InvokeMethod("CreateAccount", lams.path,
+ Name = self.user_name,
+ System = computer_system.path,
+ Shell = shell,
+ SystemAccount = system_account,
+ DontCreateHome = dont_create_home,
+ DontCreateGroup = dont_create_group,
+ GID = pywbem.Uint32(gid),
+ GECOS = gecos,
+ HomeDirectory = home_dir,
+ Password = password,
+ UID = pywbem.Uint32(uid))
+
+ acc = out["Account"]
+ idents = out["Identities"]
+
+ # check return values
+ # account
+ self.assertEqual(rc, 0)
+ self.assertEqual(acc["Name"], self.user_name)
+ # identities
+ for identity in idents:
+ if identity["InstanceID"].find("UID") != -1:
+ # user identity
+ self.assertEqual(identity["InstanceID"], "LMI:UID:%d" %uid)
+ else:
+ # group identity
+ self.assertEqual(identity["InstanceID"], "LMI:GID:%d" %gid)
+
+ # check with system info
+ self.assertEqual(field_in_passwd(self.user_name, 2), str(uid))
+ self.assertEqual(field_in_passwd(self.user_name, 3), str(gid))
+ self.assertEqual(field_in_passwd(self.user_name, 4), gecos)
+ self.assertEqual(field_in_passwd(self.user_name, 5), home_dir)
+ self.assertEqual(field_in_passwd(self.user_name, 6), shell)
+ self.assertEqual(field_in_shadow(self.user_name, 1), password)
+ clean_account(self.user_name)
+
+ def test_create_group(self):
+ """
+ Test create group parameters
+ """
+ clean_group(self.group_name)
+ computer_system = self.wbemconnection.ExecQuery('WQL',
+ 'select * from Linux_ComputerSystem')[0]
+ lams = self.wbemconnection.ExecQuery('WQL',
+ 'select * from LMI_AccountManagementService')[0]
+ system_account = True
+ gid = 666
+ (rc, out) = self.wbemconnection.InvokeMethod("CreateGroup", lams.path,
+ Name=self.group_name,
+ System=computer_system.path,
+ SystemAccount=system_account,
+ GID=pywbem.Uint32(gid))
+
+ group = out["Group"]
+ idents = out["Identities"]
+ self.assertEqual(rc, 0)
+ self.assertEqual(group["Name"], self.group_name)
+ for identity in idents:
+ self.assertEqual(identity["InstanceID"], "LMI:GID:%d" %gid)
+
+ self.assertEqual(field_in_group(self.group_name, 2), str(gid))
+ clean_group(self.group_name)
diff --git a/src/account/test/__init__.py b/src/account/test/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/account/test/__init__.py
diff --git a/src/account/test/common.py b/src/account/test/common.py
new file mode 100644
index 0000000..2ffbbcf
--- /dev/null
+++ b/src/account/test/common.py
@@ -0,0 +1,46 @@
+# -*- encoding: utf-8 -*-
+# Copyright (C) 2012 Red Hat, Inc. All rights reserved.
+#
+# 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
+#
+# Authors: Roman Rakus <rrakus@redhat.com
+#
+
+import pywbem
+import os
+import unittest
+
+"""
+Base class for all tests
+"""
+
+
+
+class AccountBase(unittest.TestCase):
+ """
+ Base class for all LMI Account tests
+ """
+ def setUp(self):
+ """
+ Connnect to server
+ """
+ self.url = os.environ.get("LMI_CIMOM_URL", "https://localhost:5989")
+ self.username = os.environ.get("LMI_CIMOM_USERNAME", "root")
+ self.password = os.environ.get("LMI_CIMOM_PASSWORD", "")
+ self.user_name = os.environ.get("LMI_ACCOUNT_USER")
+ self.group_name = os.environ.get("LMI_ACCOUNT_GROUP")
+ self.wbemconnection = pywbem.WBEMConnection(self.url,
+ (self.username, self.password))
+
diff --git a/src/account/test/localtest.sh b/src/account/test/localtest.sh
new file mode 100755
index 0000000..f8d1b59
--- /dev/null
+++ b/src/account/test/localtest.sh
@@ -0,0 +1 @@
+LMI_CIMOM_PASSWORD="test" LMI_CIMOM_USERNAME="pegasus" LMI_ACCOUNT_USER="account_test_user" LMI_ACCOUNT_GROUP="account_test_group" nosetests -v
diff --git a/src/account/test/methods.py b/src/account/test/methods.py
new file mode 100644
index 0000000..b40859f
--- /dev/null
+++ b/src/account/test/methods.py
@@ -0,0 +1,80 @@
+# -*- encoding: utf-8 -*-
+# Copyright (C) 2012 Red Hat, Inc. All rights reserved.
+#
+# 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
+#
+# Authors: Roman Rakus <rrakus@redhat.com
+#
+
+import subprocess
+
+def field_in_passwd(username, number):
+ """
+ Return numberth field in /etc/passwd for given username
+ """
+ for line in open("/etc/passwd").readlines():
+ if line.startswith(username):
+ return line.split(":")[number].strip()
+
+def field_in_shadow(username, number):
+ """
+ Return numberth field in /etc/shadow for given username
+ """
+ for line in open("/etc/shadow").readlines():
+ if line.startswith(username):
+ return line.split(":")[number].strip()
+
+def field_in_group(groupname, number):
+ """
+ Return numberth field in /etc/group for given groupname
+ """
+ for line in open("/etc/group").readlines():
+ if line.startswith(groupname):
+ return line.split(":")[number].strip()
+
+def clean_account(user_name):
+ """
+ Force to delete testing account and remove home dir
+ """
+ subprocess.call(["userdel", "-fr", user_name])
+ # groups should be expicitely deleted
+ subprocess.call(["groupdel", user_name])
+ subprocess.call(["rm", "-fr", "/home/%s" %user_name])
+
+def add_user_to_group(user_name, group_name):
+ """
+ Will add user to group
+ """
+ subprocess.call(["usermod", "-a", "-G", group_name, user_name])
+
+def create_account(user_name):
+ """
+ Force to create account; run clean_account before creation
+ """
+ clean_account(user_name)
+ subprocess.call(["useradd", user_name])
+
+def clean_group(group_name):
+ """
+ Force to delete testing group
+ """
+ subprocess.call(["groupdel", group_name])
+
+def create_group(group_name):
+ """
+ Force to create group
+ """
+ clean_group(group_name)
+ subprocess.call(["groupadd", group_name])