Yes, to generate doc in html you can use pydoc on the modules,
but documentation in these tests is more focused on explaining 
how tests work not code under test. 
I would rather consider leaving this doc as it is and create proper
documentation for selinux module. So programmer can easily 
write help(selinux) in interpreter and get docs they need.



On Tue, May 20, 2014 at 8:37 AM, Miroslav Grepl <mgrepl@redhat.com> wrote:
On 05/20/2014 03:36 AM, Rastislav Hepner wrote:
Hey guys,

As part of my college thesis I was attempting to unit test
libselinux-2.2.2-4 on Fedora 20 via Python (used unittest framework).
I've created bunch of tests (60) for some labeling function but result
is not very good.
Decision coverage of code under test +-45%. Caused mostly by unability stub
dependencies from C source code via Python. So tests looking more like
integration/fuctional tests.
Great. Going to check it.

Also do you think there is a way how to generate DOC from these tests?

I mean I would like to create DOC? I mean I would like to see something like

setfilecon_raw(file, raw_con)

file="/tmp/test"
raw_con = "system_u:object_r:user_home_t:s0"
setfilecon_raw("/tmp/test","system_u:object_r:user_home_t:s0")

For each function. Basically the point is people don't know how to use python bindings for libselinux.

But at least these tests (probably more integration/fuctional tests as you wrote) are great examples for them.

So I will check them later this week and I don't have a problem to add them if  it looks OK.


I'm interested in your feedback if there is need for such a tests or its waste.
Thank you.


Add runner of testes and helper module which
contains usefull func def.
---
  tests/helper.py   | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
  tests/runtests.py | 46 +++++++++++++++++++++++++++++++++++++
  2 files changed, 115 insertions(+)
  create mode 100755 tests/helper.py
  create mode 100755 tests/runtests.py

diff --git a/tests/helper.py b/tests/helper.py
new file mode 100755
index 0000000..8d588ff
--- /dev/null
+++ b/tests/helper.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+
+#This program is free software: you can redistribute it and/or modify
+#it under the terms of the GNU General Public License as published by
+#the Free Software Foundation, either version 3 of the License, or
+#(at your option) any later version.
+#This program 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 General Public License for more details.
+#
+#For more information see <http://www.gnu.org/licenses/>
+
+
+import selinux
+import unittest
+import subprocess
+import re
+
+def findProcess(processName):
+    """Look whether process is active
+
+    Check whether process is up via bash command ps.
+
+    Args:
+        processName: Name of the process we are interested in.
+
+    Returns:
+        Lines of ouput from ps command which are describing active
+        instances of processName.
+    """
+    ps = subprocess.Popen("ps -ef | grep " + processName + " | grep -v grep",
+                          shell=True, stdout=subprocess.PIPE)
+    output = ps.stdout.read()
+    ps.stdout.close()
+    ps.wait()
+    return output
+
+def contextTranslation():
+    """Check if context translation is active.
+
+    It perform this by checking if daemon mcstransd is running.
+
+    Returns:
+        True when mcstransd is up.
+        False when its not.
+    """
+    processName = "mcstransd"
+    output = findProcess(processName)
+    if re.search(processName, output) is not None:
+        return True
+    else:
+        return False
+
+def read_attr_file(self, filename, pid="self"):
+    """Reads files from /proc/.../attr/
+    """
+    file_path = "/proc/%s/attr/%s" % (str(pid), filename)
+    fo = open(file_path, "r")
+    context = fo.read()[:-1]
+    fo.close()
+
+    if context == "":
+        return None
+    else:
+        return context
+
+if __name__ == '__main__':
+    print ("Module containing helpful definitions for testing libselinux.")
diff --git a/tests/runtests.py b/tests/runtests.py
new file mode 100755
index 0000000..f6a9c5c
--- /dev/null
+++ b/tests/runtests.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+
+#This program is free software: you can redistribute it and/or modify
+#it under the terms of the GNU General Public License as published by
+#the Free Software Foundation, either version 3 of the License, or
+#(at your option) any later version.
+#This program 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 General Public License for more details.
+#
+#For more information see <http://www.gnu.org/licenses/>
+
+
+import unittest
+import argparse
+import sys
+import os
+
+parser = argparse.ArgumentParser()
+parser.add_argument("-a", "--all", action="store_true", help="run all tests")
+parser.add_argument("-t", "--test", help="run single test specified
as module.testcase.test"
+                    ", specify module without '.py' suffix!")
+parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2],
+                    help="adhere verbosity of tests", default=2)
+parser.add_argument("-d", "--directory", help="choose directory with tests",
+                    default=os.path.dirname(__file__))
+
+args = parser.parse_args()
+
+if args.directory and not os.path.isdir(args.directory):
+    print "No such directory!\n\n"
+    parser.print_help()
+    sys.exit(-1)
+
+if args.all:
+    suite = unittest.TestLoader().discover(args.directory)
+elif args.test:
+    suite = unittest.TestLoader().loadTestsFromName(args.test, None)
+else:
+    parser.print_help()
+    sys.exit(0)
+
+
+unittest.TextTestRunner(verbosity=args.verbosity).run(suite)
+





--
Rastislav Hepner