You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by dm...@apache.org on 2013/11/19 19:17:20 UTC
[6/7] AMBARI-3810. Unittests for File resource an all it's attributes
(Eugene Chekanskiy via dlysnichenko)
http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e5c6e113/ambari-agent/src/test/python/TestHostCheckReportFileHandler.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/TestHostCheckReportFileHandler.py b/ambari-agent/src/test/python/TestHostCheckReportFileHandler.py
deleted file mode 100644
index 1256717..0000000
--- a/ambari-agent/src/test/python/TestHostCheckReportFileHandler.py
+++ /dev/null
@@ -1,161 +0,0 @@
-#!/usr/bin/env python2.6
-
-'''
-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.
-'''
-
-from unittest import TestCase
-import unittest
-import os
-import tempfile
-from ambari_agent.HostCheckReportFileHandler import HostCheckReportFileHandler
-import logging
-import ConfigParser
-
-class TestHostCheckReportFileHandler(TestCase):
-
- logger = logging.getLogger()
-
- def test_write_host_check_report_really_empty(self):
- tmpfile = tempfile.mktemp()
-
- config = ConfigParser.RawConfigParser()
- config.add_section('agent')
- config.set('agent', 'prefix', os.path.dirname(tmpfile))
-
- handler = HostCheckReportFileHandler(config)
- dict = {}
- handler.writeHostCheckFile(dict)
-
- configValidator = ConfigParser.RawConfigParser()
- configPath = os.path.join(os.path.dirname(tmpfile), HostCheckReportFileHandler.HOST_CHECK_FILE)
- configValidator.read(configPath)
- if configValidator.has_section('users'):
- users = configValidator.get('users', 'usr_list')
- self.assertEquals(users, '')
-
- def test_write_host_check_report_empty(self):
- tmpfile = tempfile.mktemp()
-
- config = ConfigParser.RawConfigParser()
- config.add_section('agent')
- config.set('agent', 'prefix', os.path.dirname(tmpfile))
-
- handler = HostCheckReportFileHandler(config)
- dict = {}
- dict['hostHealth'] = {}
- dict['existingUsers'] = []
- dict['alternatives'] = []
- dict['stackFoldersAndFiles'] = []
- dict['hostHealth']['activeJavaProcs'] = []
- dict['installedPackages'] = []
- dict['existingRepos'] = []
-
- handler.writeHostCheckFile(dict)
-
- configValidator = ConfigParser.RawConfigParser()
- configPath = os.path.join(os.path.dirname(tmpfile), HostCheckReportFileHandler.HOST_CHECK_FILE)
- configValidator.read(configPath)
- users = configValidator.get('users', 'usr_list')
- users = configValidator.get('users', 'usr_homedir_list')
- self.assertEquals(users, '')
- names = configValidator.get('alternatives', 'symlink_list')
- targets = configValidator.get('alternatives', 'target_list')
- self.assertEquals(names, '')
- self.assertEquals(targets, '')
-
- paths = configValidator.get('directories', 'dir_list')
- self.assertEquals(paths, '')
-
- procs = configValidator.get('processes', 'proc_list')
- self.assertEquals(procs, '')
-
- pkgs = configValidator.get('packages', 'pkg_list')
- self.assertEquals(pkgs, '')
-
- repos = configValidator.get('repositories', 'repo_list')
- self.assertEquals(repos, '')
-
- time = configValidator.get('metadata', 'created')
- self.assertTrue(time != None)
-
- def test_write_host_check_report(self):
- tmpfile = tempfile.mktemp()
-
- config = ConfigParser.RawConfigParser()
- config.add_section('agent')
- config.set('agent', 'prefix', os.path.dirname(tmpfile))
-
- handler = HostCheckReportFileHandler(config)
-
- dict = {}
- dict['hostHealth'] = {}
- dict['existingUsers'] = [{'name':'user1', 'homeDir':'/var/log', 'status':'Exists'}]
- dict['alternatives'] = [
- {'name':'/etc/alternatives/hadoop-conf', 'target':'/etc/hadoop/conf.dist'},
- {'name':'/etc/alternatives/hbase-conf', 'target':'/etc/hbase/conf.1'}
- ]
- dict['stackFoldersAndFiles'] = [{'name':'/a/b', 'type':'directory'},{'name':'/a/b.txt', 'type':'file'}]
- dict['hostHealth']['activeJavaProcs'] = [
- {'pid':355,'hadoop':True,'command':'some command','user':'root'},
- {'pid':455,'hadoop':True,'command':'some command','user':'hdfs'}
- ]
- dict['installedPackages'] = [
- {'name':'hadoop','version':'3.2.3','repoName':'HDP'},
- {'name':'hadoop-lib','version':'3.2.3','repoName':'HDP'}
- ]
- dict['existingRepos'] = ['HDP', 'HDP-epel']
- handler.writeHostCheckFile(dict)
-
- configValidator = ConfigParser.RawConfigParser()
- configPath = os.path.join(os.path.dirname(tmpfile), HostCheckReportFileHandler.HOST_CHECK_FILE)
- configValidator.read(configPath)
- users = configValidator.get('users', 'usr_list')
- homedirs = configValidator.get('users', 'usr_homedir_list')
- self.assertEquals(users, 'user1')
- self.assertEquals(homedirs, '/var/log')
-
- names = configValidator.get('alternatives', 'symlink_list')
- targets = configValidator.get('alternatives', 'target_list')
- self.chkItemsEqual(names, ['/etc/alternatives/hadoop-conf', '/etc/alternatives/hbase-conf'])
- self.chkItemsEqual(targets, ['/etc/hadoop/conf.dist','/etc/hbase/conf.1'])
-
- paths = configValidator.get('directories', 'dir_list')
- self.chkItemsEqual(paths, ['/a/b','/a/b.txt'])
-
- procs = configValidator.get('processes', 'proc_list')
- self.chkItemsEqual(procs, ['455', '355'])
-
- pkgs = configValidator.get('packages', 'pkg_list')
- self.chkItemsEqual(pkgs, ['hadoop', 'hadoop-lib'])
-
- repos = configValidator.get('repositories', 'repo_list')
- self.chkItemsEqual(repos, ['HDP', 'HDP-epel'])
-
- time = configValidator.get('metadata', 'created')
- self.assertTrue(time != None)
-
- def chkItemsEqual(self, commaDelimited, items):
- items1 = commaDelimited.split(',')
- items1.sort()
- items.sort()
- items1Str = ','.join(items1)
- items2Str = ','.join(items)
- self.assertEquals(items1Str, items2Str)
-
-if __name__ == "__main__":
- unittest.main(verbosity=2)
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e5c6e113/ambari-agent/src/test/python/TestHostCleanup.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/TestHostCleanup.py b/ambari-agent/src/test/python/TestHostCleanup.py
deleted file mode 100644
index 3534195..0000000
--- a/ambari-agent/src/test/python/TestHostCleanup.py
+++ /dev/null
@@ -1,429 +0,0 @@
-#!/usr/bin/env python2.6
-
-'''
-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.
-'''
-
-from unittest import TestCase
-import unittest
-from mock.mock import patch, MagicMock, call, create_autospec
-from ambari_agent import HostCleanup
-import StringIO
-import sys
-import tempfile
-import os.path
-import optparse
-import logging
-
-PACKAGE_SECTION = "packages"
-PACKAGE_KEY = "pkg_list"
-USER_SECTION = "users"
-USER_KEY = "usr_list"
-REPO_SECTION = "repositories"
-REPOS_KEY = "pkg_list"
-DIR_SECTION = "directories"
-DIR_KEY = "dir_list"
-PROCESS_SECTION = "processes"
-PROCESS_KEY = "proc_list"
-ALT_SECTION = "alternatives"
-ALT_KEYS = ["symlink_list", "target_list"]
-ALT_ERASE_CMD = "alternatives --remove {0} {1}"
-USER_HOMEDIR_SECTION = "usr_homedir"
-
-class TestHostCleanup(TestCase):
-
- def setUp(self):
- HostCleanup.logger = MagicMock()
- self.hostcleanup = HostCleanup.HostCleanup()
- # disable stdout
- out = StringIO.StringIO()
- sys.stdout = out
-
-
- def tearDown(self):
- # enable stdout
- sys.stdout = sys.__stdout__
-
- def test_read_host_check_file_with_content(self):
- out = StringIO.StringIO()
- sys.stdout = out
- tmpfile = tempfile.mktemp()
- f = open(tmpfile,'w')
- fileContent = """[processes]
-proc_list = 323,434
-
-[users]
-usr_list = rrdcached,ambari-qa,hive,oozie,hbase,hcat,mysql,mapred,hdfs,zookeeper,sqoop,nagios
-
-[repositories]
-repo_list = HDP-1.3.0,HDP-epel
-
-[directories]
-dir_list = /etc/hadoop,/etc/hbase,/etc/hcatalog,/tmp/hive,/tmp/nagios,/var/nagios
-
-[alternatives]
-symlink_list = hcatalog-conf,hadoop-default,hadoop-log,oozie-conf
-target_list = /etc/hcatalog/conf.dist,/usr/share/man/man1/hadoop.1.gz,/etc/oozie/conf.dist,/usr/lib/hadoop
-
-[packages]
-pkg_list = sqoop.noarch,hadoop-libhdfs.x86_64,rrdtool.x86_64,ganglia-gmond.x86_64
-
-[metadata]
-created = 2013-07-02 20:39:22.162757"""
- f.write(fileContent)
- f.close()
-
- propMap = self.hostcleanup.read_host_check_file(tmpfile)
- self.assertTrue("323" in propMap["processes"])
- self.assertTrue("mysql" in propMap["users"])
- self.assertTrue("HDP-epel" in propMap["repositories"])
- self.assertTrue("/etc/hadoop" in propMap["directories"])
- self.assertTrue("hcatalog-conf" in propMap["alternatives"]["symlink_list"])
- self.assertTrue("/etc/oozie/conf.dist" in propMap["alternatives"]["target_list"])
- self.assertTrue("hadoop-libhdfs.x86_64" in propMap["packages"])
- sys.stdout = sys.__stdout__
-
- class HostCleanupOptions:
- def __init__(self, outputfile, inputfile, skip, verbose, silent):
- self.outputfile = outputfile
- self.inputfile = inputfile
- self.skip = skip
- self.verbose = verbose
- self.silent = silent
-
- @patch.object(HostCleanup, 'get_YN_input')
- @patch.object(HostCleanup.HostCleanup, 'do_cleanup')
- @patch.object(HostCleanup.HostCleanup, 'is_current_user_root')
- @patch.object(logging.FileHandler, 'setFormatter')
- @patch.object(HostCleanup.HostCleanup,'read_host_check_file')
- @patch.object(logging,'basicConfig')
- @patch.object(logging, 'FileHandler')
- @patch.object(optparse.OptionParser, 'parse_args')
- def test_options(self, parser_mock, file_handler_mock, logging_mock, read_host_check_file_mock,
- set_formatter_mock, user_root_mock, do_cleanup_mock, get_yn_input_mock):
- parser_mock.return_value = (TestHostCleanup.HostCleanupOptions('/someoutputfile', '/someinputfile', '', False,
- False), [])
- file_handler_mock.return_value = logging.FileHandler('') # disable creating real file
- user_root_mock.return_value = True
- get_yn_input_mock.return_value = True
- HostCleanup.main()
-
- # test --out
- file_handler_mock.assert_called_with('/someoutputfile')
- # test --skip
- self.assertEquals([''],HostCleanup.SKIP_LIST)
- #test --verbose
- logging_mock.assert_called_with(level=logging.INFO)
- # test --in
- read_host_check_file_mock.assert_called_with('/someinputfile')
- self.assertTrue(get_yn_input_mock.called)
-
-
- @patch.object(HostCleanup, 'get_YN_input')
- @patch.object(HostCleanup.HostCleanup, 'do_cleanup')
- @patch.object(HostCleanup.HostCleanup, 'is_current_user_root')
- @patch.object(logging.FileHandler, 'setFormatter')
- @patch.object(HostCleanup.HostCleanup,'read_host_check_file')
- @patch.object(logging,'basicConfig')
- @patch.object(logging, 'FileHandler')
- @patch.object(optparse.OptionParser, 'parse_args')
- def test_options_silent(self, parser_mock, file_handler_mock, logging_mock, read_host_check_file_mock,
- set_formatter_mock, user_root_mock, do_cleanup_mock, get_yn_input_mock):
- parser_mock.return_value = (TestHostCleanup.HostCleanupOptions('/someoutputfile', '/someinputfile', '', False,
- True), [])
- file_handler_mock.return_value = logging.FileHandler('') # disable creating real file
- user_root_mock.return_value = True
- get_yn_input_mock.return_value = True
- HostCleanup.main()
-
- # test --out
- file_handler_mock.assert_called_with('/someoutputfile')
- # test --skip
- self.assertEquals([''],HostCleanup.SKIP_LIST)
- #test --verbose
- logging_mock.assert_called_with(level=logging.INFO)
- # test --in
- read_host_check_file_mock.assert_called_with('/someinputfile')
- self.assertFalse(get_yn_input_mock.called)
-
- @patch.object(HostCleanup.HostCleanup, 'do_erase_alternatives')
- @patch.object(HostCleanup.HostCleanup, 'find_repo_files_for_repos')
- @patch.object(HostCleanup.HostCleanup, 'get_os_type')
- @patch.object(HostCleanup.HostCleanup, 'do_kill_processes')
- @patch.object(HostCleanup.HostCleanup, 'do_erase_files_silent')
- @patch.object(HostCleanup.HostCleanup, 'do_erase_dir_silent')
- @patch.object(HostCleanup.HostCleanup, 'do_delete_users')
- @patch.object(HostCleanup.HostCleanup, 'do_erase_packages')
- def test_do_cleanup_all(self, do_erase_packages_method, do_delete_users_method,
- do_erase_dir_silent_method,
- do_erase_files_silent_method, do_kill_processes_method,
- get_os_type_method, find_repo_files_for_repos_method,
- do_erase_alternatives_method):
- out = StringIO.StringIO()
- sys.stdout = out
- propertyMap = {PACKAGE_SECTION:['abcd', 'pqrst'], USER_SECTION:['abcd', 'pqrst'],
- REPO_SECTION:['abcd', 'pqrst'], DIR_SECTION:['abcd', 'pqrst'],
- PROCESS_SECTION:['abcd', 'pqrst'],
- ALT_SECTION:{ALT_KEYS[0]:['alt1','alt2'], ALT_KEYS[1]:[
- 'dir1']}, USER_HOMEDIR_SECTION:['decf']}
- get_os_type_method.return_value = 'redhat'
- find_repo_files_for_repos_method.return_value = ['abcd', 'pqrst']
-
- self.hostcleanup.do_cleanup(propertyMap)
-
- self.assertTrue(do_delete_users_method.called)
- self.assertTrue(do_erase_dir_silent_method.called)
- self.assertTrue(do_erase_files_silent_method.called)
- self.assertTrue(do_erase_packages_method.called)
- self.assertTrue(do_kill_processes_method.called)
- self.assertTrue(do_erase_alternatives_method.called)
- calls = [call(['decf']), call(['abcd', 'pqrst'])]
- do_erase_dir_silent_method.assert_has_calls(calls)
- do_erase_packages_method.assert_called_once_with(['abcd', 'pqrst'])
- do_erase_files_silent_method.assert_called_once_with(['abcd', 'pqrst'])
- do_delete_users_method.assert_called_once_with(['abcd', 'pqrst'])
- do_kill_processes_method.assert_called_once_with(['abcd', 'pqrst'])
- do_erase_alternatives_method.assert_called_once_with({ALT_KEYS[0]:['alt1',
- 'alt2'], ALT_KEYS[1]:['dir1']})
-
- sys.stdout = sys.__stdout__
-
-
- @patch.object(HostCleanup.HostCleanup, 'do_delete_by_owner')
- @patch.object(HostCleanup.HostCleanup, 'get_user_ids')
- @patch.object(HostCleanup.HostCleanup, 'do_erase_alternatives')
- @patch.object(HostCleanup.HostCleanup, 'find_repo_files_for_repos')
- @patch.object(HostCleanup.HostCleanup, 'get_os_type')
- @patch.object(HostCleanup.HostCleanup, 'do_kill_processes')
- @patch.object(HostCleanup.HostCleanup, 'do_erase_files_silent')
- @patch.object(HostCleanup.HostCleanup, 'do_erase_dir_silent')
- @patch.object(HostCleanup.HostCleanup, 'do_delete_users')
- @patch.object(HostCleanup.HostCleanup, 'do_erase_packages')
- def test_do_cleanup_default(self, do_erase_packages_method, do_delete_users_method,
- do_erase_dir_silent_method,
- do_erase_files_silent_method, do_kill_processes_method,
- get_os_type_method, find_repo_files_for_repos_method,
- do_erase_alternatives_method, get_user_ids_method,
- do_delete_by_owner_method):
-
- global SKIP_LIST
- oldSkipList = HostCleanup.SKIP_LIST
- HostCleanup.SKIP_LIST = ["users"]
- out = StringIO.StringIO()
- sys.stdout = out
- propertyMap = {PACKAGE_SECTION:['abcd', 'pqrst'], USER_SECTION:['abcd', 'pqrst'],
- REPO_SECTION:['abcd', 'pqrst'], DIR_SECTION:['abcd', 'pqrst'],
- PROCESS_SECTION:['abcd', 'pqrst'],
- ALT_SECTION:{ALT_KEYS[0]:['alt1','alt2'], ALT_KEYS[1]:[
- 'dir1']}}
- get_os_type_method.return_value = 'redhat'
- find_repo_files_for_repos_method.return_value = ['abcd', 'pqrst']
-
- self.hostcleanup.do_cleanup(propertyMap)
-
- self.assertFalse(do_delete_by_owner_method.called)
- self.assertFalse(get_user_ids_method.called)
- self.assertFalse(do_delete_users_method.called)
- self.assertTrue(do_erase_dir_silent_method.called)
- self.assertTrue(do_erase_files_silent_method.called)
- self.assertTrue(do_erase_packages_method.called)
- self.assertTrue(do_kill_processes_method.called)
- self.assertTrue(do_erase_alternatives_method.called)
- HostCleanup.SKIP_LIST = oldSkipList
- sys.stdout = sys.__stdout__
-
- @patch.object(HostCleanup.HostCleanup, 'find_repo_files_for_repos')
- @patch.object(HostCleanup.HostCleanup, 'get_os_type')
- @patch.object(HostCleanup.HostCleanup, 'do_kill_processes')
- @patch.object(HostCleanup.HostCleanup, 'do_erase_files_silent')
- @patch.object(HostCleanup.HostCleanup, 'do_erase_dir_silent')
- @patch.object(HostCleanup.HostCleanup, 'do_delete_users')
- @patch.object(HostCleanup.HostCleanup, 'do_erase_packages')
- def test_do_cleanup_with_skip(self, do_erase_packages_method,
- do_delete_users_method,
- do_erase_dir_silent_method,
- do_erase_files_silent_method, do_kill_processes_method,
- get_os_type_method, find_repo_files_for_repos_method):
-
- out = StringIO.StringIO()
- sys.stdout = out
- propertyMap = {PACKAGE_SECTION:['abcd', 'pqrst'], USER_SECTION:['abcd', 'pqrst'],
- REPO_SECTION:['abcd', 'pqrst'], DIR_SECTION:['abcd', 'pqrst'],
- PROCESS_SECTION:['abcd', 'pqrst']}
- get_os_type_method.return_value = 'redhat'
- find_repo_files_for_repos_method.return_value = ['abcd', 'pqrst']
- HostCleanup.SKIP_LIST = [PACKAGE_SECTION, REPO_SECTION]
-
- self.hostcleanup.do_cleanup(propertyMap)
-
- self.assertTrue(do_delete_users_method.called)
- self.assertTrue(do_erase_dir_silent_method.called)
- self.assertFalse(do_erase_files_silent_method.called)
- self.assertFalse(do_erase_packages_method.called)
- self.assertTrue(do_kill_processes_method.called)
- calls = [call(None), call(['abcd', 'pqrst'])]
- do_erase_dir_silent_method.assert_has_calls(calls)
- do_delete_users_method.assert_called_once_with(['abcd', 'pqrst'])
- do_kill_processes_method.assert_called_once_with(['abcd', 'pqrst'])
-
- sys.stdout = sys.__stdout__
-
- @patch.object(HostCleanup.HostCleanup, 'do_erase_dir_silent')
- @patch("os.stat")
- @patch("os.path.join")
- @patch("os.listdir")
- def test_do_delete_by_owner(self, listdir_mock, join_mock, stat_mock, do_erase_dir_silent_method):
- listdir_mock.return_value = ["k", "j"]
- join_mock.return_value = "path"
- response = MagicMock()
- response.st_uid = 1
- stat_mock.return_value = response
- self.hostcleanup.do_delete_by_owner([1, 2], ["a"])
- self.assertTrue(do_erase_dir_silent_method.called)
- calls = [call(["path"]), call(["path"])]
- do_erase_dir_silent_method.assert_has_calls(calls)
-
- @patch.object(HostCleanup.HostCleanup, 'run_os_command')
- def test_do_delete_users(self, run_os_command_mock):
- run_os_command_mock.return_value = (1, "", "")
- self.hostcleanup.do_delete_users(["a", "b"])
- self.assertTrue(run_os_command_mock.called)
- calls = [call('userdel -rf a'), call('userdel -rf b'), call('groupdel hadoop')]
- run_os_command_mock.assert_has_calls(calls)
-
- @patch("ConfigParser.RawConfigParser")
- @patch("__builtin__.open")
- def test_read_host_check_file(self, openMock, readMock):
- out = StringIO.StringIO()
- sys.stdout = out
- f = MagicMock()
- openMock.return_value = f
-
- propertyMap = self.hostcleanup.read_host_check_file('test')
-
- self.assertTrue(openMock.called)
- self.assertTrue(readMock.called)
- self.assertTrue(propertyMap.has_key(PACKAGE_SECTION))
- self.assertTrue(propertyMap.has_key(REPO_SECTION))
- self.assertTrue(propertyMap.has_key(USER_SECTION))
- self.assertTrue(propertyMap.has_key(DIR_SECTION))
- self.assertTrue(propertyMap.has_key(PROCESS_SECTION))
-
- sys.stdout = sys.__stdout__
-
-
- @patch.object(HostCleanup.HostCleanup, 'run_os_command')
- @patch.object(HostCleanup.HostCleanup, 'get_os_type')
- def test_do_earse_packages(self, get_os_type_method, run_os_command_method):
- out = StringIO.StringIO()
- sys.stdout = out
-
- get_os_type_method.return_value = 'redhat'
- run_os_command_method.return_value = (0, 'success', 'success')
-
- retval = self.hostcleanup.do_erase_packages(['abcd', 'wxyz'])
-
- self.assertTrue(get_os_type_method.called)
- self.assertTrue(run_os_command_method.called)
- run_os_command_method.assert_called_with("yum erase -y {0}".format(' '
- .join(['abcd', 'wxyz'])))
- self.assertEquals(0, retval)
-
- get_os_type_method.reset()
- run_os_command_method.reset()
-
- get_os_type_method.return_value = 'suse'
- run_os_command_method.return_value = (0, 'success', 'success')
-
- retval = self.hostcleanup.do_erase_packages(['abcd', 'wxyz'])
-
- self.assertTrue(get_os_type_method.called)
- self.assertTrue(run_os_command_method.called)
- run_os_command_method.assert_called_with("zypper -n -q remove {0}"
- .format(' '.join(['abcd', 'wxyz'])))
- self.assertEquals(0, retval)
-
- sys.stdout = sys.__stdout__
-
- @patch.object(HostCleanup.HostCleanup, 'get_files_in_dir')
- @patch.object(HostCleanup.HostCleanup, 'get_os_type')
- def test_find_repo_files_for_repos(self, get_os_type_method,
- get_files_in_dir_method):
- out = StringIO.StringIO()
- sys.stdout = out
-
- tmpfile = tempfile.mktemp()
- fileContent = """[###]
-[aass]
-[$$]
-444]saas[333
-1122[naas]2222
-name=sd des derft 3.1
-"""
- with open(tmpfile,'w') as file:
- file.write(fileContent)
- get_os_type_method.return_value = 'redhat'
- get_files_in_dir_method.return_value = [ tmpfile ]
-
- repoFiles = self.hostcleanup.find_repo_files_for_repos(['aass'])
- self.assertTrue(get_files_in_dir_method.called)
- self.assertTrue(get_os_type_method.called)
- self.assertEquals(repoFiles, [ tmpfile ])
-
- repoFiles = self.hostcleanup.find_repo_files_for_repos(['sd des derft 3.1'])
- self.assertTrue(get_files_in_dir_method.called)
- self.assertTrue(get_os_type_method.called)
- self.assertEquals(repoFiles, [ tmpfile ])
-
- repoFiles = self.hostcleanup.find_repo_files_for_repos(['sd des derft 3.1', 'aass'])
- self.assertEquals(repoFiles, [ tmpfile ])
-
- repoFiles = self.hostcleanup.find_repo_files_for_repos(['saas'])
- self.assertEquals(repoFiles, [])
-
- repoFiles = self.hostcleanup.find_repo_files_for_repos([''])
- self.assertEquals(repoFiles, [])
-
- sys.stdout = sys.__stdout__
-
-
- @patch.object(HostCleanup.HostCleanup, 'run_os_command')
- @patch.object(HostCleanup.HostCleanup, 'do_erase_dir_silent')
- @patch.object(HostCleanup.HostCleanup, 'get_alternatives_desc')
- def test_do_erase_alternatives(self, get_alternatives_desc_mock,
- do_erase_dir_silent_mock, run_os_command_mock):
- out = StringIO.StringIO()
- sys.stdout = out
-
- get_alternatives_desc_mock.return_value = 'somepath to alternative\n'
- run_os_command_mock.return_value = (0, None, None)
-
- alt_map = {ALT_KEYS[0]:['alt1'], ALT_KEYS[1]:['dir1']}
-
- self.hostcleanup.do_erase_alternatives(alt_map)
-
- self.assertTrue(get_alternatives_desc_mock.called)
- get_alternatives_desc_mock.called_once_with('alt1')
- self.assertTrue(run_os_command_mock.called)
- run_os_command_mock.called_once_with(ALT_ERASE_CMD.format('alt1', 'somepath'))
- self.assertTrue(do_erase_dir_silent_mock.called)
- do_erase_dir_silent_mock.called_once_with(['dir1'])
-
- sys.stdout = sys.__stdout__
-
-if __name__ == "__main__":
- unittest.main()
http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e5c6e113/ambari-agent/src/test/python/TestHostInfo.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/TestHostInfo.py b/ambari-agent/src/test/python/TestHostInfo.py
deleted file mode 100644
index 95712f0..0000000
--- a/ambari-agent/src/test/python/TestHostInfo.py
+++ /dev/null
@@ -1,525 +0,0 @@
-#!/usr/bin/env python2.6
-
-'''
-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.
-'''
-
-from unittest import TestCase
-import logging
-import unittest
-import subprocess
-from mock.mock import patch
-from mock.mock import MagicMock
-from mock.mock import create_autospec
-from ambari_agent.HostCheckReportFileHandler import HostCheckReportFileHandler
-from ambari_agent.PackagesAnalyzer import PackagesAnalyzer
-from ambari_agent.HostInfo import HostInfo
-from ambari_agent.Hardware import Hardware
-from ambari_agent.AmbariConfig import AmbariConfig
-
-class TestHostInfo(TestCase):
-
- logger = logging.getLogger()
-
- @patch.object(PackagesAnalyzer, 'hasZypper')
- @patch.object(PackagesAnalyzer, 'subprocessWithTimeout')
- def test_analyze_zypper_out(self, spwt_mock, hasZy_mock):
- packageAnalyzer = PackagesAnalyzer()
- stringToRead = """Refreshing service 'susecloud'.
- Loading repository data...
- Reading installed packages...
-
- S | Name | Type | Version | Arch | Repository
- --+-----------------------------------+---------+------------------------+--------+----------------------
- i | ConsoleKit | package | 0.2.10-64.65.1 | x86_64 | SLES11-SP1-Updates
- i | gweb | package | 2.2.0-99 | noarch | Hortonworks Data Platform Utils Version - HDP-UTILS-1.1.0.15
- i | hadoop | package | 1.2.0.1.3.0.0-107 | x86_64 | HDP
- i | hadoop-libhdfs | package | 1.2.0.1.3.0.0-107 | x86_64 | HDP
- i | ambari-server | package | 1.2.4.9-1 | noarch | Ambari 1.x
- i | hdp_mon_ganglia_addons | package | 1.2.4.9-1 | noarch | Ambari 1.x
- i | Minimal | pattern | 11-38.13.9 | x86_64 | SLES11-SP1"""
- result = {}
- result['out'] = stringToRead
- result['err'] = ""
- result['retCode'] = 0
-
- spwt_mock.return_value = result
- hasZy_mock.return_value = True
- installedPackages = []
- packageAnalyzer.allInstalledPackages(installedPackages)
- self.assertEqual(7, len(installedPackages))
- self.assertTrue(installedPackages[1][0], "gweb")
- self.assertTrue(installedPackages[3][2], "HDP")
- self.assertTrue(installedPackages[6][1], "11-38.13.9")
-
- def test_getReposToRemove(self):
- l1 = ["Hortonworks Data Platform Utils Version - HDP-UTILS-1.1.0.15", "Ambari 1.x", "HDP"]
- l2 = ["Ambari", "HDP-UTIL"]
- hostInfo = HostInfo()
- l3 = hostInfo.getReposToRemove(l1, l2)
- self.assertTrue(1, len(l3))
- self.assertEqual(l3[0], "HDP")
-
- l1 = ["AMBARI.dev-1.x", "HDP-1.3.0"]
- l3 = hostInfo.getReposToRemove(l1, l2)
- self.assertTrue(1, len(l3))
- self.assertEqual(l3[0], "HDP-1.3.0")
-
- def test_perform_package_analysis(self):
- packageAnalyzer = PackagesAnalyzer()
- installedPackages = [
- ["hadoop-a", "2.3", "HDP"], ["zk", "3.1", "HDP"], ["webhcat", "3.1", "HDP"],
- ["hadoop-b", "2.3", "HDP-epel"], ["epel", "3.1", "HDP-epel"], ["epel-2", "3.1", "HDP-epel"],
- ["hadoop-c", "2.3", "Ambari"], ["ambari-s", "3.1", "Ambari"],
- ["nagios", "2.3", "NAGIOS"], ["rrd", "3.1", "RRD"],
- ["keeper-1", "2.3", "NAGIOS"], ["keeper-2", "3.1", "base"],["def-def.x86", "2.2", "DEF.3"],
- ["def.1", "1.2", "NewDEF"]
- ]
- availablePackages = [
- ["hadoop-d", "2.3", "HDP"], ["zk-2", "3.1", "HDP"], ["pig", "3.1", "HDP"],
- ["epel-3", "2.3", "HDP-epel"], ["hadoop-e", "3.1", "HDP-epel"],
- ["ambari-a", "3.1", "Ambari"],
- ["keeper-3", "3.1", "base"]
- ]
-
- packagesToLook = ["webhcat", "hadoop", "*-def"]
- reposToIgnore = ["ambari"]
- additionalPackages = ["nagios", "rrd"]
-
- repos = []
- packageAnalyzer.getInstalledRepos(packagesToLook, installedPackages + availablePackages, reposToIgnore, repos)
- self.assertEqual(3, len(repos))
- expected = ["HDP", "HDP-epel", "DEF.3"]
- for repo in expected:
- self.assertTrue(repo in repos)
-
- packagesInstalled = packageAnalyzer.getInstalledPkgsByRepo(repos, ["epel"], installedPackages)
- self.assertEqual(5, len(packagesInstalled))
- expected = ["hadoop-a", "zk", "webhcat", "hadoop-b", "def-def.x86"]
- for repo in expected:
- self.assertTrue(repo in packagesInstalled)
-
- additionalPkgsInstalled = packageAnalyzer.getInstalledPkgsByNames(
- additionalPackages, installedPackages)
- self.assertEqual(2, len(additionalPkgsInstalled))
- expected = ["nagios", "rrd"]
- for additionalPkg in expected:
- self.assertTrue(additionalPkg in additionalPkgsInstalled)
-
- allPackages = list(set(packagesInstalled + additionalPkgsInstalled))
- self.assertEqual(7, len(allPackages))
- expected = ["hadoop-a", "zk", "webhcat", "hadoop-b", "nagios", "rrd", "def-def.x86"]
- for package in expected:
- self.assertTrue(package in allPackages)
-
- @patch.object(PackagesAnalyzer, 'hasZypper')
- @patch.object(PackagesAnalyzer, 'subprocessWithTimeout')
- def test_analyze_yum_output(self, subprocessWithTimeout_mock, hasZy_mock):
- packageAnalyzer = PackagesAnalyzer()
- stringToRead = """Loaded plugins: amazon-id, product-id, rhui-lb, security, subscription-manager
- Updating certificate-based repositories.
- Installed Packages
- AMBARI.dev.noarch 1.x-1.el6 installed
- PyXML.x86_64 0.8.4-19.el6 @koji-override-0
- Red_Hat_Enterprise_Linux-Release_Notes-6-en-US.noarch
- 3-7.el6 @koji-override-0
- hcatalog.noarch 0.11.0.1.3.0.0-107.el6
- @HDP-1.3.0
- hesiod.x86_64 3.1.0-19.el6 @koji-override-0/$releasever
- hive.noarch 0.11.0.1.3.0.0-107.el6
- @HDP-1.3.0
- oracle-server-db.x86 1.3.17-2
- @Oracle-11g
- ambari-log4j.noarch 1.2.5.9-1 @AMBARI.dev-1.x
- libconfuse.x86_64 2.7-4.el6 @HDP-epel"""
- result = {}
- result['out'] = stringToRead
- result['err'] = ""
- result['retCode'] = 0
-
- subprocessWithTimeout_mock.return_value = result
- hasZy_mock.return_value = False
- installedPackages = []
- packageAnalyzer.allInstalledPackages(installedPackages)
- self.assertEqual(9, len(installedPackages))
- for package in installedPackages:
- self.assertTrue(package[0] in ["AMBARI.dev.noarch", "PyXML.x86_64", "oracle-server-db.x86",
- "Red_Hat_Enterprise_Linux-Release_Notes-6-en-US.noarch",
- "hcatalog.noarch", "hesiod.x86_64", "hive.noarch", "ambari-log4j.noarch", "libconfuse.x86_64"])
- self.assertTrue(package[1] in ["1.x-1.el6", "0.8.4-19.el6", "3-7.el6", "3.1.0-19.el6",
- "0.11.0.1.3.0.0-107.el6", "1.2.5.9-1", "1.3.17-2", "1.2.5.9-1", "2.7-4.el6"])
- self.assertTrue(package[2] in ["installed", "koji-override-0", "HDP-1.3.0",
- "koji-override-0/$releasever", "AMBARI.dev-1.x", "Oracle-11g", "HDP-epel"])
-
- packages = packageAnalyzer.getInstalledPkgsByNames(["AMBARI", "Red_Hat_Enterprise", "hesiod", "hive"],
- installedPackages)
- self.assertEqual(4, len(packages))
- expected = ["AMBARI.dev.noarch", "Red_Hat_Enterprise_Linux-Release_Notes-6-en-US.noarch",
- "hesiod.x86_64", "hive.noarch"]
- for package in expected:
- self.assertTrue(package in packages)
-
- detailedPackages = packageAnalyzer.getPackageDetails(installedPackages, packages)
- self.assertEqual(4, len(detailedPackages))
- for package in detailedPackages:
- self.assertTrue(package['version'] in ["1.x-1.el6", "3-7.el6", "3.1.0-19.el6",
- "0.11.0.1.3.0.0-107.el6"])
- self.assertTrue(package['repoName'] in ["installed", "koji-override-0", "HDP-1.3.0",
- "koji-override-0/$releasever"])
- self.assertFalse(package['repoName'] in ["AMBARI.dev-1.x"])
-
- @patch.object(PackagesAnalyzer, 'subprocessWithTimeout')
- def test_analyze_yum_output_err(self, subprocessWithTimeout_mock):
- packageAnalyzer = PackagesAnalyzer()
-
- result = {}
- result['out'] = ""
- result['err'] = ""
- result['retCode'] = 1
-
- subprocessWithTimeout_mock.return_value = result
- installedPackages = []
- packageAnalyzer.allInstalledPackages(installedPackages)
- self.assertEqual(installedPackages, [])
-
-
- @patch('os.path.exists')
- def test_checkFolders(self, path_mock):
- path_mock.return_value = True
- hostInfo = HostInfo()
- results = []
- existingUsers = [{'name':'a1', 'homeDir':'/home/a1'}, {'name':'b1', 'homeDir':'/home/b1'}]
- hostInfo.checkFolders(["/etc/conf", "/var/lib", "/home/"], ["a1", "b1"], existingUsers, results)
- self.assertEqual(4, len(results))
- names = [i['name'] for i in results]
- for item in ['/etc/conf/a1', '/var/lib/a1', '/etc/conf/b1', '/var/lib/b1']:
- self.assertTrue(item in names)
-
- @patch('os.path.exists')
- @patch('__builtin__.open')
- def test_checkUsers(self, builtins_open_mock, path_mock):
- builtins_open_mock.return_value = [
- "hdfs:x:493:502:Hadoop HDFS:/usr/lib/hadoop:/bin/bash",
- "zookeeper:x:492:502:ZooKeeper:/var/run/zookeeper:/bin/bash"]
- path_mock.side_effect = [True, False]
-
- hostInfo = HostInfo()
- results = []
- hostInfo.checkUsers(["zookeeper", "hdfs"], results)
- self.assertEqual(2, len(results))
- newlist = sorted(results, key=lambda k: k['name'])
- self.assertTrue(newlist[0]['name'], "hdfs")
- self.assertTrue(newlist[1]['name'], "zookeeper")
- self.assertTrue(newlist[0]['homeDir'], "/usr/lib/hadoop")
- self.assertTrue(newlist[1]['homeDir'], "/var/run/zookeeper")
- self.assertTrue(newlist[0]['status'], "Available")
- self.assertTrue(newlist[1]['status'], "Invalid home directory")
-
-
- @patch.object(HostInfo, 'get_os_type')
- @patch('os.umask')
- @patch.object(HostCheckReportFileHandler, 'writeHostCheckFile')
- @patch.object(PackagesAnalyzer, 'allAvailablePackages')
- @patch.object(PackagesAnalyzer, 'allInstalledPackages')
- @patch.object(PackagesAnalyzer, 'getPackageDetails')
- @patch.object(PackagesAnalyzer, 'getInstalledPkgsByNames')
- @patch.object(PackagesAnalyzer, 'getInstalledPkgsByRepo')
- @patch.object(PackagesAnalyzer, 'getInstalledRepos')
- @patch.object(HostInfo, 'checkUsers')
- @patch.object(HostInfo, 'checkLiveServices')
- @patch.object(HostInfo, 'javaProcs')
- @patch.object(HostInfo, 'checkFolders')
- @patch.object(HostInfo, 'etcAlternativesConf')
- @patch.object(HostInfo, 'hadoopVarRunCount')
- @patch.object(HostInfo, 'hadoopVarLogCount')
- def test_hostinfo_register_suse(self, hvlc_mock, hvrc_mock, eac_mock, cf_mock, jp_mock,
- cls_mock, cu_mock, gir_mock, gipbr_mock, gipbn_mock,
- gpd_mock, aip_mock, aap_mock, whcf_mock, os_umask_mock, get_os_type_mock):
- hvlc_mock.return_value = 1
- hvrc_mock.return_value = 1
- gipbr_mock.return_value = ["pkg1"]
- gipbn_mock.return_value = ["pkg2"]
- gpd_mock.return_value = ["pkg1", "pkg2"]
- get_os_type_mock.return_value = "suse"
-
- hostInfo = HostInfo()
- dict = {}
- hostInfo.register(dict, False, False)
- self.assertFalse(gir_mock.called)
- self.assertFalse(gpd_mock.called)
- self.assertFalse(aip_mock.called)
- self.assertFalse(aap_mock.called)
- self.assertTrue(os_umask_mock.called)
- self.assertFalse(whcf_mock.called)
-
- self.assertTrue(0 == len(dict['installedPackages']))
- self.assertTrue('agentTimeStampAtReporting' in dict['hostHealth'])
-
-
- @patch.object(HostInfo, 'get_os_type')
- @patch('os.umask')
- @patch.object(HostCheckReportFileHandler, 'writeHostCheckFile')
- @patch.object(PackagesAnalyzer, 'allAvailablePackages')
- @patch.object(PackagesAnalyzer, 'allInstalledPackages')
- @patch.object(PackagesAnalyzer, 'getPackageDetails')
- @patch.object(PackagesAnalyzer, 'getInstalledPkgsByNames')
- @patch.object(PackagesAnalyzer, 'getInstalledPkgsByRepo')
- @patch.object(PackagesAnalyzer, 'getInstalledRepos')
- @patch.object(HostInfo, 'checkUsers')
- @patch.object(HostInfo, 'checkLiveServices')
- @patch.object(HostInfo, 'javaProcs')
- @patch.object(HostInfo, 'checkFolders')
- @patch.object(HostInfo, 'etcAlternativesConf')
- @patch.object(HostInfo, 'hadoopVarRunCount')
- @patch.object(HostInfo, 'hadoopVarLogCount')
- @patch.object(HostInfo, 'checkIptables')
- def test_hostinfo_register(self, cit_mock, hvlc_mock, hvrc_mock, eac_mock, cf_mock, jp_mock,
- cls_mock, cu_mock, gir_mock, gipbr_mock, gipbn_mock,
- gpd_mock, aip_mock, aap_mock, whcf_mock, os_umask_mock, get_os_type_mock):
- cit_mock.return_value = True
- hvlc_mock.return_value = 1
- hvrc_mock.return_value = 1
- gipbr_mock.return_value = ["pkg1"]
- gipbn_mock.return_value = ["pkg2"]
- gpd_mock.return_value = ["pkg1", "pkg2"]
- get_os_type_mock.return_value = "redhat"
-
- hostInfo = HostInfo()
- dict = {}
- hostInfo.register(dict, True, True)
- self.verifyReturnedValues(dict)
-
- hostInfo.register(dict, True, False)
- self.verifyReturnedValues(dict)
-
- hostInfo.register(dict, False, True)
- self.verifyReturnedValues(dict)
- self.assertTrue(os_umask_mock.call_count == 2)
-
- hostInfo = HostInfo()
- dict = {}
- hostInfo.register(dict, False, False)
- self.assertTrue(gir_mock.called)
- self.assertTrue(gpd_mock.called)
- self.assertTrue(aip_mock.called)
- self.assertTrue(cit_mock.called)
-
- for existingPkg in ["pkg1", "pkg2"]:
- self.assertTrue(existingPkg in dict['installedPackages'])
- args, kwargs = gpd_mock.call_args_list[0]
- for existingPkg in ["pkg1", "pkg2"]:
- self.assertTrue(existingPkg in args[1])
-
- def verifyReturnedValues(self, dict):
- hostInfo = HostInfo()
- self.assertEqual(dict['alternatives'], [])
- self.assertEqual(dict['stackFoldersAndFiles'], [])
- self.assertEqual(dict['existingUsers'], [])
- self.assertEqual(dict['existingRepos'][0], hostInfo.RESULT_UNAVAILABLE)
- self.assertEqual(dict['installedPackages'], [])
- self.assertTrue(dict['iptablesIsRunning'])
-
- @patch("os.path.exists")
- @patch("os.path.islink")
- @patch("os.path.isdir")
- @patch("os.path.isfile")
- def test_dirType(self, os_path_isfile_mock, os_path_isdir_mock, os_path_islink_mock, os_path_exists_mock):
- host = HostInfo()
-
- os_path_exists_mock.return_value = False
- result = host.dirType("/home")
- self.assertEquals(result, 'not_exist')
-
- os_path_exists_mock.return_value = True
- os_path_islink_mock.return_value = True
- result = host.dirType("/home")
- self.assertEquals(result, 'sym_link')
-
- os_path_exists_mock.return_value = True
- os_path_islink_mock.return_value = False
- os_path_isdir_mock.return_value = True
- result = host.dirType("/home")
- self.assertEquals(result, 'directory')
-
- os_path_exists_mock.return_value = True
- os_path_islink_mock.return_value = False
- os_path_isdir_mock.return_value = False
- os_path_isfile_mock.return_value = True
- result = host.dirType("/home")
- self.assertEquals(result, 'file')
-
- os_path_exists_mock.return_value = True
- os_path_islink_mock.return_value = False
- os_path_isdir_mock.return_value = False
- os_path_isfile_mock.return_value = False
- result = host.dirType("/home")
- self.assertEquals(result, 'unknown')
-
-
- @patch("os.path.exists")
- @patch("glob.glob")
- def test_hadoopVarRunCount(self, glob_glob_mock, os_path_exists_mock):
- hostInfo = HostInfo()
-
- os_path_exists_mock.return_value = True
- glob_glob_mock.return_value = ['pid1','pid2','pid3']
- result = hostInfo.hadoopVarRunCount()
- self.assertEquals(result, 3)
-
- os_path_exists_mock.return_value = False
- result = hostInfo.hadoopVarRunCount()
- self.assertEquals(result, 0)
-
-
- @patch("os.path.exists")
- @patch("glob.glob")
- def test_hadoopVarLogCount(self, glob_glob_mock, os_path_exists_mock):
- hostInfo = HostInfo()
-
- os_path_exists_mock.return_value = True
- glob_glob_mock.return_value = ['log1','log2']
- result = hostInfo.hadoopVarLogCount()
- self.assertEquals(result, 2)
-
- os_path_exists_mock.return_value = False
- result = hostInfo.hadoopVarLogCount()
- self.assertEquals(result, 0)
-
-
- @patch("os.listdir", create=True, autospec=True)
- @patch("__builtin__.open", create=True, autospec=True)
- @patch("pwd.getpwuid", create=True, autospec=True)
- def test_javaProcs(self, pwd_getpwuid_mock, buitin_open_mock, os_listdir_mock):
- hostInfo = HostInfo()
- openRead = MagicMock()
- openRead.read.return_value = '/java/;/hadoop/'
- buitin_open_mock.side_effect = [openRead, ['Uid: 22']]
- pwuid = MagicMock()
- pwd_getpwuid_mock.return_value = pwuid
- pwuid.pw_name = 'user'
- os_listdir_mock.return_value = ['1']
- list = []
- hostInfo.javaProcs(list)
-
- self.assertEquals(list[0]['command'], '/java/;/hadoop/')
- self.assertEquals(list[0]['pid'], 1)
- self.assertTrue(list[0]['hadoop'])
- self.assertEquals(list[0]['user'], 'user')
-
-
- @patch("subprocess.Popen")
- @patch.object(Hardware, 'extractMountInfo')
- def test_osdiskAvailableSpace(self, extract_mount_info_mock, subproc_popen_mock):
- hostInfo = HostInfo()
- p = MagicMock()
- p.communicate.return_value = ['some']
- subproc_popen_mock.return_value = p
- extract_mount_info_mock.return_value = {'info' : 'info'}
- result = hostInfo.osdiskAvailableSpace('')
-
- self.assertTrue(result['info'], 'info')
-
- p.communicate.return_value = ''
- result = hostInfo.osdiskAvailableSpace('')
-
- self.assertEquals(result, {})
-
-
- @patch.object(HostInfo, "get_os_type")
- @patch("subprocess.Popen")
- def test_checkLiveServices(self, subproc_popen, get_os_type_method):
- hostInfo = HostInfo()
- p = MagicMock()
- p.returncode = 0
- p.communicate.return_value = ('', 'err')
- subproc_popen.return_value = p
- result = []
- get_os_type_method.return_value = 'redhat'
- hostInfo.checkLiveServices(['service1'], result)
-
- self.assertEquals(result[0]['status'], 'Healthy')
- self.assertEquals(result[0]['name'], 'service1')
- self.assertEquals(result[0]['desc'], '')
- self.assertEquals(str(subproc_popen.call_args_list),
- "[call(['/sbin/service', 'service1', 'status'], stderr=-1, stdout=-1)]")
-
- p.returncode = 1
- p.communicate.return_value = ('out', 'err')
- result = []
- hostInfo.checkLiveServices(['service1'], result)
-
- self.assertEquals(result[0]['status'], 'Unhealthy')
- self.assertEquals(result[0]['name'], 'service1')
- self.assertEquals(result[0]['desc'], 'out')
-
- p.communicate.return_value = ('', 'err')
- result = []
- hostInfo.checkLiveServices(['service1'], result)
-
- self.assertEquals(result[0]['status'], 'Unhealthy')
- self.assertEquals(result[0]['name'], 'service1')
- self.assertEquals(result[0]['desc'], 'err')
-
- p.communicate.return_value = ('', 'err', '')
- result = []
- hostInfo.checkLiveServices(['service1'], result)
-
- self.assertEquals(result[0]['status'], 'Unhealthy')
- self.assertEquals(result[0]['name'], 'service1')
- self.assertTrue(len(result[0]['desc']) > 0)
-
-
- @patch("os.path.exists")
- @patch("os.listdir", create=True, autospec=True)
- @patch("os.path.islink")
- @patch("os.path.realpath")
- def test_etcAlternativesConf(self, os_path_realpath_mock, os_path_islink_mock, os_listdir_mock, os_path_exists_mock):
- hostInfo = HostInfo()
- os_path_exists_mock.return_value = False
- result = hostInfo.etcAlternativesConf('',[])
-
- self.assertEquals(result, [])
-
- os_path_exists_mock.return_value = True
- os_listdir_mock.return_value = ['config1']
- os_path_islink_mock.return_value = True
- os_path_realpath_mock.return_value = 'real_path_to_conf'
- result = []
- hostInfo.etcAlternativesConf('project',result)
-
- self.assertEquals(result[0]['name'], 'config1')
- self.assertEquals(result[0]['target'], 'real_path_to_conf')
-
-
- @patch("subprocess.Popen")
- def test_checkIptables(self, subproc_popen_mock):
- hostInfo = HostInfo()
- p = MagicMock()
- p.returncode = 0
- subproc_popen_mock.return_value = p
- result = hostInfo.checkIptables()
-
- self.assertTrue(result)
-
- p.returncode = 1
- result = hostInfo.checkIptables()
-
- self.assertFalse(result)
-
-
-if __name__ == "__main__":
- unittest.main()
http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e5c6e113/ambari-agent/src/test/python/TestHostname.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/TestHostname.py b/ambari-agent/src/test/python/TestHostname.py
deleted file mode 100644
index a319b23..0000000
--- a/ambari-agent/src/test/python/TestHostname.py
+++ /dev/null
@@ -1,82 +0,0 @@
-#!/usr/bin/env python2.6
-
-'''
-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.
-'''
-
-from unittest import TestCase
-import ambari_agent.hostname as hostname
-import ambari_agent.AmbariConfig as AmbariConfig
-import socket
-import tempfile
-import shutil
-import os, pprint, json,stat
-
-class TestHostname(TestCase):
-
- def test_hostname(self):
- self.assertEquals(hostname.hostname(), socket.getfqdn(),
- "hostname should equal the socket-based hostname")
- pass
-
- def test_hostname_override(self):
- fd = tempfile.mkstemp(text=True)
- tmpname = fd[1]
- os.close(fd[0])
- os.chmod(tmpname, os.stat(tmpname).st_mode | stat.S_IXUSR)
-
- tmpfile = file(tmpname, "w+")
- config = AmbariConfig.config
- try:
- tmpfile.write("#!/bin/sh\n\necho 'test.example.com'")
- tmpfile.close()
-
- config.set('agent', 'hostname_script', tmpname)
-
- self.assertEquals(hostname.hostname(), 'test.example.com', "expected hostname 'test.example.com'")
- finally:
- os.remove(tmpname)
- config.remove_option('agent', 'hostname_script')
-
- pass
-
- def test_public_hostname_override(self):
- fd = tempfile.mkstemp(text=True)
- tmpname = fd[1]
- os.close(fd[0])
- os.chmod(tmpname, os.stat(tmpname).st_mode | stat.S_IXUSR)
-
- tmpfile = file(tmpname, "w+")
-
- config = AmbariConfig.config
- try:
- tmpfile.write("#!/bin/sh\n\necho 'test.example.com'")
- tmpfile.close()
-
- config.set('agent', 'public_hostname_script', tmpname)
-
- self.assertEquals(hostname.public_hostname(), 'test.example.com',
- "expected hostname 'test.example.com'")
- finally:
- os.remove(tmpname)
- config.remove_option('agent', 'public_hostname_script')
-
- pass
-
-
-
-
http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e5c6e113/ambari-agent/src/test/python/TestLiveStatus.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/TestLiveStatus.py b/ambari-agent/src/test/python/TestLiveStatus.py
deleted file mode 100644
index 977ae4d..0000000
--- a/ambari-agent/src/test/python/TestLiveStatus.py
+++ /dev/null
@@ -1,60 +0,0 @@
-#!/usr/bin/env python2.6
-
-'''
-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.
-'''
-
-from unittest import TestCase
-from ambari_agent.LiveStatus import LiveStatus
-from ambari_agent.AmbariConfig import AmbariConfig
-import socket
-import os, sys, StringIO
-from ambari_agent import ActualConfigHandler
-from mock.mock import patch, MagicMock, call
-
-class TestLiveStatus(TestCase):
-
- def setUp(self):
- # disable stdout
- out = StringIO.StringIO()
- sys.stdout = out
-
-
- def tearDown(self):
- # enable stdout
- sys.stdout = sys.__stdout__
-
- @patch.object(ActualConfigHandler.ActualConfigHandler, "read_actual_component")
- def test_build(self, read_actual_component_mock):
- for component in LiveStatus.COMPONENTS:
- config = AmbariConfig().getConfig()
- config.set('agent', 'prefix', "dummy_files")
- livestatus = LiveStatus('', component['serviceName'], component['componentName'], {}, config)
- livestatus.versionsHandler.versionsFilePath = os.path.join("dummy_files","dummy_current_stack")
- result = livestatus.build()
- print "LiveStatus of {0}: {1}".format(component['serviceName'], str(result))
- self.assertEquals(len(result) > 0, True, 'Livestatus should not be empty')
- if component['componentName'] == 'GANGLIA_SERVER':
- self.assertEquals(result['stackVersion'],'{"stackName":"HDP","stackVersion":"1.2.2"}',
- 'Livestatus should contain component stack version')
-
- # Test build status for CLIENT component (in LiveStatus.CLIENT_COMPONENTS)
- read_actual_component_mock.return_value = "some tags"
- livestatus = LiveStatus('c1', 'HDFS', 'HDFS_CLIENT', { }, config)
- result = livestatus.build()
- self.assertTrue(len(result) > 0, 'Livestatus should not be empty')
- self.assertTrue(result.has_key('configurationTags'))
http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e5c6e113/ambari-agent/src/test/python/TestMain.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/TestMain.py b/ambari-agent/src/test/python/TestMain.py
deleted file mode 100644
index b9ae2b9..0000000
--- a/ambari-agent/src/test/python/TestMain.py
+++ /dev/null
@@ -1,276 +0,0 @@
-#!/usr/bin/env python2.6
-
-'''
-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 ambari_agent import NetUtil, security
-from mock.mock import MagicMock, patch, ANY
-import unittest
-from ambari_agent import ProcessHelper, main
-from ambari_agent import ProcessHelper, main
-import logging
-import signal
-from ambari_agent.AmbariConfig import AmbariConfig
-import ConfigParser
-import os
-import tempfile
-from ambari_agent.PingPortListener import PingPortListener
-from ambari_agent.Controller import Controller
-from optparse import OptionParser
-from ambari_agent.DataCleaner import DataCleaner
-
-
-class TestMain(unittest.TestCase):
-
- def setUp(self):
- # disable stdout
- out = StringIO.StringIO()
- sys.stdout = out
-
-
- def tearDown(self):
- # enable stdout
- sys.stdout = sys.__stdout__
-
-
- @patch("os._exit")
- @patch("os.getpid")
- @patch.object(ProcessHelper, "stopAgent")
- def test_signal_handler(self, stopAgent_mock, os_getpid_mock, os_exit_mock):
- # testing exit of children
- main.agentPid = 4444
- os_getpid_mock.return_value = 5555
- main.signal_handler("signum", "frame")
- self.assertTrue(os_exit_mock.called)
-
- os_exit_mock.reset_mock()
-
- # testing exit of main process
- os_getpid_mock.return_value = main.agentPid
- main.signal_handler("signum", "frame")
- self.assertFalse(os_exit_mock.called)
- self.assertTrue(stopAgent_mock.called)
-
-
- @patch.object(main.logger, "addHandler")
- @patch.object(main.logger, "setLevel")
- @patch("logging.handlers.RotatingFileHandler")
- def test_setup_logging(self, rfh_mock, setLevel_mock, addHandler_mock):
- # Testing silent mode
- main.setup_logging(False)
- self.assertTrue(addHandler_mock.called)
- setLevel_mock.assert_called_with(logging.INFO)
-
- addHandler_mock.reset_mock()
- setLevel_mock.reset_mock()
-
- # Testing verbose mode
- main.setup_logging(True)
- self.assertTrue(addHandler_mock.called)
- setLevel_mock.assert_called_with(logging.DEBUG)
-
-
- @patch.object(main.logger, "setLevel")
- @patch("logging.basicConfig")
- def test_update_log_level(self, basicConfig_mock, setLevel_mock):
- config = AmbariConfig().getConfig()
-
- # Testing with default setup (config file does not contain loglevel entry)
- # Log level should not be changed
- config.set('agent', 'loglevel', None)
- main.update_log_level(config)
- self.assertFalse(setLevel_mock.called)
-
- setLevel_mock.reset_mock()
-
- # Testing debug mode
- config.set('agent', 'loglevel', 'DEBUG')
- main.update_log_level(config)
- setLevel_mock.assert_called_with(logging.DEBUG)
- setLevel_mock.reset_mock()
-
- # Testing any other mode
- config.set('agent', 'loglevel', 'INFO')
- main.update_log_level(config)
- setLevel_mock.assert_called_with(logging.INFO)
-
- setLevel_mock.reset_mock()
-
- config.set('agent', 'loglevel', 'WRONG')
- main.update_log_level(config)
- setLevel_mock.assert_called_with(logging.INFO)
-
-
- @patch("signal.signal")
- def test_bind_signal_handlers(self, signal_mock):
- main.bind_signal_handlers()
- # Check if on SIGINT/SIGTERM agent is configured to terminate
- signal_mock.assert_any_call(signal.SIGINT, main.signal_handler)
- signal_mock.assert_any_call(signal.SIGTERM, main.signal_handler)
- # Check if on SIGUSR1 agent is configured to fall into debug
- signal_mock.assert_any_call(signal.SIGUSR1, main.debug)
-
-
- @patch("os.path.exists")
- @patch("ConfigParser.RawConfigParser.read")
- def test_resolve_ambari_config(self, read_mock, exists_mock):
- # Trying case if conf file exists
- exists_mock.return_value = True
- main.resolve_ambari_config()
- self.assertTrue(read_mock.called)
-
- exists_mock.reset_mock()
- read_mock.reset_mock()
-
- # Trying case if conf file does not exist
- exists_mock.return_value = False
- main.resolve_ambari_config()
- self.assertFalse(read_mock.called)
-
-
- @patch("sys.exit")
- @patch("os.path.isfile")
- @patch("os.path.isdir")
- @patch("hostname.hostname")
- def test_perform_prestart_checks(self, hostname_mock, isdir_mock, isfile_mock, exit_mock):
- main.config = AmbariConfig().getConfig()
-
- # Check expected hostname test
- hostname_mock.return_value = "test.hst"
-
- main.perform_prestart_checks("another.hst")
- self.assertTrue(exit_mock.called)
-
- exit_mock.reset_mock()
-
- # Trying case if there is another instance running
- isfile_mock.return_value = True
- isdir_mock.return_value = True
- main.perform_prestart_checks(None)
- self.assertTrue(exit_mock.called)
-
- isfile_mock.reset_mock()
- isdir_mock.reset_mock()
- exit_mock.reset_mock()
-
- # Trying case if agent prefix dir does not exist
- isfile_mock.return_value = False
- isdir_mock.return_value = False
- main.perform_prestart_checks(None)
- self.assertTrue(exit_mock.called)
-
- isfile_mock.reset_mock()
- isdir_mock.reset_mock()
- exit_mock.reset_mock()
-
- # Trying normal case
- isfile_mock.return_value = False
- isdir_mock.return_value = True
- main.perform_prestart_checks(None)
- self.assertFalse(exit_mock.called)
-
-
- @patch("time.sleep")
- @patch("os.kill")
- @patch("os._exit")
- @patch("os.path.exists")
- def test_daemonize_and_stop(self, exists_mock, _exit_mock, kill_mock, sleep_mock):
- oldpid = ProcessHelper.pidfile
- pid = str(os.getpid())
- _, tmpoutfile = tempfile.mkstemp()
- ProcessHelper.pidfile = tmpoutfile
-
- # Test daemonization
- main.daemonize()
- saved = open(ProcessHelper.pidfile, 'r').read()
- self.assertEqual(pid, saved)
-
- # Reuse pid file when testing agent stop
- # Testing normal exit
- exists_mock.return_value = False
- main.stop_agent()
- kill_mock.assert_called_with(int(pid), signal.SIGTERM)
- _exit_mock.assert_called_with(0)
-
- # Restore
- kill_mock.reset_mock()
- _exit_mock.reset_mock()
-
- # Testing exit when failed to remove pid file
- exists_mock.return_value = True
- main.stop_agent()
- kill_mock.assert_any_call(int(pid), signal.SIGTERM)
- kill_mock.assert_any_call(int(pid), signal.SIGKILL)
- _exit_mock.assert_called_with(1)
-
- # Restore
- ProcessHelper.pidfile = oldpid
- os.remove(tmpoutfile)
-
-
- @patch.object(main, "setup_logging")
- @patch.object(main, "bind_signal_handlers")
- @patch.object(main, "stop_agent")
- @patch.object(main, "resolve_ambari_config")
- @patch.object(main, "perform_prestart_checks")
- @patch.object(main, "daemonize")
- @patch.object(main, "update_log_level")
- @patch.object(NetUtil.NetUtil, "try_to_connect")
- @patch.object(Controller, "__init__")
- @patch.object(Controller, "start")
- @patch.object(Controller, "join")
- @patch("optparse.OptionParser.parse_args")
- @patch.object(DataCleaner,"start")
- @patch.object(DataCleaner,"__init__")
- @patch.object(PingPortListener,"start")
- @patch.object(PingPortListener,"__init__")
- def test_main(self, ping_port_init_mock, ping_port_start_mock, data_clean_init_mock,data_clean_start_mock,
- parse_args_mock, join_mock, start_mock, Controller_init_mock, try_to_connect_mock,
- update_log_level_mock, daemonize_mock, perform_prestart_checks_mock,
- resolve_ambari_config_mock, stop_mock, bind_signal_handlers_mock, setup_logging_mock):
- data_clean_init_mock.return_value = None
- Controller_init_mock.return_value = None
- ping_port_init_mock.return_value = None
- options = MagicMock()
- parse_args_mock.return_value = (options, MagicMock)
-
- #testing call without command-line arguments
- main.main()
-
- self.assertTrue(setup_logging_mock.called)
- self.assertTrue(bind_signal_handlers_mock.called)
- self.assertTrue(stop_mock.called)
- self.assertTrue(resolve_ambari_config_mock.called)
- self.assertTrue(perform_prestart_checks_mock.called)
- self.assertTrue(daemonize_mock.called)
- self.assertTrue(update_log_level_mock.called)
- try_to_connect_mock.assert_called_once_with(ANY, -1, ANY)
- self.assertTrue(start_mock.called)
- self.assertTrue(data_clean_init_mock.called)
- self.assertTrue(data_clean_start_mock.called)
- self.assertTrue(ping_port_init_mock.called)
- self.assertTrue(ping_port_start_mock.called)
-
- perform_prestart_checks_mock.reset_mock()
-
- # Testing call with --expected-hostname parameter
- options.expected_hostname = "test.hst"
- main.main()
- perform_prestart_checks_mock.assert_called_once_with(options.expected_hostname)
http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e5c6e113/ambari-agent/src/test/python/TestManifestGenerator.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/TestManifestGenerator.py b/ambari-agent/src/test/python/TestManifestGenerator.py
deleted file mode 100644
index f4ea6dc..0000000
--- a/ambari-agent/src/test/python/TestManifestGenerator.py
+++ /dev/null
@@ -1,142 +0,0 @@
-#!/usr/bin/env python2.6
-
-'''
-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 os, sys, StringIO
-
-from unittest import TestCase
-from ambari_agent import manifestGenerator
-import ambari_agent.AmbariConfig
-import tempfile
-import json
-import shutil
-from ambari_agent.AmbariConfig import AmbariConfig
-from mock.mock import patch, MagicMock, call
-
-
-class TestManifestGenerator(TestCase):
-
- def setUp(self):
- # disable stdout
- out = StringIO.StringIO()
- sys.stdout = out
-
- self.dir = tempfile.mkdtemp()
- self.config = AmbariConfig()
- jsonCommand = file('../../main/python/ambari_agent/test.json').read()
- self.parsedJson = json.loads(jsonCommand)
-
-
- def tearDown(self):
- shutil.rmtree(self.dir)
-
- # enable stdout
- sys.stdout = sys.__stdout__
-
-
- def testWriteImports(self):
- tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
- print tmpFileName
- tmpFile = file(tmpFileName, 'r+')
-
- manifestGenerator.writeImports(tmpFile, '../../main/puppet/modules', self.config.getImports())
- tmpFile.seek(0)
- print tmpFile.read()
- tmpFile.close()
-
-
- pass
-
- @patch.object(manifestGenerator, 'writeImports')
- @patch.object(manifestGenerator, 'writeNodes')
- @patch.object(manifestGenerator, 'writeParams')
- @patch.object(manifestGenerator, 'writeTasks')
- def testGenerateManifest(self, writeTasksMock, writeParamsMock, writeNodesMock, writeImportsMock):
- tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
- self.parsedJson['roleParams'] = 'role param'
- manifestGenerator.generateManifest(self.parsedJson, tmpFileName, '../../main/puppet/modules', self.config.getConfig())
-
- self.assertTrue(writeParamsMock.called)
- self.assertTrue(writeNodesMock.called)
- self.assertTrue(writeImportsMock.called)
- self.assertTrue(writeTasksMock.called)
-
- print file(tmpFileName).read()
-
- def raiseTypeError():
- raise TypeError()
- writeNodesMock.side_effect = raiseTypeError
- manifestGenerator.generateManifest(self.parsedJson, tmpFileName, '../../main/puppet/modules', self.config.getConfig())
- pass
-
- def testEscape(self):
- shouldBe = '\\\'\\\\'
- result = manifestGenerator.escape('\'\\')
- self.assertEqual(result, shouldBe)
-
-
- def test_writeNodes(self):
- tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
- tmpFile = file(tmpFileName, 'r+')
-
- clusterHostInfo = self.parsedJson['clusterHostInfo']
- clusterHostInfo['zookeeper_hosts'] = ["h1.hortonworks.com", "h2.hortonworks.com"]
- manifestGenerator.writeNodes(tmpFile, clusterHostInfo)
- tmpFile.seek(0)
- print tmpFile.read()
- tmpFile.close()
- os.remove(tmpFileName)
-
- def test_writeNodes_failed(self):
- tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
- tmpFile = file(tmpFileName, 'r+')
-
- clusterHostInfo = self.parsedJson['clusterHostInfo']
- clusterHostInfo.update({u'ZOOKEEPER':[None]})
- clusterHostInfo['zookeeper_hosts'] = ["h1.hortonworks.com", "h2.hortonworks.com"]
- self.assertRaises(TypeError, manifestGenerator.writeNodes, tmpFile, clusterHostInfo)
- tmpFile.seek(0)
- print tmpFile.read()
- tmpFile.close()
- os.remove(tmpFileName)
-
- def test_writeHostAttributes(self):
- tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
- tmpFile = file(tmpFileName, 'r+')
-
- hostAttributes = {'HostAttr1' : '1', 'HostAttr2' : '2'}
- manifestGenerator.writeHostAttributes(tmpFile, hostAttributes)
- tmpFile.seek(0)
- print tmpFile.read()
- tmpFile.close()
- os.remove(tmpFileName)
-
-
- def test_writeTasks(self):
- tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
- tmpFile = file(tmpFileName, 'r+')
- roles = [{'role' : 'ZOOKEEPER_SERVER',
- 'cmd' : 'NONE',
- 'roleParams' : {'someRoleParams': '-x'}}]
- clusterHostInfo = self.parsedJson['clusterHostInfo']
- clusterHostInfo['zookeeper_hosts'] = ["h1.hortonworks.com", "h2.hortonworks.com"]
- manifestGenerator.writeTasks(tmpFile, roles, self.config, clusterHostInfo, "h1.hortonworks.com")
- tmpFile.seek(0)
- print tmpFile.read()
- tmpFile.close()
- os.remove(tmpFileName)
http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e5c6e113/ambari-agent/src/test/python/TestNetUtil.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/TestNetUtil.py b/ambari-agent/src/test/python/TestNetUtil.py
deleted file mode 100644
index e1fe02d..0000000
--- a/ambari-agent/src/test/python/TestNetUtil.py
+++ /dev/null
@@ -1,77 +0,0 @@
-#!/usr/bin/env python2.6
-
-'''
-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.
-'''
-
-from ambari_agent import NetUtil
-from mock.mock import MagicMock, patch
-import unittest
-
-class TestNetUtil(unittest.TestCase):
-
- @patch("urlparse.urlparse")
- @patch("httplib.HTTPSConnection")
- def test_checkURL(self, httpsConMock, parseMock):
-
- NetUtil.logger = MagicMock()
- parseMock.return_value = [1, 2]
- ca_connection = MagicMock()
- response = MagicMock()
- response.status = 200
- ca_connection.getresponse.return_value = response
- httpsConMock.return_value = ca_connection
-
- # test 200
- netutil = NetUtil.NetUtil()
- self.assertTrue(netutil.checkURL("url"))
-
- # test fail
- response.status = 404
- self.assertFalse(netutil.checkURL("url"))
-
- # test Exception
- response.status = 200
- httpsConMock.side_effect = Exception("test")
- self.assertFalse(netutil.checkURL("url"))
-
-
- @patch("time.sleep")
- def test_try_to_connect(self, sleepMock):
-
- netutil = NetUtil.NetUtil()
- checkURL = MagicMock(name="checkURL")
- checkURL.return_value = True
- netutil.checkURL = checkURL
- l = MagicMock()
-
- # one successful get
- self.assertEqual(0, netutil.try_to_connect("url", 10))
-
- # got successful after N retries
- gets = [True, False, False]
- def side_effect(*args):
- return gets.pop()
- checkURL.side_effect = side_effect
- self.assertEqual(2, netutil.try_to_connect("url", 10))
-
- # max retries
- checkURL.side_effect = None
- checkURL.return_value = False
- self.assertEqual(5, netutil.try_to_connect("url", 5))
-
-
http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e5c6e113/ambari-agent/src/test/python/TestPingPortListener.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/TestPingPortListener.py b/ambari-agent/src/test/python/TestPingPortListener.py
deleted file mode 100644
index a61f7e0..0000000
--- a/ambari-agent/src/test/python/TestPingPortListener.py
+++ /dev/null
@@ -1,60 +0,0 @@
-#!/usr/bin/env python2.6
-
-'''
-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 mock.mock import patch, MagicMock, call, Mock
-from ambari_agent import PingPortListener
-import socket
-import sys
-
-class TestPingPortListener(unittest.TestCase):
-
- def setUp(self):
- self.config = MagicMock()
- self.config.get.return_value = 55000
- PingPortListener.logger = MagicMock()
-
- @patch("socket.socket")
- def test_init_success(self,socketMock):
- PingPortListener.logger.reset_mock()
- allive_daemon = PingPortListener.PingPortListener(self.config)
- self.assertFalse(PingPortListener.logger.warn.called)
- self.assertTrue(socketMock.call_args_list[0][0][0] == socket.AF_INET)
- self.assertTrue(socketMock.call_args_list[0][0][1] == socket.SOCK_STREAM)
- self.assertTrue(allive_daemon.socket.bind.call_args_list[0][0][0] == ('0.0.0.0',55000))
- self.assertTrue(allive_daemon.socket.listen.call_args_list[0][0][0] == 1)
- self.assertTrue(allive_daemon.config.set.call_args_list[0][0][0] == 'agent')
- self.assertTrue(allive_daemon.config.set.call_args_list[0][0][1] == 'current_ping_port')
-
-
-
- @patch.object(socket.socket,"bind")
- @patch.object(socket.socket,"listen")
- @patch.object(socket.socket,"__init__")
- @patch.object(sys, "exit")
- def test_init_warn(self, sys_exit_mock, socketInitMock,socketListenMock,socketBindMock):
- PingPortListener.logger.reset_mock()
- allive_daemon = PingPortListener.PingPortListener(self.config)
- self.assertTrue(socketInitMock.called)
- self.assertTrue(sys_exit_mock.called)
-
-if __name__ == "__main__":
- suite = unittest.TestLoader().loadTestsFromTestCase(PingPortListener)
- unittest.TextTestRunner(verbosity=2).run(suite)
http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e5c6e113/ambari-agent/src/test/python/TestProcessHelper.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/TestProcessHelper.py b/ambari-agent/src/test/python/TestProcessHelper.py
deleted file mode 100644
index c7a4261..0000000
--- a/ambari-agent/src/test/python/TestProcessHelper.py
+++ /dev/null
@@ -1,68 +0,0 @@
-#!/usr/bin/env python2.6
-# -*- coding: utf-8 -*-
-
-'''
-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 os
-import tempfile
-import unittest
-from mock.mock import patch, MagicMock
-from ambari_agent import ProcessHelper
-
-
-class TestProcessHelper(unittest.TestCase):
-
- @patch.object(ProcessHelper, "getTempFiles")
- def test_clean(self, getTempFilesMock):
-
- tf1 = tempfile.NamedTemporaryFile(delete=False)
- tf2 = tempfile.NamedTemporaryFile(delete=False)
- tf3 = tempfile.NamedTemporaryFile(delete=False)
-
- getTempFilesMock.return_value = [tf2.name, tf3.name]
- ProcessHelper.pidfile = tf1.name
- ProcessHelper.logger = MagicMock()
-
- ProcessHelper._clean()
-
- self.assertFalse(os.path.exists(tf1.name))
- self.assertFalse(os.path.exists(tf2.name))
- self.assertFalse(os.path.exists(tf3.name))
-
-
- @patch("os._exit")
- @patch.object(ProcessHelper, "_clean")
- def test_stopAgent(self, _clean_mock, exitMock):
-
- ProcessHelper.stopAgent()
- self.assertTrue(_clean_mock.called)
- self.assertTrue(exitMock.called)
-
-
- @patch("os.execvp")
- @patch.object(ProcessHelper, "_clean")
- def test_restartAgent(self, _clean_mock, execMock):
-
- ProcessHelper.logger = MagicMock()
- ProcessHelper.restartAgent()
-
- self.assertTrue(_clean_mock.called)
- self.assertTrue(execMock.called)
- self.assertEqual(2, len(execMock.call_args_list[0]))
-