1 package net.sourceforge.cruisecontrol.distributed; 2 3 import junit.framework.TestCase; 4 5 import java.util.Arrays ; 6 import java.util.Date ; 7 import java.util.Map ; 8 import java.util.HashMap ; 9 import java.io.File ; 10 import java.rmi.RemoteException ; 11 12 import org.jdom.Element; 13 import org.apache.log4j.Logger; 14 import net.sourceforge.cruisecontrol.util.Util; 15 import net.sourceforge.cruisecontrol.distributed.util.PropertiesHelper; 16 import net.sourceforge.cruisecontrol.CruiseControlException; 17 import net.sourceforge.cruisecontrol.builders.DistributedMasterBuilderTest; 18 19 26 public class BuildAgentServiceImplTest extends TestCase { 27 28 private static final Logger LOG = Logger.getLogger(BuildAgentServiceImplTest.class); 29 30 public static final File TEST_CONFIG_FILE = new File ("test/testdist.config.xml"); 31 public static final String TEST_AGENT_PROPERTIES_FILE = "testdist.agent.properties"; 32 public static final String TEST_USER_DEFINED_PROPERTIES_FILE = "testdist.user-defined.properties"; 33 34 private static final File DIR_LOGS = new File (PropertiesHelper.RESULT_TYPE_LOGS); 35 private static final File DIR_OUTPUT = new File (PropertiesHelper.RESULT_TYPE_OUTPUT); 36 37 protected void setUp() throws Exception { 38 DIR_LOGS.delete(); 39 DIR_OUTPUT.delete(); 40 } 41 42 protected void tearDown() throws Exception { 43 deleteDirConfirm(DIR_LOGS); 44 deleteDirConfirm(DIR_OUTPUT); 45 } 46 47 private static void deleteDirConfirm(final File dirToDelete) { 48 if (dirToDelete.exists()) { 49 assertTrue("Error cleaning up test directory: " + dirToDelete.getAbsolutePath() 50 + "\nDir Contents:\n" + Arrays.asList(dirToDelete.listFiles()), 51 dirToDelete.delete()); 52 } 53 } 54 55 56 private static class MyAgentStatusListener implements BuildAgent.AgentStatusListener 57 { 58 private int agentStatusChangeCount; 59 60 public void statusChanged(BuildAgentService buildAgentServiceImpl) { 61 agentStatusChangeCount++; 62 } 63 64 int getAgentStatusChangeCount() { 65 return agentStatusChangeCount; 66 } 67 68 void setAgentStatusChangeCount(int agentStatusChangeCount) { 69 this.agentStatusChangeCount = agentStatusChangeCount; 70 } 71 } 72 73 77 public void testGetPropertiesMap() throws Exception { 78 final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl(); 79 agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE); 80 81 MyAgentStatusListener agentListener = new MyAgentStatusListener(); 82 agentImpl.addAgentStatusListener(agentListener); 83 84 String agentAsString = agentImpl.asString(); 85 assertTrue("Wrong value: " + agentAsString, 86 agentAsString.startsWith("Machine Name: ")); 87 assertTrue("Wrong value: " + agentAsString, 88 agentAsString.endsWith("Busy: false;\tSince: null;\tModule: null\n\t" 89 + "Pending Restart: false;\tPending Restart Since: null\n\t" 90 + "Pending Kill: false;\tPending Kill Since: null")); 91 92 93 final Map distributedAgentProps = new HashMap (); 94 final String testModuleName = "testModuleName"; 95 distributedAgentProps.put(PropertiesHelper.DISTRIBUTED_MODULE, testModuleName); 96 97 distributedAgentProps.put(PropertiesHelper.DISTRIBUTED_OVERRIDE_TARGET, null); 98 try { 99 agentImpl.doBuild(null, null, distributedAgentProps); 102 fail("should fail w/ NPE"); 103 } catch (NullPointerException e) { 104 assertEquals(null, e.getMessage()); 105 } 106 assertEquals("Wrong agent status", 1, agentListener.getAgentStatusChangeCount()); 107 108 109 distributedAgentProps.remove(PropertiesHelper.DISTRIBUTED_OVERRIDE_TARGET); 110 agentImpl.setBusy(false); 111 agentListener.setAgentStatusChangeCount(0); 112 try { 113 agentImpl.doBuild(null, new HashMap (), distributedAgentProps); 115 fail("should fail w/ NPE"); 116 } catch (NullPointerException e) { 117 assertEquals(null, e.getMessage()); 118 } 119 assertEquals("Wrong agent status", 2, agentListener.getAgentStatusChangeCount()); 120 121 agentAsString = agentImpl.asString(); 122 assertTrue("Wrong value: " + agentAsString, 123 agentAsString.startsWith("Machine Name: ")); 124 assertTrue("Wrong value: " + agentAsString, 125 agentAsString.indexOf("Busy: true;\tSince: ") > -1); 126 assertTrue("Wrong value: " + agentAsString, 127 agentAsString.endsWith(";\tModule: " + testModuleName 128 + "\n\tPending Restart: false;\tPending Restart Since: null\n\t" 129 + "Pending Kill: false;\tPending Kill Since: null")); 130 } 131 132 public void testAsString() throws Exception { 133 final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl(); 134 agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE); 135 136 String agentAsString = agentImpl.asString(); 137 assertTrue("Wrong value: " + agentAsString, 138 agentAsString.startsWith("Machine Name: ")); 139 assertTrue("Wrong value: " + agentAsString, 140 agentAsString.endsWith("Busy: false;\tSince: null;\tModule: null\n\t" 141 + "Pending Restart: false;\tPending Restart Since: null\n\t" 142 + "Pending Kill: false;\tPending Kill Since: null")); 143 144 final Map projectProps = null; 145 146 final Map distributedAgentProps = new HashMap (); 147 final String testModuleName = "testModuleName"; 148 distributedAgentProps.put(PropertiesHelper.DISTRIBUTED_MODULE, testModuleName); 149 150 try { 151 agentImpl.doBuild(null, projectProps, distributedAgentProps); fail("should fail w/ NPE"); 153 } catch (NullPointerException e) { 154 assertEquals(null, e.getMessage()); 155 } 156 157 agentAsString = agentImpl.asString(); 158 assertTrue("Wrong value: " + agentAsString, 159 agentAsString.startsWith("Machine Name: ")); 160 assertTrue("Wrong value: " + agentAsString, 161 agentAsString.indexOf("Busy: true;\tSince: ") > -1); 162 assertTrue("Wrong value: " + agentAsString, 163 agentAsString.endsWith(";\tModule: " + testModuleName 164 + "\n\tPending Restart: false;\tPending Restart Since: null\n\t" 165 + "Pending Kill: false;\tPending Kill Since: null")); 166 167 agentImpl.kill(true); 168 agentAsString = agentImpl.asString(); 169 assertTrue("Wrong value: " + agentAsString, 170 agentAsString.indexOf("Busy: true;\tSince: ") > -1); 171 assertTrue("Wrong value: " + agentAsString, 172 agentAsString.indexOf(";\tModule: " + testModuleName 173 + "\n\tPending Restart: false;\tPending Restart Since: null\n\t" 174 + "Pending Kill: true;\tPending Kill Since: ") > -1); 175 assertNotNull(agentImpl.getPendingKillSince()); 176 177 agentImpl.setBusy(false); agentAsString = agentImpl.asString(); 179 assertTrue("Wrong value: " + agentAsString, 180 agentAsString.indexOf("Busy: false;\tSince: null;\tModule: null\n\t" 181 + "Pending Restart: false;\tPending Restart Since: null\n\t" 182 + "Pending Kill: true;\tPending Kill Since: ") > -1); 183 assertNotNull(agentImpl.getPendingKillSince()); 184 } 185 186 public void testGetBuildingModule() throws Exception { 187 final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl(); 188 agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE); 189 190 assertNull(agentImpl.getModule()); 191 192 final Map projectProps = null; 193 194 final Map distributedAgentProps = new HashMap (); 195 final String testModuleName = "testModuleName"; 196 distributedAgentProps.put(PropertiesHelper.DISTRIBUTED_MODULE, testModuleName); 197 198 try { 199 agentImpl.doBuild(null, projectProps, distributedAgentProps); fail("should fail w/ NPE"); 201 } catch (NullPointerException e) { 202 assertEquals(null, e.getMessage()); 203 } 204 205 assertEquals(testModuleName, agentImpl.getModule()); 206 207 agentImpl.setBusy(false); assertNull(agentImpl.getModule()); 209 } 210 211 public void testKillNoWait() throws Exception { 212 final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl(); 213 agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE); 214 215 assertFalse(agentImpl.isBusy()); 216 assertFalse(agentImpl.isPendingKill()); 217 assertNull(agentImpl.getPendingKillSince()); 218 assertFalse(agentImpl.isPendingRestart()); 219 assertNull(agentImpl.getPendingRestartSince()); 220 agentImpl.claim(); 222 assertTrue(agentImpl.isBusy()); 223 assertFalse(agentImpl.isPendingKill()); 224 assertFalse(agentImpl.isPendingRestart()); 225 agentImpl.kill(false); 226 assertTrue(agentImpl.isBusy()); 227 assertTrue(agentImpl.isPendingKill()); 228 assertNotNull(agentImpl.getPendingKillSince()); 229 assertFalse(agentImpl.isPendingRestart()); 230 assertNull(agentImpl.getPendingRestartSince()); 231 232 agentImpl.setBusy(false); 234 } 235 236 public void testRestartNoWait() throws Exception { 237 final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl(); 238 agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE); 239 240 assertFalse(agentImpl.isBusy()); 241 assertFalse(agentImpl.isPendingKill()); 242 assertNull(agentImpl.getPendingKillSince()); 243 assertFalse(agentImpl.isPendingRestart()); 244 assertNull(agentImpl.getPendingRestartSince()); 245 agentImpl.claim(); 247 assertTrue(agentImpl.isBusy()); 248 assertFalse(agentImpl.isPendingKill()); 249 assertFalse(agentImpl.isPendingRestart()); 250 try { 252 agentImpl.restart(false); 253 fail("Restart should fail outside of webstart"); 254 } catch (RuntimeException e) { 255 assertEquals("Couldn't find webstart Basic Service. Is Agent running outside of webstart?", 256 e.getMessage()); 257 } 258 assertTrue(agentImpl.isBusy()); 259 assertFalse(agentImpl.isPendingKill()); 260 assertNull(agentImpl.getPendingKillSince()); 261 assertTrue(agentImpl.isPendingRestart()); 262 assertNotNull(agentImpl.getPendingRestartSince()); 263 } 264 265 public void testKillWithWait() throws Exception { 266 final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl(); 267 agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE); 268 269 assertFalse(agentImpl.isBusy()); 270 assertFalse(agentImpl.isPendingKill()); 271 assertNull(agentImpl.getPendingKillSince()); 272 assertFalse(agentImpl.isPendingRestart()); 273 assertNull(agentImpl.getPendingRestartSince()); 274 agentImpl.claim(); 276 assertTrue(agentImpl.isBusy()); 277 assertFalse(agentImpl.isPendingKill()); 278 assertFalse(agentImpl.isPendingRestart()); 279 agentImpl.kill(true); 280 assertTrue(agentImpl.isBusy()); 281 assertTrue(agentImpl.isPendingKill()); 282 assertNotNull(agentImpl.getPendingKillSince()); 283 assertFalse(agentImpl.isPendingRestart()); 284 285 agentImpl.setBusy(false); 287 } 288 289 public void testRestartWithWait() throws Exception { 290 final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl(); 291 agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE); 292 293 assertFalse(agentImpl.isBusy()); 294 assertFalse(agentImpl.isPendingKill()); 295 assertNull(agentImpl.getPendingKillSince()); 296 assertFalse(agentImpl.isPendingRestart()); 297 assertNull(agentImpl.getPendingRestartSince()); 298 agentImpl.claim(); 300 assertTrue(agentImpl.isBusy()); 301 assertFalse(agentImpl.isPendingKill()); 302 assertNull(agentImpl.getPendingRestartSince()); 303 assertFalse(agentImpl.isPendingRestart()); 304 assertNull(agentImpl.getPendingRestartSince()); 305 agentImpl.restart(true); 306 assertTrue(agentImpl.isBusy()); 307 assertFalse(agentImpl.isPendingKill()); 308 assertTrue(agentImpl.isPendingRestart()); 309 assertNotNull(agentImpl.getPendingRestartSince()); 310 311 try { 313 agentImpl.setBusy(false); 314 fail("Restart should fail outside of webstart"); 315 } catch (RuntimeException e) { 316 assertEquals("Couldn't find webstart Basic Service. Is Agent running outside of webstart?", 317 e.getMessage()); 318 } 319 } 320 321 public void testRetrieveResultsWithAgentLogDir() throws Exception { 322 final File testLogDir = new File (DIR_LOGS, "myTestLogDir"); 323 testLogDir.deleteOnExit(); 324 testLogDir.mkdirs(); 325 final File testLog = new File (testLogDir, "myTestLog"); 326 testLog.createNewFile(); 327 testLog.deleteOnExit(); 328 329 final Map distributedAgentProps = new HashMap (); 330 distributedAgentProps.put(PropertiesHelper.DISTRIBUTED_AGENT_LOGDIR, 331 testLogDir.getAbsolutePath()); 332 333 final BuildAgentServiceImpl agentImpl = createTestAgent(false, distributedAgentProps); 334 335 final File tmSuccessDir = new File (DIR_LOGS, "testmodule-success"); 337 new File (tmSuccessDir, "TEST-bogustestclassSuccess.xml").delete(); 338 deleteDirConfirm(tmSuccessDir); 339 340 try { 341 assertTrue(agentImpl.resultsExist(PropertiesHelper.RESULT_TYPE_LOGS)); 343 344 assertTrue(agentImpl.resultsExist(PropertiesHelper.RESULT_TYPE_OUTPUT)); 345 assertTrue("Agent should be busy until build results are retrived and cleared.", 346 agentImpl.isBusy()); 347 } finally { 348 agentImpl.clearOutputFiles(); 350 testLog.delete(); 351 testLogDir.delete(); 352 } 353 assertFalse(agentImpl.isBusy()); 354 } 355 356 public void testRetrieveResultsAsZipBuildSuccess() throws Exception { 357 final BuildAgentServiceImpl agentImpl = createTestAgent(false, new HashMap ()); 358 try { 359 assertTrue(agentImpl.resultsExist(PropertiesHelper.RESULT_TYPE_LOGS)); 360 assertTrue(agentImpl.resultsExist(PropertiesHelper.RESULT_TYPE_OUTPUT)); 361 assertTrue("Agent should be busy until build results are retrived and cleared.", 362 agentImpl.isBusy()); 363 } finally { 364 agentImpl.clearOutputFiles(); 366 } 367 assertFalse(agentImpl.isBusy()); 368 } 369 370 public void testRetrieveResultsAsZipBuildFail() throws Exception { 371 final BuildAgentServiceImpl agentImpl = createTestAgent(true, new HashMap ()); 372 try { 373 assertTrue(agentImpl.resultsExist(PropertiesHelper.RESULT_TYPE_LOGS)); 374 assertFalse(agentImpl.resultsExist(PropertiesHelper.RESULT_TYPE_OUTPUT)); 375 assertTrue("Agent should be busy until build results are retrived and cleared.", 376 agentImpl.isBusy()); 377 } finally { 378 agentImpl.clearOutputFiles(); 380 } 381 assertFalse(agentImpl.isBusy()); 382 } 383 384 private static BuildAgentServiceImpl createTestAgent(boolean isBuildFailure, 385 final Map distributedAgentProps) 386 throws CruiseControlException, RemoteException { 387 388 final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl(); 389 agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE); 390 391 callTestDoBuild(isBuildFailure, agentImpl, distributedAgentProps); 393 394 return agentImpl; 395 } 396 397 398 399 public static Element callTestDoBuild(boolean isBuildFailure, 400 final BuildAgentService agent) 401 throws CruiseControlException, RemoteException { 402 403 return callTestDoBuild(isBuildFailure, agent, new HashMap ()); 404 } 405 406 407 public static Element callTestDoBuild(boolean isBuildFailure, 408 final BuildAgentService agent, 409 final Map distributedAgentProps) 410 throws CruiseControlException, RemoteException { 411 412 final Element rootElement = Util.loadConfigFile(TEST_CONFIG_FILE); 413 414 final Element project = (Element) rootElement.getChildren("project").get(0); 415 assertEquals("testproject", project.getAttributeValue("name")); 416 417 final Element schedule = (Element) project.getChildren("schedule").get(0); 418 final int distributedElementIndex; 419 if (isBuildFailure) { 420 distributedElementIndex = 0; 421 } else { 422 distributedElementIndex = 1; 423 } 424 final Element distributed = ((Element) schedule.getChildren("distributed").get(distributedElementIndex)); 425 DistributedMasterBuilderTest.addMissingPluginDefaults(distributed); 426 427 final String expectedModuleName; 428 if (isBuildFailure) { 429 expectedModuleName = "testmodule-fail"; 430 } else { 431 expectedModuleName = "testmodule-success"; 432 } 433 final String moduleName = distributed.getAttributeValue("module"); 434 assertEquals(expectedModuleName, moduleName); 435 436 distributedAgentProps.put(PropertiesHelper.DISTRIBUTED_MODULE, moduleName); 437 438 distributedAgentProps.put(PropertiesHelper.DISTRIBUTED_OVERRIDE_TARGET, null); 440 441 final Element antBuilderElement = (Element) distributed.getChildren().get(0); 442 DistributedMasterBuilderTest.addMissingPluginDefaults(antBuilderElement); 443 444 if (getANT_HOME() != null) { 447 antBuilderElement.setAttribute("anthome", getANT_HOME()); 448 } else { 449 LOG.warn("Unit Test couldn't find ANT_HOME env var. Might work if java/bin is in the path. Here goes..."); 450 } 451 452 final Map projectProps = new HashMap (); 453 454 final Element buildResult = agent.doBuild(antBuilderElement, projectProps, distributedAgentProps); 455 return buildResult; 456 } 457 458 public static String getANT_HOME() { 459 return DistributedMasterBuilderTest.OS_ENV.getVariable("ANT_HOME"); 460 } 461 462 public void testClaim() { 463 final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl(); 464 agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE); 465 466 assertFalse(agentImpl.isBusy()); 467 assertNull(agentImpl.getDateClaimed()); 468 agentImpl.claim(); 469 assertTrue(agentImpl.isBusy()); 470 assertNotNull(agentImpl.getDateClaimed()); 471 final Date firstClaimDate = agentImpl.getDateClaimed(); 472 473 try { 474 agentImpl.claim(); 475 fail("Should throw exception if attempt is made to claim a busy agent"); 476 } catch (IllegalStateException e) { 477 assertTrue("Unexpected error message w/ invalid call to claim(): " + e.getMessage(), 478 e.getMessage().startsWith("Cannot claim agent on ")); 479 assertTrue("Unexpected error message w/ invalid call to claim(): " + e.getMessage(), 480 e.getMessage().indexOf("that is busy building module: null") > 0); 481 } 482 assertEquals(firstClaimDate, agentImpl.getDateClaimed()); 483 484 try { 486 agentImpl.doBuild(null, null, null); 487 fail("Should have failed to build"); 488 } catch (NullPointerException e) { 489 assertEquals("Unexpected build error: " + e.getMessage(), null, e.getMessage()); 490 } catch (RemoteException e) { 491 assertTrue("Unexpected build error: " + e.getMessage(), 492 e.getMessage().startsWith("Failed to complete build on agent; nested exception is: \n" 493 + "\tnet.sourceforge.cruisecontrol.CruiseControlException: ant logfile ")); 494 } 495 assertEquals(firstClaimDate, agentImpl.getDateClaimed()); 496 497 agentImpl.clearOutputFiles(); 499 assertFalse("Expected agent busy flag to be false after cleanup call.", agentImpl.isBusy()); 500 assertNull(agentImpl.getDateClaimed()); 501 } 502 } 503 | Popular Tags |