Search in sources :

Example 26 with SystemEnvironment

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));
}
Also used : AgentInstance(com.thoughtworks.go.domain.AgentInstance) GoConfigDao(com.thoughtworks.go.config.GoConfigDao) SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) AgentConfig(com.thoughtworks.go.config.AgentConfig) AgentIdentifier(com.thoughtworks.go.remote.AgentIdentifier) AgentsUpdateCommand(com.thoughtworks.go.config.update.AgentsUpdateCommand) Test(org.junit.Test)

Example 27 with SystemEnvironment

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");
}
Also used : SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) AgentInstances(com.thoughtworks.go.server.domain.AgentInstances) AgentConfig(com.thoughtworks.go.config.AgentConfig) UuidGenerator(com.thoughtworks.go.server.util.UuidGenerator) AgentDao(com.thoughtworks.go.server.persistence.AgentDao) ServerHealthService(com.thoughtworks.go.serverhealth.ServerHealthService) Before(org.junit.Before)

Example 28 with SystemEnvironment

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);
}
Also used : AgentInstance(com.thoughtworks.go.domain.AgentInstance) SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) LogFixture(com.thoughtworks.go.util.LogFixture) IOException(java.io.IOException) Test(org.junit.Test)

Example 29 with SystemEnvironment

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));
}
Also used : AgentInstance(com.thoughtworks.go.domain.AgentInstance) SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) HttpOperationResult(com.thoughtworks.go.server.service.result.HttpOperationResult) Username(com.thoughtworks.go.server.domain.Username) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) HealthStateType(com.thoughtworks.go.serverhealth.HealthStateType) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) Test(org.junit.Test)

Example 30 with SystemEnvironment

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"));
}
Also used : AgentInstance(com.thoughtworks.go.domain.AgentInstance) SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) AgentConfig(com.thoughtworks.go.config.AgentConfig) AgentsViewModel(com.thoughtworks.go.server.ui.AgentsViewModel) AgentViewModel(com.thoughtworks.go.server.ui.AgentViewModel) Test(org.junit.Test)

Aggregations

SystemEnvironment (com.thoughtworks.go.util.SystemEnvironment)174 Test (org.junit.Test)93 Before (org.junit.Before)38 File (java.io.File)37 AgentInstance (com.thoughtworks.go.domain.AgentInstance)27 MaterialRevisions (com.thoughtworks.go.domain.MaterialRevisions)15 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)14 ArrayList (java.util.ArrayList)14 AgentRuntimeInfo (com.thoughtworks.go.server.service.AgentRuntimeInfo)11 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)10 BuildCause (com.thoughtworks.go.domain.buildcause.BuildCause)10 AgentStatusChangeListener (com.thoughtworks.go.listener.AgentStatusChangeListener)10 AgentIdentifier (com.thoughtworks.go.remote.AgentIdentifier)9 AgentConfig (com.thoughtworks.go.config.AgentConfig)8 MaterialRevision (com.thoughtworks.go.domain.MaterialRevision)8 PipelineConfigDependencyGraph (com.thoughtworks.go.server.domain.PipelineConfigDependencyGraph)8 SvnMaterial (com.thoughtworks.go.config.materials.svn.SvnMaterial)7 EnvironmentVariableContext (com.thoughtworks.go.util.command.EnvironmentVariableContext)7 Date (java.util.Date)7 CruiseConfig (com.thoughtworks.go.config.CruiseConfig)6