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/02/04 03:24:02 UTC

svn commit: r1442010 [14/29] - in /incubator/ambari/branches/branch-1.2: ./ ambari-agent/ ambari-agent/conf/unix/ ambari-agent/src/examples/ ambari-agent/src/main/puppet/modules/hdp-ganglia/files/ ambari-agent/src/main/puppet/modules/hdp-ganglia/manife...

Modified: incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/controller/jmx/JMXPropertyProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/controller/jmx/JMXPropertyProviderTest.java?rev=1442010&r1=1442009&r2=1442010&view=diff
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/controller/jmx/JMXPropertyProviderTest.java (original)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/controller/jmx/JMXPropertyProviderTest.java Mon Feb  4 02:23:55 2013
@@ -26,7 +26,8 @@ import org.junit.Assert;
 import org.junit.Test;
 
 import java.util.Collections;
-import java.util.Map;
+import java.util.HashSet;
+import java.util.Set;
 
 /**
  * JMX property provider tests.
@@ -58,12 +59,15 @@ public class JMXPropertyProviderTest {
 
     Assert.assertEquals(1, propertyProvider.populateResources(Collections.singleton(resource), request, null).size());
 
-    Assert.assertEquals(propertyProvider.getSpec("ec2-50-17-129-192.compute-1.amazonaws.com:50070"), streamProvider.getLastSpec());
+    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-0e-34-e1.compute-1.internal:50070"), streamProvider.getLastSpec());
 
     // see test/resources/hdfs_namenode_jmx.json for values
-    Assert.assertEquals(1084287,  resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
-    Assert.assertEquals(173,      resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/dfs/namenode", "CreateFileOps")));
-    Assert.assertEquals(405.8686, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "memHeapUsedM")));
+    Assert.assertEquals(13670605,  resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
+    Assert.assertEquals(28,      resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/dfs/namenode", "CreateFileOps")));
+    Assert.assertEquals(1006632960, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax")));
+    Assert.assertEquals(473433016, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryUsed")));
+    Assert.assertEquals(136314880, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryMax")));
+    Assert.assertEquals(23634400, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryUsed")));
 
 
     // datanode
@@ -77,11 +81,14 @@ public class JMXPropertyProviderTest {
 
     propertyProvider.populateResources(Collections.singleton(resource), request, null);
 
-    Assert.assertEquals(propertyProvider.getSpec("ec2-23-23-71-42.compute-1.amazonaws.com:50075"), streamProvider.getLastSpec());
+    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-14-ee-b3.compute-1.internal:50075"), streamProvider.getLastSpec());
 
     // see test/resources/hdfs_datanode_jmx.json for values
-    Assert.assertEquals(0,  resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
-    Assert.assertEquals(16.870667, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "memHeapUsedM")));
+    Assert.assertEquals(856,  resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
+    Assert.assertEquals(954466304, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax")));
+    Assert.assertEquals(9772616, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryUsed")));
+    Assert.assertEquals(136314880, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryMax")));
+    Assert.assertEquals(21933376, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryUsed")));
 
 
     // jobtracker
@@ -90,31 +97,88 @@ public class JMXPropertyProviderTest {
     resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-14-ee-b3.compute-1.internal");
     resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "JOBTRACKER");
 
-    // only ask for one property
-    request = PropertyHelper.getReadRequest(Collections.singleton(PropertyHelper.getPropertyId("metrics/jvm", "threadsWaiting")));
+    // only ask for specific properties
+    Set<String> properties = new HashSet<String>();
+    properties.add(PropertyHelper.getPropertyId("metrics/jvm", "threadsWaiting"));
+    properties.add(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax"));
+    properties.add(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryUsed"));
+    properties.add(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryMax"));
+    properties.add(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryUsed"));
+    request = PropertyHelper.getReadRequest(properties);
 
     propertyProvider.populateResources(Collections.singleton(resource), request, null);
 
-    Assert.assertEquals(propertyProvider.getSpec("ec2-23-23-71-42.compute-1.amazonaws.com:50030"), streamProvider.getLastSpec());
+    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-14-ee-b3.compute-1.internal:50030"), streamProvider.getLastSpec());
 
     // see test/resources/mapreduce_jobtracker_jmx.json for values
-    // resource should now contain 3 properties... host name, component name, and jvm.threadsWaiting (from request)
-    Assert.assertEquals(3, PropertyHelper.getProperties(resource).size());
+    Assert.assertEquals(7, PropertyHelper.getProperties(resource).size());
     Assert.assertEquals(59, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "threadsWaiting")));
+    Assert.assertEquals(1052770304, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax")));
+    Assert.assertEquals(43580400, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryUsed")));
+    Assert.assertEquals(136314880, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryMax")));
+    Assert.assertEquals(29602888, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryUsed")));
+
+    Assert.assertNull(resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "gcCount")));
+
+    // tasktracker
+    resource = new ResourceImpl(Resource.Type.HostComponent);
+
+    resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-14-ee-b3.compute-1.internal");
+    resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "TASKTRACKER");
+
+    // only ask for specific properties
+    properties = new HashSet<String>();
+    properties.add(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax"));
+    properties.add(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryUsed"));
+    properties.add(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryMax"));
+    properties.add(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryUsed"));
+    request = PropertyHelper.getReadRequest(properties);
+
+    propertyProvider.populateResources(Collections.singleton(resource), request, null);
+
+    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-14-ee-b3.compute-1.internal:50060"), streamProvider.getLastSpec());
+
+    Assert.assertEquals(6, PropertyHelper.getProperties(resource).size());
+    Assert.assertEquals(954466304, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax")));
+    Assert.assertEquals(18330984, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryUsed")));
+    Assert.assertEquals(136314880, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryMax")));
+    Assert.assertEquals(24235104, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryUsed")));
+
     Assert.assertNull(resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "gcCount")));
-  }
 
+    // hbase master
+    resource = new ResourceImpl(Resource.Type.HostComponent);
+
+    resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-14-ee-b3.compute-1.internal");
+    resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "HBASE_MASTER");
+
+    // only ask for specific properties
+    properties = new HashSet<String>();
+    properties.add(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax"));
+    properties.add(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryUsed"));
+    properties.add(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryMax"));
+    properties.add(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryUsed"));
+    properties.add(PropertyHelper.getPropertyId("metrics/load", "AverageLoad"));
+    request = PropertyHelper.getReadRequest(properties);
+
+    propertyProvider.populateResources(Collections.singleton(resource), request, null);
+
+    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-14-ee-b3.compute-1.internal:60010"), streamProvider.getLastSpec());
 
+    Assert.assertEquals(7, PropertyHelper.getProperties(resource).size());
+    Assert.assertEquals(1069416448, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax")));
+    Assert.assertEquals(4806976, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryUsed")));
+    Assert.assertEquals(136314880, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryMax")));
+    Assert.assertEquals(28971240, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryUsed")));
+    Assert.assertEquals(3.0, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/load", "AverageLoad")));
+
+    Assert.assertNull(resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "gcCount")));
+  }
 
   private static class TestJMXHostProvider implements JMXHostProvider {
     @Override
     public String getHostName(String clusterName, String componentName) {
       return null;
     }
-
-    @Override
-    public Map<String, String> getHostMapping(String clusterName) {
-      return TestHostMappingProvider.getHostMap();
-    }
   }
 }

Modified: incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/controller/jmx/TestStreamProvider.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/controller/jmx/TestStreamProvider.java?rev=1442010&r1=1442009&r2=1442010&view=diff
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/controller/jmx/TestStreamProvider.java (original)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/controller/jmx/TestStreamProvider.java Mon Feb  4 02:23:55 2013
@@ -34,7 +34,7 @@ public class TestStreamProvider implemen
     FILE_MAPPING.put("50075", "hdfs_datanode_jmx.json");
     FILE_MAPPING.put("50030", "mapreduce_jobtracker_jmx.json");
     FILE_MAPPING.put("50060", "mapreduce_tasktracker_jmx.json");
-    FILE_MAPPING.put("60010", "hdfs_namenode_jmx.json");
+    FILE_MAPPING.put("60010", "hbase_hbasemaster_jmx.json");
   }
 
   private String lastSpec;

Modified: incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/stageplanner/TestStagePlanner.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/stageplanner/TestStagePlanner.java?rev=1442010&r1=1442009&r2=1442010&view=diff
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/stageplanner/TestStagePlanner.java (original)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/stageplanner/TestStagePlanner.java Mon Feb  4 02:23:55 2013
@@ -25,6 +25,7 @@ import java.util.List;
 import org.apache.ambari.server.Role;
 import org.apache.ambari.server.RoleCommand;
 import org.apache.ambari.server.actionmanager.Stage;
+import org.apache.ambari.server.controller.HostsMap;
 import org.apache.ambari.server.metadata.RoleCommandOrder;
 import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStartEvent;
 import org.apache.ambari.server.utils.StageUtils;

Modified: incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java?rev=1442010&r1=1442009&r2=1442010&view=diff
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java (original)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java Mon Feb  4 02:23:55 2013
@@ -39,6 +39,7 @@ import org.apache.ambari.server.orm.enti
 import org.apache.ambari.server.orm.entities.HostComponentStateEntityPK;
 import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntityPK;
+import org.apache.ambari.server.state.svccomphost.ServiceComponentHostImpl;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -308,4 +309,21 @@ public class ServiceComponentTest {
     Assert.assertFalse(sb.toString().isEmpty());
   }
 
+  @Test
+  public void testCanBeRemoved() throws Exception{
+    String componentName = "NAMENODE";
+    ServiceComponent component = serviceComponentFactory.createNew(service,
+        componentName);
+
+    for (State state : State.values()) {
+      component.setDesiredState(state);
+
+      if (state.isRemovableState()) {
+        org.junit.Assert.assertTrue(component.canBeRemoved());
+      }
+      else {
+        org.junit.Assert.assertFalse(component.canBeRemoved());
+      }
+    }
+  }
 }

Modified: incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java?rev=1442010&r1=1442009&r2=1442010&view=diff
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java (original)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java Mon Feb  4 02:23:55 2013
@@ -18,8 +18,6 @@
 
 package org.apache.ambari.server.state;
 
-import static org.junit.Assert.fail;
-
 import java.util.HashMap;
 import java.util.Map;
 
@@ -38,6 +36,8 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import static org.junit.Assert.*;
+
 public class ServiceTest {
 
   private Clusters clusters;
@@ -224,4 +224,59 @@ public class ServiceTest {
 
   }
 
+  @Test
+  public void testDeleteServiceComponent() throws Exception {
+    Service hdfs = cluster.addService("HDFS");
+    Service mapReduce = cluster.addService("MAPREDUCE");
+
+    hdfs.persist();
+
+    ServiceComponent nameNode = hdfs.addServiceComponent("NAMENODE");
+    nameNode.persist();
+    ServiceComponent jobTracker = mapReduce.addServiceComponent("JOBTRACKER");
+
+    assertEquals(2, cluster.getServices().size());
+    assertEquals(1, hdfs.getServiceComponents().size());
+    assertEquals(1, mapReduce.getServiceComponents().size());
+    assertTrue(hdfs.isPersisted());
+    assertFalse(mapReduce.isPersisted());
+
+    hdfs.deleteServiceComponent("NAMENODE");
+
+    assertEquals(0, hdfs.getServiceComponents().size());
+    assertEquals(1, mapReduce.getServiceComponents().size());
+
+    mapReduce.deleteServiceComponent("JOBTRACKER");
+
+    assertEquals(0, hdfs.getServiceComponents().size());
+    assertEquals(0, mapReduce.getServiceComponents().size());
+
+  }
+
+  @Test
+  public void testCanBeRemoved() throws Exception{
+    Service service = cluster.addService("HDFS");
+
+    for (State state : State.values()) {
+      service.setDesiredState(state);
+
+      if (state.isRemovableState()) {
+        org.junit.Assert.assertTrue(service.canBeRemoved());
+      }
+      else {
+        org.junit.Assert.assertFalse(service.canBeRemoved());
+      }
+    }
+
+    ServiceComponent component = service.addServiceComponent("NAMENODE");
+    // can't remove a STARTED component
+    component.setDesiredState(State.STARTED);
+
+    for (State state : State.values()) {
+      service.setDesiredState(state);
+      // should always be false if the sub component can not be removed
+      org.junit.Assert.assertFalse(service.canBeRemoved());
+    }
+  }
+
 }

Modified: incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java?rev=1442010&r1=1442009&r2=1442010&view=diff
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java (original)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java Mon Feb  4 02:23:55 2013
@@ -18,24 +18,34 @@
 
 package org.apache.ambari.server.state.cluster;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import static org.mockito.Mockito.*;
+
+import javax.persistence.EntityManager;
 
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
 import junit.framework.Assert;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.agent.AgentEnv;
+import org.apache.ambari.server.agent.AgentEnv.Directory;
 import org.apache.ambari.server.agent.DiskInfo;
 import org.apache.ambari.server.agent.HostInfo;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.ClusterResponse;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.apache.ambari.server.orm.entities.ClusterEntity;
+import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
+import org.apache.ambari.server.orm.entities.HostEntity;
+import org.apache.ambari.server.orm.entities.HostStateEntity;
+import org.apache.ambari.server.orm.entities.ServiceDesiredStateEntity;
 import org.apache.ambari.server.state.AgentVersion;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
@@ -54,9 +64,17 @@ import org.apache.ambari.server.state.ho
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-public class ClusterTest {
+import com.google.gson.Gson;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.persist.PersistService;
 
+public class ClusterTest {
+  private static final Logger LOG = LoggerFactory.getLogger(ClusterTest.class);
+  
   private Clusters clusters;
   private Cluster c1;
   private Injector injector;
@@ -139,11 +157,22 @@ public class ClusterTest {
         "5000000", "4000000", "10%", "size", "fstype"));
     hostInfo.setMounts(mounts);
 
+    AgentEnv agentEnv = new AgentEnv();
+    
+    Directory dir1 = new Directory();
+    dir1.setName("/etc/hadoop");
+    dir1.setType("not_exist");
+    Directory dir2 = new Directory();
+    dir2.setName("/var/log/hadoop");
+    dir2.setType("not_exist");
+    agentEnv.setPaths(new Directory[] { dir1, dir2 });
+    
+    
     AgentVersion agentVersion = new AgentVersion("0.0.x");
     long currentTime = 1001;
 
     clusters.getHost("h1").handleEvent(new HostRegistrationRequestEvent(
-        "h1", agentVersion, currentTime, hostInfo));
+        "h1", agentVersion, currentTime, hostInfo, agentEnv));
 
     Assert.assertEquals(HostState.WAITING_FOR_HOST_STATUS_UPDATES,
         clusters.getHost("h1").getState());
@@ -152,7 +181,7 @@ public class ClusterTest {
 
     try {
       clusters.getHost("h1").handleEvent(
-          new HostHealthyHeartbeatEvent("h1", currentTime));
+          new HostHealthyHeartbeatEvent("h1", currentTime, null));
       fail("Exception should be thrown on invalid event");
     }
     catch (InvalidStateTransitionException e) {
@@ -262,6 +291,70 @@ public class ClusterTest {
     // public Config getConfig(String configType, String versionTag);
     // public void addConfig(Config config);
   }
+  
+  public ClusterEntity createDummyData() {
+    ClusterEntity clusterEntity = new ClusterEntity();
+    clusterEntity.setClusterName("test_cluster1");
+    clusterEntity.setClusterInfo("test_cluster_info1");
+
+    HostEntity host1 = new HostEntity();
+    HostEntity host2 = new HostEntity();
+    HostEntity host3 = new HostEntity();
+
+    host1.setHostName("test_host1");
+    host2.setHostName("test_host2");
+    host3.setHostName("test_host3");
+    host1.setIpv4("192.168.0.1");
+    host2.setIpv4("192.168.0.2");
+    host3.setIpv4("192.168.0.3");
+
+    List<HostEntity> hostEntities = new ArrayList<HostEntity>();
+    hostEntities.add(host1);
+    hostEntities.add(host2);
+
+    clusterEntity.setHostEntities(hostEntities);
+    clusterEntity.setClusterConfigEntities(Collections.EMPTY_LIST);
+    //both sides of relation should be set when modifying in runtime
+    host1.setClusterEntities(Arrays.asList(clusterEntity));
+    host2.setClusterEntities(Arrays.asList(clusterEntity));
+
+    HostStateEntity hostStateEntity1 = new HostStateEntity();
+    hostStateEntity1.setCurrentState(HostState.HEARTBEAT_LOST);
+    hostStateEntity1.setHostEntity(host1);
+    HostStateEntity hostStateEntity2 = new HostStateEntity();
+    hostStateEntity2.setCurrentState(HostState.HEALTHY);
+    hostStateEntity2.setHostEntity(host2);
+    host1.setHostStateEntity(hostStateEntity1);
+    host2.setHostStateEntity(hostStateEntity2);
+
+    ClusterServiceEntity clusterServiceEntity = new ClusterServiceEntity();
+    clusterServiceEntity.setServiceName("HDFS");
+    clusterServiceEntity.setClusterEntity(clusterEntity);
+    clusterServiceEntity.setServiceComponentDesiredStateEntities(
+        Collections.EMPTY_LIST);
+    clusterServiceEntity.setServiceConfigMappings(Collections.EMPTY_LIST);
+    ServiceDesiredStateEntity stateEntity = mock(ServiceDesiredStateEntity.class);
+    Gson gson = new Gson();
+    when(stateEntity.getDesiredStackVersion()).thenReturn(gson.toJson(new StackId("HDP-0.1"),
+        StackId.class));
+    clusterServiceEntity.setServiceDesiredStateEntity(stateEntity);
+    List<ClusterServiceEntity> clusterServiceEntities = new ArrayList<ClusterServiceEntity>();
+    clusterServiceEntities.add(clusterServiceEntity);
+    clusterEntity.setClusterServiceEntities(clusterServiceEntities);
+    return clusterEntity;
+  }
+  
+  @Test
+  public void testClusterRecovery() throws AmbariException {
+    ClusterEntity entity = createDummyData();
+    ClusterImpl cluster = new ClusterImpl(entity, injector);
+    Service service = cluster.getService("HDFS");
+    /* make sure the services are recovered */
+    Assert.assertEquals("HDFS",service.getName());
+    Map<String, Service> services = cluster.getServices();
+    Assert.assertNotNull(services.get("HDFS"));
+  }
+  
 
   @Test
   public void testConvertToResponse() throws AmbariException {
@@ -275,4 +368,24 @@ public class ClusterTest {
     c1.debugDump(sb);
   }
 
+  @Test
+  public void testDeleteService() throws Exception {
+    c1.addService("MAPREDUCE").persist();
+
+    Service hdfs = c1.addService("HDFS");
+    hdfs.persist();
+    ServiceComponent nameNode = hdfs.addServiceComponent("NAMENODE");
+    nameNode.persist();
+
+
+    assertEquals(2, c1.getServices().size());
+    assertEquals(2, injector.getProvider(EntityManager.class).get().
+        createQuery("SELECT service FROM ClusterServiceEntity service").getResultList().size());
+
+    c1.deleteService("HDFS");
+
+    assertEquals(1, c1.getServices().size());
+    assertEquals(1, injector.getProvider(EntityManager.class).get().
+        createQuery("SELECT service FROM ClusterServiceEntity service").getResultList().size());
+  }
 }

Modified: incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java?rev=1442010&r1=1442009&r2=1442010&view=diff
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java (original)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java Mon Feb  4 02:23:55 2013
@@ -18,12 +18,7 @@
 
 package org.apache.ambari.server.state.cluster;
 
-import static org.junit.Assert.fail;
-
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 import com.google.inject.Guice;
 import com.google.inject.Inject;
@@ -38,14 +33,18 @@ import org.apache.ambari.server.HostNotF
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Host;
-import org.apache.ambari.server.state.StackId;
+import org.apache.ambari.server.orm.dao.*;
+import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntityPK;
+import org.apache.ambari.server.orm.entities.HostComponentStateEntityPK;
+import org.apache.ambari.server.state.*;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import javax.persistence.EntityManager;
+
+import static org.junit.Assert.*;
+
 public class ClustersTest {
 
   private Clusters clusters;
@@ -212,14 +211,20 @@ public class ClustersTest {
 
     clusters.mapHostToCluster(h1, c1);
     clusters.mapHostToCluster(h2, c1);
-
+    
     try {
       clusters.mapHostToCluster(h1, c1);
       fail("Expected exception for duplicate");
     } catch (DuplicateResourceException e) {
       // expected
     }
-
+    
+    /* make sure 2 host mapping to same cluster are the same cluster objects */
+    
+    Cluster c3 = (Cluster) clusters.getClustersForHost(h1).toArray()[0];
+    Cluster c4 = (Cluster) clusters.getClustersForHost(h2).toArray()[0];
+    
+    Assert.assertEquals(c3, c4);
     Set<String> hostnames = new HashSet<String>();
     hostnames.add(h1);
     hostnames.add(h2);
@@ -228,7 +233,7 @@ public class ClustersTest {
 
     c = clusters.getClustersForHost(h1);
     Assert.assertEquals(2, c.size());
-
+    
     c = clusters.getClustersForHost(h2);
     Assert.assertEquals(2, c.size());
 
@@ -270,4 +275,80 @@ public class ClustersTest {
     // TODO verify dump output?
   }
 
+  @Test
+  public void testDeleteCluster() throws Exception {
+    String c1 = "c1";
+    final String h1 = "h1";
+    final String h2 = "h2";
+
+    clusters.addCluster(c1);
+
+    Cluster cluster = clusters.getCluster(c1);
+    cluster.setDesiredStackVersion(new StackId("HDP-0.1"));
+
+    Config config = injector.getInstance(ConfigFactory.class).createNew(cluster, "t1", new HashMap<String, String>() {{
+      put("prop1", "val1");
+    }});
+    config.setVersionTag("1");
+    config.persist();
+
+    clusters.addHost(h1);
+    clusters.addHost(h2);
+
+    Host host1 = clusters.getHost(h1);
+    host1.setOsType("centos5");
+    Host host2 = clusters.getHost(h2);
+    host2.setOsType("centos5");
+    host1.persist();
+    host2.persist();
+
+    clusters.mapHostsToCluster(new HashSet<String>() {
+      {
+        addAll(Arrays.asList(h1, h2));
+      }
+    }, c1);
+
+
+    Service hdfs = cluster.addService("HDFS");
+    hdfs.persist();
+
+    assertNotNull(injector.getInstance(ClusterServiceDAO.class).findByClusterAndServiceNames(c1, "HDFS"));
+
+    ServiceComponent nameNode = hdfs.addServiceComponent("NAMENODE");
+    nameNode.persist();
+    ServiceComponent dataNode = hdfs.addServiceComponent("DATANODE");
+    dataNode.persist();
+
+    ServiceComponentHost nameNodeHost = nameNode.addServiceComponentHost(h1);
+    nameNodeHost.persist();
+
+    ServiceComponentHost dataNodeHost = dataNode.addServiceComponentHost(h2);
+    dataNodeHost.persist();
+
+    HostComponentStateEntityPK hkspk = new HostComponentStateEntityPK();
+    HostComponentDesiredStateEntityPK hkdspk = new HostComponentDesiredStateEntityPK();
+
+    hkspk.setClusterId(nameNodeHost.getClusterId());
+    hkspk.setHostName(nameNodeHost.getHostName());
+    hkspk.setServiceName(nameNodeHost.getServiceName());
+    hkspk.setComponentName(nameNodeHost.getServiceComponentName());
+
+    hkdspk.setClusterId(nameNodeHost.getClusterId());
+    hkdspk.setHostName(nameNodeHost.getHostName());
+    hkdspk.setServiceName(nameNodeHost.getServiceName());
+    hkdspk.setComponentName(nameNodeHost.getServiceComponentName());
+
+    assertNotNull(injector.getInstance(HostComponentStateDAO.class).findByPK(hkspk));
+    assertNotNull(injector.getInstance(HostComponentDesiredStateDAO.class).findByPK(hkdspk));
+    assertEquals(1, injector.getProvider(EntityManager.class).get().createQuery("SELECT config FROM ClusterConfigEntity config").getResultList().size());
+
+    clusters.deleteCluster(c1);
+
+    assertEquals(2, injector.getInstance(HostDAO.class).findAll().size());
+    assertNull(injector.getInstance(HostComponentStateDAO.class).findByPK(hkspk));
+    assertNull(injector.getInstance(HostComponentDesiredStateDAO.class).findByPK(hkdspk));
+    //configs are removed implicitly by cascade operation
+    assertEquals(0, injector.getProvider(EntityManager.class).get().createQuery("SELECT config FROM ClusterConfigEntity config").getResultList().size());
+
+  }
 }

Modified: incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java?rev=1442010&r1=1442009&r2=1442010&view=diff
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java (original)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java Mon Feb  4 02:23:55 2013
@@ -29,10 +29,10 @@ import java.util.List;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.actionmanager.ActionManager;
 import org.apache.ambari.server.agent.ActionQueue;
+import org.apache.ambari.server.agent.AgentEnv;
 import org.apache.ambari.server.agent.DiskInfo;
 import org.apache.ambari.server.agent.HeartBeatHandler;
 import org.apache.ambari.server.agent.HostInfo;
-import org.apache.ambari.server.agent.TestHeartbeatHandler;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.dao.HostDAO;
@@ -142,15 +142,19 @@ public class HostTest {
 
     AgentVersion agentVersion = null;
     long currentTime = System.currentTimeMillis();
+    
+    AgentEnv agentEnv = new AgentEnv();
 
     HostRegistrationRequestEvent e =
         new HostRegistrationRequestEvent("foo", agentVersion, currentTime,
-            info);
+            info, agentEnv);
     if (!firstReg) {
       Assert.assertTrue(host.isPersisted());
     }
     host.handleEvent(e);
     Assert.assertEquals(currentTime, host.getLastRegistrationTime());
+    
+    Assert.assertNotNull(host.getLastAgentEnv());
 
     HostEntity entity = hostDAO.findByName(host.getHostName());
     Assert.assertEquals(currentTime,
@@ -173,7 +177,7 @@ public class HostTest {
   private void sendHealthyHeartbeat(Host host, long counter)
       throws Exception {
     HostHealthyHeartbeatEvent e = new HostHealthyHeartbeatEvent(
-        host.getHostName(), counter);
+        host.getHostName(), counter, null);
     host.handleEvent(e);
   }
 

Modified: incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java?rev=1442010&r1=1442009&r2=1442010&view=diff
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java (original)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java Mon Feb  4 02:23:55 2013
@@ -22,7 +22,9 @@ import static org.junit.Assert.fail;
 
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 
 import com.google.inject.Provider;
 import org.apache.ambari.server.AmbariException;
@@ -655,4 +657,20 @@ public class ServiceComponentHostTest {
     }
   }
 
+  @Test
+  public void testCanBeRemoved() throws Exception{
+    ServiceComponentHostImpl impl = (ServiceComponentHostImpl)
+        createNewServiceComponentHost("HDFS", "HDFS_CLIENT", "h1", true);
+
+    for (State state : State.values()) {
+      impl.setState(state);
+
+      if (state.isRemovableState()) {
+        Assert.assertTrue(impl.canBeRemoved());
+      }
+      else {
+        Assert.assertFalse(impl.canBeRemoved());
+      }
+    }
+  }
 }

Modified: incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/utils/TestStageUtils.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/utils/TestStageUtils.java?rev=1442010&r1=1442009&r2=1442010&view=diff
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/utils/TestStageUtils.java (original)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/java/org/apache/ambari/server/utils/TestStageUtils.java Mon Feb  4 02:23:55 2013
@@ -30,6 +30,8 @@ import org.apache.ambari.server.actionma
 import org.apache.ambari.server.actionmanager.Stage;
 import org.apache.ambari.server.agent.ExecutionCommand;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
+import org.apache.ambari.server.configuration.Configuration;
+import org.apache.ambari.server.controller.HostsMap;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.state.*;
@@ -168,7 +170,7 @@ public class TestStageUtils {
     addHdfsService(fsm.getCluster("c1"), hostList, injector);
     addHbaseService(fsm.getCluster("c1"), hostList, injector);
     Map<String, List<String>> info = StageUtils.getClusterHostInfo(fsm
-        .getCluster("c1"));
+        .getCluster("c1"), new HostsMap(injector.getInstance(Configuration.class)));
     assertEquals(2, info.get("slave_hosts").size());
     assertEquals(1, info.get("hbase_master_host").size());
     assertEquals("h1", info.get("hbase_master_host").get(0));

Added: incubator/ambari/branches/branch-1.2/ambari-server/src/test/python/TestAmbaryServer.py
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/python/TestAmbaryServer.py?rev=1442010&view=auto
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/python/TestAmbaryServer.py (added)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/python/TestAmbaryServer.py Mon Feb  4 02:23:55 2013
@@ -0,0 +1,1118 @@
+'''
+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
+from unittest import TestCase
+import sys
+from mock.mock import patch
+from mock.mock import MagicMock
+from mock.mock import create_autospec
+import os, errno, tempfile
+import stat
+# We have to use this import HACK because the filename contains a dash
+ambari_server = __import__('ambari-server')
+
+
+class TestAmbariServer(TestCase):
+
+  @patch.object(ambari_server, 'configure_postgres_username_password')
+  @patch.object(ambari_server, 'run_os_command')
+  @patch('optparse.Values')
+  def test_configure_pg_hba_ambaridb_users(self, OptParseValuesMock,
+                                run_os_command_method,
+                                configure_postgres_username_password_method):
+    # Prepare mocks
+    run_os_command_method.return_value = (0, "", "")
+    opvm = OptParseValuesMock.return_value
+    opvm.postgres_username = "ffdf"
+    tf1 = tempfile.NamedTemporaryFile()
+    ambari_server.PG_HBA_CONF_FILE = tf1.name
+    # Run test
+    ambari_server.configure_pg_hba_ambaridb_users()
+    # Check results
+    self.assertTrue(run_os_command_method.called)
+    self.assertTrue(configure_postgres_username_password_method.called)
+    string_expected = self.get_file_string(self
+      .get_samples_dir("configure_pg_hba_ambaridb_users1"))
+    string_actual = self.get_file_string(ambari_server.PG_HBA_CONF_FILE)
+    self.assertEquals(string_expected, string_actual)
+    pass
+
+
+
+  def test_configure_pg_hba_postgres_user(self):
+
+    tf1 = tempfile.NamedTemporaryFile()
+    ambari_server.PG_HBA_CONF_FILE = tf1.name
+
+    with open(ambari_server.PG_HBA_CONF_FILE, 'w') as fout:
+      fout.write("\n")
+      fout.write("local  all  all md5\n")
+      fout.write("host  all   all 0.0.0.0/0  md5\n")
+      fout.write("host  all   all ::/0 md5\n")
+
+    ambari_server.configure_pg_hba_postgres_user()
+
+    expected  = self.get_file_string(self.get_samples_dir(
+      "configure_pg_hba_ambaridb_users2"))
+    result = self.get_file_string(ambari_server.PG_HBA_CONF_FILE)
+    self.assertEqual(expected, result, "pg_hba_conf not processed")
+
+    mode = oct(os.stat(ambari_server.PG_HBA_CONF_FILE)[stat.ST_MODE])
+    str_mode = str(mode)[-4:]
+    self.assertEqual("0644", str_mode, "Wrong file permissions")
+
+
+
+  @patch('__builtin__.raw_input')
+  def test_get_choice_string_input(self, raw_input_method):
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+
+    prompt = "blablabla"
+    default = "default blablabla"
+    firstChoice = set(['yes','ye', 'y'])
+    secondChoice = set(['no','n'])
+    # test first input
+    raw_input_method.return_value = "Y"
+
+    result = ambari_server.get_choice_string_input(prompt, default,
+        firstChoice, secondChoice)
+    self.assertEquals(result, True)
+    raw_input_method.reset_mock()
+    # test second input
+
+    raw_input_method.return_value = "N"
+
+    result = ambari_server.get_choice_string_input(prompt, default,
+        firstChoice, secondChoice)
+    self.assertEquals(result, False)
+
+    raw_input_method.reset_mock()
+
+    # test enter pressed
+
+    raw_input_method.return_value = ""
+
+    result = ambari_server.get_choice_string_input(prompt, default,
+        firstChoice, secondChoice)
+    self.assertEquals(result, default)
+
+    raw_input_method.reset_mock()
+
+    # test wrong input
+    list_of_return_values= ['yes', 'dsad', 'fdsfds']
+    def side_effect(list):
+      return list_of_return_values.pop()
+    raw_input_method.side_effect = side_effect
+
+    result = ambari_server.get_choice_string_input(prompt, default,
+        firstChoice, secondChoice)
+    self.assertEquals(result, True)
+    self.assertEquals(raw_input_method.call_count, 3)
+
+    sys.stdout = sys.__stdout__
+
+    pass
+
+
+
+  @patch('re.search')
+  @patch('__builtin__.raw_input')
+  @patch('getpass.getpass')
+  def test_get_validated_string_input(self, get_pass_method,
+      raw_input_method, re_search_method):
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    prompt = "blabla"
+    default = "default_pass"
+    pattern = "pattern_pp"
+    description = "blabla2"
+    # check password input
+    is_pass = True
+    get_pass_method.return_value = "dfdsfdsfds"
+
+    result = ambari_server.get_validated_string_input(prompt, default,
+        pattern, description, is_pass)
+
+    self.assertEquals(get_pass_method.return_value, result)
+    get_pass_method.assure_called_once(prompt)
+    self.assertFalse(raw_input_method.called)
+
+    # check raw input
+    get_pass_method.reset_mock()
+    raw_input_method.reset_mock()
+    is_pass = False
+    raw_input_method.return_value = "dkf90ewuf0"
+
+    result = ambari_server.get_validated_string_input(prompt, default,
+        pattern, description, is_pass)
+
+    self.assertEquals(raw_input_method.return_value, result)
+    self.assertFalse(get_pass_method.called)
+    raw_input_method.assure_called_once(prompt)
+
+    sys.stdout = sys.__stdout__
+
+
+
+  def test_get_pass_file_path(self):
+    result = ambari_server.get_pass_file_path("/etc/ambari/conf_file")
+    self.assertEquals("/etc/ambari/password.dat", result)
+    pass
+
+
+  @patch('__builtin__.file')
+  @patch('__builtin__.open')
+  @patch.object(ambari_server, 'Properties')
+  @patch.object(ambari_server, 'search_file')
+  def test_configure_postgres_username_password_test_configured(self,
+                  search_file_message, properties_mock, open_method, file_obj):
+    """
+      Tests situation when database username + password are already configured
+    """
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    search_file_message.return_value = "blablabla-properties"
+    pm  = properties_mock.return_value
+    def tf(self, key):
+      return {
+               ambari_server.JDBC_USER_NAME_PROPERTY : "fake_username",
+               ambari_server.JDBC_PASSWORD_FILE_PROPERTY : "fake_passwd_file"
+             }[key]
+      pass
+    pm.__getitem__ = tf
+    options = MagicMock()
+    open_method.return_value = file_obj
+    file_obj.read.return_value = "fake_password"
+
+    ambari_server.configure_postgres_username_password(options)
+
+    self.assertTrue(pm.load.called)
+    self.assertTrue(file_obj.read.called)
+    self.assertEquals(options.postgres_username,
+      pm[ambari_server.JDBC_USER_NAME_PROPERTY])
+    self.assertEquals(options.postgres_password, file_obj.read.return_value)
+
+    sys.stdout = sys.__stdout__
+
+
+
+  @patch.object(ambari_server, 'get_pass_file_path', autospec=True)
+  @patch('os.chmod', autospec=True)
+  @patch.object(ambari_server, 'write_property', autospec=True)
+  @patch.object(ambari_server, 'configure_postgres_password')
+  @patch.object(ambari_server, 'get_validated_string_input')
+  @patch.object(ambari_server, 'get_YN_input')
+  @patch('__builtin__.file')
+  @patch('__builtin__.open')
+  @patch.object(ambari_server, 'Properties')
+  @patch.object(ambari_server, 'search_file')
+  def test_configure_postgres_username_password_test_full_setup(self,
+          search_file_message, properties_mock, open_method, file_obj,
+          get_YN_input_method, get_validated_string_input_method,
+          configure_postgres_password_method, write_property_method,
+          os_chmod_method, get_pass_file_path_method):
+    """
+      Tests situation when database username + password are not
+      already configured. Includes advanced DB configuration
+    """
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    search_file_message.return_value = "blablabla-properties"
+    pm  = properties_mock.return_value
+    def tf(self, key):
+      return {
+               ambari_server.JDBC_USER_NAME_PROPERTY : "fake_user",
+               ambari_server.JDBC_PASSWORD_FILE_PROPERTY : False
+             }[key]
+      pass
+    pm.__getitem__ = tf
+    options = MagicMock()
+    open_method.return_value = file_obj
+    file_obj.read.return_value = "fake_password"
+    file_obj.write.return_value = None
+    get_YN_input_method.return_value = False
+    get_validated_string_input_method.return_value = "blablabla-input"
+    get_pass_file_path_method.return_value = "blablabla-path"
+
+    ambari_server.configure_postgres_username_password(options)
+
+    self.assertTrue(get_YN_input_method.called)
+    self.assertEquals(write_property_method.call_args_list, [
+      ((ambari_server.JDBC_USER_NAME_PROPERTY,
+        'ambari-server'),),
+      ((ambari_server.JDBC_PASSWORD_FILE_PROPERTY,
+        get_pass_file_path_method.return_value),)
+    ])
+    get_pass_file_path_method.\
+        assert_called_once_with(search_file_message.return_value)
+    os_chmod_method.assert_called_once_with("blablabla-path", 384)
+    self.assertTrue(pm.load.called)
+    self.assertFalse(get_validated_string_input_method.called)
+    self.assertFalse(configure_postgres_password_method.called)
+
+    sys.stdout = sys.__stdout__
+
+
+  @patch.object(ambari_server, 'get_pass_file_path', autospec=True)
+  @patch('os.chmod', autospec=True)
+  @patch.object(ambari_server, 'write_property', autospec=True)
+  @patch.object(ambari_server, 'configure_postgres_password')
+  @patch.object(ambari_server, 'get_validated_string_input')
+  @patch.object(ambari_server, 'get_YN_input')
+  @patch('__builtin__.file')
+  @patch('__builtin__.open')
+  @patch.object(ambari_server, 'Properties')
+  @patch.object(ambari_server, 'search_file')
+  def test_configure_postgres_username_password_test_full_setup_advanced(self,
+          search_file_message, properties_mock, open_method, file_obj,
+          get_YN_input_method, get_validated_string_input_method,
+          configure_postgres_password_method, write_property_method,
+          os_chmod_method, get_pass_file_path_method):
+    """
+      Tests situation when database username + password are not
+      already configured. Includes advanced DB configuration
+    """
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    search_file_message.return_value = "blablabla-properties"
+    pm  = properties_mock.return_value
+    def tf(self, key):
+      return {
+               ambari_server.JDBC_USER_NAME_PROPERTY : "fake_user",
+               ambari_server.JDBC_PASSWORD_FILE_PROPERTY : False
+             }[key]
+      pass
+    pm.__getitem__ = tf
+    options = MagicMock()
+    open_method.return_value = file_obj
+    file_obj.read.return_value = "fake_password"
+    file_obj.write.return_value = None
+    get_YN_input_method.return_value = True
+    get_validated_string_input_method.return_value = "blablabla-input"
+    get_pass_file_path_method.return_value = "blablabla-path"
+
+    ambari_server.configure_postgres_username_password(options)
+
+    self.assertTrue(get_YN_input_method.called)
+    self.assertEquals(write_property_method.call_args_list, [
+      ((ambari_server.JDBC_USER_NAME_PROPERTY,
+        get_validated_string_input_method.return_value),),
+      ((ambari_server.JDBC_PASSWORD_FILE_PROPERTY,
+        get_pass_file_path_method.return_value),)
+    ])
+    get_pass_file_path_method.\
+        assert_called_once_with(search_file_message.return_value)
+    os_chmod_method.assert_called_once_with("blablabla-path", 384)
+    self.assertTrue(pm.load.called)
+    self.assertTrue(get_validated_string_input_method.called)
+    self.assertTrue(configure_postgres_password_method.called)
+
+    sys.stdout = sys.__stdout__
+
+
+
+  @patch.object(ambari_server, 'setup')
+  @patch.object(ambari_server, 'start')
+  @patch.object(ambari_server, 'stop')
+  @patch.object(ambari_server, 'reset')
+  @patch('optparse.OptionParser')
+  def test_main_test_setup(self, OptionParserMock, reset_method, stop_method,
+                           start_method, setup_method):
+    opm = OptionParserMock.return_value
+    options = MagicMock()
+    args = ["setup"]
+    opm.parse_args.return_value = (options, args)
+
+    ambari_server.main()
+
+    self.assertTrue(setup_method.called)
+    self.assertFalse(start_method.called)
+    self.assertFalse(stop_method.called)
+    self.assertFalse(reset_method.called)
+
+    self.assertFalse(False, ambari_server.VERBOSE)
+    self.assertFalse(False, ambari_server.SILENT)
+
+
+
+  @patch.object(ambari_server, 'setup')
+  @patch.object(ambari_server, 'start')
+  @patch.object(ambari_server, 'stop')
+  @patch.object(ambari_server, 'reset')
+  @patch('optparse.OptionParser')
+  def test_main_test_start(self, OptionParserMock, reset_method, stop_method,
+                           start_method, setup_method):
+    opm = OptionParserMock.return_value
+    options = MagicMock()
+    args = ["setup"]
+    opm.parse_args.return_value = (options, args)
+
+    ambari_server.main()
+
+    self.assertTrue(setup_method.called)
+    self.assertFalse(start_method.called)
+    self.assertFalse(stop_method.called)
+    self.assertFalse(reset_method.called)
+
+    self.assertFalse(False, ambari_server.VERBOSE)
+    self.assertFalse(False, ambari_server.SILENT)
+
+
+
+  @patch.object(ambari_server, 'setup')
+  @patch.object(ambari_server, 'start')
+  @patch.object(ambari_server, 'stop')
+  @patch.object(ambari_server, 'reset')
+  @patch('optparse.OptionParser')
+  def test_main_test_stop(self, OptionParserMock, reset_method, stop_method,
+                          start_method, setup_method):
+    opm = OptionParserMock.return_value
+    options = MagicMock()
+    args = ["stop"]
+    opm.parse_args.return_value = (options, args)
+
+    ambari_server.main()
+
+    self.assertFalse(setup_method.called)
+    self.assertFalse(start_method.called)
+    self.assertTrue(stop_method.called)
+    self.assertFalse(reset_method.called)
+
+    self.assertFalse(False, ambari_server.VERBOSE)
+    self.assertFalse(False, ambari_server.SILENT)
+
+
+
+  @patch.object(ambari_server, 'setup')
+  @patch.object(ambari_server, 'start')
+  @patch.object(ambari_server, 'stop')
+  @patch.object(ambari_server, 'reset')
+  @patch('optparse.OptionParser')
+  def test_main_test_reset(self, OptionParserMock, reset_method, stop_method,
+                           start_method, setup_method):
+    opm = OptionParserMock.return_value
+
+    options = MagicMock()
+    args = ["reset"]
+    opm.parse_args.return_value = (options, args)
+
+    ambari_server.main()
+
+    self.assertFalse(setup_method.called)
+    self.assertFalse(start_method.called)
+    self.assertFalse(stop_method.called)
+    self.assertTrue(reset_method.called)
+
+    self.assertFalse(False, ambari_server.VERBOSE)
+    self.assertFalse(False, ambari_server.SILENT)
+
+
+
+  def test_configure_postgresql_conf(self):
+
+    tf1 = tempfile.NamedTemporaryFile()
+    ambari_server.POSTGRESQL_CONF_FILE = tf1.name
+
+    with open(ambari_server.POSTGRESQL_CONF_FILE, 'w') as f:
+      f.write("#listen_addresses = '127.0.0.1'        #\n")
+      f.write("#listen_addresses = '127.0.0.1'")
+
+    ambari_server.configure_postgresql_conf()
+
+    expected  = self.get_file_string(self.get_samples_dir(
+      "configure_postgresql_conf1"))
+    result = self.get_file_string(ambari_server.POSTGRESQL_CONF_FILE)
+    self.assertEqual(expected, result, "postgresql.conf not updated")
+
+    mode = oct(os.stat(ambari_server.POSTGRESQL_CONF_FILE)[stat.ST_MODE])
+    str_mode = str(mode)[-4:]
+    self.assertEqual("0644", str_mode, "Wrong file permissions")
+
+
+
+  @patch.object(ambari_server, "restart_postgres")
+  @patch.object(ambari_server, "get_postgre_status")
+  @patch.object(ambari_server, "configure_postgresql_conf")
+  @patch.object(ambari_server, "configure_pg_hba_ambaridb_users")
+  @patch.object(ambari_server, "configure_pg_hba_postgres_user")
+  def test_configure_postgres(self, configure_pg_hba_postgres_user_mock,
+                              configure_pg_hba_ambaridb_users_mock,
+                              configure_postgresql_conf_mock,
+                              get_postgre_status_mock,
+                              restart_postgres_mock):
+
+
+    tf1 = tempfile.NamedTemporaryFile()
+    tf2 = tempfile.NamedTemporaryFile()
+    ambari_server.PG_HBA_CONF_FILE = tf1.name
+    ambari_server.PG_HBA_CONF_FILE_BACKUP = tf2.name
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+    rcode = ambari_server.configure_postgres()
+    sys.stdout = sys.__stdout__
+    self.assertEqual(0, rcode)
+    self.assertEqual("Backup for pg_hba found, reconfiguration not required\n",
+      out.getvalue())
+
+    ambari_server.PG_HBA_CONF_FILE_BACKUP = tempfile.mktemp()
+    get_postgre_status_mock.return_value = ambari_server.PG_STATUS_RUNNING
+    restart_postgres_mock.return_value = 0
+
+    rcode = ambari_server.configure_postgres()
+
+    self.assertTrue(os.path.isfile(ambari_server.PG_HBA_CONF_FILE_BACKUP),
+      "postgresql.conf backup not created")
+    self.assertTrue(configure_pg_hba_postgres_user_mock.called)
+    self.assertTrue(configure_pg_hba_ambaridb_users_mock.called)
+    mode = oct(os.stat(ambari_server.PG_HBA_CONF_FILE)[stat.ST_MODE])
+    str_mode = str(mode)[-4:]
+    self.assertEqual("0644", str_mode, "Wrong file permissions")
+    self.assertTrue(configure_postgresql_conf_mock.called)
+    self.assertEqual(0, rcode)
+
+    os.unlink(ambari_server.PG_HBA_CONF_FILE_BACKUP)
+    get_postgre_status_mock.return_value = "stopped"
+    rcode = ambari_server.configure_postgres()
+    self.assertEqual(0, rcode)
+    os.unlink(ambari_server.PG_HBA_CONF_FILE_BACKUP)
+    sys.stdout = sys.__stdout__
+
+
+
+  @patch("time.sleep")
+  @patch("subprocess.Popen")
+  @patch.object(ambari_server, "run_os_command")
+  @patch.object(ambari_server, "get_postgre_status")
+  @patch.object(ambari_server, "print_info_msg")
+  def test_restart_postgres(self, printInfoMsg_mock, get_postgre_status_mock,
+                            run_os_command_mock, popenMock, sleepMock):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    p = MagicMock()
+    p.poll.return_value = 0
+    popenMock.return_value = p
+    rcode = ambari_server.restart_postgres()
+    self.assertEqual(0, rcode)
+
+    p.poll.return_value = None
+    get_postgre_status_mock.return_value = "stopped"
+    run_os_command_mock.return_value = (1, None, None)
+    rcode = ambari_server.restart_postgres()
+    self.assertEqual(1, rcode)
+    sys.stdout = sys.__stdout__
+
+
+
+  @patch("shlex.split")
+  @patch("subprocess.Popen")
+  @patch.object(ambari_server, "print_info_msg")
+  def test_run_os_command(self, printInfoMsg_mock, popenMock, splitMock):
+
+    p = MagicMock()
+    p.communicate.return_value = (None, None)
+    p.returncode = 3
+    popenMock.return_value = p
+
+    # with list arg
+    cmd = ["exec", "arg"]
+    ambari_server.run_os_command(cmd)
+    self.assertFalse(splitMock.called)
+
+    # with str arg
+    resp = ambari_server.run_os_command("runme")
+    self.assertEqual(3, resp[0])
+    self.assertTrue(splitMock.called)
+
+
+
+  @patch.object(ambari_server, "get_conf_dir")
+  @patch.object(ambari_server, "search_file")
+  def test_write_property(self, search_file_mock, get_conf_dir_mock):
+
+    expected_content = "key1=val1\n"
+
+    tf1 = tempfile.NamedTemporaryFile()
+    search_file_mock.return_value = tf1.name
+    ambari_server.write_property("key1", "val1")
+    result = tf1.read()
+    self.assertEqual(expected_content, result)
+
+
+
+  @patch.object(ambari_server, "configure_postgres_username_password")
+  @patch.object(ambari_server, "run_os_command")
+  def test_setup_db(self, run_os_command_mock,
+                    configure_postgres_username_password_mock):
+
+    run_os_command_mock.return_value = (0, None, None)
+    result = ambari_server.setup_db(MagicMock())
+    self.assertTrue(configure_postgres_username_password_mock.called)
+    self.assertEqual(0, result)
+
+
+
+  @patch.object(ambari_server, "get_YN_input")
+  @patch.object(ambari_server, "run_os_command")
+  def test_check_selinux(self, run_os_command_mock, getYNInput_mock):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    run_os_command_mock.return_value = (0, ambari_server.SE_STATUS_DISABLED,
+                                        None)
+    rcode = ambari_server.check_selinux()
+    self.assertEqual(0, rcode)
+
+    getYNInput_mock.return_value = True
+    run_os_command_mock.return_value = (0,"enabled "
+                                          + ambari_server.SE_MODE_ENFORCING,
+                                        None)
+    rcode = ambari_server.check_selinux()
+    self.assertEqual(0, rcode)
+    self.assertTrue(run_os_command_mock.called)
+    self.assertTrue(getYNInput_mock.called)
+    sys.stdout = sys.__stdout__
+
+
+
+  @patch.object(ambari_server, "print_info_msg")
+  def test_get_ambari_jars(self, printInfoMsg_mock):
+
+    env = "/ambari/jars"
+    os.environ[ambari_server.AMBARI_SERVER_LIB] = env
+    result = ambari_server.get_ambari_jars()
+    self.assertEqual(env, result)
+
+    del os.environ[ambari_server.AMBARI_SERVER_LIB]
+    result = ambari_server.get_ambari_jars()
+    self.assertEqual("/usr/lib/ambari-server", result)
+    self.assertTrue(printInfoMsg_mock.called)
+
+
+  @patch.object(ambari_server, "print_info_msg")
+  def test_get_conf_dir(self, printInfoMsg_mock):
+
+    env = "/ambari/conf"
+    os.environ[ambari_server.AMBARI_CONF_VAR] = env
+    result = ambari_server.get_conf_dir()
+    self.assertEqual(env, result)
+
+    del os.environ[ambari_server.AMBARI_CONF_VAR]
+    result = ambari_server.get_conf_dir()
+    self.assertEqual("/etc/ambari-server/conf", result)
+    self.assertTrue(printInfoMsg_mock.called)
+
+
+
+  def test_search_file(self):
+
+    path = os.path.dirname(__file__)
+    result = ambari_server.search_file(__file__, path)
+    expected = os.path.abspath(__file__)
+    self.assertEqual(expected, result)
+
+    result = ambari_server.search_file("non_existent_file", path)
+    self.assertEqual(None, result)
+
+
+  @patch.object(ambari_server, "run_os_command")
+  def test_check_iptables(self, run_os_command_mock):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    run_os_command_mock.return_value = (1, "test", "")
+    rcode, info = ambari_server.check_iptables()
+    self.assertEqual(1, rcode)
+    self.assertEqual("test", info)
+
+    run_os_command_mock.return_value = (2, "",
+                                        ambari_server.IP_TBLS_SRVC_NT_FND)
+    rcode = ambari_server.check_iptables()
+    self.assertEqual(0, rcode)
+
+    sys.stdout = sys.__stdout__
+
+
+  def test_dlprogress(self):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+    ambari_server.dlprogress("filename", 10, 2, 100)
+    sys.stdout = sys.__stdout__
+    self.assertNotEqual("", out.getvalue())
+
+
+  @patch("urllib2.urlopen")
+  @patch("__builtin__.open")
+  @patch.object(ambari_server, "dlprogress")
+  def test_track_jdk(self, dlprogress_mock, openMock, urlopenMock):
+
+    u = MagicMock()
+    u.info.return_value = {"Content-Length":"24576"}
+    chunks = [None, "second", "first"]
+    def side_effect(*args, **kwargs):
+      return chunks.pop()
+    u.read.side_effect = side_effect
+    urlopenMock.return_value = u
+
+    f = MagicMock()
+    openMock.return_value = f
+
+    ambari_server.track_jdk("base", "url", "local")
+
+    self.assertEqual(0, len(chunks))
+    self.assertTrue(f.write.called)
+    self.assertTrue(f.flush.called)
+    self.assertTrue(f.close.called)
+    self.assertEqual(2, len(dlprogress_mock.call_args_list))
+
+
+
+  @patch("os.stat")
+  @patch("os.path.isfile")
+  @patch("os.path.exists")
+  @patch("__builtin__.open")
+  @patch.object(ambari_server, "track_jdk")
+  @patch.object(ambari_server, "get_YN_input")
+  @patch.object(ambari_server, "run_os_command")
+  @patch.object(ambari_server, "Properties")
+  @patch.object(ambari_server, "write_property")
+  @patch.object(ambari_server, "print_info_msg")
+  @patch.object(ambari_server, "get_JAVA_HOME")
+  @patch.object(ambari_server, "get_conf_dir")
+  @patch.object(ambari_server, "search_file")
+  def test_download_jdk(self, search_file_mock, get_conf_dir_mock,
+                        get_JAVA_HOME_mock, print_info_msg_mock,
+                        write_property_mock, Properties_mock,
+                        run_os_command_mock, get_YN_input_mock, track_jdk_mock,
+                        openMock, path_existsMock,
+                        path_isfileMock, statMock):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    args = MagicMock()
+    args.java_home = "somewhere"
+    search_file_mock.return_value = None
+
+    rcode = ambari_server.download_jdk(args)
+
+    self.assertEqual(-1, rcode)
+    self.assertTrue(search_file_mock.called)
+    self.assertTrue(get_conf_dir_mock.called)
+
+    search_file_mock.return_value = "something"
+    get_JAVA_HOME_mock.return_value = True
+    path_existsMock.return_value = True
+    rcode = ambari_server.download_jdk(args)
+    self.assertEqual(0, rcode)
+
+    get_JAVA_HOME_mock.return_value = False
+    rcode = ambari_server.download_jdk(args)
+    self.assertEqual(0, rcode)
+    self.assertTrue(write_property_mock.called)
+
+    p = MagicMock()
+    Properties_mock.return_value = p
+    openMock.side_effect = Exception("test exception")
+    path_existsMock.return_value = False
+    rcode = ambari_server.download_jdk(args)
+    self.assertEqual(-1, rcode)
+
+    openMock.side_effect = None
+    p.__getitem__.side_effect = KeyError("test exception")
+    rcode = ambari_server.download_jdk(args)
+    self.assertEqual(-1, rcode)
+
+    p.__getitem__.return_value = "somewhere"
+    p.__getitem__.side_effect = None
+    path_existsMock.return_value = False
+    run_os_command_mock.return_value = (0, "Wrong out", None)
+    rcode = ambari_server.download_jdk(args)
+    self.assertEqual(-1, rcode)
+
+    ambari_server.JDK_INSTALL_DIR = os.getcwd()
+    get_YN_input_mock.return_value = True
+    run_os_command_mock.return_value = (0, "Creating jdk-1.2/jre"
+                                           "Content-Length: 32000\r\n"
+                                           , None)
+    statResult = MagicMock()
+    statResult.st_size = 32000
+    statMock.return_value = statResult
+    rcode = ambari_server.download_jdk(args)
+    self.assertEqual(0, rcode)
+
+    sys.stdout = sys.__stdout__
+
+
+
+  @patch.object(ambari_server, "run_os_command")
+  def test_get_postgre_status(self, run_os_command_mock):
+
+    run_os_command_mock.return_value = (1, "running", None)
+    result = ambari_server.get_postgre_status()
+    self.assertEqual("running", result)
+
+    run_os_command_mock.return_value = (1, "wrong", None)
+    result = ambari_server.get_postgre_status()
+    self.assertEqual(None, result)
+
+
+
+  @patch.object(ambari_server, "run_os_command")
+  @patch.object(ambari_server, "get_postgre_status")
+  def test_check_postgre_up(self, get_postgre_status_mock, run_os_command_mock):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    get_postgre_status_mock.return_value = ambari_server.PG_STATUS_RUNNING
+    rcode = ambari_server.check_postgre_up()
+    self.assertEqual(0, rcode)
+
+    run_os_command_mock.return_value = (4, None, None)
+    get_postgre_status_mock.return_value = None
+    rcode = ambari_server.check_postgre_up()
+    self.assertEqual(4, rcode)
+
+    sys.stdout = sys.__stdout__
+
+
+
+  @patch("platform.linux_distribution")
+  @patch("platform.system")
+  @patch("__builtin__.open")
+  @patch.object(ambari_server, "Properties")
+  @patch.object(ambari_server, "print_info_msg")
+  @patch.object(ambari_server, "print_error_msg")
+  @patch.object(ambari_server, "search_file")
+  @patch.object(ambari_server, "get_conf_dir")
+  def test_configure_os_settings(self, get_conf_dir_mock, search_file_mock,
+                                 print_error_msg_mock, print_info_msg_mock,
+                                 Properties_mock, openMock, systemMock,
+                                 distMock):
+
+    search_file_mock.return_value = None
+    rcode = ambari_server.configure_os_settings()
+    self.assertEqual(-1, rcode)
+
+    search_file_mock.return_value = "something"
+    p = MagicMock()
+    Properties_mock.return_value = p
+    openMock.side_effect = Exception("exception")
+    rcode = ambari_server.configure_os_settings()
+    self.assertEqual(-1, rcode)
+
+    p.__getitem__.return_value = "something"
+    openMock.side_effect = None
+    rcode = ambari_server.configure_os_settings()
+    self.assertEqual(0, rcode)
+
+    p.__getitem__.return_value = ""
+    systemMock.return_value = "NonLinux"
+    rcode = ambari_server.configure_os_settings()
+    self.assertEqual(-1, rcode)
+
+    systemMock.return_value = "Linux"
+    distMock.return_value = ("CentOS", "6.3", None)
+    f = MagicMock()
+    openMock.return_value = f
+    rcode = ambari_server.configure_os_settings()
+    self.assertEqual(0, rcode)
+
+
+
+  @patch("__builtin__.open")
+  @patch.object(ambari_server, "Properties")
+  @patch.object(ambari_server, "search_file")
+  @patch.object(ambari_server, "get_conf_dir")
+  def test_get_JAVA_HOME(self, get_conf_dir_mock, search_file_mock,
+                         Properties_mock, openMock):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    openMock.side_effect = Exception("exception")
+    result = ambari_server.get_JAVA_HOME()
+    self.assertEqual(None, result)
+
+    expected = os.path.dirname(__file__)
+    p = MagicMock()
+    p.__getitem__.return_value = expected
+    openMock.side_effect = None
+    Properties_mock.return_value = p
+    result = ambari_server.get_JAVA_HOME()
+    self.assertEqual(expected, result)
+
+    sys.stdout = sys.__stdout__
+
+
+
+  @patch("glob.glob")
+  @patch.object(ambari_server, "get_JAVA_HOME")
+  def test_find_jdk(self, get_JAVA_HOME_mock, globMock):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    get_JAVA_HOME_mock.return_value = "somewhere"
+    result = ambari_server.find_jdk()
+    self.assertEqual("somewhere", result)
+
+    get_JAVA_HOME_mock.return_value = None
+    globMock.return_value = []
+    result = ambari_server.find_jdk()
+    self.assertEqual(None, result)
+
+    globMock.return_value = ["one", "two"]
+    result = ambari_server.find_jdk()
+    self.assertNotEqual(None, result)
+
+    sys.stdout = sys.__stdout__
+
+
+  @patch.object(ambari_server, "configure_os_settings")
+  @patch.object(ambari_server, "download_jdk")
+  @patch.object(ambari_server, "configure_postgres")
+  @patch.object(ambari_server, "setup_db")
+  @patch.object(ambari_server, "check_postgre_up")
+  @patch.object(ambari_server, "check_iptables")
+  @patch.object(ambari_server, "check_selinux")
+  def test_setup(self, check_selinux_mock, check_iptables_mock,
+                 check_postgre_up_mock, setup_db_mock, configure_postgres_mock,
+                 download_jdk_mock, configure_os_settings_mock):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    args = MagicMock()
+
+    check_selinux_mock.return_value = 0
+    check_iptables_mock.return_value = (0, "other")
+    check_postgre_up_mock.return_value = 0
+    setup_db_mock.return_value = 0
+    configure_postgres_mock.return_value = 0
+    download_jdk_mock.return_value = 0
+    configure_os_settings_mock.return_value = 0
+    result = ambari_server.setup(args)
+    self.assertEqual(None, result)
+
+    sys.stdout = sys.__stdout__
+
+
+
+  @patch("__builtin__.raw_input")
+  @patch.object(ambari_server, "setup_db")
+  @patch.object(ambari_server, "print_info_msg")
+  @patch.object(ambari_server, "run_os_command")
+  @patch.object(ambari_server, "configure_postgres_username_password")
+  def test_reset(self, configure_postgres_username_password_mock,
+                 run_os_command_mock, print_info_msg_mock,
+                 setup_db_mock, raw_inputMock):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    args = MagicMock()
+    raw_inputMock.return_value = "No"
+    rcode = ambari_server.reset(args)
+    self.assertEqual(-1, rcode)
+
+    raw_inputMock.return_value = "yes"
+    run_os_command_mock.return_value = (1, None, None)
+    rcode = ambari_server.reset(args)
+    self.assertEqual(1, rcode)
+
+    run_os_command_mock.return_value = (0, None, None)
+    rcode = ambari_server.reset(args)
+    self.assertEqual(None, rcode)
+    self.assertTrue(setup_db_mock.called)
+
+    sys.stdout = sys.__stdout__
+
+
+
+  @patch("os.kill")
+  @patch("os.path.exists")
+  @patch("__builtin__.open")
+  @patch("subprocess.Popen")
+  @patch.object(ambari_server, "print_info_msg")
+  @patch.object(ambari_server, "get_conf_dir")
+  @patch.object(ambari_server, "find_jdk")
+  @patch.object(ambari_server, "print_error_msg")
+  @patch.object(ambari_server, "check_postgre_up")
+  @patch.object(ambari_server, "check_iptables")
+  def test_start(self, check_iptables_mock, check_postgre_up_mock,
+                 print_error_msg_mock, find_jdk_mock, get_conf_dir_mock,
+                 print_info_msg_mock, popenMock, openMock, pexistsMock,
+                 killMock):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    args = MagicMock()
+    f = MagicMock()
+    f.readline.return_value = 42
+    openMock.return_value = f
+    pexistsMock.return_value = True
+    rcode = ambari_server.start(args)
+    self.assertTrue(killMock.called)
+    self.assertEqual(None, rcode)
+
+    pexistsMock.return_value = False
+    find_jdk_mock.return_value = None
+    rcode = ambari_server.start(args)
+    self.assertEqual(-1, rcode)
+
+    find_jdk_mock.return_value = "somewhere"
+    check_postgre_up_mock.return_value = 0
+    check_iptables_mock.return_value = (0, None)
+    p = MagicMock()
+    popenMock.return_value = p
+    rcode = ambari_server.start(args)
+    self.assertEqual(None, rcode)
+    self.assertTrue(f.write.called)
+
+    sys.stdout = sys.__stdout__
+
+
+
+  @patch("__builtin__.open")
+  @patch("os.path.exists")
+  @patch("os.remove")
+  @patch("os.killpg")
+  @patch("os.getpgid")
+  @patch.object(ambari_server, "print_info_msg")
+  def test_stop(self, print_info_msg_mock, gpidMock, removeMock,
+                killMock, pexistsMock, openMock):
+
+    pexistsMock.return_value = True
+    f = MagicMock()
+    f.readline.return_value = "42"
+    openMock.return_value = f
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    ambari_server.stop(None)
+
+    self.assertTrue(f.readline.called)
+    self.assertTrue(killMock.called)
+    self.assertTrue(killMock.called)
+    self.assertTrue(f.close.called)
+    self.assertTrue(removeMock.called)
+
+    sys.stdout = sys.__stdout__
+
+
+
+  def test_print_info_msg(self):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    ambari_server.VERBOSE = True
+    ambari_server.print_info_msg("msg")
+    self.assertNotEqual("", out.getvalue())
+
+    sys.stdout = sys.__stdout__
+
+
+
+  def test_print_error_msg(self):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    ambari_server.VERBOSE = True
+    ambari_server.print_error_msg("msg")
+    self.assertNotEqual("", out.getvalue())
+
+    sys.stdout = sys.__stdout__
+
+
+
+  def test_print_warning_msg(self):
+
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    ambari_server.VERBOSE = True
+    ambari_server.print_warning_msg("msg")
+    self.assertNotEqual("", out.getvalue())
+
+    sys.stdout = sys.__stdout__
+
+
+
+  @patch.object(ambari_server, "get_choice_string_input")
+  def test_get_YN_input(self, get_choice_string_input_mock):
+
+    ambari_server.get_YN_input("prompt", "default")
+    self.assertTrue(get_choice_string_input_mock.called)
+    self.assertEqual(4, len(get_choice_string_input_mock.call_args_list[0][0]))
+
+
+
+  def get_sample(self, sample):
+    """
+    Returns sample file content as string with normalized line endings
+    """
+    path = self.get_samples_dir(sample)
+    return self.get_file_string(path)
+
+
+
+  def get_file_string(self, file):
+    """
+    Returns file content as string with normalized line endings
+    """
+    string = open(file, 'r').read()
+    return self.normalize(string)
+
+
+
+  def normalize(self, string):
+    """
+    Normalizes line ending in string according to platform-default encoding
+    """
+    return string.replace("\n", os.linesep)
+
+
+
+  def get_samples_dir(self, sample):
+    """
+    Returns full file path by sample name
+    """
+    testdir = os.path.dirname(__file__)
+    return os.path.dirname(testdir) + os.sep + "resources" + os.sep \
+           + 'TestAmbaryServer.samples/' + sample

Added: incubator/ambari/branches/branch-1.2/ambari-server/src/test/python/unitTests.py
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/python/unitTests.py?rev=1442010&view=auto
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/python/unitTests.py (added)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/python/unitTests.py Mon Feb  4 02:23:55 2013
@@ -0,0 +1,45 @@
+'''
+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
+import glob
+import os
+import sys
+
+TEST_MASK = 'Test*.py'
+
+def main():
+
+  pwd = os.path.dirname(__file__)
+  if pwd:
+    global TEST_MASK
+    TEST_MASK = pwd + os.sep + TEST_MASK
+
+  tests = glob.glob(TEST_MASK)
+  modules = [os.path.basename(s)[:-3] for s in tests]
+  suites = [unittest.defaultTestLoader.loadTestsFromName(name) for name in
+    modules]
+  testSuite = unittest.TestSuite(suites)
+
+  textRunner = unittest.TextTestRunner(verbosity=2).run(testSuite)
+  return 0 if textRunner.wasSuccessful() else 1
+
+
+if __name__ == "__main__":
+  sys.exit(main())
+

Added: incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_pg_hba_ambaridb_users1
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_pg_hba_ambaridb_users1?rev=1442010&view=auto
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_pg_hba_ambaridb_users1 (added)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_pg_hba_ambaridb_users1 Mon Feb  4 02:23:55 2013
@@ -0,0 +1,4 @@
+
+local  all  ffdf,mapred md5
+host  all   ffdf,mapred 0.0.0.0/0  md5
+host  all   ffdf,mapred ::/0 md5

Added: incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_pg_hba_ambaridb_users2
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_pg_hba_ambaridb_users2?rev=1442010&view=auto
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_pg_hba_ambaridb_users2 (added)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_pg_hba_ambaridb_users2 Mon Feb  4 02:23:55 2013
@@ -0,0 +1,4 @@
+
+local  all   postgres md5
+host  all   postgres 0.0.0.0/0  md5
+host  all   postgres ::/0 md5

Added: incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_postgresql_conf1
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_postgresql_conf1?rev=1442010&view=auto
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_postgresql_conf1 (added)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/TestAmbaryServer.samples/configure_postgresql_conf1 Mon Feb  4 02:23:55 2013
@@ -0,0 +1,2 @@
+listen_addresses = '*'        #
+listen_addresses = '*'        #
\ No newline at end of file

Added: incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/gsInstaller-hosts.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/gsInstaller-hosts.txt?rev=1442010&view=auto
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/gsInstaller-hosts.txt (added)
+++ incubator/ambari/branches/branch-1.2/ambari-server/src/test/resources/gsInstaller-hosts.txt Mon Feb  4 02:23:55 2013
@@ -0,0 +1,35 @@
+CLUSTER=ambari
+VERSION=HDP-1.2.0
+HDFS HDFS_CLIENT ip-10-190-97-104.ec2.internal
+HDFS NAMENODE ip-10-8-113-183.ec2.internal
+HDFS SECONDARY_NAMENODE ip-10-8-113-183.ec2.internal
+HDFS DATANODE ip-10-140-16-157.ec2.internal
+MAPREDUCE TASKTRACKER ip-10-140-16-157.ec2.internal
+HDFS DATANODE ip-10-191-122-198.ec2.internal
+MAPREDUCE TASKTRACKER ip-10-191-122-198.ec2.internal
+HDFS DATANODE ip-10-68-150-107.ec2.internal
+MAPREDUCE TASKTRACKER ip-10-68-150-107.ec2.internal
+MAPREDUCE MAPREDUCE_CLIENT ip-10-190-97-104.ec2.internal
+MAPREDUCE JOBTRACKER ip-10-8-113-183.ec2.internal
+PIG PIG ip-10-190-97-104.ec2.internal
+HBASE HBASE_CLIENT ip-10-190-97-104.ec2.internal
+HBASE HBASE_MASTER ip-10-8-113-183.ec2.internal
+HBASE HBASE_REGIONSERVER ip-10-140-16-157.ec2.internal
+HBASE HBASE_REGIONSERVER ip-10-191-122-198.ec2.internal
+HBASE HBASE_REGIONSERVER ip-10-68-150-107.ec2.internal
+ZOOKEEPER ZOOKEEPER_CLIENT ip-10-190-97-104.ec2.internal
+ZOOKEEPER ZOOKEEPER_SERVER ip-10-140-16-157.ec2.internal
+ZOOKEEPER ZOOKEEPER_SERVER ip-10-191-122-198.ec2.internal
+ZOOKEEPER ZOOKEEPER_SERVER ip-10-68-150-107.ec2.internal
+HIVE HIVE_CLIENT ip-10-190-97-104.ec2.internal
+HIVE HIVE_SERVER ip-10-8-113-183.ec2.internal
+HIVE HIVE_METASTORE ip-10-8-113-183.ec2.internal
+HIVE MYSQL_SERVER ip-10-190-97-104.ec2.internal
+HCATALOG HCAT ip-10-190-97-104.ec2.internal
+WEBHCAT WEBHCAT_SERVER ip-10-190-97-104.ec2.internal
+SQOOP SQOOP ip-10-190-97-104.ec2.internal
+OOZIE OOZIE_CLIENT ip-10-190-97-104.ec2.internal
+OOZIE OOZIE_SERVER ip-10-8-113-183.ec2.internal
+GANGLIA GANGLIA ip-10-190-97-104.ec2.internal
+GANGLIA GANGLIA_MONITOR ip-10-190-97-104.ec2.internal
+NAGIOS NAGIOS_SERVER ip-10-190-97-104.ec2.internal