use of com.thoughtworks.go.util.SystemEnvironment in project gocd by gocd.
the class AgentConfigServiceTest method shouldEnableMultipleAgents.
@Test
public void shouldEnableMultipleAgents() {
AgentRuntimeInfo agentRuntimeInfo = AgentRuntimeInfo.fromAgent(new AgentIdentifier("remote-host", "50.40.30.20", "abc"), AgentRuntimeStatus.Unknown, "cookie", false);
AgentInstance pending = AgentInstance.createFromLiveAgent(agentRuntimeInfo, new SystemEnvironment(), null);
AgentConfig agentConfig = new AgentConfig("UUID2", "remote-host", "50.40.30.20");
agentConfig.disable();
AgentInstance fromConfigFile = AgentInstance.createFromConfig(agentConfig, new SystemEnvironment(), null);
when(goConfigService.hasAgent(fromConfigFile.getUuid())).thenReturn(true);
when(goConfigService.hasAgent(pending.getUuid())).thenReturn(false);
agentConfigService.enableAgents(Username.ANONYMOUS, pending, fromConfigFile);
GoConfigDao.CompositeConfigCommand command = new GoConfigDao.CompositeConfigCommand(new AgentConfigService.AddAgentCommand(pending.agentConfig()), new AgentConfigService.UpdateAgentApprovalStatus("UUID2", false));
ArgumentCaptor<AgentsUpdateCommand> captor = ArgumentCaptor.forClass(AgentsUpdateCommand.class);
verify(goConfigService).updateConfig(captor.capture(), eq(Username.ANONYMOUS));
AgentsUpdateCommand updateCommand = captor.getValue();
assertThat(ReflectionUtil.getField(updateCommand, "command"), is(command));
}
use of com.thoughtworks.go.util.SystemEnvironment in project gocd by gocd.
the class AgentServiceTest method setUp.
@Before
public void setUp() {
agentInstances = mock(AgentInstances.class);
agentConfig = new AgentConfig("uuid", "host", "192.168.1.1");
when(agentInstances.findAgentAndRefreshStatus("uuid")).thenReturn(AgentInstance.createFromConfig(agentConfig, new SystemEnvironment(), null));
agentDao = mock(AgentDao.class);
uuidGenerator = mock(UuidGenerator.class);
agentConfigService = mock(AgentConfigService.class);
agentService = new AgentService(agentConfigService, new SystemEnvironment(), agentInstances, mock(EnvironmentConfigService.class), mock(SecurityService.class), agentDao, uuidGenerator, serverHealthService = mock(ServerHealthService.class), null);
agentIdentifier = agentConfig.getAgentIdentifier();
when(agentDao.cookieFor(agentIdentifier)).thenReturn("cookie");
}
use of com.thoughtworks.go.util.SystemEnvironment in project gocd by gocd.
the class AgentServiceTest method shouldThrowExceptionWhenADuplicateAgentTriesToUpdateStatus.
@Test
public void shouldThrowExceptionWhenADuplicateAgentTriesToUpdateStatus() throws Exception {
AgentRuntimeInfo runtimeInfo = new AgentRuntimeInfo(agentIdentifier, AgentRuntimeStatus.Idle, currentWorkingDirectory(), null, false);
runtimeInfo.setCookie("invalid_cookie");
AgentInstance original = AgentInstance.createFromLiveAgent(new AgentRuntimeInfo(agentIdentifier, AgentRuntimeStatus.Idle, currentWorkingDirectory(), null, false), new SystemEnvironment(), null);
try (LogFixture logFixture = logFixtureFor(AgentService.class, Level.DEBUG)) {
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(logFixture.getRawMessages(), 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='https://docs.gocd.org/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);
}
use of com.thoughtworks.go.util.SystemEnvironment in project gocd by gocd.
the class AgentServiceTest method shouldFailWhenDeleteIsNotSuccessful.
@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.canBeDeleted()).thenReturn(true);
doThrow(new RuntimeException()).when(agentConfigService).deleteAgents(username, agentInstance);
when(agentInstances.findAgentAndRefreshStatus(uuid)).thenReturn(agentInstance);
AgentService agentService = new AgentService(agentConfigService, new SystemEnvironment(), agentInstances, mock(EnvironmentConfigService.class), securityService, agentDao, uuidGenerator, serverHealthService = mock(ServerHealthService.class), null);
agentService.deleteAgents(username, operationResult, Arrays.asList(uuid));
verify(operationResult).internalServerError(any(String.class), any(HealthStateType.class));
}
use of com.thoughtworks.go.util.SystemEnvironment in project gocd by gocd.
the class AgentServiceTest method shouldUnderstandFilteringAgentListBasedOnUuid.
@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", false), new SystemEnvironment(), null);
AgentInstance instance3 = AgentInstance.createFromLiveAgent(AgentRuntimeInfo.fromServer(new AgentConfig("uuid-3", "host-3", "192.168.1.4"), true, "/baz/quux", 300l, "linux", false), new SystemEnvironment(), null);
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"));
}
Aggregations