Search in sources :

Example 1 with AgentInstance

use of com.thoughtworks.go.domain.AgentInstance in project gocd by gocd.

the class ElasticAgentRequestProcessorTest method shouldProcessDisableAgentRequest.

@Test
public void shouldProcessDisableAgentRequest() throws Exception {
    AgentMetadata agent = new AgentMetadata("foo", null, null, null);
    DefaultGoApiRequest goPluginApiRequest = new DefaultGoApiRequest(PROCESS_DISABLE_AGENTS, "1.0", pluginIdentifier);
    goPluginApiRequest.setRequestBody(extension.getElasticAgentMessageConverter(goPluginApiRequest.apiVersion()).listAgentsResponseBody(Arrays.asList(agent)));
    AgentInstance agentInstance = AgentInstance.createFromConfig(new AgentConfig("uuid"), null);
    when(agentService.findElasticAgent("foo", "docker")).thenReturn(agentInstance);
    processor.process(pluginDescriptor, goPluginApiRequest);
    verify(agentConfigService).disableAgents(processor.usernameFor(pluginDescriptor), agentInstance);
}
Also used : AgentInstance(com.thoughtworks.go.domain.AgentInstance) AgentConfig(com.thoughtworks.go.config.AgentConfig) ElasticAgentMetadata(com.thoughtworks.go.server.domain.ElasticAgentMetadata) AgentMetadata(com.thoughtworks.go.plugin.access.elastic.models.AgentMetadata) DefaultGoApiRequest(com.thoughtworks.go.plugin.api.request.DefaultGoApiRequest) Test(org.junit.Test)

Example 2 with AgentInstance

use of com.thoughtworks.go.domain.AgentInstance 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 3 with AgentInstance

use of com.thoughtworks.go.domain.AgentInstance 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 4 with AgentInstance

use of com.thoughtworks.go.domain.AgentInstance 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 5 with AgentInstance

use of com.thoughtworks.go.domain.AgentInstance 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

AgentInstance (com.thoughtworks.go.domain.AgentInstance)129 Test (org.junit.Test)61 NullAgentInstance (com.thoughtworks.go.domain.NullAgentInstance)32 SystemEnvironment (com.thoughtworks.go.util.SystemEnvironment)32 ArrayList (java.util.ArrayList)21 Agent (com.thoughtworks.go.config.Agent)20 Test (org.junit.jupiter.api.Test)20 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)17 AgentConfig (com.thoughtworks.go.config.AgentConfig)16 AgentStatusChangeListener (com.thoughtworks.go.listener.AgentStatusChangeListener)12 AgentInstance.createFromLiveAgent (com.thoughtworks.go.domain.AgentInstance.createFromLiveAgent)10 AgentInstances (com.thoughtworks.go.server.domain.AgentInstances)10 AgentIdentifier (com.thoughtworks.go.remote.AgentIdentifier)8 AgentRuntimeInfo (com.thoughtworks.go.server.service.AgentRuntimeInfo)8 AgentInstance.createFromAgent (com.thoughtworks.go.domain.AgentInstance.createFromAgent)7 ResponseEntity (org.springframework.http.ResponseEntity)7 BuildWork (com.thoughtworks.go.remote.work.BuildWork)6 NoWork (com.thoughtworks.go.remote.work.NoWork)6 Work (com.thoughtworks.go.remote.work.Work)6 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)6