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 2012/10/08 03:38:08 UTC
svn commit: r1395430 [12/13] - in /incubator/ambari/branches/AMBARI-666: ./
ambari-agent/src/main/puppet/manifestloader/
ambari-agent/src/main/puppet/modules/configgenerator/manifests/
ambari-agent/src/main/puppet/modules/hdp-hadoop/manifests/ ambari-a...
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/ambari-server.py
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/ambari-server.py?rev=1395430&r1=1395429&r2=1395430&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/ambari-server.py (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/ambari-server.py Mon Oct 8 01:37:59 2012
@@ -18,45 +18,127 @@ See the License for the specific languag
limitations under the License.
'''
-import optparse
import argparse
import sys
+import os
+import signal
import subprocess
+import re
SETUP_ACTION = "setup"
START_ACTION = "start"
STOP_ACTION = "stop"
-SETUP_CMD = "sudo -u postgres psql -f setup_db.sql"
-
-
-def setup():
- command = SETUP_CMD
- process = subprocess.Popen(command.split(' '),
+SETUP_DB_CMD = "psql -W -f{2} -d{0} --username {1}"
+GET_SE_LINUX_ST_CMD = "sestatus"
+SE_STATUS_DISABLED="disabled"
+SE_STATUS_ENABLED="enabled"
+IP_TBLS_ST_CMD = "service iptables status"
+IP_TBLS_STOP_CMD = "service iptables stop"
+IP_TBLS_ENABLED="Firewall is running"
+IP_TBLS_DISABLED="Firewall is not running"
+IP_TBLS_SRVC_NT_FND="iptables: unrecognized service"
+SERVER_START_CMD="java -cp ../etc/ambari-server/conf:../lib/ambari-server/* org.apache.ambari.server.controller.AmbariServer"
+
+def run_os_command(cmd):
+ print 'about to run command: ' + cmd
+ process = subprocess.Popen(cmd.split(' '),
stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
- process.communicate()
+ stdin=subprocess.PIPE,
+ stderr=subprocess.PIPE
+ )
+ (stdoutdata, stderrdata) = process.communicate()
+ return process.returncode, stdoutdata, stderrdata
+
+def setup_db(args):
+ dbname = args.postgredbname
+ username = args.postgreuser
+ file = args.init_script_file
+ command = SETUP_DB_CMD.format(dbname, username, file)
+ retcode, outdata, errdata = run_os_command(command)
+ return retcode
+
+def check_se_down():
+ retcode, out, err = run_os_command(GET_SE_LINUX_ST_CMD)
+ se_status = re.search('(disabled|enabled)', out).group(0)
+ print 'SELinux status:' + se_status
+ if se_status == SE_STATUS_DISABLED:
+ return 0
+ else:
+ return 1
+def ip_tables_down():
+ retcode, out, err = run_os_command(IP_TBLS_ST_CMD)
+ if out == IP_TBLS_ENABLED:
+ print 'iptables is enabled now'
+ print 'Stopping iptables service'
+ retcode, out, err = run_os_command(IP_TBLS_STOP_CMD)
+ print 'iptables is disabled now'
+
+ if not retcode == 0:
+ print err
+
+ if err.strip() == IP_TBLS_SRVC_NT_FND:
+ return 0
+ else:
+ return retcode
+
+def setup(args):
+
+ print 'About to setup database'
+ retcode = setup_db(args)
+ if not retcode == 0:
+ print 'Error! Running database init script was failed'
+ sys.exit(retcode)
+
+ print 'About to check SELinux down'
+ retcode = check_se_down()
+ if not retcode == 0:
+ print 'Error! SELinux is enabled, unable to proceed'
+ sys.exit(retcode)
+
+ print 'About to check iptables down'
+ retcode = ip_tables_down()
+ if (not retcode == 0):
+ print 'Error! Failed to stop iptables'
+ sys.exit(retcode)
+
+ print 'Setup was finished sucessfully'
+
+def start(args):
+ server_process = subprocess.Popen(SERVER_START_CMD.split(' '))
+ f = open("pid", "w")
+ f.write(str(server_process.pid))
+ f.close()
+
+def stop(args):
+ f = open("pid", "r")
+ pid = int(f.readline())
+ os.kill(pid, signal.SIGKILL)
+ f.close()
+
+
+
+
def main():
parser = argparse.ArgumentParser()
parser.add_argument("action", help="action to perform with ambari server")
- parser.add_argument('-postgreuser', '--postgreuser', default='root',
+ parser.add_argument('-d', '--postgredbname', default='postgres',
+ help="Database name in postgresql")
+ parser.add_argument('-u', '--postgreuser', default='postgres',
help="User in postgresql to run init scripts")
- parser.add_argument('-postgrepass', '--postgrepass')
+ parser.add_argument('-f', '--init-script-file', default='setup_db.sql',
+ help="File with setup script")
args = parser.parse_args()
-
action = args.action
if action == SETUP_ACTION:
- setup()
+ setup(args)
elif action == START_ACTION:
- print START_ACTION
+ start(args)
elif action == STOP_ACTION:
- print STOP_ACTION
+ stop(args)
else:
print "Incorrect action"
- sys.exit(2)
-
+ sys.exit(1)
-
-
if __name__ == "__main__":
main()
\ No newline at end of file
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/Configuration.py
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/Configuration.py?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/Configuration.py (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/Configuration.py Mon Oct 8 01:37:59 2012
@@ -0,0 +1,81 @@
+from pip.backwardcompat import raw_input
+
+# 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.
+
+class Configuration:
+
+ CORE_FILE = "core-site.xml";
+ NAME_NODE_FILE = "hdfs-site.xml";
+ JOB_TRACKER_FILE = "mapred-site.xml";
+ OOZIE_FILE = "oozie-site.xml";
+ HBASE_MASTER_FILE = "hbase-site.xml";
+
+ servicesPath = {"Core": "core-site.xml",
+ "NameNode": "hdfs-site.xml",
+ "JobTracker": "mapred-site.xml",
+ "OOZIE": "oozie-site.xml",
+ "HBASE": "hbase-site.xml",
+ "ZooKeeper": "zk.properties",
+ }
+
+ CONFIG_INIT_TYPE = ("CURRENT_DIR", "USER_PATH_INPUT");
+ configurationType = 0;
+
+ def __init__(self, type):
+ self.configurationType = type
+ self.initPaths()
+
+ def initPaths(self):
+ if self.configurationType != 0:
+ for service in self.servicesPath.keys():
+ path = raw_input("Please enter path for "+ service+" :")
+ if len(path) > 0:
+ self.servicesPath[service] = path
+ else:
+ raise ValueError("Path to the configuration file can't be empty.") #Catch it layter and start input mode automatically
+ #self.getDataFromServices()
+
+ def getDataFromServices(self):
+ #TODO
+ self.getCoreData()
+ self.getNameNodeData()
+ self.getOozieData()
+ self.getHBaseData()
+ self.getZooKeeperData()
+ print("###getDataFromServices")
+
+
+ def getCoreData(self):
+ print("TODO get data from core configuration")
+
+ def getNameNodeData(self):
+ print("TODO get data from NameNode configuration")
+
+ def getOozieData(self):
+ print("TODO get data from Oozie configuration")
+
+ def getHBaseData(self):
+ print("TODO get data from HBase configuration")
+
+ def getZooKeeperData(self):
+ print("TODO get data from ZooKeeper configuration")
+
+
+
+
+
+
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/ConsoleManager.py
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/ConsoleManager.py?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/ConsoleManager.py (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/ConsoleManager.py Mon Oct 8 01:37:59 2012
@@ -0,0 +1,31 @@
+from Tools.Scripts.treesync import raw_input
+
+# 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 org.apache.ambari.serverstate import Configuration
+
+class ConsoleManager:
+ def chooseConfInitType(self):
+ "Configuration types are base on Configuration.CONFIG_INIT_TYPE tuple"
+ configurationType = int(raw_input("\tInput configuration type:\n" +
+ "0)Current path contains all required configurationfiles files.\n" +
+ "1)Enter path for each conf file manually.\n" +
+ "Choose:"
+ )
+ ).numerator
+ return Configuration(configurationType);
+
+
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/Main.py
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/Main.py?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/Main.py (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/Main.py Mon Oct 8 01:37:59 2012
@@ -0,0 +1,25 @@
+from org.apache.ambari.serverstate.ConsoleManager import *
+
+# 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.
+
+def startTest():
+ cm = ConsoleManager()
+ conf = cm.chooseConfInitType()
+ conf.getDataFromServices()
+
+
+startTest()
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/__init__.py
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/__init__.py?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/__init__.py (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/python/org/apache/ambari/serverstate/__init__.py Mon Oct 8 01:37:59 2012
@@ -0,0 +1,19 @@
+from org.apache.ambari.serverstate.Configuration import *
+from org.apache.ambari.serverstate.ConsoleManager import *
+
+# 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.
+
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/resources/webapp/WEB-INF/spring-security.xml
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/resources/webapp/WEB-INF/spring-security.xml?rev=1395430&r1=1395429&r2=1395430&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/resources/webapp/WEB-INF/spring-security.xml (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/resources/webapp/WEB-INF/spring-security.xml Mon Oct 8 01:37:59 2012
@@ -34,7 +34,7 @@
<authentication-manager>
<authentication-provider user-service-ref="ambariLocalUserService">
- <password-encoder ref="passwordEncoder"/>
+ <password-encoder ref="standardPasswordEncoder"/>
</authentication-provider>
<authentication-provider ref="ambariLdapAuthenticationProvider"/>
@@ -57,6 +57,6 @@
value="org.apache.ambari.server.security.authorization.AmbariLdapAuthenticationProvider"/>
</beans:bean>
- <beans:bean id="passwordEncoder"
+ <beans:bean id="standardPasswordEncoder"
class="org.springframework.security.crypto.password.StandardPasswordEncoder"/>
</beans:beans>
\ No newline at end of file
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionManager.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionManager.java?rev=1395430&r1=1395429&r2=1395430&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionManager.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionManager.java Mon Oct 8 01:37:59 2012
@@ -25,8 +25,8 @@ import java.util.List;
import org.apache.ambari.server.Role;
import org.apache.ambari.server.agent.ActionQueue;
import org.apache.ambari.server.agent.CommandReport;
-import org.apache.ambari.server.state.live.ClustersImpl;
-import org.apache.ambari.server.state.live.svccomphost.ServiceComponentHostStartEvent;
+import org.apache.ambari.server.state.cluster.ClustersImpl;
+import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStartEvent;
import org.apache.ambari.server.utils.StageUtils;
import org.junit.Test;
@@ -60,7 +60,7 @@ public class TestActionManager {
Stage s = new Stage(requestId, "/a/b", "cluster1");
s.setStageId(stageId);
HostAction ha = new HostAction(hostname);
- HostRoleCommand cmd = new HostRoleCommand(Role.HBASE_MASTER,
+ HostRoleCommand cmd = new HostRoleCommand("HBASE", Role.HBASE_MASTER,
new ServiceComponentHostStartEvent(Role.HBASE_MASTER.toString(),
hostname, System.currentTimeMillis()));
ha.addHostRoleCommand(cmd);
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionScheduler.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionScheduler.java?rev=1395430&r1=1395429&r2=1395430&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionScheduler.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionScheduler.java Mon Oct 8 01:37:59 2012
@@ -29,7 +29,10 @@ import org.apache.ambari.server.agent.Ag
import org.apache.ambari.server.agent.ExecutionCommand;
import org.apache.ambari.server.state.Cluster;
import org.apache.ambari.server.state.Clusters;
-import org.junit.Ignore;
+import org.apache.ambari.server.state.Service;
+import org.apache.ambari.server.state.ServiceComponent;
+import org.apache.ambari.server.state.ServiceComponentHost;
+import org.apache.ambari.server.state.svccomphost.ServiceComponentHostInstallEvent;
import org.junit.Test;
public class TestActionScheduler {
@@ -39,13 +42,19 @@ public class TestActionScheduler {
* shows up in the action queue.
*/
@Test
- // TODO fix
- @Ignore
public void testActionSchedule() throws Exception {
ActionQueue aq = new ActionQueue();
Clusters fsm = mock(Clusters.class);
Cluster oneClusterMock = mock(Cluster.class);
+ Service serviceObj = mock(Service.class);
+ ServiceComponent scomp = mock(ServiceComponent.class);
+ ServiceComponentHost sch = mock(ServiceComponentHost.class);
when(fsm.getCluster(anyString())).thenReturn(oneClusterMock);
+ when(oneClusterMock.getService(anyString())).thenReturn(serviceObj);
+ when(serviceObj.getServiceComponent(anyString())).thenReturn(scomp);
+ when(scomp.getServiceComponentHost(anyString())).thenReturn(sch);
+ when(serviceObj.getCluster()).thenReturn(oneClusterMock);
+
ActionDBAccessor db = mock(ActionDBAccessorImpl.class);
List<Stage> stages = new ArrayList<Stage>();
Stage s = new Stage(1, "/bogus", "clusterName");
@@ -53,13 +62,12 @@ public class TestActionScheduler {
stages.add(s);
String hostname = "ahost.ambari.apache.org";
HostAction ha = new HostAction(hostname);
- HostRoleCommand hrc = new HostRoleCommand(Role.DATANODE,
- null);
+ HostRoleCommand hrc = new HostRoleCommand("HDFS", Role.DATANODE,
+ new ServiceComponentHostInstallEvent(Role.DATANODE.toString(), hostname, 35678901));
ha.addHostRoleCommand(hrc);
- // ha.setManifest("1-977-manifest");
s.addHostAction(hostname, ha);
when(db.getStagesInProgress()).thenReturn(stages);
-
+
//Keep large number of attempts so that the task is not expired finally
//Small action timeout to test rescheduling
ActionScheduler scheduler = new ActionScheduler(1000, 100, db, aq, fsm, 10000);
@@ -93,13 +101,19 @@ public class TestActionScheduler {
* Test whether scheduler times out an action
*/
@Test
- // TODO fix
- @Ignore
public void testActionTimeout() throws Exception {
ActionQueue aq = new ActionQueue();
Clusters fsm = mock(Clusters.class);
Cluster oneClusterMock = mock(Cluster.class);
+ Service serviceObj = mock(Service.class);
+ ServiceComponent scomp = mock(ServiceComponent.class);
+ ServiceComponentHost sch = mock(ServiceComponentHost.class);
when(fsm.getCluster(anyString())).thenReturn(oneClusterMock);
+ when(oneClusterMock.getService(anyString())).thenReturn(serviceObj);
+ when(serviceObj.getServiceComponent(anyString())).thenReturn(scomp);
+ when(scomp.getServiceComponentHost(anyString())).thenReturn(sch);
+ when(serviceObj.getCluster()).thenReturn(oneClusterMock);
+
ActionDBAccessorImpl db = mock(ActionDBAccessorImpl.class);
List<Stage> stages = new ArrayList<Stage>();
Stage s = new Stage(1, "/bogus", "clusterName");
@@ -107,13 +121,12 @@ public class TestActionScheduler {
stages.add(s);
String hostname = "ahost.ambari.apache.org";
HostAction ha = new HostAction(hostname);
- HostRoleCommand hrc = new HostRoleCommand(Role.DATANODE,
+ HostRoleCommand hrc = new HostRoleCommand("HDFS", Role.DATANODE,
null);
ha.addHostRoleCommand(hrc);
- // ha.setManifest("1-977-manifest");
s.addHostAction(hostname, ha);
when(db.getStagesInProgress()).thenReturn(stages);
-
+
//Keep large number of attempts so that the task is not expired finally
//Small action timeout to test rescheduling
ActionScheduler scheduler = new ActionScheduler(100, 100, db, aq, fsm, 3);
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java?rev=1395430&r1=1395429&r2=1395430&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java Mon Oct 8 01:37:59 2012
@@ -22,14 +22,16 @@ import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import junit.framework.Assert;
import org.apache.ambari.server.actionmanager.ActionManager;
+import org.apache.ambari.server.agent.rest.AgentJackSonJsonProvider;
import org.apache.ambari.server.agent.rest.AgentResource;
import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.live.ClustersImpl;
+import org.apache.ambari.server.state.cluster.ClustersImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jettison.json.JSONException;
@@ -39,11 +41,18 @@ import org.junit.Test;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
+import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.config.ClientConfig;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
+import com.sun.jersey.api.json.JSONConfiguration;
+import com.sun.jersey.server.wadl.generators.ApplicationDocs;
import com.sun.jersey.spi.container.servlet.ServletContainer;
+import com.sun.jersey.test.framework.AppDescriptor;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;
+import com.sun.jersey.test.framework.spi.container.TestContainer;
public class AgentResourceTest extends JerseyTest {
static String PACKAGE_NAME = "org.apache.ambari.server.agent.rest";
@@ -51,9 +60,11 @@ public class AgentResourceTest extends J
HeartBeatHandler handler;
ActionManager actionManager;
Injector injector;
-
+ protected Client client;
+
public AgentResourceTest() {
super(new WebAppDescriptor.Builder(PACKAGE_NAME).servletClass(ServletContainer.class)
+ .initParam("com.sun.jersey.api.json.POJOMappingFeature", "true")
.build());
}
@@ -94,9 +105,9 @@ public class AgentResourceTest extends J
private JSONObject createDummyJSONRegister() throws JSONException {
JSONObject json = new JSONObject();
- json.append("responseId" , -1);
- json.append("timestamp" , System.currentTimeMillis());
- json.append("hostname", "dummyHost");
+ json.put("responseId" , -1);
+ json.put("timestamp" , System.currentTimeMillis());
+ json.put("hostname", "dummyHost");
return json;
}
@@ -107,12 +118,15 @@ public class AgentResourceTest extends J
json.put("hostname", "dummyHost");
return json;
}
-
+
@Test
public void agentRegistration() throws UniformInterfaceException, JSONException {
RegistrationResponse response;
- WebResource webResource = resource();
- response = webResource.path("/register/dummyhost").type(MediaType.APPLICATION_JSON)
+ ClientConfig clientConfig = new DefaultClientConfig();
+ clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
+ client = Client.create(clientConfig);
+ WebResource webResource = client.resource("http://localhost:9998/register/dummyhost");
+ response = webResource.type(MediaType.APPLICATION_JSON)
.post(RegistrationResponse.class, createDummyJSONRegister());
LOG.info("Returned from Server " + response.getResponseStatus());
Assert.assertEquals(response.getResponseStatus(), RegistrationStatus.OK);
@@ -121,10 +135,13 @@ public class AgentResourceTest extends J
@Test
public void agentHeartBeat() throws UniformInterfaceException, JSONException {
HeartBeatResponse response;
- WebResource resource = resource();
- response = resource.path("/heartbeat/dummyhost").type(MediaType.APPLICATION_JSON)
+ ClientConfig clientConfig = new DefaultClientConfig();
+ clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
+ client = Client.create(clientConfig);
+ WebResource webResource = client.resource("http://localhost:9998/heartbeat/dummyhost");
+ response = webResource.type(MediaType.APPLICATION_JSON)
.post(HeartBeatResponse.class, createDummyHeartBeat());
- LOG.info("Returned from Server: " + "clusterid = " + response.getClusterId()
+ LOG.info("Returned from Server: "
+ " responseid=" + response.getResponseId());
Assert.assertEquals(response.getResponseId(), 0L);
}
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java?rev=1395430&r1=1395429&r2=1395430&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java Mon Oct 8 01:37:59 2012
@@ -27,9 +27,9 @@ import org.apache.ambari.server.agent.Ho
import org.apache.ambari.server.state.Clusters;
import org.apache.ambari.server.state.Host;
import org.apache.ambari.server.state.HostState;
+import org.apache.ambari.server.state.cluster.ClustersImpl;
import org.apache.ambari.server.state.fsm.InvalidStateTransitonException;
-import org.apache.ambari.server.state.live.ClustersImpl;
-import org.apache.ambari.server.state.live.host.HostImpl;
+import org.apache.ambari.server.state.host.HostImpl;
import org.junit.Test;
public class TestHeartbeatHandler {
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java?rev=1395430&r1=1395429&r2=1395430&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java Mon Oct 8 01:37:59 2012
@@ -41,7 +41,7 @@ public class TestHeartbeatMonitor {
List<Host> allHosts = new ArrayList<Host>();
Host hostObj = mock(Host.class);
allHosts.add(hostObj);
- when(fsm.getAllHosts()).thenReturn(allHosts);
+ when(fsm.getHosts()).thenReturn(allHosts);
when(fsm.getHost("host1")).thenReturn(hostObj);
when(hostObj.getState()).thenReturn(hs);
when(hostObj.getHostName()).thenReturn("host1");
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java Mon Oct 8 01:37:59 2012
@@ -0,0 +1,172 @@
+/**
+ * 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.
+ */
+
+package org.apache.ambari.server.controller;
+
+import static org.junit.Assert.fail;
+
+import java.util.Set;
+
+import junit.framework.Assert;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.actionmanager.ActionDBAccessor;
+import org.apache.ambari.server.actionmanager.ActionDBInMemoryImpl;
+import org.apache.ambari.server.actionmanager.ActionManager;
+import org.apache.ambari.server.agent.ActionQueue;
+import org.apache.ambari.server.state.Clusters;
+import org.apache.ambari.server.state.State;
+import org.apache.ambari.server.state.cluster.ClustersImpl;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class AmbariManagementControllerTest {
+
+ private AmbariManagementController controller;
+ private Clusters clusters;
+
+ @Before
+ public void setup() {
+ clusters = new ClustersImpl();
+ ActionDBAccessor db = new ActionDBInMemoryImpl();
+ ActionManager am = new ActionManager(5000, 1200000, new ActionQueue(),
+ clusters, db);
+ controller = new AmbariManagementControllerImpl(am, clusters);
+ }
+
+ @After
+ public void teardown() {
+ controller = null;
+ clusters = null;
+ }
+
+ private void createCluster(String clusterName) throws AmbariException {
+ ClusterRequest r = new ClusterRequest(null, clusterName, "1.0.0", null);
+ controller.createCluster(r);
+ }
+
+ private void createService(String clusterName,
+ String serviceName, State desiredState) throws AmbariException {
+ String dStateStr = null;
+ if (desiredState != null) {
+ dStateStr = desiredState.toString();
+ }
+ ServiceRequest r = new ServiceRequest(clusterName, serviceName, null,
+ dStateStr);
+ controller.createService(r);
+ }
+
+
+ @Test
+ public void testCreateCluster() throws AmbariException {
+ String clusterName = "foo1";
+ createCluster(clusterName);
+ Set<ClusterResponse> r =
+ controller.getClusters(new ClusterRequest(null, clusterName, null, null));
+ Assert.assertEquals(1, r.size());
+ ClusterResponse c = r.iterator().next();
+ Assert.assertEquals(clusterName, c.getClusterName());
+ }
+
+ @Test
+ public void testCreateService() throws AmbariException {
+ String clusterName = "foo1";
+ createCluster(clusterName);
+ String serviceName = "HDFS";
+ try {
+ createService(clusterName, serviceName, State.INSTALLING);
+ fail("Service creation should fail for invalid state");
+ } catch (Exception e) {
+ // Expected
+ }
+ try {
+ clusters.getCluster(clusterName).getService(serviceName);
+ fail("Service creation should have failed");
+ } catch (Exception e) {
+ // Expected
+ }
+ try {
+ createService(clusterName, serviceName, State.INSTALLED);
+ fail("Service creation should fail for invalid initial state");
+ } catch (Exception e) {
+ // Expected
+ }
+
+ createService(clusterName, serviceName, null);
+
+ String serviceName2 = "MAPREDUCE";
+ createService(clusterName, serviceName2, State.INIT);
+
+ ServiceRequest r = new ServiceRequest(clusterName, null, null, null);
+ Set<ServiceResponse> response = controller.getServices(r);
+ Assert.assertEquals(2, response.size());
+
+ for (ServiceResponse svc : response) {
+ Assert.assertTrue(svc.getServiceName().equals(serviceName)
+ || svc.getServiceName().equals(serviceName2));
+ Assert.assertEquals("", svc.getDesiredStackVersion());
+ Assert.assertEquals(State.INIT.toString(), svc.getDesiredState());
+ }
+
+
+ }
+
+ @Test
+ public void testCreateServiceComponent() throws AmbariException {
+
+ }
+
+ @Test
+ public void testCreateServiceComponentHost() throws AmbariException {
+
+ }
+
+ @Test
+ public void testCreateHost() throws AmbariException {
+
+ }
+
+ @Test
+ public void testGetCluster() throws AmbariException {
+
+ }
+
+ @Test
+ public void testGetService() throws AmbariException {
+
+ }
+
+ @Test
+ public void testGetServiceComponent() throws AmbariException {
+
+ }
+
+ @Test
+ public void testGetServiceComponentHost() throws AmbariException {
+
+ }
+
+ @Test
+ public void testGetHost() throws AmbariException {
+
+ }
+
+
+
+}
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/AndPredicateTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/AndPredicateTest.java?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/AndPredicateTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/AndPredicateTest.java Mon Oct 8 01:37:59 2012
@@ -0,0 +1,74 @@
+/**
+ * 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.
+ */
+package org.apache.ambari.server.controller.predicate;
+
+import junit.framework.Assert;
+import org.apache.ambari.server.controller.spi.PropertyId;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.junit.Test;
+
+import java.util.Set;
+
+/**
+ *
+ */
+public class AndPredicateTest {
+
+ @Test
+ public void testApply() {
+ Resource resource = new ResourceImpl(Resource.Type.HostComponent);
+ PropertyIdImpl propertyId1 = new PropertyIdImpl("property1", "category1", false);
+ PropertyIdImpl propertyId2 = new PropertyIdImpl("property2", "category1", false);
+ PropertyIdImpl propertyId3 = new PropertyIdImpl("property3", "category1", false);
+
+ EqualsPredicate predicate1 = new EqualsPredicate(propertyId1, "v1");
+ EqualsPredicate predicate2 = new EqualsPredicate(propertyId2, "v2");
+ EqualsPredicate predicate3 = new EqualsPredicate(propertyId3, "v3");
+
+ AndPredicate andPredicate = new AndPredicate(predicate1, predicate2, predicate3);
+
+ resource.setProperty(propertyId1, "v1");
+ resource.setProperty(propertyId2, "monkey");
+ resource.setProperty(propertyId3, "v3");
+ Assert.assertFalse(andPredicate.evaluate(resource));
+
+ resource.setProperty(propertyId2, "v2");
+ Assert.assertTrue(andPredicate.evaluate(resource));
+ }
+
+ @Test
+ public void testGetProperties() {
+ PropertyIdImpl propertyId1 = new PropertyIdImpl("property1", "category1", false);
+ PropertyIdImpl propertyId2 = new PropertyIdImpl("property2", "category1", false);
+ PropertyIdImpl propertyId3 = new PropertyIdImpl("property3", "category1", false);
+
+ EqualsPredicate predicate1 = new EqualsPredicate(propertyId1, "v1");
+ EqualsPredicate predicate2 = new EqualsPredicate(propertyId2, "v2");
+ EqualsPredicate predicate3 = new EqualsPredicate(propertyId3, "v3");
+
+ AndPredicate andPredicate = new AndPredicate(predicate1, predicate2, predicate3);
+
+ Set<PropertyId> ids = andPredicate.getPropertyIds();
+
+ Assert.assertEquals(3, ids.size());
+ Assert.assertTrue(ids.contains(propertyId1));
+ Assert.assertTrue(ids.contains(propertyId2));
+ Assert.assertTrue(ids.contains(propertyId3));
+ }
+
+}
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/EqualsPredicateTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/EqualsPredicateTest.java?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/EqualsPredicateTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/EqualsPredicateTest.java Mon Oct 8 01:37:59 2012
@@ -0,0 +1,58 @@
+/**
+ * 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.
+ */
+package org.apache.ambari.server.controller.predicate;
+
+import junit.framework.Assert;
+import org.apache.ambari.server.controller.spi.Predicate;
+import org.apache.ambari.server.controller.spi.PropertyId;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.junit.Test;
+
+import java.util.Set;
+
+/**
+ *
+ */
+public class EqualsPredicateTest {
+
+ @Test
+ public void testApply() {
+ Resource resource = new ResourceImpl(Resource.Type.HostComponent);
+ PropertyIdImpl propertyId = new PropertyIdImpl("foo", "category1", false);
+ Predicate predicate = new EqualsPredicate(propertyId, "bar");
+
+ resource.setProperty(propertyId, "monkey");
+ Assert.assertFalse(predicate.evaluate(resource));
+
+ resource.setProperty(propertyId, "bar");
+ Assert.assertTrue(predicate.evaluate(resource));
+ }
+
+ @Test
+ public void testGetProperties() {
+ PropertyIdImpl propertyId = new PropertyIdImpl("foo", "category1", false);
+ EqualsPredicate predicate = new EqualsPredicate(propertyId, "bar");
+
+ Set<PropertyId> ids = predicate.getPropertyIds();
+
+ Assert.assertEquals(1, ids.size());
+ Assert.assertTrue(ids.contains(propertyId));
+ }
+
+
+}
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/GreaterEqualsPredicateTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/GreaterEqualsPredicateTest.java?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/GreaterEqualsPredicateTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/GreaterEqualsPredicateTest.java Mon Oct 8 01:37:59 2012
@@ -0,0 +1,59 @@
+/**
+ * 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.
+ */
+package org.apache.ambari.server.controller.predicate;
+
+import junit.framework.Assert;
+import org.apache.ambari.server.controller.spi.Predicate;
+import org.apache.ambari.server.controller.spi.PropertyId;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.junit.Test;
+
+import java.util.Set;
+
+/**
+ *
+ */
+public class GreaterEqualsPredicateTest {
+
+ @Test
+ public void testApply() {
+ Resource resource = new ResourceImpl(Resource.Type.HostComponent);
+ PropertyIdImpl propertyId = new PropertyIdImpl("foo", "category1", false);
+ Predicate predicate = new GreaterEqualsPredicate(propertyId, Comparables.forInteger(10));
+
+ resource.setProperty(propertyId, "1");
+ Assert.assertFalse(predicate.evaluate(resource));
+
+ resource.setProperty(propertyId, "100");
+ Assert.assertTrue(predicate.evaluate(resource));
+
+ resource.setProperty(propertyId, "10");
+ Assert.assertTrue(predicate.evaluate(resource));
+ }
+
+ @Test
+ public void testGetProperties() {
+ PropertyIdImpl propertyId = new PropertyIdImpl("foo", "category1", false);
+ GreaterEqualsPredicate predicate = new GreaterEqualsPredicate(propertyId, Comparables.forInteger(10));
+
+ Set<PropertyId> ids = predicate.getPropertyIds();
+
+ Assert.assertEquals(1, ids.size());
+ Assert.assertTrue(ids.contains(propertyId));
+ }
+}
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/GreaterPredicateTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/GreaterPredicateTest.java?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/GreaterPredicateTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/GreaterPredicateTest.java Mon Oct 8 01:37:59 2012
@@ -0,0 +1,59 @@
+/**
+ * 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.
+ */
+package org.apache.ambari.server.controller.predicate;
+
+import junit.framework.Assert;
+import org.apache.ambari.server.controller.spi.Predicate;
+import org.apache.ambari.server.controller.spi.PropertyId;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.junit.Test;
+
+import java.util.Set;
+
+/**
+ *
+ */
+public class GreaterPredicateTest {
+
+ @Test
+ public void testApply() {
+ Resource resource = new ResourceImpl(Resource.Type.HostComponent);
+ PropertyIdImpl propertyId = new PropertyIdImpl("foo", "category1", false);
+ Predicate predicate = new GreaterPredicate(propertyId, Comparables.forInteger(10));
+
+ resource.setProperty(propertyId, "1");
+ Assert.assertFalse(predicate.evaluate(resource));
+
+ resource.setProperty(propertyId, "100");
+ Assert.assertTrue(predicate.evaluate(resource));
+
+ resource.setProperty(propertyId, "10");
+ Assert.assertFalse(predicate.evaluate(resource));
+ }
+
+ @Test
+ public void testGetProperties() {
+ PropertyIdImpl propertyId = new PropertyIdImpl("foo", "category1", false);
+ GreaterPredicate predicate = new GreaterPredicate(propertyId, Comparables.forInteger(10));
+
+ Set<PropertyId> ids = predicate.getPropertyIds();
+
+ Assert.assertEquals(1, ids.size());
+ Assert.assertTrue(ids.contains(propertyId));
+ }
+}
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/LessEqualsPredicateTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/LessEqualsPredicateTest.java?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/LessEqualsPredicateTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/LessEqualsPredicateTest.java Mon Oct 8 01:37:59 2012
@@ -0,0 +1,59 @@
+/**
+ * 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.
+ */
+package org.apache.ambari.server.controller.predicate;
+
+import junit.framework.Assert;
+import org.apache.ambari.server.controller.spi.Predicate;
+import org.apache.ambari.server.controller.spi.PropertyId;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.junit.Test;
+
+import java.util.Set;
+
+/**
+ *
+ */
+public class LessEqualsPredicateTest {
+
+ @Test
+ public void testApply() {
+ Resource resource = new ResourceImpl(Resource.Type.HostComponent);
+ PropertyIdImpl propertyId = new PropertyIdImpl("foo", "category1", false);
+ Predicate predicate = new LessEqualsPredicate(propertyId, Comparables.forInteger(10));
+
+ resource.setProperty(propertyId, "1");
+ Assert.assertTrue(predicate.evaluate(resource));
+
+ resource.setProperty(propertyId, "100");
+ Assert.assertFalse(predicate.evaluate(resource));
+
+ resource.setProperty(propertyId, "10");
+ Assert.assertTrue(predicate.evaluate(resource));
+ }
+
+ @Test
+ public void testGetProperties() {
+ PropertyIdImpl propertyId = new PropertyIdImpl("foo", "category1", false);
+ LessEqualsPredicate predicate = new LessEqualsPredicate(propertyId, Comparables.forInteger(10));
+
+ Set<PropertyId> ids = predicate.getPropertyIds();
+
+ Assert.assertEquals(1, ids.size());
+ Assert.assertTrue(ids.contains(propertyId));
+ }
+}
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/LessPredicateTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/LessPredicateTest.java?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/LessPredicateTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/LessPredicateTest.java Mon Oct 8 01:37:59 2012
@@ -0,0 +1,59 @@
+/**
+ * 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.
+ */
+package org.apache.ambari.server.controller.predicate;
+
+import junit.framework.Assert;
+import org.apache.ambari.server.controller.spi.Predicate;
+import org.apache.ambari.server.controller.spi.PropertyId;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.junit.Test;
+
+import java.util.Set;
+
+/**
+ *
+ */
+public class LessPredicateTest {
+
+ @Test
+ public void testApply() {
+ Resource resource = new ResourceImpl(Resource.Type.HostComponent);
+ PropertyIdImpl propertyId = new PropertyIdImpl("foo", "category1", false);
+ Predicate predicate = new LessPredicate(propertyId, Comparables.forInteger(10));
+
+ resource.setProperty(propertyId, "1");
+ Assert.assertTrue(predicate.evaluate(resource));
+
+ resource.setProperty(propertyId, "100");
+ Assert.assertFalse(predicate.evaluate(resource));
+
+ resource.setProperty(propertyId, "10");
+ Assert.assertFalse(predicate.evaluate(resource));
+ }
+
+ @Test
+ public void testGetProperties() {
+ PropertyIdImpl propertyId = new PropertyIdImpl("foo", "category1", false);
+ LessPredicate predicate = new LessPredicate(propertyId, Comparables.forInteger(10));
+
+ Set<PropertyId> ids = predicate.getPropertyIds();
+
+ Assert.assertEquals(1, ids.size());
+ Assert.assertTrue(ids.contains(propertyId));
+ }
+}
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/NotPredicateTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/NotPredicateTest.java?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/NotPredicateTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/NotPredicateTest.java Mon Oct 8 01:37:59 2012
@@ -0,0 +1,58 @@
+/**
+ * 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.
+ */
+package org.apache.ambari.server.controller.predicate;
+
+import junit.framework.Assert;
+import org.apache.ambari.server.controller.spi.PropertyId;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.junit.Test;
+
+import java.util.Set;
+
+/**
+ *
+ */
+public class NotPredicateTest {
+
+ @Test
+ public void testApply() {
+ Resource resource = new ResourceImpl(Resource.Type.HostComponent);
+ PropertyIdImpl propertyId = new PropertyIdImpl("foo", "category1", false);
+ EqualsPredicate predicate = new EqualsPredicate(propertyId, "bar");
+ NotPredicate notPredicate = new NotPredicate(predicate);
+
+ resource.setProperty(propertyId, "monkey");
+ Assert.assertTrue(notPredicate.evaluate(resource));
+
+ resource.setProperty(propertyId, "bar");
+ Assert.assertFalse(notPredicate.evaluate(resource));
+ }
+
+ @Test
+ public void testGetProperties() {
+ PropertyIdImpl propertyId = new PropertyIdImpl("foo", "category1", false);
+ EqualsPredicate predicate = new EqualsPredicate(propertyId, "bar");
+
+ Set<PropertyId> ids = predicate.getPropertyIds();
+
+ Assert.assertEquals(1, ids.size());
+ Assert.assertTrue(ids.contains(propertyId));
+ }
+
+
+}
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/OrPredicateTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/OrPredicateTest.java?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/OrPredicateTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/OrPredicateTest.java Mon Oct 8 01:37:59 2012
@@ -0,0 +1,74 @@
+/**
+ * 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.
+ */
+package org.apache.ambari.server.controller.predicate;
+
+import junit.framework.Assert;
+import org.apache.ambari.server.controller.spi.PropertyId;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.junit.Test;
+
+import java.util.Set;
+
+/**
+ *
+ */
+public class OrPredicateTest {
+
+ @Test
+ public void testApply() {
+ Resource resource = new ResourceImpl(Resource.Type.HostComponent);
+ PropertyIdImpl propertyId1 = new PropertyIdImpl("property1", "category1", false);
+ PropertyIdImpl propertyId2 = new PropertyIdImpl("property2", "category1", false);
+ PropertyIdImpl propertyId3 = new PropertyIdImpl("property3", "category1", false);
+
+ EqualsPredicate predicate1 = new EqualsPredicate(propertyId1, "v1");
+ EqualsPredicate predicate2 = new EqualsPredicate(propertyId2, "v2");
+ EqualsPredicate predicate3 = new EqualsPredicate(propertyId3, "v3");
+
+ OrPredicate orPredicate = new OrPredicate(predicate1, predicate2, predicate3);
+
+ resource.setProperty(propertyId1, "big");
+ resource.setProperty(propertyId2, "monkey");
+ resource.setProperty(propertyId3, "runner");
+ Assert.assertFalse(orPredicate.evaluate(resource));
+
+ resource.setProperty(propertyId2, "v2");
+ Assert.assertTrue(orPredicate.evaluate(resource));
+ }
+
+ @Test
+ public void testGetProperties() {
+ PropertyIdImpl propertyId1 = new PropertyIdImpl("property1", "category1", false);
+ PropertyIdImpl propertyId2 = new PropertyIdImpl("property2", "category1", false);
+ PropertyIdImpl propertyId3 = new PropertyIdImpl("property3", "category1", false);
+
+ EqualsPredicate predicate1 = new EqualsPredicate(propertyId1, "v1");
+ EqualsPredicate predicate2 = new EqualsPredicate(propertyId2, "v2");
+ EqualsPredicate predicate3 = new EqualsPredicate(propertyId3, "v3");
+
+ OrPredicate orPredicate = new OrPredicate(predicate1, predicate2, predicate3);
+
+ Set<PropertyId> ids = orPredicate.getPropertyIds();
+
+ Assert.assertEquals(3, ids.size());
+ Assert.assertTrue(ids.contains(propertyId1));
+ Assert.assertTrue(ids.contains(propertyId2));
+ Assert.assertTrue(ids.contains(propertyId3));
+ }
+
+}
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/PredicateVisitorTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/PredicateVisitorTest.java?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/PredicateVisitorTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/PredicateVisitorTest.java Mon Oct 8 01:37:59 2012
@@ -0,0 +1,82 @@
+/**
+ * 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.
+ */
+package org.apache.ambari.server.controller.predicate;
+
+import junit.framework.Assert;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class PredicateVisitorTest {
+
+ @Test
+ public void testVisitor() {
+
+ PropertyIdImpl propertyId = new PropertyIdImpl("foo", "category1", false);
+ EqualsPredicate equalsPredicate = new EqualsPredicate(propertyId, "bar");
+
+ TestPredicateVisitor visitor = new TestPredicateVisitor();
+ equalsPredicate.accept(visitor);
+
+ Assert.assertSame(equalsPredicate, visitor.visitedComparisonPredicate);
+ Assert.assertNull(visitor.visitedArrayPredicate);
+ Assert.assertNull(visitor.visitedUnaryPredicate);
+
+ AndPredicate andPredicate = new AndPredicate(equalsPredicate);
+
+ visitor = new TestPredicateVisitor();
+ andPredicate.accept(visitor);
+
+ Assert.assertNull(visitor.visitedComparisonPredicate);
+ Assert.assertSame(andPredicate, visitor.visitedArrayPredicate);
+ Assert.assertNull(visitor.visitedUnaryPredicate);
+
+ NotPredicate notPredicate = new NotPredicate(andPredicate);
+
+ visitor = new TestPredicateVisitor();
+ notPredicate.accept(visitor);
+
+ Assert.assertNull(visitor.visitedComparisonPredicate);
+ Assert.assertNull(visitor.visitedArrayPredicate);
+ Assert.assertSame(notPredicate, visitor.visitedUnaryPredicate);
+ }
+
+ public static class TestPredicateVisitor implements PredicateVisitor {
+
+ ComparisonPredicate visitedComparisonPredicate = null;
+ ArrayPredicate visitedArrayPredicate = null;
+ UnaryPredicate visitedUnaryPredicate = null;
+
+ @Override
+ public void acceptComparisonPredicate(ComparisonPredicate predicate) {
+ visitedComparisonPredicate = predicate;
+ }
+
+ @Override
+ public void acceptArrayPredicate(ArrayPredicate predicate) {
+ visitedArrayPredicate = predicate;
+ }
+
+ @Override
+ public void acceptUnaryPredicate(UnaryPredicate predicate) {
+ visitedUnaryPredicate = predicate;
+ }
+ }
+
+}
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/PropertyIdImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/PropertyIdImpl.java?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/PropertyIdImpl.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/PropertyIdImpl.java Mon Oct 8 01:37:59 2012
@@ -0,0 +1,121 @@
+/**
+ * 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.
+ */
+
+package org.apache.ambari.server.controller.predicate;
+
+import org.apache.ambari.server.controller.spi.PropertyId;
+
+/**
+ * Simple PropertyId implementation.
+ */
+public class PropertyIdImpl implements PropertyId {
+ /**
+ * The property name.
+ */
+ private String name;
+
+ /**
+ * The category name.
+ */
+ private String category;
+
+ /**
+ * Indicates whether or not this property is temporal.
+ */
+ private boolean temporal;
+
+
+ // ----- Constructors ------------------------------------------------------
+
+ /**
+ * Create a property id. Required for JSON serialization.
+ */
+ public PropertyIdImpl() {
+ }
+
+ /**
+ * Create a property id.
+ *
+ * @param name the property name.
+ * @param category the property category.
+ * @param temporal a temporal indicator
+ */
+ public PropertyIdImpl(String name, String category, boolean temporal) {
+ this.name = name;
+ this.category = category;
+ this.temporal = temporal;
+ }
+
+
+ // ----- PropertyId --------------------------------------------------------
+
+ public String getName() {
+ return name;
+ }
+
+ public String getCategory() {
+ return category;
+ }
+
+ public boolean isTemporal() {
+ return temporal;
+ }
+
+
+ // ----- Object overrides --------------------------------------------------
+
+ @Override
+ public int hashCode() {
+ return name.hashCode() +
+ (category == null ? 0 : category.hashCode()) +
+ (temporal ? 1 : 0);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+
+ if (this == o) {
+ return true;
+ }
+
+ if (!(o.getClass().equals(PropertyIdImpl.class))) {
+ return false;
+ }
+
+ PropertyIdImpl that = (PropertyIdImpl) o;
+
+ return this.name.equals(that.getName()) &&
+ equals(this.category, that.getCategory()) &&
+ this.isTemporal() == that.isTemporal();
+ }
+
+ @Override
+ public String toString() {
+ return "PropertyId[" + category + ", " + name + "]";
+ }
+
+
+ // ----- helper methods ----------------------------------------------------
+
+ private static boolean equals(Object o1, Object o2) {
+ if (o1 == null) {
+ return o2 == null;
+ }
+ return o2 != null && o1.equals(o2);
+ }
+}
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/ResourceImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/ResourceImpl.java?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/ResourceImpl.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/ResourceImpl.java Mon Oct 8 01:37:59 2012
@@ -0,0 +1,127 @@
+/**
+ * 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.
+ */
+
+package org.apache.ambari.server.controller.predicate;
+
+import org.apache.ambari.server.controller.spi.PropertyId;
+import org.apache.ambari.server.controller.spi.Resource;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Simple resource implementation.
+ */
+public class ResourceImpl implements Resource {
+
+ /**
+ * The resource type.
+ */
+ private final Type type;
+
+ /**
+ * The map of categories/properties for this resource.
+ */
+ private final Map<String, Map<String, String>> categories = new HashMap<String, Map<String, String>>();
+
+
+ // ----- Constructors ------------------------------------------------------
+
+ /**
+ * Create a resource of the given type.
+ *
+ * @param type the resource type
+ */
+ public ResourceImpl(Type type) {
+ this.type = type;
+ }
+
+
+ // ----- Resource ----------------------------------------------------------
+
+ @Override
+ public Type getType() {
+ return type;
+ }
+
+ @Override
+ public Map<String, Map<String, String>> getCategories() {
+ return categories;
+ }
+
+ @Override
+ public void setProperty(PropertyId id, String value) {
+ String category = id.getCategory();
+
+ Map<String, String> properties = categories.get(category);
+
+ if (properties == null) {
+ properties = new HashMap<String, String>();
+ categories.put(category, properties);
+ }
+
+ properties.put(id.getName(), value);
+ }
+
+ @Override
+ public void setProperty(PropertyId id, Integer value) {
+ setProperty(id, value.toString());
+ }
+
+ @Override
+ public void setProperty(PropertyId id, Float value) {
+ setProperty(id, value.toString());
+ }
+
+ @Override
+ public void setProperty(PropertyId id, Double value) {
+ setProperty(id, value.toString());
+ }
+
+ @Override
+ public void setProperty(PropertyId id, Long value) {
+ setProperty(id, value.toString());
+ }
+
+ @Override
+ public String getPropertyValue(PropertyId id) {
+
+ Map<String, String> properties = categories.get(id.getCategory());
+
+ if (properties != null) {
+ return properties.get(id.getName());
+ }
+ return null;
+ }
+
+
+ // ----- Object overrides --------------------------------------------------
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+
+ sb.append("Resource : ").append(type).append("\n");
+ for (Map.Entry<String, Map<String, String>> catEntry : categories.entrySet()) {
+ for (Map.Entry<String, String> propEntry : catEntry.getValue().entrySet()) {
+ sb.append(" ").append(catEntry.getKey()).append(".").append(propEntry.getKey()).append(" : ").append(propEntry.getValue()).append("\n");
+ }
+ }
+ return sb.toString();
+ }
+}
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/TestClusterImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/TestClusterImpl.java?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/TestClusterImpl.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/TestClusterImpl.java Mon Oct 8 01:37:59 2012
@@ -0,0 +1,135 @@
+/**
+ * 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.
+ */
+
+package org.apache.ambari.server.state.cluster;
+
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.agent.DiskInfo;
+import org.apache.ambari.server.agent.HostInfo;
+import org.apache.ambari.server.state.AgentVersion;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.Clusters;
+import org.apache.ambari.server.state.State;
+import org.apache.ambari.server.state.HostState;
+import org.apache.ambari.server.state.StackVersion;
+import org.apache.ambari.server.state.cluster.ClustersImpl;
+import org.apache.ambari.server.state.fsm.InvalidStateTransitonException;
+import org.apache.ambari.server.state.host.HostHealthyHeartbeatEvent;
+import org.apache.ambari.server.state.host.HostRegistrationRequestEvent;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestClusterImpl {
+
+ private Clusters clusters;
+ private Cluster c1;
+ String h1 = "h1";
+ String s1 = "s1";
+ String sc1 = "sc1";
+
+ @Before
+ public void setup() throws AmbariException {
+ clusters = new ClustersImpl();
+ clusters.addCluster("c1");
+ c1 = clusters.getCluster("c1");
+ Assert.assertEquals("c1", c1.getClusterName());
+ clusters.addHost(h1);
+ clusters.mapHostToCluster(h1, "c1");
+ }
+
+ @After
+ public void teardown() {
+ clusters = null;
+ c1 = null;
+ }
+
+ @Test
+ public void testAddHost() throws AmbariException {
+ clusters.addHost("h2");
+
+ try {
+ clusters.addHost("h2");
+ fail("Duplicate add should fail");
+ }
+ catch (AmbariException e) {
+ // Expected
+ }
+
+ }
+
+
+ @Test
+ public void testGetHostState() throws AmbariException {
+ Assert.assertEquals(HostState.INIT, clusters.getHost(h1).getState());
+ }
+
+ @Test
+ public void testSetHostState() throws AmbariException {
+ clusters.getHost(h1).setState(HostState.HEARTBEAT_LOST);
+ Assert.assertEquals(HostState.HEARTBEAT_LOST,
+ clusters.getHost(h1).getState());
+ }
+
+ @Test
+ public void testHostEvent() throws AmbariException,
+ InvalidStateTransitonException {
+ HostInfo hostInfo = new HostInfo();
+ hostInfo.setHostName(h1);
+ hostInfo.setInterfaces("fip_4");
+ hostInfo.setArchitecture("os_arch");
+ hostInfo.setOS("os_type");
+ hostInfo.setMemoryTotal(10);
+ hostInfo.setMemorySize(100);
+ hostInfo.setProcessorCount(10);
+ List<DiskInfo> mounts = new ArrayList<DiskInfo>();
+ mounts.add(new DiskInfo("/dev/sda", "/mnt/disk1",
+ "5000000", "4000000", "10%", "size"));
+ hostInfo.setMounts(mounts);
+
+ AgentVersion agentVersion = new AgentVersion("0.0.x");
+ long currentTime = 1001;
+
+ clusters.getHost(h1).handleEvent(new HostRegistrationRequestEvent(
+ h1, agentVersion, currentTime, hostInfo));
+
+ Assert.assertEquals(HostState.WAITING_FOR_HOST_STATUS_UPDATES,
+ clusters.getHost(h1).getState());
+
+ clusters.getHost(h1).setState(HostState.HEARTBEAT_LOST);
+
+ try {
+ clusters.getHost(h1).handleEvent(
+ new HostHealthyHeartbeatEvent(h1, currentTime));
+ fail("Exception should be thrown on invalid event");
+ }
+ catch (InvalidStateTransitonException e) {
+ // Expected
+ }
+
+ }
+
+
+}
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/TestClustersImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/TestClustersImpl.java?rev=1395430&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/TestClustersImpl.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/TestClustersImpl.java Mon Oct 8 01:37:59 2012
@@ -0,0 +1,173 @@
+/**
+ * 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.
+ */
+
+package org.apache.ambari.server.state.cluster;
+
+import static org.junit.Assert.fail;
+
+import java.util.List;
+import java.util.Set;
+
+import junit.framework.Assert;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.ClusterNotFoundException;
+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.cluster.ClustersImpl;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestClustersImpl {
+
+ private Clusters clusters;
+
+ @Before
+ public void setup() {
+ clusters = new ClustersImpl();
+ }
+
+ @After
+ public void teardown() {
+ clusters = null;
+ }
+
+ @Test
+ public void testGetInvalidCluster() throws AmbariException {
+ try {
+ clusters.getCluster("foo");
+ fail("Exception should be thrown on invalid get");
+ }
+ catch (ClusterNotFoundException e) {
+ // Expected
+ }
+
+ }
+
+ @Test
+ public void testAddAndGetCluster() throws AmbariException {
+
+ String c1 = "foo";
+ String c2 = "foo";
+ clusters.addCluster(c1);
+
+ try {
+ clusters.addCluster(c1);
+ fail("Exception should be thrown on invalid add");
+ }
+ catch (AmbariException e) {
+ // Expected
+ }
+
+ try {
+ clusters.addCluster(c2);
+ fail("Exception should be thrown on invalid add");
+ }
+ catch (AmbariException e) {
+ // Expected
+ }
+
+ c2 = "foo2";
+ clusters.addCluster(c2);
+
+ Assert.assertNotNull(clusters.getCluster(c1));
+ Assert.assertNotNull(clusters.getCluster(c2));
+
+ Assert.assertEquals(c1, clusters.getCluster(c1).getClusterName());
+ Assert.assertEquals(c2, clusters.getCluster(c2).getClusterName());
+
+ }
+
+
+ @Test
+ public void testAddAndGetHost() throws AmbariException {
+ String h1 = "h1";
+ String h2 = "h2";
+ String h3 = "h3";
+
+ clusters.addHost(h1);
+
+ try {
+ clusters.addHost(h1);
+ fail("Expected exception on duplicate host entry");
+ } catch (Exception e) {
+ // Expected
+ }
+
+ clusters.addHost(h2);
+ clusters.addHost(h3);
+
+ List<Host> hosts = clusters.getHosts();
+ Assert.assertEquals(3, hosts.size());
+
+ Assert.assertNotNull(clusters.getHost(h1));
+ Assert.assertNotNull(clusters.getHost(h2));
+ Assert.assertNotNull(clusters.getHost(h3));
+
+ }
+
+ @Test
+ public void testClusterHostMapping() throws AmbariException {
+ String c1 = "c1";
+ String c2 = "c2";
+ String h1 = "h1";
+ String h2 = "h2";
+ String h3 = "h3";
+
+ try {
+ clusters.mapHostToCluster(h1, c1);
+ fail("Expected exception for invalid cluster/host");
+ } catch (Exception e) {
+ // Expected
+ }
+
+ clusters.addCluster(c1);
+ clusters.addCluster(c2);
+ Assert.assertNotNull(clusters.getCluster(c1));
+ Assert.assertNotNull(clusters.getCluster(c2));
+ try {
+ clusters.mapHostToCluster(h1, c1);
+ fail("Expected exception for invalid host");
+ } catch (Exception e) {
+ // Expected
+ }
+
+ clusters.addHost(h1);
+ clusters.addHost(h2);
+ clusters.addHost(h3);
+ Assert.assertNotNull(clusters.getHost(h1));
+
+ clusters.mapHostToCluster(h1, c1);
+ clusters.mapHostToCluster(h1, c2);
+ clusters.mapHostToCluster(h2, c1);
+ clusters.mapHostToCluster(h2, c2);
+ clusters.mapHostToCluster(h1, c2);
+
+ Set<Cluster> c = clusters.getClustersForHost(h3);
+ Assert.assertEquals(0, c.size());
+
+ c = clusters.getClustersForHost(h1);
+ Assert.assertEquals(2, c.size());
+
+ c = clusters.getClustersForHost(h2);
+ Assert.assertEquals(2, c.size());
+
+ }
+}