Repository: ambari Updated Branches: refs/heads/trunk d212ea58b -> deb151713
AMBARI-14692 Ambari Server Unit Test failures on trunk (org.apache.ambari.server.agent.AgentResourceTest) (dsen) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/deb15171 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/deb15171 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/deb15171 Branch: refs/heads/trunk Commit: deb15171388ae965fdda92467378908ca09c0f8d Parents: d212ea5 Author: Dmytro Sen <d...@apache.org> Authored: Fri Jan 15 18:29:00 2016 +0200 Committer: Dmytro Sen <d...@apache.org> Committed: Fri Jan 15 18:29:37 2016 +0200 ---------------------------------------------------------------------- .../ambari/server/RandomPortJerseyTest.java | 64 ++++++++++++++++++++ .../ambari/server/agent/AgentResourceTest.java | 13 ++-- .../server/api/services/PersistServiceTest.java | 7 ++- 3 files changed, 75 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/deb15171/ambari-server/src/test/java/org/apache/ambari/server/RandomPortJerseyTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/RandomPortJerseyTest.java b/ambari-server/src/test/java/org/apache/ambari/server/RandomPortJerseyTest.java new file mode 100644 index 0000000..b4269f1 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/RandomPortJerseyTest.java @@ -0,0 +1,64 @@ +/** + * 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; + +import com.sun.jersey.test.framework.AppDescriptor; +import com.sun.jersey.test.framework.JerseyTest; + +import java.io.IOException; +import java.net.ServerSocket; + +/** + * Makes JerseyTest use random port in case default one is unavailable. + */ +public class RandomPortJerseyTest extends JerseyTest { + private static int testPort; + + public RandomPortJerseyTest(AppDescriptor ad) { + super(ad); + } + + @Override + protected int getPort(int defaultPort) { + ServerSocket server = null; + int port = -1; + try { + server = new ServerSocket(defaultPort); + port = server.getLocalPort(); + } catch (IOException e) { + } finally { + if (server != null) { + try { + server.close(); + } catch (IOException e) { + } + } + } + if ((port != -1) || (defaultPort == 0)) { + this.testPort = port; + return port; + } + return getPort(0); + } + + public int getTestPort() { + return testPort; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/deb15171/ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java index 273158f..510e1fb 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java @@ -26,6 +26,7 @@ import javax.ws.rs.core.MediaType; import junit.framework.Assert; +import org.apache.ambari.server.RandomPortJerseyTest; import org.apache.ambari.server.actionmanager.ActionDBAccessor; import org.apache.ambari.server.actionmanager.ActionManager; import org.apache.ambari.server.actionmanager.HostRoleCommandFactory; @@ -87,10 +88,10 @@ 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.spi.container.servlet.ServletContainer; -import com.sun.jersey.test.framework.JerseyTest; import com.sun.jersey.test.framework.WebAppDescriptor; -public class AgentResourceTest extends JerseyTest { + +public class AgentResourceTest extends RandomPortJerseyTest { static String PACKAGE_NAME = "org.apache.ambari.server.agent.rest"; private static Log LOG = LogFactory.getLog(AgentResourceTest.class); protected Client client; @@ -156,7 +157,7 @@ public class AgentResourceTest extends JerseyTest { 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"); + WebResource webResource = client.resource(String.format("http://localhost:%d/register/dummyhost", getTestPort())); response = webResource.type(MediaType.APPLICATION_JSON) .post(RegistrationResponse.class, createDummyJSONRegister()); LOG.info("Returned from Server responce=" + response); @@ -169,7 +170,7 @@ public class AgentResourceTest extends JerseyTest { 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"); + WebResource webResource = client.resource(String.format("http://localhost:%d/heartbeat/dummyhost", getTestPort())); response = webResource.type(MediaType.APPLICATION_JSON) .post(HeartBeatResponse.class, createDummyHeartBeat()); LOG.info("Returned from Server: " @@ -183,7 +184,7 @@ public class AgentResourceTest extends JerseyTest { 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"); + WebResource webResource = client.resource(String.format("http://localhost:%d/heartbeat/dummyhost", getTestPort())); response = webResource.type(MediaType.APPLICATION_JSON) .post(HeartBeatResponse.class, createDummyHeartBeatWithAgentEnv()); LOG.info("Returned from Server: " @@ -263,7 +264,7 @@ public class AgentResourceTest extends JerseyTest { ClientConfig clientConfig = new DefaultClientConfig(); clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); client = Client.create(clientConfig); - WebResource webResource = client.resource("http://localhost:9998/components/dummycluster"); + WebResource webResource = client.resource(String.format("http://localhost:%d/components/dummycluster", getTestPort())); response = webResource.get(ComponentsResponse.class); Assert.assertEquals(response.getClusterName(), "dummycluster"); } http://git-wip-us.apache.org/repos/asf/ambari/blob/deb15171/ambari-server/src/test/java/org/apache/ambari/server/api/services/PersistServiceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/PersistServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/services/PersistServiceTest.java index bb11843..3b738c0 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/api/services/PersistServiceTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/PersistServiceTest.java @@ -24,6 +24,7 @@ import java.util.Map; import com.google.inject.persist.PersistService; import junit.framework.Assert; +import org.apache.ambari.server.RandomPortJerseyTest; import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; import org.apache.ambari.server.utils.StageUtils; @@ -47,7 +48,7 @@ import com.sun.jersey.spi.container.servlet.ServletContainer; import com.sun.jersey.test.framework.JerseyTest; import com.sun.jersey.test.framework.WebAppDescriptor; -public class PersistServiceTest extends JerseyTest { +public class PersistServiceTest extends RandomPortJerseyTest { static String PACKAGE_NAME = "org.apache.ambari.server.api.services"; private static Log LOG = LogFactory.getLog(PersistServiceTest.class); Injector injector; @@ -89,7 +90,7 @@ public class PersistServiceTest extends JerseyTest { ClientConfig clientConfig = new DefaultClientConfig(); clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); client = Client.create(clientConfig); - WebResource webResource = client.resource("http://localhost:9998/persist"); + WebResource webResource = client.resource(String.format("http://localhost:%d/persist", getTestPort())); webResource.post("{\"xyx\" : \"t\"}"); LOG.info("Done posting to the server"); @@ -98,7 +99,7 @@ public class PersistServiceTest extends JerseyTest { Map<String, String> jsonOutput = StageUtils.fromJson(output, Map.class); String value = jsonOutput.get("xyx"); Assert.assertEquals("t", value); - webResource = client.resource("http://localhost:9998/persist/xyx"); + webResource = client.resource(String.format("http://localhost:%d/persist/xyx", getTestPort())); output = webResource.get(String.class); Assert.assertEquals("t", output); LOG.info("Value for xyx " + output);