From 2e805e26e29809016b66a9abeed3629335c77c0b Mon Sep 17 00:00:00 2001 From: Lukasz Kostyra Date: Fri, 30 May 2014 09:52:37 +0200 Subject: [PATCH] Add framework for integration tests [Feature] Framework for integration tests in security-containers. [Cause] Integration tests in python are to be added to security-containers. [Solution] Add framework for integration tests using Python's unittest module. [Verification] Successful build and installation. Change-Id: I8812f044215fb282de90c1a906a9e433c545f046 Signed-off-by: Lukasz Kostyra --- CMakeLists.txt | 9 ++++ packaging/security-containers.spec | 6 ++- tests/CMakeLists.txt | 1 + tests/integration_tests/CMakeLists.txt | 40 ++++++++++++++++ tests/integration_tests/__init__.py | 2 + tests/integration_tests/common/CMakeLists.txt | 27 +++++++++++ tests/integration_tests/common/__init__.py | 2 + tests/integration_tests/common/sc_test_utils.py | 63 +++++++++++++++++++++++++ tests/integration_tests/sc_int_tests.py | 20 ++++++++ 9 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 tests/integration_tests/CMakeLists.txt create mode 100644 tests/integration_tests/__init__.py create mode 100644 tests/integration_tests/common/CMakeLists.txt create mode 100644 tests/integration_tests/common/__init__.py create mode 100644 tests/integration_tests/common/sc_test_utils.py create mode 100644 tests/integration_tests/sc_int_tests.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 18786ef..5d5cd90 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,15 @@ ADD_DEFINITIONS(-DPROGRAM_VERSION="${VERSION}") ADD_DEFINITIONS(-DPROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}") +## Python packages directory ################################################### + +IF(NOT DEFINED PYTHON_SITELIB) + EXECUTE_PROCESS(COMMAND python -c + "from distutils.sysconfig import get_python_lib; import sys; sys.stdout.write(get_python_lib())" + OUTPUT_VARIABLE PYTHON_SITELIB) +ENDIF(NOT DEFINED PYTHON_SITELIB) + + ## Subdirectories ############################################################## SET(COMMON_FOLDER ${PROJECT_SOURCE_DIR}/common) SET(CLIENT_FOLDER ${PROJECT_SOURCE_DIR}/client) diff --git a/packaging/security-containers.spec b/packaging/security-containers.spec index b6c7e78..49c2d84 100644 --- a/packaging/security-containers.spec +++ b/packaging/security-containers.spec @@ -47,7 +47,8 @@ between them. A process from inside a container can request a switch of context %cmake . -DVERSION=%{version} \ -DCMAKE_BUILD_TYPE=%{build_type} \ -DSCRIPT_INSTALL_DIR=%{script_dir} \ - -DSYSTEMD_UNIT_DIR=%{_unitdir} + -DSYSTEMD_UNIT_DIR=%{_unitdir} \ + -DPYTHON_SITELIB=%{python_sitelib} make -k %{?jobs:-j%jobs} %install @@ -153,6 +154,9 @@ Unit tests for both: server and client and integration tests. %defattr(644,root,root,755) %attr(755,root,root) %{_bindir}/security-containers-server-unit-tests %attr(755,root,root) %{script_dir}/sc_all_tests.py +%attr(755,root,root) %{script_dir}/sc_int_tests.py %attr(755,root,root) %{script_dir}/sc_launch_test.py %{script_dir}/sc_test_parser.py %{_datadir}/security-containers +%{python_sitelib}/sc_integration_tests/ + diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 54dd155..60214f9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -18,5 +18,6 @@ # ADD_SUBDIRECTORY(scripts) +ADD_SUBDIRECTORY(integration_tests) ADD_SUBDIRECTORY(unit_tests) diff --git a/tests/integration_tests/CMakeLists.txt b/tests/integration_tests/CMakeLists.txt new file mode 100644 index 0000000..044f6f7 --- /dev/null +++ b/tests/integration_tests/CMakeLists.txt @@ -0,0 +1,40 @@ +# Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# +# @file CMakeLists.txt +# @author Lukasz Kostyra (l.kostyra@samsung.com) +# + +MESSAGE(STATUS "Generating makefile for Integration Tests...") + + +## Defines for further package installation #################################### +SET(TEST_DEST_DIR "${PYTHON_SITELIB}/sc_integration_tests") + + +## Search for .py files available ############################################## +FILE(GLOB py_SCRIPTS *.py) +FILE(GLOB main_SCRIPT sc_int_tests.py) +LIST(REMOVE_ITEM py_SCRIPTS ${main_SCRIPT}) + + +## Install ##################################################################### +INSTALL(FILES ${py_SCRIPTS} DESTINATION ${TEST_DEST_DIR}) +INSTALL(PROGRAMS ${main_SCRIPT} DESTINATION ${SCRIPT_INSTALL_DIR}) + + +## Subdirectories ############################################################## +ADD_SUBDIRECTORY(common) + diff --git a/tests/integration_tests/__init__.py b/tests/integration_tests/__init__.py new file mode 100644 index 0000000..cb111b5 --- /dev/null +++ b/tests/integration_tests/__init__.py @@ -0,0 +1,2 @@ +__all__ = ["common", + ] diff --git a/tests/integration_tests/common/CMakeLists.txt b/tests/integration_tests/common/CMakeLists.txt new file mode 100644 index 0000000..d1b6511 --- /dev/null +++ b/tests/integration_tests/common/CMakeLists.txt @@ -0,0 +1,27 @@ +# Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# +# @file CMakeLists.txt +# @author Lukasz Kostyra (l.kostyra@samsung.com) +# + +MESSAGE(STATUS "Including common package for Integration Tests...") + +SET(TEST_COMMON_DEST_DIR "${TEST_DEST_DIR}/common") + +FILE(GLOB common_SCRIPTS *.py) + +INSTALL(FILES ${common_SCRIPTS} DESTINATION ${TEST_COMMON_DEST_DIR}) + diff --git a/tests/integration_tests/common/__init__.py b/tests/integration_tests/common/__init__.py new file mode 100644 index 0000000..1be1917 --- /dev/null +++ b/tests/integration_tests/common/__init__.py @@ -0,0 +1,2 @@ +__all__ = ["sc_test_utils", + ] diff --git a/tests/integration_tests/common/sc_test_utils.py b/tests/integration_tests/common/sc_test_utils.py new file mode 100644 index 0000000..09f45e6 --- /dev/null +++ b/tests/integration_tests/common/sc_test_utils.py @@ -0,0 +1,63 @@ +'''! Module containing utilities for domain-tests + +@author Lukasz Kostyra (l.kostyra@samsung.com) +''' +import subprocess +import os + + + +def launchProc(cmd): + '''! Launch specified command as a subprocess. + + Launches provided command using Python's subprocess module. Returns output from stdout and + stderr. + + @param cmd Command to be launched + @return Output provided by specified command. + @exception Exception When a process exits with error code, a Exception object containing message + with error data is raised. + ''' + p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + ret = p.wait() + output = p.stdout.read() + + if ret != 0: + raise Exception(cmd + " failed. error: " + os.strerror(ret) + ", output: " + output) + + return output + + + +def mount(dir, opts = []): + '''! Mounts specified directory with additional command line options. + + @param dir Directory to be mounted + @param opts Additional command line options for mount. This argument is optional. + @see launchProc + ''' + launchProc(" ".join(["mount"] + opts + [dir])) + + + +def umount(dir): + '''! Unmounts specified directory. + + @param dir Directory to be umounted + @see launchProc + ''' + launchProc(" ".join(["umount"] + [dir])) + + + +def isNumber(str): + '''! Checks if provided String is a number. + + @param str String to be checked if is a number. + @return True if string is a number, False otherwise. + ''' + try: + int(str) + return True + except ValueError: + return False diff --git a/tests/integration_tests/sc_int_tests.py b/tests/integration_tests/sc_int_tests.py new file mode 100644 index 0000000..8a05cba --- /dev/null +++ b/tests/integration_tests/sc_int_tests.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +'''@package: sc_integration_tests +@author: Lukasz Kostyra (l.kostyra@samsung.com) + +Security-containers integration tests launcher. Launches all integration tests. +''' +import unittest + +test_groups = [# add tests here... # + ] + +def main(): + for test_group in test_groups: + print "Starting", test_group.__name__, " ..." + suite = unittest.TestLoader().loadTestsFromModule(test_group) + unittest.TextTestRunner(verbosity=2).run(suite) + print "\n" + +if __name__ == "__main__": + main() -- 2.7.4