@Autowired public AgentService(AgentConfigService agentConfigService, SystemEnvironment systemEnvironment, final EnvironmentConfigService environmentConfigService, SecurityService securityService, AgentDao agentDao, UuidGenerator uuidGenerator, ServerHealthService serverHealthService, final EmailSender emailSender, AgentStatusChangeNotifier agentStatusChangeNotifier) { this(agentConfigService, systemEnvironment, null, environmentConfigService, securityService, agentDao, uuidGenerator, serverHealthService, agentStatusChangeNotifier); this.agentInstances = new AgentInstances(agentStatusChangeNotifier); }
public AgentInstances allAgents() { AgentInstances agents = new AgentInstances(agentStatusChangeListener); for (AgentInstance agent : currentInstances()) { agents.add(agent); } return agents; }
public AgentInstances findDisabledAgents() { AgentInstances agentInstances = new AgentInstances(agentStatusChangeListener); for (AgentInstance agentInstance : currentInstances()) { if (agentInstance.isDisabled()){ agentInstances.add(agentInstance); } } return agentInstances; }
public AgentInstances findRegisteredAgents() { this.refresh(); AgentInstances registered = new AgentInstances(agentStatusChangeListener); synchronized (agentInstances) { for (AgentInstance agentInstance : this) { if (agentInstance.getStatus().isRegistered()) { registered.add(agentInstance); } } } return registered; }
public AgentInstances findEnabledAgents() { AgentInstances agentInstances = new AgentInstances(agentStatusChangeListener); for (AgentInstance agentInstance : currentInstances()) { if (agentInstance.getStatus().isEnabled()) { agentInstances.add(agentInstance); } } return agentInstances; }
@Test public void shouldRemovePendingAgentThatIsTimedOut() { when(systemEnvironment.getAgentConnectionTimeout()).thenReturn(-1); AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, pending, building, disabled); agentInstances.refresh(); assertThat(agentInstances.findAgentAndRefreshStatus("uuid4"), is(instanceOf(NullAgentInstance.class))); }
@Test public void shouldFindAgentsByItHostName() throws Exception { AgentInstance idle = AgentInstanceMother.idle(new Date(), "ghost-name"); AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, idle, AgentInstanceMother.building()); AgentInstance byHostname = agentInstances.findFirstByHostname("ghost-name"); assertThat(byHostname, is(idle)); }
@Test public void shouldReturnNullAgentsWhenHostNameIsNotFound() throws Exception { AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, AgentInstanceMother.building()); agentInstances.add(idle); agentInstances.add(building); AgentInstance byHostname = agentInstances.findFirstByHostname("not-exist"); assertThat(byHostname, is(instanceOf(NullAgentInstance.class))); }
@Test(expected = MaxPendingAgentsLimitReachedException.class) public void registerShouldErrorOutIfMaxPendingAgentsLimitIsReached() { AgentConfig agentConfig = new AgentConfig("uuid2", "CCeDev01", "10.18.5.1"); AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, AgentInstanceMother.pending()); when(systemEnvironment.get(SystemEnvironment.MAX_PENDING_AGENTS_ALLOWED)).thenReturn(1); agentInstances.register(AgentRuntimeInfo.fromServer(agentConfig, false, "/var/lib", 0L, "linux", false)); }
private AgentInstances sample() { AgentInstances agentInstances = new AgentInstances(null); agentInstances.add(idle); agentInstances.add(building); agentInstances.add(pending); agentInstances.add(disabled); return agentInstances; }
@Test public void shouldAddAgentIntoMemoryAfterAgentIsManuallyAddedInConfigFile() throws Exception { AgentInstances agentInstances = new AgentInstances(mock(AgentStatusChangeListener.class)); AgentConfig agentConfig = new AgentConfig("uuid20", "CCeDev01", "10.18.5.20"); agentInstances.sync(new Agents(agentConfig)); assertThat(agentInstances.size(), is(1)); assertThat(agentInstances.findAgentAndRefreshStatus("uuid20").agentConfig(), is(agentConfig)); }
@Test public void agentHostnameShouldBeUnique() { AgentConfig agentConfig = new AgentConfig("uuid2", "CCeDev01", "10.18.5.1"); AgentInstances agentInstances = new AgentInstances(mock(AgentStatusChangeListener.class)); agentInstances.register(AgentRuntimeInfo.fromServer(agentConfig, false, "/var/lib", 0L, "linux", false)); agentInstances.register(AgentRuntimeInfo.fromServer(agentConfig, false, "/var/lib", 0L, "linux", false)); }
@Test public void shouldReturnFirstMatchedAgentsWhenHostNameHasMoreThanOneMatch() throws Exception { AgentInstance agent = AgentInstance.createFromConfig(new AgentConfig("uuid20", "CCeDev01", "10.18.5.20"), systemEnvironment, null); AgentInstance duplicatedAgent = AgentInstance.createFromConfig(new AgentConfig("uuid21", "CCeDev01", "10.18.5.20"), systemEnvironment, null); AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, agent, duplicatedAgent); AgentInstance byHostname = agentInstances.findFirstByHostname("CCeDev01"); assertThat(byHostname, is(agent)); }
@Test public void shouldUnderstandFilteringAgentListBasedOnUuid() { AgentInstances instances = new AgentInstances(mock(AgentStatusChangeListener.class)); AgentRuntimeInfo agent1 = AgentRuntimeInfo.fromServer(new AgentConfig("uuid-1", "host-1", "192.168.1.2"), true, "/foo/bar", 100l, "linux", false); AgentRuntimeInfo agent2 = AgentRuntimeInfo.fromServer(new AgentConfig("uuid-2", "host-2", "192.168.1.3"), true, "/bar/baz", 200l, "linux", false); AgentRuntimeInfo agent3 = AgentRuntimeInfo.fromServer(new AgentConfig("uuid-3", "host-3", "192.168.1.4"), true, "/baz/quux", 300l, "linux", false); AgentInstance instance1 = AgentInstance.createFromLiveAgent(agent1, systemEnvironment, mock(AgentStatusChangeListener.class)); instances.add(instance1); instances.add(AgentInstance.createFromLiveAgent(agent2, systemEnvironment, mock(AgentStatusChangeListener.class))); AgentInstance instance3 = AgentInstance.createFromLiveAgent(agent3, systemEnvironment, mock(AgentStatusChangeListener.class)); instances.add(instance3); List<AgentInstance> agents = instances.filter(Arrays.asList("uuid-1", "uuid-3")); assertThat(agents, hasItems(instance1, instance3)); assertThat(agents.size(), is(2)); }
@Test public void shouldNotRemovePendingAgentDuringSync() throws Exception { AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, AgentInstanceMother.building()); agentInstances.add(pending); Agents agents = new Agents(); agentInstances.sync(agents); assertThat(agentInstances.size(), is(1)); assertThat(agentInstances.findAgentAndRefreshStatus("uuid4").getStatus(), is(AgentStatus.Pending)); }
@Test public void shouldRemoveAgentWhenAgentIsRemovedFromConfigFile() throws Exception { AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, idle, building); Agents oneAgentIsRemoved = new Agents(new AgentConfig("uuid2", "CCeDev01", "10.18.5.1")); agentInstances.sync(oneAgentIsRemoved); assertThat(agentInstances.size(), is(1)); assertThat(agentInstances.findAgentAndRefreshStatus("uuid2"), is(idle)); assertThat(agentInstances.findAgentAndRefreshStatus("uuid1"), is(new NullAgentInstance("uuid1"))); }
@Test public void shouldSyncAgent() throws Exception { AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, AgentInstanceMother.building(), idle); AgentConfig agentConfig = new AgentConfig("uuid2", "CCeDev01", "10.18.5.1"); agentConfig.setDisabled(true); Agents oneAgentIsRemoved = new Agents(agentConfig); agentInstances.sync(oneAgentIsRemoved); assertThat(agentInstances.findAgentAndRefreshStatus("uuid2").getStatus(), is(AgentStatus.Disabled)); }
@Test public void shouldSupportConcurrentOperations() throws Exception { final AgentInstances agentInstances = new AgentInstances(mock(AgentStatusChangeListener.class)); // register 100 agents for (int i = 0; i < 100; i++) { AgentConfig agentConfig = new AgentConfig("uuid" + i, "CCeDev_" + i, "10.18.5." + i); agentInstances.register(AgentRuntimeInfo.fromServer(agentConfig, false, "/var/lib", Long.MAX_VALUE, "linux", false)); } thrown.expect(MaxPendingAgentsLimitReachedException.class); thrown.expectMessage("Max pending agents allowed 100, limit reached"); AgentConfig agentConfig = new AgentConfig("uuid" + 200, "CCeDev_" + 200, "10.18.5." + 200); agentInstances.register(AgentRuntimeInfo.fromServer(agentConfig, false, "/var/lib", Long.MAX_VALUE, "linux", false)); }