Repository: incubator-slider Updated Branches: refs/heads/develop 773c78420 -> a82e2cc41
SLIDER-550. Basic unit test to preserve default JVM opts and add libdir Project: http://git-wip-us.apache.org/repos/asf/incubator-slider/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-slider/commit/a82e2cc4 Tree: http://git-wip-us.apache.org/repos/asf/incubator-slider/tree/a82e2cc4 Diff: http://git-wip-us.apache.org/repos/asf/incubator-slider/diff/a82e2cc4 Branch: refs/heads/develop Commit: a82e2cc41ef78326ac2b4df4b861469856bfbbb9 Parents: 773c784 Author: Sumit Mohanty <[email protected]> Authored: Sat Oct 25 12:16:26 2014 -0700 Committer: Sumit Mohanty <[email protected]> Committed: Sat Oct 25 12:16:26 2014 -0700 ---------------------------------------------------------------------- slider-assembly/pom.xml | 27 +++++ .../src/test/python/scripts/TestSlider.py | 55 +++++++++ slider-assembly/src/test/python/unitTests.py | 118 +++++++++++++++++++ 3 files changed, 200 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/a82e2cc4/slider-assembly/pom.xml ---------------------------------------------------------------------- diff --git a/slider-assembly/pom.xml b/slider-assembly/pom.xml index e0a5e8d..d3cb928 100644 --- a/slider-assembly/pom.xml +++ b/slider-assembly/pom.xml @@ -38,6 +38,7 @@ <src.confdir>src/conf-hdp</src.confdir> <src.libdir>${project.build.directory}/lib</src.libdir> <src.agent.ini.dir>${project.build.directory}/../../slider-agent/conf</src.agent.ini.dir> + <skipTests>false</skipTests> </properties> <build> @@ -63,6 +64,32 @@ </execution> </executions> </plugin> + + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <version>${maven-exec-plugin.version}</version> + <executions> + <execution> + <configuration> + <executable>python</executable> + <workingDirectory>src/test/python</workingDirectory> + <arguments> + <argument>unitTests.py</argument> + </arguments> + <environmentVariables> + <PYTHONPATH>${project.basedir}/src/main/scripts:${project.basedir}/../slider-agent/src/test/python/mock:${project.basedir}/src/test/python/scripts</PYTHONPATH> + </environmentVariables> + <skip>${skipTests}</skip> + </configuration> + <id>python-test</id> + <phase>test</phase> + <goals> + <goal>exec</goal> + </goals> + </execution> + </executions> + </plugin> <!-- pull in all dependencies --> <plugin> http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/a82e2cc4/slider-assembly/src/test/python/scripts/TestSlider.py ---------------------------------------------------------------------- diff --git a/slider-assembly/src/test/python/scripts/TestSlider.py b/slider-assembly/src/test/python/scripts/TestSlider.py new file mode 100644 index 0000000..6fe2a47 --- /dev/null +++ b/slider-assembly/src/test/python/scripts/TestSlider.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +''' +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. +''' +import StringIO +import sys + +from mock import MagicMock, patch, ANY +import unittest +import logging +import slider +import os + +logger = logging.getLogger() + +class TestSlider(unittest.TestCase): + + @patch.object(slider, "confDir") + @patch.object(slider, "libDir") + @patch.object(slider, "executeEnvSh") + @patch("os.path.exists") + @patch.object(slider, "java") + def test_main(self, java_mock, exists_mock, executeEnvSh_mock, libDir_mock, confDir_mock): + sys.argv = ["slider", "list"] + exists_mock.return_value = True + libDir_mock.return_value = "/dir/libdir" + confDir_mock.return_value = "/dir/confdir" + slider.main() + self.assertTrue(java_mock.called) + java_mock.assert_called_with( + 'org.apache.slider.Slider', + ['list'], + '/dir/libdir/*:/dir/confdir::', + ['-Dslider.confdir=/dir/confdir', '-Dslider.libdir=/dir/libdir', '-Djava.net.preferIPv4Stack=true', '-Djava.awt.headless=true', '-Xmx256m']) + pass + + +if __name__ == "__main__": + logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG) + unittest.main() \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/a82e2cc4/slider-assembly/src/test/python/unitTests.py ---------------------------------------------------------------------- diff --git a/slider-assembly/src/test/python/unitTests.py b/slider-assembly/src/test/python/unitTests.py new file mode 100644 index 0000000..aebf4ba --- /dev/null +++ b/slider-assembly/src/test/python/unitTests.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python + +""" +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. +""" + +import unittest +from os.path import isdir +import logging +import os +import sys + +LOG_FILE_NAME='tests.log' +SELECTED_PREFIX = "_" +PY_EXT='.py' +ignoredDirs = ["mock"] + +class TestAgent(unittest.TestSuite): + def run(self, result, debug=False): + run = unittest.TestSuite.run + run(self, result, debug) + return result + + +def parent_dir(path): + if isdir(path): + if path.endswith(os.sep): + path = os.path.dirname(path) + parent = os.path.dirname(path) + else: + parent = os.path.dirname(os.path.dirname(path)) + + return parent + + +def all_tests_suite(): + root_dir = os.getcwd() + files_list = [] + for directory in os.listdir(root_dir): + if os.path.isdir(directory) and not directory in ignoredDirs: + files_list += os.listdir(root_dir + os.sep + directory) + ## temporarily deleting to add more predictability + ## shuffle(files_list) + files_list.sort() + tests_list = [] + + logger.info('------------------------TESTS LIST:-------------------------------------') + # If test with special name exists, run only this test + selected_test = None + for file_name in files_list: + if file_name.endswith(PY_EXT) and not file_name == __file__ and file_name.startswith(SELECTED_PREFIX): + logger.info("Running only selected test " + str(file_name)) + selected_test = file_name + if selected_test is not None: + tests_list.append(selected_test.replace(PY_EXT, '')) + else: + for file_name in files_list: + if file_name.endswith(PY_EXT) and not file_name == __file__: + replaced = file_name.replace(PY_EXT, '') + logger.info(replaced) + tests_list.append(replaced) + logger.info('------------------------------------------------------------------------') + + suite = unittest.TestLoader().loadTestsFromNames(tests_list) + return TestAgent([suite]) + +def main(): + + logger.info('------------------------------------------------------------------------') + logger.info('PYTHON SCRIPT TESTS') + logger.info('------------------------------------------------------------------------') + runner = unittest.TextTestRunner(verbosity=2, stream=sys.stdout) + suite = all_tests_suite() + status = runner.run(suite).wasSuccessful() + + if not status: + logger.error('-----------------------------------------------------------------------') + logger.error('Python unit tests failed') + logger.error('Find detailed logs in ' + path) + logger.error('-----------------------------------------------------------------------') + exit(1) + else: + logger.info('------------------------------------------------------------------------') + logger.info('Python unit tests finished succesfully') + logger.info('------------------------------------------------------------------------') + +if __name__ == '__main__': + + sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) + sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + os.sep + 'main' + os.sep + 'python') + sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + os.sep + 'main' + os.sep + 'python' + os.sep + 'agent') + logger = logging.getLogger() + logger.setLevel(logging.INFO) + formatter = logging.Formatter("[%(levelname)s] %(message)s") + src_dir = os.getcwd() + target_dir = parent_dir(parent_dir(parent_dir(src_dir))) + os.sep + 'target' + if not os.path.exists(target_dir): + os.mkdir(target_dir) + path = target_dir + os.sep + LOG_FILE_NAME + file=open(path, "w") + consoleLog = logging.StreamHandler(file) + consoleLog.setFormatter(formatter) + logger.addHandler(consoleLog) + main()
