@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 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 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 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); }