@Test
  public void shouldUpdateNumberOfActiveRemoteAgentsAfterAssigned() {
    AgentConfig agentConfig = AgentMother.remoteAgent();
    configHelper.addAgent(agentConfig);
    fixture.createPipelineWithFirstStageScheduled();
    buildAssignmentService.onTimer();

    int before = agentService.numberOfActiveRemoteAgents();
    Work work = buildAssignmentService.assignWorkToAgent(agent(agentConfig));
    assertThat(work, instanceOf(BuildWork.class));
    assertThat(agentService.numberOfActiveRemoteAgents(), is(before + 1));
  }
 @Test
 public void shouldUnderstandFilteringAgentListBasedOnUuid() {
   AgentInstance instance1 =
       AgentInstance.createFromLiveAgent(
           AgentRuntimeInfo.fromServer(
               new AgentConfig("uuid-1", "host-1", "192.168.1.2"),
               true,
               "/foo/bar",
               100l,
               "linux"),
           new SystemEnvironment());
   AgentInstance instance3 =
       AgentInstance.createFromLiveAgent(
           AgentRuntimeInfo.fromServer(
               new AgentConfig("uuid-3", "host-3", "192.168.1.4"),
               true,
               "/baz/quux",
               300l,
               "linux"),
           new SystemEnvironment());
   when(agentInstances.filter(Arrays.asList("uuid-1", "uuid-3")))
       .thenReturn(Arrays.asList(instance1, instance3));
   AgentsViewModel agents = agentService.filter(Arrays.asList("uuid-1", "uuid-3"));
   AgentViewModel view1 = new AgentViewModel(instance1);
   AgentViewModel view2 = new AgentViewModel(instance3);
   assertThat(agents, is(new AgentsViewModel(view1, view2)));
   verify(agentInstances).filter(Arrays.asList("uuid-1", "uuid-3"));
 }
  @Test
  public void shouldCallForReregisterIfAgentInstanceIsNotRegistered() {
    AgentConfig agentConfig = AgentMother.remoteAgent();
    fixture.createPipelineWithFirstStageScheduled();
    AgentRuntimeInfo info =
        AgentRuntimeInfo.fromServer(agentConfig, true, "location", 1000000l, "OS");
    agentService.requestRegistration(info);

    assertThat(agentService.findAgent(info.getUUId()).isRegistered(), is(false));

    info.setCookie("cookie");
    agentRemoteHandler.process(agent, new Message(Action.ping, info));
    buildAssignmentService.onTimer();

    assertThat(agent.messages.size(), is(1));
    assertThat(agent.messages.get(0).getAction(), is(Action.reregister));
  }
  @Test
  public void shouldAssignMatchedJobToAgentsRegisteredInAgentRemoteHandler() {
    AgentConfig agentConfig = AgentMother.remoteAgent();
    configHelper.addAgent(agentConfig);
    fixture.createPipelineWithFirstStageScheduled();
    AgentRuntimeInfo info =
        AgentRuntimeInfo.fromServer(agentConfig, true, "location", 1000000l, "OS");
    info.setCookie("cookie");

    agentRemoteHandler.process(agent, new Message(Action.ping, info));

    int before = agentService.numberOfActiveRemoteAgents();

    buildAssignmentService.onTimer();

    assertThat(agent.messages.size(), is(1));
    assertThat(agent.messages.get(0).getData(), instanceOf(BuildWork.class));
    assertThat(agentService.numberOfActiveRemoteAgents(), is(before + 1));
  }
 @Test
 public void shouldGetAgentLauncherJarInputStream() throws IOException {
   try {
     writeToFile(SystemEnvironment.AGENT_LAUNCHER_JAR_PATH);
     assertThat(
         FileUtil.readToEnd(agentService.agentLauncherJarInputStream()),
         is(SystemEnvironment.AGENT_LAUNCHER_JAR_PATH));
   } finally {
     new File(SystemEnvironment.AGENT_LAUNCHER_JAR_PATH).delete();
   }
 }
 @After
 public void teardown() throws Exception {
   goCache.clear();
   agentService.clearAll();
   fixture.onTearDown();
   dbHelper.onTearDown();
   configHelper.onTearDown();
   FileUtil.deleteFolder(goConfigService.artifactsDir());
   agentAssignment.clear();
   agentRemoteHandler.connectedAgents().clear();
 }
 @Test
 public void shouldUpdateStatus() throws Exception {
   AgentRuntimeInfo runtimeInfo =
       new AgentRuntimeInfo(
           agentIdentifier,
           AgentRuntimeStatus.Idle,
           currentWorkingDirectory(),
           "pavanIsGreat",
           null);
   when(agentDao.cookieFor(runtimeInfo.getIdentifier())).thenReturn("pavanIsGreat");
   agentService.updateRuntimeInfo(runtimeInfo);
   verify(agentInstances).updateAgentRuntimeInfo(runtimeInfo);
 }
 @Test
 public void shouldThrowExceptionWhenADuplicateAgentTriesToUpdateStatus() throws Exception {
   AgentRuntimeInfo runtimeInfo =
       new AgentRuntimeInfo(
           agentIdentifier, AgentRuntimeStatus.Idle, currentWorkingDirectory(), null, null);
   runtimeInfo.setCookie("invalid_cookie");
   AgentInstance original =
       AgentInstance.createFromLiveAgent(
           new AgentRuntimeInfo(
               agentIdentifier, AgentRuntimeStatus.Idle, currentWorkingDirectory(), null, null),
           new SystemEnvironment());
   try {
     when(agentService.findAgentAndRefreshStatus(runtimeInfo.getUUId())).thenReturn(original);
     agentService.updateRuntimeInfo(runtimeInfo);
     fail("should throw exception when cookie mismatched");
   } catch (Exception e) {
     assertThat(
         e.getMessage(),
         is(format("Agent [%s] has invalid cookie", runtimeInfo.agentInfoDebugString())));
     assertThat(
         Arrays.asList(logFixture.getMessages()),
         hasItem(
             format(
                 "Found agent [%s] with duplicate uuid. Please check the agent installation.",
                 runtimeInfo.agentInfoDebugString())));
     verify(serverHealthService)
         .update(
             ServerHealthState.warning(
                 format(
                     "[%s] has duplicate unique identifier which conflicts with [%s]",
                     runtimeInfo.agentInfoForDisplay(), original.agentInfoForDisplay()),
                 "Please check the agent installation. Click <a href='http://www.go.cd/documentation/user/current/faq/agent_guid_issue.html' target='_blank'>here</a> for more info.",
                 HealthStateType.duplicateAgent(
                     HealthStateScope.forAgent(runtimeInfo.getCookie())),
                 Timeout.THIRTY_SECONDS));
   }
   verify(agentInstances).findAgentAndRefreshStatus(runtimeInfo.getUUId());
   verifyNoMoreInteractions(agentInstances);
 }
  @Test
  public void shouldFailWhenDeleteIsNotSuccessful() throws Exception {
    SecurityService securityService = mock(SecurityService.class);
    AgentConfigService agentConfigService = mock(AgentConfigService.class);
    AgentInstance agentInstance = mock(AgentInstance.class);
    String uuid = "1234";
    Username username = new Username(new CaseInsensitiveString("test"));
    HttpOperationResult operationResult = mock(HttpOperationResult.class);

    when(securityService.hasOperatePermissionForAgents(username)).thenReturn(true);

    when(agentInstance.isNullAgent()).thenReturn(false);
    when(agentInstance.isDisabled()).thenReturn(true);
    when(agentInstance.isBuilding()).thenReturn(false);
    when(agentInstance.isCancelled()).thenReturn(false);

    doThrow(new RuntimeException()).when(agentConfigService).deleteAgents(agentInstance);

    when(agentInstances.findAgent(uuid)).thenReturn(agentInstance);

    AgentService agentService =
        new AgentService(
            agentConfigService,
            new SystemEnvironment(),
            agentInstances,
            mock(EnvironmentConfigService.class),
            mock(GoConfigService.class),
            securityService,
            agentDao,
            uuidGenerator,
            serverHealthService = mock(ServerHealthService.class));

    agentService.deleteAgents(username, operationResult, Arrays.asList(uuid));

    verify(operationResult).internalServerError(any(String.class), any(HealthStateType.class));
  }
Example #10
0
 @Test
 public void shouldThrowExceptionWhenAgentWithNoCookieTriesToUpdateStatus() throws Exception {
   AgentRuntimeInfo runtimeInfo =
       new AgentRuntimeInfo(
           agentIdentifier, AgentRuntimeStatus.Idle, currentWorkingDirectory(), null, null);
   try {
     agentService.updateRuntimeInfo(runtimeInfo);
     fail("should throw exception when no cookie is set");
   } catch (Exception e) {
     assertThat(e, instanceOf(AgentNoCookieSetException.class));
     assertThat(
         e.getMessage(),
         is(format("Agent [%s] has no cookie set", runtimeInfo.agentInfoDebugString())));
     assertThat(
         Arrays.asList(logFixture.getMessages()),
         hasItem(format("Agent [%s] has no cookie set", runtimeInfo.agentInfoDebugString())));
   }
 }
  @Test
  public void shouldNotAssignWorkToCanceledAgentsRegisteredInAgentRemoteHandler() {
    AgentConfig agentConfig = AgentMother.remoteAgent();
    configHelper.addAgent(agentConfig);
    fixture.createPipelineWithFirstStageScheduled();
    AgentRuntimeInfo info =
        AgentRuntimeInfo.fromServer(agentConfig, true, "location", 1000000l, "OS");
    info.setCookie("cookie");

    agentRemoteHandler.process(agent, new Message(Action.ping, info));

    AgentInstance agentInstance = agentService.findAgentAndRefreshStatus(info.getUUId());
    agentInstance.cancel();

    buildAssignmentService.onTimer();

    assertThat(
        "Should not assign work when agent status is Canceled", agent.messages.size(), is(0));
  }
Example #12
0
 @Test
 public void shouldAssociateCookieForAnAgent() throws Exception {
   when(uuidGenerator.randomUuid()).thenReturn("foo");
   assertThat(agentService.assignCookie(agentIdentifier), is("foo"));
   verify(agentDao).associateCookie(eq(agentIdentifier), any(String.class));
 }
 private AgentIdentifier agent(AgentConfig agentConfig) {
   agentService.sync(new Agents(agentConfig));
   agentService.approve(agentConfig.getUuid());
   return agentService.findAgent(agentConfig.getUuid()).getAgentIdentifier();
 }