You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ma...@apache.org on 2013/01/19 02:57:22 UTC

svn commit: r1435460 [1/6] - in /incubator/ambari/trunk: ./ ambari-agent/ ambari-agent/conf/unix/ ambari-agent/src/main/puppet/modules/hdp-hcat-old/files/ ambari-agent/src/main/puppet/modules/hdp-hcat-old/manifests/ ambari-agent/src/main/puppet/modules...

Author: mahadev
Date: Sat Jan 19 01:57:20 2013
New Revision: 1435460

URL: http://svn.apache.org/viewvc?rev=1435460&view=rev
Log:
AMBARI-1213. Cleanup python test cases and introduce third party library for mock testing python code. (mahadev)

Added:
    incubator/ambari/trunk/ambari-agent/src/test/python/TestManifestGenerator.py
    incubator/ambari/trunk/ambari-agent/src/test/python/TestProcessHelper.py
    incubator/ambari/trunk/ambari-agent/src/test/python/TestRepoInstaller.py
    incubator/ambari/trunk/ambari-common/
    incubator/ambari/trunk/ambari-common/src/
    incubator/ambari/trunk/ambari-common/src/test/
    incubator/ambari/trunk/ambari-common/src/test/python/
    incubator/ambari/trunk/ambari-common/src/test/python/mock/
    incubator/ambari/trunk/ambari-common/src/test/python/mock/LICENSE.txt
    incubator/ambari/trunk/ambari-common/src/test/python/mock/MANIFEST.in
    incubator/ambari/trunk/ambari-common/src/test/python/mock/README.txt
    incubator/ambari/trunk/ambari-common/src/test/python/mock/__init__.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/docs/
    incubator/ambari/trunk/ambari-common/src/test/python/mock/extendmock.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/mock.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/mock.wpr
    incubator/ambari/trunk/ambari-common/src/test/python/mock/setup.cfg
    incubator/ambari/trunk/ambari-common/src/test/python/mock/setup.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/tests/
    incubator/ambari/trunk/ambari-common/src/test/python/mock/tests/__init__.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/tests/_testwith.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/tests/support.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/tests/support_with.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/tests/testcallable.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/tests/testhelpers.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/tests/testmagicmethods.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/tests/testmock.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/tests/testpatch.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/tests/testsentinel.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/tests/testwith.py
    incubator/ambari/trunk/ambari-common/src/test/python/mock/tox.ini
    incubator/ambari/trunk/ambari-common/src/test/python/mock/unittest.cfg
    incubator/ambari/trunk/ambari-server/src/test/python/
    incubator/ambari/trunk/ambari-server/src/test/python/TestAmbaryServer.py
    incubator/ambari/trunk/ambari-server/src/test/python/unitTests.py
    incubator/ambari/trunk/ambari-server/src/test/resources/TestAmbaryServer.samples/
    incubator/ambari/trunk/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_pg_hba_ambaridb_users1
    incubator/ambari/trunk/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_pg_hba_ambaridb_users2
    incubator/ambari/trunk/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_postgresql_conf1
Removed:
    incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat-old/files/hcatSmoke.sh
    incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat-old/files/hiveSmoke.sh
    incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat-old/manifests/client.pp
    incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat-old/manifests/hcat/service_check.pp
    incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat-old/manifests/hive/service_check.pp
    incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat-old/manifests/init.pp
    incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat-old/manifests/mysql-connector.pp
    incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat-old/manifests/params.pp
    incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat-old/manifests/server.pp
    incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat-old/manifests/service.pp
    incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat-old/templates/hcat-env.sh.erb
    incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat-old/templates/hive-env.sh.erb
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/imports.txt
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/rolesToClass.dict
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/serviceStates.dict
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/servicesToPidNames.dict
Modified:
    incubator/ambari/trunk/CHANGES.txt
    incubator/ambari/trunk/LICENSE.txt
    incubator/ambari/trunk/NOTICE.txt
    incubator/ambari/trunk/ambari-agent/conf/unix/ambari-agent.ini
    incubator/ambari/trunk/ambari-agent/pom.xml
    incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat/files/hcatSmoke.sh
    incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat/manifests/hcat/service_check.pp
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Controller.py
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/LiveStatus.py
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/ProcessHelper.py
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/RepoInstaller.py
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/StatusCheck.py
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/main.py
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/manifestGenerator.py
    incubator/ambari/trunk/ambari-agent/src/test/python/TestController.py
    incubator/ambari/trunk/ambari-agent/src/test/python/TestHeartbeat.py
    incubator/ambari/trunk/ambari-agent/src/test/python/TestLiveStatus.py
    incubator/ambari/trunk/ambari-agent/src/test/python/TestNetUtil.py
    incubator/ambari/trunk/ambari-agent/src/test/python/TestStatusCheck.py
    incubator/ambari/trunk/ambari-server/pom.xml
    incubator/ambari/trunk/ambari-server/src/main/python/ambari-server.py
    incubator/ambari/trunk/ambari-server/src/main/resources/stacks/HDP/1.2.0/services/MAPREDUCE/configuration/mapred-site.xml
    incubator/ambari/trunk/pom.xml

Modified: incubator/ambari/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/CHANGES.txt?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/CHANGES.txt (original)
+++ incubator/ambari/trunk/CHANGES.txt Sat Jan 19 01:57:20 2013
@@ -76,6 +76,9 @@ Trunk (unreleased changes):
  AMBARI-1198. Ambari API Performance: Parsing of Ganglia json data is slow.
  (jspeidel via mahadev)
 
+ AMBARI-1213. Cleanup python test cases and introduce third party library for
+ mock testing python code. (mahadev)
+
  BUG FIXES
 
  AMBARI-1212. After successful install with Ambari, the user is taken to the

Modified: incubator/ambari/trunk/LICENSE.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/LICENSE.txt?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/LICENSE.txt (original)
+++ incubator/ambari/trunk/LICENSE.txt Sat Jan 19 01:57:20 2013
@@ -231,3 +231,32 @@ You may obtain a copy of the License at
     See the License for the specific language governing permissions and
     limitations under the License.
 
+For ambari-common/src/test/python:
+
+Copyright (c) 2003-2012, Michael Foord
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+

Modified: incubator/ambari/trunk/NOTICE.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/NOTICE.txt?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/NOTICE.txt (original)
+++ incubator/ambari/trunk/NOTICE.txt Sat Jan 19 01:57:20 2013
@@ -9,3 +9,7 @@ Copyright (C) 2011 Puppet Labs Inc
 and some parts:
 Copyright (C) 2011 Krzysztof Wilczynski
 
+Component ambari-common/src/test/python are under the following copyright:
+
+Copyright (c) 2003-2012, Michael Foord
+All rights reserved.

Modified: incubator/ambari/trunk/ambari-agent/conf/unix/ambari-agent.ini
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/conf/unix/ambari-agent.ini?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/conf/unix/ambari-agent.ini (original)
+++ incubator/ambari/trunk/ambari-agent/conf/unix/ambari-agent.ini Sat Jan 19 01:57:20 2013
@@ -28,9 +28,6 @@ puppetmodules=/var/lib/ambari-agent/pupp
 ruby_home=/usr/lib/ambari-agent/lib/ruby-1.8.7-p370
 puppet_home=/usr/lib/ambari-agent/lib/puppet-2.7.9
 facter_home=/usr/lib/ambari-agent/lib/facter-1.6.10
-imports_file=/usr/lib/python2.6/site-packages/ambari_agent/imports.txt
-roles_to_class=/usr/lib/python2.6/site-packages/ambari_agent/rolesToClass.dict
-service_states=/usr/lib/python2.6/site-packages/ambari_agent/serviceStates.dict
 
 [command]
 maxretries=2
@@ -42,5 +39,4 @@ server_crt=ca.crt
 passphrase_env_var_name=AMBARI_PASSPHRASE
 
 [services]
-serviceToPidMapFile=/usr/lib/python2.6/site-packages/ambari_agent/servicesToPidNames.dict
 pidLookupPath=/var/run/

Modified: incubator/ambari/trunk/ambari-agent/pom.xml
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/pom.xml?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/pom.xml (original)
+++ incubator/ambari/trunk/ambari-agent/pom.xml Sat Jan 19 01:57:20 2013
@@ -90,7 +90,7 @@
                 <argument>unitTests.py</argument>
               </arguments>
               <environmentVariables>
-                <PYTHONPATH>src/main/python/ambari_agent:$PYTHONPATH</PYTHONPATH>
+                <PYTHONPATH>${project.basedir}/../ambari-common/src/test/python:${project.basedir}/src/main/python/ambari_agent:$PYTHONPATH</PYTHONPATH>
               </environmentVariables>
               <skip>${skipTests}</skip>
             </configuration>
@@ -292,7 +292,6 @@
             <exclude>src/test/python/dummy*.txt</exclude>
             <exclude>src/main/puppet/modules/stdlib/**</exclude>
             <exclude>**/*.erb</exclude>
-            <exclude>src/main/python/ambari_agent/imports.txt</exclude>
             <exclude>**/*.json</exclude>
           </excludes>
         </configuration>

Modified: incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat/files/hcatSmoke.sh
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat/files/hcatSmoke.sh?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat/files/hcatSmoke.sh (original)
+++ incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat/files/hcatSmoke.sh Sat Jan 19 01:57:20 2013
@@ -19,6 +19,17 @@
 #
 #
 export tablename=$1
-hcat -e "show tables"
-hcat -e "drop table IF EXISTS ${tablename}"
-hcat -e "create table ${tablename} ( id INT, name string ) stored as rcfile ;"
+
+case "$2" in
+
+prepare)
+  hcat -e "show tables"
+  hcat -e "drop table IF EXISTS ${tablename}"
+  hcat -e "create table ${tablename} ( id INT, name string ) stored as rcfile ;"
+;;
+
+cleanup)
+  hcat -e "drop table IF EXISTS ${tablename}"
+;;
+
+esac
\ No newline at end of file

Modified: incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat/manifests/hcat/service_check.pp
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat/manifests/hcat/service_check.pp?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat/manifests/hcat/service_check.pp (original)
+++ incubator/ambari/trunk/ambari-agent/src/main/puppet/modules/hdp-hcat/manifests/hcat/service_check.pp Sat Jan 19 01:57:20 2013
@@ -43,8 +43,8 @@ class hdp-hcat::hcat::service_check() 
     mode => '0755',
   }
 
-  exec { '/tmp/hcatSmoke.sh':
-    command   => "su - ${smoke_test_user} -c '${smoke_user_kinitcmd}sh /tmp/hcatSmoke.sh hcatsmoke${unique}'",
+  exec { 'hcatSmoke.sh prepare':
+    command   => "su - ${smoke_test_user} -c '${smoke_user_kinitcmd}sh /tmp/hcatSmoke.sh hcatsmoke${unique} prepare'",
     tries     => 3,
     try_sleep => 5,
     require   => File['/tmp/hcatSmoke.sh'],
@@ -56,8 +56,17 @@ class hdp-hcat::hcat::service_check() 
   hdp-hadoop::exec-hadoop { 'hcat::service_check::test':
     command     => $test_cmd,
     refreshonly => true,
-    require     => Exec['/tmp/hcatSmoke.sh'],
-    before      => Anchor['hdp-hcat::hcat::service_check::end'] 
+    require     => Exec['hcatSmoke.sh prepare'],
+  }
+
+  exec { 'hcatSmoke.sh cleanup':
+    command   => "su - ${smoke_test_user} -c '${smoke_user_kinitcmd}sh /tmp/hcatSmoke.sh hcatsmoke${unique} cleanup'",
+    tries     => 3,
+    try_sleep => 5,
+    path      => '/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin',
+    require   => Hdp-hadoop::Exec-hadoop['hcat::service_check::test'],
+    before    => Anchor['hdp-hcat::hcat::service_check::end'],
+    logoutput => "true"
   }
   
   anchor{ 'hdp-hcat::hcat::service_check::end':}

Modified: incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py (original)
+++ incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py Sat Jan 19 01:57:20 2013
@@ -35,7 +35,6 @@ secured_url_port=8441
 prefix=/tmp/ambari-agent
 
 [services]
-serviceToPidMapFile=servicesToPidNames.dict
 pidLookupPath=/var/run/
 
 [stack]
@@ -58,15 +57,120 @@ passphrase_env_var_name=AMBARI_PASSPHRAS
 s = StringIO.StringIO(content)
 config.readfp(s)
 
+imports = [
+  "hdp/manifests/*.pp",
+  "hdp-hadoop/manifests/*.pp",
+  "hdp-hbase/manifests/*.pp",
+  "hdp-zookeeper/manifests/*.pp",
+  "hdp-oozie/manifests/*.pp",
+  "hdp-pig/manifests/*.pp",
+  "hdp-sqoop/manifests/*.pp",
+  "hdp-templeton/manifests/*.pp",
+  "hdp-hive/manifests/*.pp",
+  "hdp-hcat/manifests/*.pp",
+  "hdp-mysql/manifests/*.pp",
+  "hdp-monitor-webserver/manifests/*.pp",
+  "hdp-repos/manifests/*.pp"
+]
+
+rolesToClass = {
+  'NAMENODE': 'hdp-hadoop::namenode',
+  'DATANODE': 'hdp-hadoop::datanode',
+  'SECONDARY_NAMENODE': 'hdp-hadoop::snamenode',
+  'JOBTRACKER': 'hdp-hadoop::jobtracker',
+  'TASKTRACKER': 'hdp-hadoop::tasktracker',
+  'HDFS_CLIENT': 'hdp-hadoop::client',
+  'MAPREDUCE_CLIENT': 'hdp-hadoop::client',
+  'ZOOKEEPER_SERVER': 'hdp-zookeeper',
+  'ZOOKEEPER_CLIENT': 'hdp-zookeeper::client',
+  'HBASE_MASTER': 'hdp-hbase::master',
+  'HBASE_REGIONSERVER': 'hdp-hbase::regionserver',
+  'HBASE_CLIENT': 'hdp-hbase::client',
+  'PIG': 'hdp-pig',
+  'SQOOP': 'hdp-sqoop',
+  'OOZIE_SERVER': 'hdp-oozie::server',
+  'OOZIE_CLIENT': 'hdp-oozie::client',
+  'HIVE_CLIENT': 'hdp-hive::client',
+  'HCAT': 'hdp-hcat',
+  'HIVE_SERVER': 'hdp-hive::server',
+  'HIVE_METASTORE': 'hdp-hive::metastore',
+  'MYSQL_SERVER': 'hdp-mysql::server',
+  'WEBHCAT_SERVER': 'hdp-templeton::server',
+  'DASHBOARD': 'hdp-dashboard',
+  'NAGIOS_SERVER': 'hdp-nagios::server',
+  'GANGLIA_SERVER': 'hdp-ganglia::server',
+  'GANGLIA_MONITOR': 'hdp-ganglia::monitor',
+  'HTTPD': 'hdp-monitor-webserver',
+  'HDFS_SERVICE_CHECK': 'hdp-hadoop::hdfs::service_check',
+  'MAPREDUCE_SERVICE_CHECK': 'hdp-hadoop::mapred::service_check',
+  'ZOOKEEPER_SERVICE_CHECK': 'hdp-zookeeper::zookeeper::service_check',
+  'ZOOKEEPER_QUORUM_SERVICE_CHECK': 'hdp-zookeeper::quorum::service_check',
+  'HBASE_SERVICE_CHECK': 'hdp-hbase::hbase::service_check',
+  'HIVE_SERVICE_CHECK': 'hdp-hive::hive::service_check',
+  'HCAT_SERVICE_CHECK': 'hdp-hcat::hcat::service_check',
+  'OOZIE_SERVICE_CHECK': 'hdp-oozie::oozie::service_check',
+  'PIG_SERVICE_CHECK': 'hdp-pig::pig::service_check',
+  'SQOOP_SERVICE_CHECK': 'hdp-sqoop::sqoop::service_check',
+  'WEBHCAT_SERVICE_CHECK': 'hdp-templeton::templeton::service_check',
+  'DASHBOARD_SERVICE_CHECK': 'hdp-dashboard::dashboard::service_check',
+  'DECOMMISSION_DATANODE': 'hdp-hadoop::hdfs::decommission'
+}
+
+serviceStates = {
+  'START': 'running',
+  'INSTALL': 'installed_and_configured',
+  'STOP': 'stopped'
+}
+
+servicesToPidNames = {
+  'NAMENODE': 'hadoop-[A-Za-z0-9_]+-namenode.pid$',
+  'SECONDARY_NAMENODE': 'hadoop-[A-Za-z0-9_]+-secondarynamenode.pid$',
+  'DATANODE': 'hadoop-[A-Za-z0-9_]+-datanode.pid$',
+  'JOBTRACKER': 'hadoop-[A-Za-z0-9_]+-jobtracker.pid$',
+  'TASKTRACKER': 'hadoop-[A-Za-z0-9_]+-tasktracker.pid$',
+  'OOZIE_SERVER': 'oozie.pid',
+  'ZOOKEEPER_SERVER': 'zookeeper_server.pid',
+  'TEMPLETON_SERVER': 'templeton.pid',
+  'NAGIOS_SERVER': 'nagios.pid',
+  'GANGLIA_SERVER': 'gmetad.pid',
+  'GANGLIA_MONITOR': 'gmond.pid',
+  'HBASE_MASTER': 'hbase-[A-Za-z0-9_]+-master.pid',
+  'HBASE_REGIONSERVER': 'hbase-[A-Za-z0-9_]+-regionserver.pid',
+  'HCATALOG_SERVER': 'hcat.pid',
+  'KERBEROS_SERVER': 'kadmind.pid',
+  'HIVE_SERVER': 'hive-server.pid',
+  'HIVE_METASTORE': 'hive.pid',
+  'MYSQL_SERVER': 'mysqld.pid'
+}
+
+
 class AmbariConfig:
   def getConfig(self):
     global config
     return config
 
+  def getImports(self):
+    global imports
+    return imports
+
+  def getRolesToClass(self):
+    global rolesToClass
+    return rolesToClass
+
+  def getServiceStates(self):
+    global serviceStates
+    return serviceStates
+
+  def getServicesToPidNames(self):
+    global servicesToPidNames
+    return servicesToPidNames
+
+
 def setConfig(customConfig):
   global config
   config = customConfig
 
+
 def main():
   print config
 

Modified: incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Controller.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Controller.py?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Controller.py (original)
+++ incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Controller.py Sat Jan 19 01:57:20 2013
@@ -226,8 +226,7 @@ class Controller(threading.Thread):
     self.heartbeatWithServer()
 
   def restartAgent(self):
-    #stopping for now, restart will be added later
-    ProcessHelper.stopAgent()
+    ProcessHelper.restartAgent()
     pass
 
   def sendRequest(self, url, data):

Modified: incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/LiveStatus.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/LiveStatus.py?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/LiveStatus.py (original)
+++ incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/LiveStatus.py Sat Jan 19 01:57:20 2013
@@ -105,8 +105,8 @@ class LiveStatus:
   def build(self):
     global SERVICES, COMPONENTS, LIVE_STATUS, DEAD_STATUS
     pidLookupPath = AmbariConfig.config.get('services','pidLookupPath')
-    serviceToPidMapFile = AmbariConfig.config.get('services','serviceToPidMapFile')
-    statusCheck = StatusCheck(pidLookupPath, serviceToPidMapFile)
+
+    statusCheck = StatusCheck(pidLookupPath, AmbariConfig.servicesToPidNames)
     livestatus = None
     for component in self.COMPONENTS:
       if component["serviceName"] == self.service and component["componentName"] == self.component:

Modified: incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/ProcessHelper.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/ProcessHelper.py?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/ProcessHelper.py (original)
+++ incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/ProcessHelper.py Sat Jan 19 01:57:20 2013
@@ -21,6 +21,7 @@ limitations under the License.
 import os
 import logging
 import traceback
+import sys
 from shell import getTempFiles
 
 logger = logging.getLogger()
@@ -32,20 +33,42 @@ else:
     pidfile = "/var/run/ambari-agent/ambari-agent.pid"
 
 
-def stopAgent():
+def _clean():
+
+  logger.info("Removing pid file")
   try:
     os.unlink(pidfile)
-  except Exception:
-    logger.warn("Unable to remove: "+pidfile)
+  except Exception as ex:
     traceback.print_exc()
+    logger.warn("Unable to remove pid file: %s", ex)
 
-  tempFiles = getTempFiles()
-  for tempFile in tempFiles:
-    if os.path.exists(tempFile):
+  logger.info("Removing temp files")
+  for f in getTempFiles():
+    if os.path.exists(f):
       try:
-          os.unlink(tempFile)
-      except Exception:
-          traceback.print_exc()
-          logger.warn("Unable to remove: "+tempFile)
+        os.unlink(f)
+      except Exception as ex:
+        traceback.print_exc()
+        logger.warn("Unable to remove: %s, %s", f, ex)
+
+
+def stopAgent():
+
+  _clean()
   os._exit(0)
-  pass
\ No newline at end of file
+  pass
+
+
+def restartAgent():
+
+  _clean()
+
+  executable = sys.executable
+  args = sys.argv[:]
+  args.insert(0, executable)
+
+  logger.info("Restarting self: %s %s", executable, args)
+
+  os.execvp(executable, args)
+
+

Modified: incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/RepoInstaller.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/RepoInstaller.py?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/RepoInstaller.py (original)
+++ incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/RepoInstaller.py Sat Jan 19 01:57:20 2013
@@ -25,6 +25,7 @@ from shell import shellRunner
 from manifestGenerator import writeImports
 from pprint import pprint, pformat
 import ast
+import AmbariConfig
 import urlparse, urllib
 import re
 
@@ -60,10 +61,8 @@ class RepoInstaller:
     for repo in self.repoInfoList:
       repoFile = open(self.path + os.sep + repo['repoId'] + '-' + 
                       str(self.taskId) + PUPPET_EXT, 'w+')
-      importsfile = "imports.txt"
-      if self.config.has_option('puppet','imports_file'):
-        importsfile = self.config.get('puppet', 'imports_file')
-      writeImports(repoFile, self.modulesdir, importsfile)
+
+      writeImports(repoFile, self.modulesdir, AmbariConfig.imports)
       
       baseUrl = ''
       mirrorList = ''

Modified: incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/StatusCheck.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/StatusCheck.py?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/StatusCheck.py (original)
+++ incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/StatusCheck.py Sat Jan 19 01:57:20 2013
@@ -56,22 +56,17 @@ class StatusCheck:
     except OSError as e:
       logger.info(e.strerror + ' to ' + e.filename)
 
-  def __init__(self, path, mappingFilePath):
+  def __init__(self, path, serviceToPidDict):
     if not os.path.isdir(path):
       raise ValueError("Path argument must be valid directory")
 
-    if not os.path.exists(mappingFilePath):
-      raise IOError("File with services to pid mapping doesn't exist")
     self.path = path
-    self.mappingFilePath = mappingFilePath
+    self.serToPidDict = serviceToPidDict
     self.sh = shellRunner()
     self.pidFilesDict = {}
     self.listFiles(self.path)
 
 
-    with open(self.mappingFilePath) as fd:    
-      self.serToPidDict = dict(self.get_pair(line) for line in fd)
-
   def getIsLive(self, pidPath):
     if not pidPath:
       return False

Modified: incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/main.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/main.py?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/main.py (original)
+++ incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/main.py Sat Jan 19 01:57:20 2013
@@ -111,10 +111,28 @@ def main():
         os.kill(pid, signal.SIGKILL)
       os._exit(1)
 
+  # Check for ambari configuration file.
+  try:
+    config = AmbariConfig.config
+    if os.path.exists('/etc/ambari-agent/conf/ambari-agent.ini'):
+      config.read('/etc/ambari-agent/conf/ambari-agent.ini')
+      AmbariConfig.setConfig(config)
+    else:
+      raise Exception("No config found, use default")
+  except Exception, err:
+    logger.warn(err)
+
   # Check if there is another instance running
   if os.path.isfile(ProcessHelper.pidfile):
     print("%s already exists, exiting" % ProcessHelper.pidfile)
     sys.exit(1)
+  # check if ambari prefix exists
+  elif not os.path.isdir(config.get("agent", "prefix")):
+    msg = "Ambari prefix dir %s does not exists, can't continue" \
+          % config.get("agent", "prefix")
+    logger.error(msg)
+    print(msg)
+    sys.exit(1)
   else:
     # Daemonize current instance of Ambari Agent
     #retCode = createDaemon()
@@ -123,17 +141,6 @@ def main():
 
   credential = None
 
-  # Check for ambari configuration file.
-  try:
-    config = AmbariConfig.config
-    if os.path.exists('/etc/ambari-agent/conf/ambari-agent.ini'):
-      config.read('/etc/ambari-agent/conf/ambari-agent.ini')
-      AmbariConfig.setConfig(config)
-    else:
-      raise Exception("No config found, use default")
-  except Exception, err:
-    logger.warn(err)
-
   killstaleprocesses()
 
   server_url = 'https://' + config.get('server', 'hostname') + ':' + config.get('server', 'url_port')

Modified: incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/manifestGenerator.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/manifestGenerator.py?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/manifestGenerator.py (original)
+++ incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/manifestGenerator.py Sat Jan 19 01:57:20 2013
@@ -24,7 +24,7 @@ import logging
 from uuid import getnode as get_mac
 from shell import shellRunner
 from datetime import datetime
-from AmbariConfig import AmbariConfig
+import AmbariConfig
 
 
 logger = logging.getLogger()
@@ -37,16 +37,14 @@ non_global_configuration_types = ["hdfs-
                              "webhcat-site", "hdfs-exclude-file"]
 
 #read static imports from file and write them to manifest
-def writeImports(outputFile, modulesdir, inputFileName='imports.txt'):
-  inputFile = open(inputFileName, 'r')
+def writeImports(outputFile, modulesdir, importsList):
   logger.info("Modules dir is " + modulesdir)
   outputFile.write('#' + datetime.now().strftime('%d.%m.%Y %H:%M:%S') + os.linesep)
-  for line in inputFile:
+  for line in importsList:
     modulename = line.rstrip()
     line = "import '" + modulesdir + os.sep + modulename + "'" + os.linesep
     outputFile.write(line)
-    
-  inputFile.close()
+
 
 def generateManifest(parsedJson, fileName, modulesdir, ambariconfig):
   logger.info("JSON Received:")
@@ -77,13 +75,9 @@ def generateManifest(parsedJson, fileNam
   #writing manifest
   manifest = open(fileName, 'w')
   #Check for Ambari Config and make sure you pick the right imports file
-  importsfile = "imports.txt"
-  if ambariconfig.has_option('puppet', 'imports_file') :
-    importsfile = ambariconfig.get('puppet', 'imports_file')
     
-  logger.info("Using imports file " + importsfile)   
   #writing imports from external static file
-  writeImports(outputFile=manifest, modulesdir=modulesdir, inputFileName=importsfile)
+  writeImports(outputFile=manifest, modulesdir=modulesdir, importsList=AmbariConfig.imports)
   
   #writing nodes
   writeNodes(manifest, clusterHostInfo)
@@ -116,17 +110,6 @@ def generateManifest(parsedJson, fileNam
      
   manifest.close()
     
-  
-  #read dictionary
-def readDict(file, separator='='):
-  result = dict()
-  
-  for line in file :
-    dictTuple = line.partition(separator)
-    result[dictTuple[0].strip()] = dictTuple[2].strip()
-  
-  return result
-  
 
   #write nodes
 def writeNodes(outputFile, clusterHostInfo):
@@ -209,21 +192,9 @@ def writeNonGlobalConfigurations(outputF
 def writeTasks(outputFile, roles, ambariconfig, clusterHostInfo=None, 
                hostname="localhost"):
   #reading dictionaries
-  rolestoclass = "rolesToClass.dict"
-  if ambariconfig.has_option('puppet','roles_to_class'):
-    rolestoclass = ambariconfig.get('puppet', 'roles_to_class')
-                              
-  rolesToClassFile = open(rolestoclass, 'r')
-  rolesToClass = readDict(rolesToClassFile)
-  rolesToClassFile.close()
-
-  servicestates = "serviceStates.dict"
-  if ambariconfig.has_option('puppet','service_states'):
-    servicestates = ambariconfig.get('puppet', 'service_states')
-                              
-  serviceStatesFile =  open(servicestates, 'r')
-  serviceStates = readDict(serviceStatesFile)
-  serviceStatesFile.close()
+  rolesToClass = AmbariConfig.rolesToClass
+
+  serviceStates = AmbariConfig.serviceStates
 
   outputFile.write('node /default/ {\n ')
 

Modified: incubator/ambari/trunk/ambari-agent/src/test/python/TestController.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/test/python/TestController.py?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/test/python/TestController.py (original)
+++ incubator/ambari/trunk/ambari-agent/src/test/python/TestController.py Sat Jan 19 01:57:20 2013
@@ -1,4 +1,5 @@
 #!/usr/bin/env python2.6
+# -*- coding: utf-8 -*-
 
 '''
 Licensed to the Apache Software Foundation (ASF) under one
@@ -18,308 +19,316 @@ See the License for the specific languag
 limitations under the License.
 '''
 
-from unittest import TestCase
-from ambari_agent.Register import Register
-from ambari_agent.Controller import Controller
-from ambari_agent.Heartbeat import Heartbeat
-from ambari_agent.ActionQueue import ActionQueue
-from ambari_agent import AmbariConfig
-from ambari_agent.NetUtil import NetUtil
-import socket, ConfigParser, logging
-import os, pprint, json, sys
-from threading import Thread
-import time
-import Queue
-
-
-BAD_URL = 'http://localhost:54222/badurl/'
-
-logger = logging.getLogger()
-
-class TestController(TestCase):
-
-  def setUp(self):
-    logger.disabled = True
-    self.defaulttimeout = -1.0
-    if hasattr(socket, 'getdefaulttimeout'):
-      # get the default timeout on sockets
-      self.defaulttimeout = socket.getdefaulttimeout()
-
-
-  def tearDown(self):
-    if self.defaulttimeout is not None and self.defaulttimeout > 0 and hasattr(socket, 'setdefaulttimeout'):
-      # Set the default timeout on sockets
-      socket.setdefaulttimeout(self.defaulttimeout)
-    logger.disabled = False
-
-
-  def test_reregister_loop(self):
-    class ControllerMock(Controller):
-      def __init__(self, config, range=0):
-        self.repeatRegistration = False
-        self.range = range
-
-      callCounter = 0
-
-      def registerAndHeartbeat(self):
-        if self.callCounter < 3:
-          self.repeatRegistration = True;
-          self.callCounter += 1
-        else:
-          self.repeatRegistration = False;
-
-    config = ConfigParser.RawConfigParser()
-    mock = ControllerMock(config)
-    mock.run()
-    self.assertEquals(mock.callCounter, 3)
-    pass
-
-
-  def test_nonincremental_ids1(self):
-    '''
-      test to make sure nothing we act appropriately on getting non incremental reponse ids
-    '''
-    #timings adjustment
-    netutil = NetUtil()
-    netutil.HEARTBEAT_IDDLE_INTERVAL_SEC=0.05
-    netutil.HEARTBEAT_NOT_IDDLE_INTERVAL_SEC=0.05
-    #building fake responces
-    responces = Queue.Queue()
-    responce1 = {
-      'responseId':8,
-      'executionCommands':[],
-      'statusCommands':[],
-      'restartAgent':'False',
-      }
-    responce1 = json.dumps(responce1)
-
-    responce2 = {
-      'responseId':11,
-      'executionCommands':[],
-      'statusCommands':[],
-      'restartAgent':'False',
-      }
-    responce2 = json.dumps(responce2)
-    responces.put(responce1)
-    responces.put(responce2)
-    #building heartbeat object
-    testsPath = os.path.dirname(os.path.realpath(__file__))
-    dictPath = testsPath + os.sep + '..' + os.sep + '..' + os.sep + 'main' + os.sep + 'python' + os.sep + 'ambari_agent' + os.sep + 'servicesToPidNames.dict'
-    AmbariConfig.config.set('services','serviceToPidMapFile', dictPath)
-    actionQueue = ActionQueue(AmbariConfig.AmbariConfig().getConfig())
-    heartbeat = Heartbeat(actionQueue)
-    # testing controller with our heartbeat
-    controller = self.ControllerMock_fake_restartAgent(AmbariConfig.config, responces)
-    controller.heartbeat = heartbeat
-    controller.actionQueue = actionQueue
-    controller.logger = logger
-    controller.netutil = netutil
-    controller.heartbeatWithServer()
-    restarts = controller.restartCount
-    self.assertEquals(restarts, 1, "Agent should restart on non incremental responce ids")
-    pass
-
-
-  def test_nonincremental_ids2(self):
-    '''
-      test to make sure nothing we act appropriately on getting incremental reponse ids
-    '''
-    #timings adjustment
-    netutil = NetUtil()
-    netutil.HEARTBEAT_IDDLE_INTERVAL_SEC=0.05
-    netutil.HEARTBEAT_NOT_IDDLE_INTERVAL_SEC=0.05
-    #building fake responces
-    responces = Queue.Queue()
-    responce1 = {
-      'responseId':8,
-      'executionCommands':[],
-      'statusCommands':[],
-      'restartAgent':'False',
-      }
-    responce1 = json.dumps(responce1)
-
-    responce2 = {
-      'responseId':9,
-      'executionCommands':[],
-      'statusCommands':[],
-      'restartAgent':'False',
-      }
-    responce2 = json.dumps(responce2)
-    responces.put(responce1)
-    responces.put(responce2)
-    #building heartbeat object
-    testsPath = os.path.dirname(os.path.realpath(__file__))
-    dictPath = testsPath + os.sep + '..' + os.sep + '..' + os.sep + 'main' + os.sep + 'python' + os.sep + 'ambari_agent' + os.sep + 'servicesToPidNames.dict'
-    AmbariConfig.config.set('services','serviceToPidMapFile', dictPath)
-    actionQueue = ActionQueue(AmbariConfig.AmbariConfig().getConfig())
-    heartbeat = Heartbeat(actionQueue)
-    # testing controller with our heartbeat
-    controller = self.ControllerMock_fake_restartAgent(AmbariConfig.config, responces)
-    controller.heartbeat = heartbeat
-    controller.actionQueue = actionQueue
-    controller.logger = logger
-    controller.netutil = netutil
-    controller.heartbeatWithServer()
-    restarts = controller.restartCount
-    self.assertEquals(restarts, 0, "Agent should not restart on incremental responce ids")
-    pass
-
-
-  def test_reregister(self):
-    '''
-      test to make sure if we can get a re register command, we register with the server
-    '''
-    #timings adjustment
-    netutil = NetUtil()
-    netutil.HEARTBEAT_IDDLE_INTERVAL_SEC=0.05
-    netutil.HEARTBEAT_NOT_IDDLE_INTERVAL_SEC=0.05
-    #building fake responces
-    responces = Queue.Queue()
-    responce1 = {
-      'responseId':8,
-      'executionCommands':[],
-      'statusCommands':[],
-      'restartAgent':'true',
-      }
-    responce1 = json.dumps(responce1)
-    responces.put(responce1)
-    #building heartbeat object
-    testsPath = os.path.dirname(os.path.realpath(__file__))
-    dictPath = testsPath + os.sep + '..' + os.sep + '..' + os.sep + 'main' + os.sep + 'python' + os.sep + 'ambari_agent' + os.sep + 'servicesToPidNames.dict'
-    AmbariConfig.config.set('services','serviceToPidMapFile', dictPath)
-    actionQueue = ActionQueue(AmbariConfig.AmbariConfig().getConfig())
-    heartbeat = Heartbeat(actionQueue)
-    # testing controller with our heartbeat
-    controller = self.ControllerMock_fake_restartAgent(AmbariConfig.config, responces)
-    controller.heartbeat = heartbeat
-    controller.actionQueue = actionQueue
-    controller.logger = logger
-    controller.netutil = netutil
-    controller.heartbeatWithServer()
-    restarts = controller.restartCount
-    self.assertEquals(restarts, 1, "Agent should restart if we get a re register command")
-
-
-  def test_heartbeat_retries(self):
-    netutil = NetUtil()
-    netutil.HEARTBEAT_IDDLE_INTERVAL_SEC=0.05
-    netutil.HEARTBEAT_NOT_IDDLE_INTERVAL_SEC=0.05
-    #building heartbeat object
-    testsPath = os.path.dirname(os.path.realpath(__file__))
-    dictPath = testsPath + os.sep + '..' + os.sep + '..' + os.sep + 'main' + os.sep + 'python' + os.sep + 'ambari_agent' + os.sep + 'servicesToPidNames.dict'
-    AmbariConfig.config.set('services','serviceToPidMapFile', dictPath)
-    actionQueue = ActionQueue(AmbariConfig.AmbariConfig().getConfig())
-    heartbeat = Heartbeat(actionQueue)
-    # testing controller with our heartbeat and wrong url
-    controller = self.ControllerMock_failure_sendRequest(AmbariConfig.config)
-    controller.heartbeat = heartbeat
-    controller.actionQueue = actionQueue
-    controller.logger = logger
-    controller.netutil = netutil
-    thread = Thread(target =  controller.heartbeatWithServer)
-    thread.start()
-    time.sleep(0.5)
-
-    # I have to stop the thread anyway, so I'll check results later
-    threadWasAlive = thread.isAlive()
-    successfull_heartbits0 = controller.DEBUG_SUCCESSFULL_HEARTBEATS
-    heartbeat_retries0 = controller.DEBUG_HEARTBEAT_RETRIES
-    # Stopping thread
-    controller.DEBUG_STOP_HEARTBITTING = True
-    time.sleep(0.3)
-    # Checking results before thread stop
-    self.assertEquals(threadWasAlive, True, "Heartbeat should be alive now")
-    self.assertEquals(successfull_heartbits0, 0, "Heartbeat should not have any success")
-    self.assertEquals(heartbeat_retries0 > 1, True, "Heartbeat should retry connecting")
-    # Checking results after thread stop
-    self.assertEquals(thread.isAlive(), False, "Heartbeat should stop now")
-    self.assertEquals(controller.DEBUG_SUCCESSFULL_HEARTBEATS, 0, "Heartbeat should not have any success")
-
-
-  def test_status_command_on_registration(self):
-    '''
-    test to make sure if we get a status check command from the server, we are able to evaluate and register at the server
-    '''
-    #timings adjustment
-    netutil = NetUtil()
-    netutil.HEARTBEAT_IDDLE_INTERVAL_SEC=0.05
-    netutil.HEARTBEAT_NOT_IDDLE_INTERVAL_SEC=0.05
-    #building fake registration responce
-    responces = Queue.Queue()
-    responce1 = {
-      'response':'OK',
-      'responseId':8,
-      'statusCommands':[{
-        'clusterName' : "c1",
-        'commandType' : "STATUS_COMMAND",
-        'componentName' : "NAMENODE",
-        'serviceName' : "HDFS",
-        }],
-      }
-    responce1 = json.dumps(responce1)
-    responces.put(responce1)
-    #building heartbeat object
-    testsPath = os.path.dirname(os.path.realpath(__file__))
-    dictPath = testsPath + os.sep + '..' + os.sep + '..' + os.sep + 'main' + os.sep + 'python' + os.sep + 'ambari_agent' + os.sep + 'servicesToPidNames.dict'
-    AmbariConfig.config.set('services','serviceToPidMapFile', dictPath)
-    actionQueue = ActionQueue(AmbariConfig.AmbariConfig().getConfig())
-    heartbeat = Heartbeat(actionQueue)
-    # testing controller with our heartbeat
-    controller = self.ControllerMock_fake_restartAgent(AmbariConfig.config, responces)
-    controller.heartbeat = heartbeat
-    controller.actionQueue = actionQueue
-    controller.logger = logger
-    controller.netutil = netutil
-    controller.registerWithServer()
-    # If test does not hang, registration is successful
-    # So, checking queue
-    queue = controller.actionQueue.getCommandQueue()
-    self.assertEquals(queue.qsize(), 1, "Status command should be queued once")
-    # Checking parsed status command
-    command = queue.get()
-    self.assertEquals(command['clusterName'], 'c1')
-    self.assertEquals(command['commandType'], 'STATUS_COMMAND')
-    self.assertEquals(command['componentName'], 'NAMENODE')
-    self.assertEquals(command['serviceName'], 'HDFS')
-
-
-  class ControllerMock_fake_restartAgent(Controller):
-    def __init__(self, config, responces, range=3):
-      self.repeatRegistration = False
-      self.responces = responces
-      self.heartbeatUrl = "fakeurl"
-      self.registerUrl = "fakeregisterurl"
-      self.responseId = 7
-      self.register = Register()
-      self.range = range
-      
-    def restartAgent(self):
-      self.restartCount += 1
-      pass
-
-    restartCount = 0
-
-    def sendRequest(self, url, data):
-      responce = self.responces.get(block=False)
-      if self.responces.empty():
-        self.DEBUG_STOP_HEARTBITTING = True # Because we have nothing to reply next time
-      return responce
-
-
-  class ControllerMock_failure_sendRequest(Controller):
-    def __init__(self, config, range=0):
-      self.repeatRegistration = False
-      self.heartbeatUrl = "fakeurl"
-      self.registerUrl = "fakeregisterurl"
-      self.responseId = 7
-      self.register = Register()
-      self.range = range
-
-    def restartAgent(self):
-      self.restartCount += 1
-      pass
+import StringIO
+import unittest
+from ambari_agent import Controller
+import sys
+from mock.mock import patch, MagicMock, call
+
+
+class TestController(unittest.TestCase):
+
+  @patch("threading.Thread")
+  @patch("threading.Lock")
+  @patch("socket.gethostname")
+  @patch.object(Controller, "NetUtil")
+  def setUp(self, NetUtil_mock, hostnameMock, lockMock, threadMock):
+
+    Controller.logger = MagicMock()
+    hostnameMock.return_value = "test_hostname"
+    lockMock.return_value = MagicMock()
+    NetUtil_mock.return_value = MagicMock()
+
+    config = MagicMock()
+    config.get.return_value = "something"
+
+    self.controller = Controller.Controller(config)
+
+
+  @patch.object(Controller, "Heartbeat")
+  @patch.object(Controller, "Register")
+  @patch.object(Controller, "ActionQueue")
+  def test_start(self, ActionQueue_mock, Register_mock, Heartbeat_mock):
+
+    aq = MagicMock()
+    ActionQueue_mock.return_value = aq
+
+    self.controller.start()
+    self.assertTrue(ActionQueue_mock.called)
+    self.assertTrue(aq.start.called)
+    self.assertTrue(Register_mock.called)
+    self.assertTrue(Heartbeat_mock.called)
+
+  @patch("json.dumps")
+  @patch("json.loads")
+  @patch("time.sleep")
+  @patch("pprint.pformat")
+  @patch.object(Controller, "randint")
+  def test_registerWithServer(self, randintMock, pformatMock, sleepMock,
+                              loadsMock, dumpsMock):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    register = MagicMock()
+    self.controller.register = register
+
+    sendRequest = MagicMock()
+    self.controller.sendRequest = sendRequest
+
+    dumpsMock.return_value = "request"
+    response = {"responseId":1,}
+    loadsMock.return_value = response
+
+    self.assertEqual(response, self.controller.registerWithServer())
+
+    response["statusCommands"] = "commands"
+    self.controller.addToQueue = MagicMock(name="addToQueue")
+
+    self.assertEqual(response, self.controller.registerWithServer())
+    self.controller.addToQueue.assert_called_with("commands")
+
+    calls = []
+
+    def side_effect(*args):
+      if len(calls) == 0:
+        calls.append(1)
+        raise Exception("test")
+      return "request"
+
+    del response["statusCommands"]
+
+    dumpsMock.side_effect = side_effect
+    self.assertEqual(response, self.controller.registerWithServer())
+    self.assertTrue(randintMock.called)
+    self.assertTrue(sleepMock.called)
+
+    sys.stdout = sys.__stdout__
+
+    self.controller.sendRequest = Controller.Controller.sendRequest
+    self.controller.addToQueue = Controller.Controller.addToQueue
+
+
+  @patch("pprint.pformat")
+  def test_addToQueue(self, pformatMock):
+
+    actionQueue = MagicMock()
+    self.controller.actionQueue = actionQueue
+    self.controller.addToQueue(None)
+    self.assertFalse(actionQueue.put.called)
+    self.controller.addToQueue("cmd")
+    self.assertTrue(actionQueue.put.called)
+
+
+  @patch("urllib2.build_opener")
+  @patch("urllib2.install_opener")
+  def test_run(self, installMock, buildMock):
+
+    buildMock.return_value = "opener"
+    registerAndHeartbeat  = MagicMock("registerAndHeartbeat")
+    calls = []
+    def side_effect():
+      if len(calls) == 0:
+        self.controller.repeatRegistration = True
+      calls.append(1)
+    registerAndHeartbeat.side_effect = side_effect
+    self.controller.registerAndHeartbeat = registerAndHeartbeat
+
+    # repeat registration
+    self.controller.run()
+
+    self.assertTrue(buildMock.called)
+    installMock.called_once_with("opener")
+    self.assertEqual(2, registerAndHeartbeat.call_count)
+
+    # one call, +1
+    registerAndHeartbeat.side_effect = None
+    self.controller.run()
+    self.assertEqual(3, registerAndHeartbeat.call_count)
+
+
+  def test_heartbeatWithServer(self, installMock, buildMock):
+
+    registerAndHeartbeat = MagicMock(name="registerAndHeartbeat")
+
+    self.controller.registerAndHeartbeat = registerAndHeartbeat
+    self.controller.run()
+    self.assertTrue(installMock.called)
+    self.assertTrue(buildMock.called)
+    self.controller.registerAndHeartbeat.assert_called_once_with()
+
+    calls = []
+    def switchBool():
+      if len(calls) == 0:
+        self.controller.repeatRegistration = True
+        calls.append(1)
+      self.controller.repeatRegistration = False
+
+    registerAndHeartbeat.side_effect = switchBool
+    self.controller.run()
+    self.assertEqual(2, registerAndHeartbeat.call_count)
+
+    self.controller.registerAndHeartbeat = \
+      Controller.Controller.registerAndHeartbeat
+
+
+  @patch("time.sleep")
+  def test_registerAndHeartbeat(self, sleepMock):
+
+    registerWithServer = MagicMock(name="registerWithServer")
+    registerWithServer.return_value = {"response":"resp"}
+    self.controller.registerWithServer = registerWithServer
+    heartbeatWithServer = MagicMock(name="heartbeatWithServer")
+    self.controller.heartbeatWithServer = heartbeatWithServer
+
+    self.controller.registerAndHeartbeat()
+    registerWithServer.assert_called_once_with()
+    heartbeatWithServer.assert_called_once_with()
+
+    self.controller.registerWithServer = \
+      Controller.Controller.registerWithServer
+    self.controller.heartbeatWithServer = \
+      Controller.Controller.registerWithServer
+
+
+  @patch.object(Controller, "ProcessHelper")
+  def test_restartAgent(self, ProcessHelper_mock):
+
+    self.controller.restartAgent()
+    self.assertTrue(ProcessHelper_mock.restartAgent.called)
+
+
+  @patch("urllib2.Request")
+  @patch.object(Controller, "security")
+  def test_sendRequest(self, security_mock, requestMock):
+
+    conMock = MagicMock()
+    conMock.request.return_value = "response"
+    security_mock.CachedHTTPSConnection.return_value = conMock
+    url = "url"
+    data = "data"
+    requestMock.return_value = "request"
+
+    self.controller.cachedconnect = None
+
+    self.assertEqual("response", self.controller.sendRequest(url, data))
+    security_mock.CachedHTTPSConnection.assert_called_once_with(
+      self.controller.config)
+    requestMock.called_once_with(url, data,
+      {'Content-Type': 'application/json'})
+
+
+  @patch("time.sleep")
+  @patch("json.loads")
+  @patch("json.dumps")
+  def test_heartbeatWithServer(self, dumpsMock, loadsMock, sleepMock):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    hearbeat = MagicMock()
+    self.controller.heartbeat = hearbeat
+
+    dumpsMock.return_value = "data"
+
+    sendRequest = MagicMock(name="sendRequest")
+    self.controller.sendRequest = sendRequest
+
+    self.controller.responseId = 1
+    response = {"responseId":"2", "restartAgent":"false"}
+    loadsMock.return_value = response
+
+    def one_heartbeat(*args, **kwargs):
+      self.controller.DEBUG_STOP_HEARTBITTING = True
+      return "data"
+
+    sendRequest.side_effect = one_heartbeat
+
+    actionQueue = MagicMock()
+    actionQueue.isIdle.return_value = True
+
+    # one successful request, after stop
+    self.controller.actionQueue = actionQueue
+    self.controller.heartbeatWithServer()
+    self.assertTrue(sendRequest.called)
+
+    calls = []
+    def retry(*args, **kwargs):
+      if len(calls) == 0:
+        calls.append(1)
+        response["responseId"] = "3"
+        raise Exception()
+      if len(calls) > 0:
+        self.controller.DEBUG_STOP_HEARTBITTING = True
+      return "data"
+
+    # exception, retry, successful and stop
+    sendRequest.side_effect = retry
+    self.controller.DEBUG_STOP_HEARTBITTING = False
+    self.controller.heartbeatWithServer()
+
+    self.assertEqual(1, self.controller.DEBUG_SUCCESSFULL_HEARTBEATS)
+
+    # retry registration
+    response["registrationCommand"] = "true"
+    sendRequest.side_effect = one_heartbeat
+    self.controller.DEBUG_STOP_HEARTBITTING = False
+    self.controller.heartbeatWithServer()
+
+    self.assertTrue(self.controller.repeatRegistration)
+
+    # wrong responseId => restart
+    response = {"responseId":"2", "restartAgent":"false"}
+    loadsMock.return_value = response
+
+    restartAgent = MagicMock(name="restartAgent")
+    self.controller.restartAgent = restartAgent
+    self.controller.DEBUG_STOP_HEARTBITTING = False
+    self.controller.heartbeatWithServer()
+
+    restartAgent.assert_called_once_with()
+
+    # executionCommands, statusCommands
+    self.controller.responseId = 1
+    addToQueue = MagicMock(name="addToQueue")
+    self.controller.addToQueue = addToQueue
+    response["executionCommands"] = "executionCommands"
+    response["statusCommands"] = "statusCommands"
+    self.controller.DEBUG_STOP_HEARTBITTING = False
+    self.controller.heartbeatWithServer()
+
+    addToQueue.assert_has_calls([call("executionCommands"),
+                                 call("statusCommands")])
+
+    # restartAgent command
+    self.controller.responseId = 1
+    self.controller.DEBUG_STOP_HEARTBITTING = False
+    response["restartAgent"] = "true"
+    restartAgent = MagicMock(name="restartAgent")
+    self.controller.restartAgent = restartAgent
+    self.controller.heartbeatWithServer()
+
+    restartAgent.assert_called_once_with()
+
+    # actionQueue not idle
+    self.controller.responseId = 1
+    self.controller.DEBUG_STOP_HEARTBITTING = False
+    actionQueue.isIdle.return_value = False
+    response["restartAgent"] = "false"
+    self.controller.heartbeatWithServer()
+
+    sleepMock.assert_called_with(
+      self.controller.netutil.HEARTBEAT_NOT_IDDLE_INTERVAL_SEC)
+
+    sys.stdout = sys.__stdout__
+    self.controller.sendRequest = Controller.Controller.sendRequest
+    self.controller.sendRequest = Controller.Controller.addToQueue
+
+
+if __name__ == "__main__":
+
+  unittest.main(verbosity=2)
+
+
 
-    restartCount = 0
 
-    def sendRequest(self, url, data):
-      raise Exception("Fake exception")

Modified: incubator/ambari/trunk/ambari-agent/src/test/python/TestHeartbeat.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/test/python/TestHeartbeat.py?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/test/python/TestHeartbeat.py (original)
+++ incubator/ambari/trunk/ambari-agent/src/test/python/TestHeartbeat.py Sat Jan 19 01:57:20 2013
@@ -29,12 +29,8 @@ import time
 
 class TestHeartbeat(TestCase):
 
-  def setUp(self):
-    testsPath = os.path.dirname(os.path.realpath(__file__))
-    self.dictPath = testsPath + os.sep + '..' + os.sep + '..' + os.sep + 'main' + os.sep + 'python' + os.sep + 'ambari_agent' + os.sep + 'servicesToPidNames.dict'
 
   def test_build(self):
-    AmbariConfig.config.set('services','serviceToPidMapFile', self.dictPath)
     actionQueue = ActionQueue(AmbariConfig.AmbariConfig().getConfig())
     heartbeat = Heartbeat(actionQueue)
     result = heartbeat.build(100)
@@ -52,7 +48,6 @@ class TestHeartbeat(TestCase):
 
 
   def test_heartbeat_with_status(self):
-    AmbariConfig.config.set('services','serviceToPidMapFile', self.dictPath)
     actionQueue = ActionQueue(AmbariConfig.AmbariConfig().getConfig())
     heartbeat = Heartbeat(actionQueue)
     statusCommand = {
@@ -70,7 +65,6 @@ class TestHeartbeat(TestCase):
     self.assertEquals(len(result['componentStatus']) > 0, True, 'Heartbeat should contain status of HDFS components')
 
   def test_heartbeat_with_status_multiple(self):
-    AmbariConfig.config.set('services','serviceToPidMapFile', self.dictPath)
     actionQueue = ActionQueue(AmbariConfig.AmbariConfig().getConfig())
     actionQueue.IDLE_SLEEP_TIME = 0.01
     heartbeat = Heartbeat(actionQueue)
@@ -97,7 +91,6 @@ class TestHeartbeat(TestCase):
     self.assertEquals(max_number_of_status_entries == NUMBER_OF_COMPONENTS, True)
 
   def test_heartbeat_with_task_in_progress(self):
-    AmbariConfig.config.set('services','serviceToPidMapFile', self.dictPath)
     actionQueue = ActionQueue(AmbariConfig.AmbariConfig().getConfig())
     actionQueue.commandInProgress= {
       'role' : "role",

Modified: incubator/ambari/trunk/ambari-agent/src/test/python/TestLiveStatus.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/test/python/TestLiveStatus.py?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/test/python/TestLiveStatus.py (original)
+++ incubator/ambari/trunk/ambari-agent/src/test/python/TestLiveStatus.py Sat Jan 19 01:57:20 2013
@@ -26,9 +26,6 @@ import os
 
 class TestLiveStatus(TestCase):
   def test_build(self):
-    testsPath = os.path.dirname(os.path.realpath(__file__))
-    dictPath = testsPath + os.sep + '..' + os.sep + '..' + os.sep + 'main' + os.sep + 'python' + os.sep + 'ambari_agent' + os.sep + 'servicesToPidNames.dict'
-    AmbariConfig.config.set('services','serviceToPidMapFile', dictPath)
     for component in LiveStatus.COMPONENTS:
       livestatus = LiveStatus('', component['serviceName'], component['componentName'])
       result = livestatus.build()

Added: incubator/ambari/trunk/ambari-agent/src/test/python/TestManifestGenerator.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/test/python/TestManifestGenerator.py?rev=1435460&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/test/python/TestManifestGenerator.py (added)
+++ incubator/ambari/trunk/ambari-agent/src/test/python/TestManifestGenerator.py Sat Jan 19 01:57:20 2013
@@ -0,0 +1,76 @@
+#!/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
+
+from unittest import TestCase
+from numpy.testing import assert_
+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):
+    self.dir = tempfile.mkdtemp()
+    self.config = AmbariConfig()
+    jsonCommand = file('../../main/python/ambari_agent/test.json').read()
+    self.parsedJson = json.loads(jsonCommand)
+
+    pass
+
+  def tearDown(self):
+    shutil.rmtree(self.dir)
+    pass
+
+
+  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]
+    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()
+
+    pass
\ No newline at end of file

Modified: incubator/ambari/trunk/ambari-agent/src/test/python/TestNetUtil.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/test/python/TestNetUtil.py?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/test/python/TestNetUtil.py (original)
+++ incubator/ambari/trunk/ambari-agent/src/test/python/TestNetUtil.py Sat Jan 19 01:57:20 2013
@@ -18,79 +18,60 @@ See the License for the specific languag
 limitations under the License.
 '''
 
-from unittest import TestCase
-from ambari_agent.ServerStatus import ServerStatus
-from ambari_agent.NetUtil import NetUtil
-import ambari_agent.main
-from threading import Thread
-import time
-from ambari_agent.Heartbeat import Heartbeat
-from ambari_agent.ActionQueue import ActionQueue
-from ambari_agent import AmbariConfig
-import socket
-import os
-import logging
-from ambari_agent.Controller import Controller
-import socket
-
-NON_EXISTING_DOMAIN = 'non-existing-domain43342432.com'
-BAD_URL = 'http://localhost:54222/badurl/'
-
-class TestNetUtil(TestCase):
-
-  logger = logging.getLogger()
-
-  def setUp(self):
-    self.logger.info("Starting TestConnectionRetries test")
-    self.logger.disabled = True
-    self.defaulttimeout = -1.0
-    if hasattr(socket, 'getdefaulttimeout'):
-      # get the default timeout on sockets
-      self.defaulttimeout = socket.getdefaulttimeout()
-
-
-# Test was failing: BUG-3112
-#  def test_url_checks(self):
-#    netutil = NetUtil()
-#    if hasattr(socket, 'setdefaulttimeout'):
-#      # Set the default timeout on sockets
-#      socket.setdefaulttimeout(1)
-#    self.assertEquals(netutil.checkURL('http://' + NON_EXISTING_DOMAIN), False, "Not existing domain")
-#    self.assertEquals(netutil.checkURL(BAD_URL), False, "Bad url")
-#    self.assertEquals(netutil.checkURL('http://192.168.253.177'), False, "Not reachable IP")
-#    if hasattr(socket, 'setdefaulttimeout'):
-#      # Set the default timeout on sockets
-#      socket.setdefaulttimeout(20)
-#    self.assertEquals(netutil.checkURL('http://www.iana.org/domains/example/'), True, "Good url - HTTP code 200")
-#    self.assertEquals(netutil.checkURL('https://www.iana.org/domains/example/'), True, "Good HTTPS url - HTTP code 200")
-
-
-  def test_registration_retries(self):
-    netutil = NetUtil()
-    netutil.CONNECT_SERVER_RETRY_INTERVAL_SEC=0.05
-    retries = netutil.try_to_connect(BAD_URL, 3)
-    self.assertEquals(retries, 3)
-
-  def test_infinit_registration_retries(self):
-    netutil = NetUtil()
-    netutil.CONNECT_SERVER_RETRY_INTERVAL_SEC=0.05
-    thread = Thread(target = netutil.try_to_connect, args = (BAD_URL, -1))
-    thread.start()
-    time.sleep(0.25)
-    # I have to stop the thread anyway, so I'll check results later
-    threadWasAlive = thread.isAlive()
-    netutil.DEBUG_STOP_RETRIES_FLAG = True
-    time.sleep(0.5)
-    # Checking results before thread stop
-    self.assertEquals(threadWasAlive, True, "Thread should still be retrying to connect")
-    # Checking results after thread stop
-    self.assertEquals(thread.isAlive(), False, "Thread should stop now")
-
-  def tearDown(self):
-    if self.defaulttimeout is not None and self.defaulttimeout > 0 and hasattr(socket, 'setdefaulttimeout'):
-      # Set the default timeout on sockets
-      socket.setdefaulttimeout(self.defaulttimeout)
-    self.logger.disabled = False
-    self.logger.info("Finished TestConnectionRetries test")
+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))
 
 

Added: incubator/ambari/trunk/ambari-agent/src/test/python/TestProcessHelper.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/test/python/TestProcessHelper.py?rev=1435460&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/test/python/TestProcessHelper.py (added)
+++ incubator/ambari/trunk/ambari-agent/src/test/python/TestProcessHelper.py Sat Jan 19 01:57:20 2013
@@ -0,0 +1,68 @@
+#!/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]))
+

Added: incubator/ambari/trunk/ambari-agent/src/test/python/TestRepoInstaller.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/test/python/TestRepoInstaller.py?rev=1435460&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/test/python/TestRepoInstaller.py (added)
+++ incubator/ambari/trunk/ambari-agent/src/test/python/TestRepoInstaller.py Sat Jan 19 01:57:20 2013
@@ -0,0 +1,52 @@
+#!/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.RepoInstaller import RepoInstaller
+import tempfile
+import json
+import shutil
+from ambari_agent.AmbariConfig import AmbariConfig
+from mock.mock import patch, MagicMock, call
+
+class TestRepoInstaller(TestCase):
+
+  def setUp(self):
+    self.dir = tempfile.mkdtemp()
+    jsonCommand = file('../../main/python/ambari_agent/test.json').read()
+    self.parsedJson= json.loads(jsonCommand)
+    self.config = AmbariConfig().getConfig()
+    self.repoInstaller = RepoInstaller(self.parsedJson, self.dir, '../../main/puppet/modules', 1, self.config)
+
+    pass
+
+  def tearDown(self):
+    shutil.rmtree(self.dir)
+    pass
+
+
+  @patch.object(RepoInstaller, 'prepareReposInfo')
+  @patch.object(RepoInstaller, 'generateFiles')
+  def testInstallRepos(self, generateFilesMock, prepareReposInfoMock):
+    result = self.repoInstaller.installRepos()
+    self.assertTrue(prepareReposInfoMock.called)
+    self.assertTrue(generateFilesMock.called)
+    print('installRepos result: ' + result.__str__())
+    pass

Modified: incubator/ambari/trunk/ambari-agent/src/test/python/TestStatusCheck.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/test/python/TestStatusCheck.py?rev=1435460&r1=1435459&r2=1435460&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/test/python/TestStatusCheck.py (original)
+++ incubator/ambari/trunk/ambari-agent/src/test/python/TestStatusCheck.py Sat Jan 19 01:57:20 2013
@@ -46,9 +46,10 @@ DEAD_PID=0
 class TestStatusCheck(TestCase):
 
   def setUp(self):
+
     self.tmpdir = tempfile.mkdtemp()
-    self.tmpdict = tempfile.NamedTemporaryFile(dir=self.tmpdir)
-    self.tmpdict = open(self.tmpdir + os.sep + MAPPING_FILE_NAME, 'w')
+    self.tmpdict = { COMPONENT_LIVE : COMPONENT_LIVE_PID,
+                     COMPONENT_DEAD : COMPONENT_DEAD_PID}
 
     self.sh = shellRunner()
     
@@ -56,10 +57,6 @@ class TestStatusCheck(TestCase):
     p = subprocess.Popen([COMPONENT_LIVE_CMD], stdout=subprocess.PIPE, 
                          stderr=subprocess.PIPE, shell=True, close_fds=True)
 
-    #Write mapping for pid files for both live and dead process
-    self.tmpdict.write(COMPONENT_LIVE + '=' + COMPONENT_LIVE_PID + os.linesep)
-    self.tmpdict.write(COMPONENT_DEAD + '=' + COMPONENT_DEAD_PID + os.linesep)
-    self.tmpdict.close()
 
     #Write pid of live process to file
     live_pid_file = open(self.tmpdir + os.sep + COMPONENT_LIVE_PID, 'w')
@@ -73,12 +70,11 @@ class TestStatusCheck(TestCase):
     dead_pid_file.close()
 
     #Init status checker
-    self.statusCheck = StatusCheck(self.tmpdir, self.tmpdict.name)
+    self.statusCheck = StatusCheck(self.tmpdir, self.tmpdict)
 
   # Ensure that status checker throws exceptions on invalid params
   def test_exceptions(self):
     self.assertRaises(ValueError,StatusCheck,"tmp","tmp")
-    self.assertRaises(IOError, StatusCheck,self.tmpdir,"tmp")
 
   # Ensure that status checker return True for running process
   def test_live(self):

Added: incubator/ambari/trunk/ambari-common/src/test/python/mock/LICENSE.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-common/src/test/python/mock/LICENSE.txt?rev=1435460&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-common/src/test/python/mock/LICENSE.txt (added)
+++ incubator/ambari/trunk/ambari-common/src/test/python/mock/LICENSE.txt Sat Jan 19 01:57:20 2013
@@ -0,0 +1,26 @@
+Copyright (c) 2003-2012, Michael Foord
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Added: incubator/ambari/trunk/ambari-common/src/test/python/mock/MANIFEST.in
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-common/src/test/python/mock/MANIFEST.in?rev=1435460&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-common/src/test/python/mock/MANIFEST.in (added)
+++ incubator/ambari/trunk/ambari-common/src/test/python/mock/MANIFEST.in Sat Jan 19 01:57:20 2013
@@ -0,0 +1,2 @@
+include LICENSE.txt tox.ini tests/*.py
+recursive-include docs *.txt *.py *.png *.css *.html *.js

Added: incubator/ambari/trunk/ambari-common/src/test/python/mock/README.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-common/src/test/python/mock/README.txt?rev=1435460&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-common/src/test/python/mock/README.txt (added)
+++ incubator/ambari/trunk/ambari-common/src/test/python/mock/README.txt Sat Jan 19 01:57:20 2013
@@ -0,0 +1,179 @@
+mock is a library for testing in Python. It allows you to replace parts of
+your system under test with mock objects and make assertions about how they
+have been used.
+
+mock is now part of the Python standard library, available as `unittest.mock
+<http://docs.python.org/py3k/library/unittest.mock.html#module-unittest.mock>`_
+in Python 3.3 onwards.
+
+mock provides a core `MagicMock` class removing the need to create a host of
+stubs throughout your test suite. After performing an action, you can make
+assertions about which methods / attributes were used and arguments they were
+called with. You can also specify return values and set needed attributes in
+the normal way.
+
+mock is tested on Python versions 2.5-2.7 and Python 3. mock is also tested
+with the latest versions of Jython and pypy.
+
+The mock module also provides utility functions / objects to assist with
+testing, particularly monkey patching.
+
+* `PDF documentation for 1.0.1
+  <http://www.voidspace.org.uk/downloads/mock-1.0.1.pdf>`_
+* `mock on google code (repository and issue tracker)
+  <http://code.google.com/p/mock/>`_
+* `mock documentation
+  <http://www.voidspace.org.uk/python/mock/>`_
+* `mock on PyPI <http://pypi.python.org/pypi/mock/>`_
+* `Mailing list (testing-in-python@lists.idyll.org)
+  <http://lists.idyll.org/listinfo/testing-in-python>`_
+
+Mock is very easy to use and is designed for use with
+`unittest <http://pypi.python.org/pypi/unittest2>`_. Mock is based on
+the 'action -> assertion' pattern instead of 'record -> replay' used by many
+mocking frameworks. See the `mock documentation`_ for full details.
+
+Mock objects create all attributes and methods as you access them and store
+details of how they have been used. You can configure them, to specify return
+values or limit what attributes are available, and then make assertions about
+how they have been used::
+
+    >>> from mock import Mock
+    >>> real = ProductionClass()
+    >>> real.method = Mock(return_value=3)
+    >>> real.method(3, 4, 5, key='value')
+    3
+    >>> real.method.assert_called_with(3, 4, 5, key='value')
+
+`side_effect` allows you to perform side effects, return different values or
+raise an exception when a mock is called::
+
+   >>> mock = Mock(side_effect=KeyError('foo'))
+   >>> mock()
+   Traceback (most recent call last):
+    ...
+   KeyError: 'foo'
+   >>> values = {'a': 1, 'b': 2, 'c': 3}
+   >>> def side_effect(arg):
+   ...     return values[arg]
+   ...
+   >>> mock.side_effect = side_effect
+   >>> mock('a'), mock('b'), mock('c')
+   (3, 2, 1)
+   >>> mock.side_effect = [5, 4, 3, 2, 1]
+   >>> mock(), mock(), mock()
+   (5, 4, 3)
+
+Mock has many other ways you can configure it and control its behaviour. For
+example the `spec` argument configures the mock to take its specification from
+another object. Attempting to access attributes or methods on the mock that
+don't exist on the spec will fail with an `AttributeError`.
+
+The `patch` decorator / context manager makes it easy to mock classes or
+objects in a module under test. The object you specify will be replaced with a
+mock (or other object) during the test and restored when the test ends::
+
+    >>> from mock import patch
+    >>> @patch('test_module.ClassName1')
+    ... @patch('test_module.ClassName2')
+    ... def test(MockClass2, MockClass1):
+    ...     test_module.ClassName1()
+    ...     test_module.ClassName2()
+
+    ...     assert MockClass1.called
+    ...     assert MockClass2.called
+    ...
+    >>> test()
+
+.. note::
+
+   When you nest patch decorators the mocks are passed in to the decorated
+   function in the same order they applied (the normal *python* order that
+   decorators are applied). This means from the bottom up, so in the example
+   above the mock for `test_module.ClassName2` is passed in first.
+
+   With `patch` it matters that you patch objects in the namespace where they
+   are looked up. This is normally straightforward, but for a quick guide
+   read `where to patch
+   <http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch>`_.
+
+As well as a decorator `patch` can be used as a context manager in a with
+statement::
+
+    >>> with patch.object(ProductionClass, 'method') as mock_method:
+    ...     mock_method.return_value = None
+    ...     real = ProductionClass()
+    ...     real.method(1, 2, 3)
+    ...
+    >>> mock_method.assert_called_once_with(1, 2, 3)
+
+There is also `patch.dict` for setting values in a dictionary just during the
+scope of a test and restoring the dictionary to its original state when the
+test ends::
+
+   >>> foo = {'key': 'value'}
+   >>> original = foo.copy()
+   >>> with patch.dict(foo, {'newkey': 'newvalue'}, clear=True):
+   ...     assert foo == {'newkey': 'newvalue'}
+   ...
+   >>> assert foo == original
+
+Mock supports the mocking of Python magic methods. The easiest way of
+using magic methods is with the `MagicMock` class. It allows you to do
+things like::
+
+    >>> from mock import MagicMock
+    >>> mock = MagicMock()
+    >>> mock.__str__.return_value = 'foobarbaz'
+    >>> str(mock)
+    'foobarbaz'
+    >>> mock.__str__.assert_called_once_with()
+
+Mock allows you to assign functions (or other Mock instances) to magic methods
+and they will be called appropriately. The MagicMock class is just a Mock
+variant that has all of the magic methods pre-created for you (well - all the
+useful ones anyway).
+
+The following is an example of using magic methods with the ordinary Mock
+class::
+
+    >>> from mock import Mock
+    >>> mock = Mock()
+    >>> mock.__str__ = Mock(return_value = 'wheeeeee')
+    >>> str(mock)
+    'wheeeeee'
+
+For ensuring that the mock objects your tests use have the same api as the
+objects they are replacing, you can use "auto-speccing". Auto-speccing can
+be done through the `autospec` argument to patch, or the `create_autospec`
+function. Auto-speccing creates mock objects that have the same attributes
+and methods as the objects they are replacing, and any functions and methods
+(including constructors) have the same call signature as the real object.
+
+This ensures that your mocks will fail in the same way as your production
+code if they are used incorrectly::
+
+   >>> from mock import create_autospec
+   >>> def function(a, b, c):
+   ...     pass
+   ...
+   >>> mock_function = create_autospec(function, return_value='fishy')
+   >>> mock_function(1, 2, 3)
+   'fishy'
+   >>> mock_function.assert_called_once_with(1, 2, 3)
+   >>> mock_function('wrong arguments')
+   Traceback (most recent call last):
+    ...
+   TypeError: <lambda>() takes exactly 3 arguments (1 given)
+
+`create_autospec` can also be used on classes, where it copies the signature of
+the `__init__` method, and on callable objects where it copies the signature of
+the `__call__` method.
+
+The distribution contains tests and documentation. The tests require
+`unittest2 <http://pypi.python.org/pypi/unittest2>`_ to run on Python 2.5, 2.6
+or 3.1. For Python 2.7 and 3.2 they can be run with
+`python -m unittest discover`.
+
+Docs from the in-development version of `mock` can be found at
+`mock.readthedocs.org <http://mock.readthedocs.org>`_.

Added: incubator/ambari/trunk/ambari-common/src/test/python/mock/__init__.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-common/src/test/python/mock/__init__.py?rev=1435460&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-common/src/test/python/mock/__init__.py (added)
+++ incubator/ambari/trunk/ambari-common/src/test/python/mock/__init__.py Sat Jan 19 01:57:20 2013
@@ -0,0 +1 @@
+__author__ = 'Michael Foord'

Added: incubator/ambari/trunk/ambari-common/src/test/python/mock/extendmock.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-common/src/test/python/mock/extendmock.py?rev=1435460&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-common/src/test/python/mock/extendmock.py (added)
+++ incubator/ambari/trunk/ambari-common/src/test/python/mock/extendmock.py Sat Jan 19 01:57:20 2013
@@ -0,0 +1 @@
+# merged into mock.py in Mock 0.7