|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.agent; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertFalse; |
| 21 | +import static org.junit.Assert.assertNotNull; |
| 22 | +import static org.junit.Assert.assertSame; |
| 23 | +import static org.junit.Assert.assertTrue; |
| 24 | +import static org.mockito.Mockito.any; |
| 25 | +import static org.mockito.Mockito.doReturn; |
| 26 | +import static org.mockito.Mockito.doThrow; |
| 27 | +import static org.mockito.Mockito.mock; |
| 28 | +import static org.mockito.Mockito.eq; |
| 29 | +import static org.mockito.Mockito.times; |
| 30 | +import static org.mockito.Mockito.verify; |
| 31 | +import static org.mockito.Mockito.when; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.net.InetSocketAddress; |
| 35 | + |
| 36 | +import javax.naming.ConfigurationException; |
| 37 | + |
| 38 | +import org.apache.logging.log4j.Logger; |
| 39 | +import org.junit.Before; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.runner.RunWith; |
| 42 | +import org.mockito.junit.MockitoJUnitRunner; |
| 43 | +import org.springframework.test.util.ReflectionTestUtils; |
| 44 | + |
| 45 | +import com.cloud.resource.ServerResource; |
| 46 | +import com.cloud.utils.backoff.impl.ConstantTimeBackoff; |
| 47 | +import com.cloud.utils.nio.Link; |
| 48 | +import com.cloud.utils.nio.NioConnection; |
| 49 | + |
| 50 | +@RunWith(MockitoJUnitRunner.class) |
| 51 | +public class AgentTest { |
| 52 | + Agent agent; |
| 53 | + private AgentShell shell; |
| 54 | + private ServerResource serverResource; |
| 55 | + private Logger logger; |
| 56 | + |
| 57 | + @Before |
| 58 | + public void setUp() throws ConfigurationException { |
| 59 | + shell = mock(AgentShell.class); |
| 60 | + serverResource = mock(ServerResource.class); |
| 61 | + doReturn(true).when(serverResource).configure(any(), any()); |
| 62 | + doReturn(1).when(shell).getWorkers(); |
| 63 | + doReturn(1).when(shell).getPingRetries(); |
| 64 | + agent = new Agent(shell, 1, serverResource); |
| 65 | + logger = mock(Logger.class); |
| 66 | + ReflectionTestUtils.setField(agent, "logger", logger); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testGetLinkLogNullLinkReturnsEmptyString() { |
| 71 | + Link link = null; |
| 72 | + String result = agent.getLinkLog(link); |
| 73 | + assertEquals("", result); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testGetLinkLogLinkWithTraceEnabledReturnsLinkLogWithHashCode() { |
| 78 | + Link link = mock(Link.class); |
| 79 | + InetSocketAddress socketAddress = mock(InetSocketAddress.class); |
| 80 | + when(socketAddress.toString()).thenReturn("192.168.1.100"); |
| 81 | + when(link.getSocketAddress()).thenReturn(socketAddress); |
| 82 | + when(logger.isTraceEnabled()).thenReturn(true); |
| 83 | + |
| 84 | + String result = agent.getLinkLog(link); |
| 85 | + System.out.println(result); |
| 86 | + assertTrue(result.startsWith(System.identityHashCode(link) + "-")); |
| 87 | + assertTrue(result.contains("192.168.1.100")); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testGetAgentNameWhenServerResourceIsNull() { |
| 92 | + ReflectionTestUtils.setField(agent, "serverResource", null); |
| 93 | + assertEquals("Agent", agent.getAgentName()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testGetAgentNameWhenAppendAgentNameIsTrue() { |
| 98 | + when(serverResource.isAppendAgentNameToLogs()).thenReturn(true); |
| 99 | + when(serverResource.getName()).thenReturn("TestAgent"); |
| 100 | + |
| 101 | + String agentName = agent.getAgentName(); |
| 102 | + assertEquals("TestAgent", agentName); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testGetAgentNameWhenAppendAgentNameIsFalse() { |
| 107 | + when(serverResource.isAppendAgentNameToLogs()).thenReturn(false); |
| 108 | + |
| 109 | + String agentName = agent.getAgentName(); |
| 110 | + assertEquals("Agent", agentName); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testAgentInitialization() { |
| 115 | + Runtime.getRuntime().removeShutdownHook(agent.shutdownThread); |
| 116 | + when(shell.getPingRetries()).thenReturn(3); |
| 117 | + when(shell.getWorkers()).thenReturn(5); |
| 118 | + agent.setupShutdownHookAndInitExecutors(); |
| 119 | + assertNotNull(agent.selfTaskExecutor); |
| 120 | + assertNotNull(agent.outRequestHandler); |
| 121 | + assertNotNull(agent.requestHandler); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testAgentShutdownHookAdded() { |
| 126 | + Runtime.getRuntime().removeShutdownHook(agent.shutdownThread); |
| 127 | + when(logger.isTraceEnabled()).thenReturn(true); |
| 128 | + agent.setupShutdownHookAndInitExecutors(); |
| 129 | + verify(logger).trace("Adding shutdown hook"); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testGetResourceGuidValidGuidAndResourceName() { |
| 134 | + when(shell.getGuid()).thenReturn("12345"); |
| 135 | + String result = agent.getResourceGuid(); |
| 136 | + assertTrue(result.startsWith("12345-" + ServerResource.class.getSimpleName())); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testGetZoneReturnsValidZone() { |
| 141 | + when(shell.getZone()).thenReturn("ZoneA"); |
| 142 | + String result = agent.getZone(); |
| 143 | + assertEquals("ZoneA", result); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testGetPodReturnsValidPod() { |
| 148 | + when(shell.getPod()).thenReturn("PodA"); |
| 149 | + String result = agent.getPod(); |
| 150 | + assertEquals("PodA", result); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testSetLinkAssignsLink() { |
| 155 | + Link mockLink = mock(Link.class); |
| 156 | + agent.setLink(mockLink); |
| 157 | + assertEquals(mockLink, agent.link); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void testGetResourceReturnsServerResource() { |
| 162 | + ServerResource mockResource = mock(ServerResource.class); |
| 163 | + ReflectionTestUtils.setField(agent, "serverResource", mockResource); |
| 164 | + ServerResource result = agent.getResource(); |
| 165 | + assertSame(mockResource, result); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void testGetResourceName() { |
| 170 | + String result = agent.getResourceName(); |
| 171 | + assertTrue(result.startsWith(ServerResource.class.getSimpleName())); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void testUpdateLastPingResponseTimeUpdatesCurrentTime() { |
| 176 | + long beforeUpdate = System.currentTimeMillis(); |
| 177 | + agent.updateLastPingResponseTime(); |
| 178 | + long updatedTime = agent.lastPingResponseTime.get(); |
| 179 | + assertTrue(updatedTime >= beforeUpdate); |
| 180 | + assertTrue(updatedTime <= System.currentTimeMillis()); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void testGetNextSequenceIncrementsSequence() { |
| 185 | + long initialSequence = agent.getNextSequence(); |
| 186 | + long nextSequence = agent.getNextSequence(); |
| 187 | + assertEquals(initialSequence + 1, nextSequence); |
| 188 | + long thirdSequence = agent.getNextSequence(); |
| 189 | + assertEquals(nextSequence + 1, thirdSequence); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + public void testRegisterControlListenerAddsListener() { |
| 194 | + IAgentControlListener listener = mock(IAgentControlListener.class); |
| 195 | + agent.registerControlListener(listener); |
| 196 | + assertTrue(agent.controlListeners.contains(listener)); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + public void testUnregisterControlListenerRemovesListener() { |
| 201 | + IAgentControlListener listener = mock(IAgentControlListener.class); |
| 202 | + agent.registerControlListener(listener); |
| 203 | + assertTrue(agent.controlListeners.contains(listener)); |
| 204 | + agent.unregisterControlListener(listener); |
| 205 | + assertFalse(agent.controlListeners.contains(listener)); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + public void testCloseAndTerminateLinkLinkIsNullDoesNothing() { |
| 210 | + agent.closeAndTerminateLink(null); |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + public void testCloseAndTerminateLinkValidLinkCallsCloseAndTerminate() { |
| 215 | + Link mockLink = mock(Link.class); |
| 216 | + agent.closeAndTerminateLink(mockLink); |
| 217 | + verify(mockLink).close(); |
| 218 | + verify(mockLink).terminated(); |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + public void testStopAndCleanupConnectionConnectionIsNullDoesNothing() { |
| 223 | + agent.connection = null; |
| 224 | + agent.stopAndCleanupConnection(false); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + public void testStopAndCleanupConnectionValidConnectionNoWaitStopsAndCleansUp() throws IOException { |
| 229 | + NioConnection mockConnection = mock(NioConnection.class); |
| 230 | + agent.connection = mockConnection; |
| 231 | + agent.stopAndCleanupConnection(false); |
| 232 | + verify(mockConnection).stop(); |
| 233 | + verify(mockConnection).cleanUp(); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + public void testStopAndCleanupConnectionCleanupThrowsIOExceptionLogsWarning() throws IOException { |
| 238 | + NioConnection mockConnection = mock(NioConnection.class); |
| 239 | + agent.connection = mockConnection; |
| 240 | + doThrow(new IOException("Cleanup failed")).when(mockConnection).cleanUp(); |
| 241 | + agent.stopAndCleanupConnection(false); |
| 242 | + verify(mockConnection).stop(); |
| 243 | + verify(logger).warn(eq("Fail to clean up old connection. {}"), any(IOException.class)); |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + public void testStopAndCleanupConnectionValidConnectionWaitForStopWaitsForStartupToStop() throws IOException { |
| 248 | + NioConnection mockConnection = mock(NioConnection.class); |
| 249 | + ConstantTimeBackoff mockBackoff = mock(ConstantTimeBackoff.class); |
| 250 | + mockBackoff.setTimeToWait(0); |
| 251 | + agent.connection = mockConnection; |
| 252 | + when(shell.getBackoffAlgorithm()).thenReturn(mockBackoff); |
| 253 | + when(mockConnection.isStartup()).thenReturn(true, true, false); |
| 254 | + agent.stopAndCleanupConnection(true); |
| 255 | + verify(mockConnection).stop(); |
| 256 | + verify(mockConnection).cleanUp(); |
| 257 | + verify(mockBackoff, times(3)).waitBeforeRetry(); |
| 258 | + } |
| 259 | +} |
0 commit comments