public static AgentConfig deniedAgent() { AgentConfig agentConfig = new AgentConfig("uuid", "deniedAgent", "192.168.0.1"); agentConfig.disable(); return agentConfig; }
@Test public void shouldNotBeEqualIfUuidIsNotEqual() throws Exception { AgentInstance agentA = new AgentInstance(new AgentConfig("UUID", "A", "127.0.0.1"), LOCAL, systemEnvironment, null); AgentInstance copyOfAgentA = new AgentInstance(new AgentConfig("UUID", "A", "127.0.0.1"), LOCAL, systemEnvironment, null); AgentInstance agentB = new AgentInstance(new AgentConfig("UUID", "B", "127.0.0.2"), LOCAL, systemEnvironment, null); assertThat(agentA, is(not(agentB))); assertThat(agentB, is(not(agentA))); assertThat(agentA, is(copyOfAgentA)); }
@Test public void shouldOrderByHostname() throws Exception { AgentInstance agentA = new AgentInstance(new AgentConfig("UUID", "A", "127.0.0.1"), LOCAL, systemEnvironment, null); AgentInstance agentB = new AgentInstance(new AgentConfig("UUID", "B", "127.0.0.2"), LOCAL, systemEnvironment, null); assertThat(agentA.compareTo(agentA), is(0)); assertThat(agentA.compareTo(agentB), lessThan(0)); assertThat(agentB.compareTo(agentA), greaterThan(0)); }
@Test public void shouldMarkAgentAsMissingWhenLastHeardTimeIsNull() { AgentConfig agentConfig = new AgentConfig("1234", "localhost", "192.168.0.1"); AgentInstance agentInstance = AgentInstance.createFromConfig(agentConfig, new SystemEnvironment(), mock(AgentStatusChangeListener.class)); agentInstance.refresh(); assertThat(agentInstance.getRuntimeStatus(), is(AgentRuntimeStatus.Missing)); assertThat(agentInstance.getLastHeardTime(), not(nullValue())); }
@Test public void agentWithNoIpAddressShouldBeValid() throws Exception { CruiseConfig cruiseConfig = new BasicCruiseConfig(); AgentConfig agent = new AgentConfig("uuid", null, null); cruiseConfig.agents().add(agent); assertThat(cruiseConfig.validateAfterPreprocess().isEmpty(), is(true)); }
private void shouldBeValid(String ipAddress) throws Exception { AgentConfig agentConfig = new AgentConfig(); agentConfig.setIpAddress(ipAddress); agentConfig.validate(ConfigSaveValidationContext.forChain(agentConfig)); assertThat(agentConfig.errors().on(AgentConfig.IP_ADDRESS), is(nullValue())); } }
public static AgentInstance missing() { AgentConfig agentConfig = new AgentConfig("1234", "localhost", "192.168.0.1"); AgentInstance instance = AgentInstance.createFromConfig(agentConfig, new SystemEnvironment(), mock(AgentStatusChangeListener.class)); AgentRuntimeInfo newRuntimeInfo = AgentRuntimeInfo.initialState(agentConfig); newRuntimeInfo.setStatus(AgentStatus.Missing); instance.update(newRuntimeInfo); return instance; }
public static AgentInstance idleWith(String uuid) { final AgentInstance agentInstance = idle(); agentInstance.syncConfig(new AgentConfig(uuid, agentInstance.getHostname(), agentInstance.getIpAddress())); return agentInstance; }
@Test public void shouldFailValidationWhenUUidIsBlank() { AgentConfig agentConfig = new AgentConfig(""); agentConfig.validate(ConfigSaveValidationContext.forChain(agentConfig)); assertThat(agentConfig.errors().on(AgentConfig.UUID), is("UUID cannot be empty")); agentConfig = new AgentConfig(null); agentConfig.validate(ConfigSaveValidationContext.forChain(agentConfig)); assertThat(agentConfig.errors().on(AgentConfig.UUID), is("UUID cannot be empty")); }
@Test public void shouldBeNoChangeIfAllAgentsHaveThatResource() { resourceConfigs.add(new ResourceConfig("some")); agents.add(new AgentConfig("uuid1", "host1", "127.0.0.1", new ResourceConfigs("some"))); agents.add(new AgentConfig("uuid2", "host2", "127.0.0.2", new ResourceConfigs())); List<TriStateSelection> selections = TriStateSelection.forAgentsResources(resourceConfigs, agents); assertThat(selections, hasItem(new TriStateSelection("some", TriStateSelection.Action.nochange))); }
@Test public void shouldHaveActionRemoveIfNoAgentsHaveResource() { resourceConfigs.add(new ResourceConfig("none")); agents.add(new AgentConfig("uuid1", "host1", "127.0.0.1", new ResourceConfigs("one"))); agents.add(new AgentConfig("uuid2", "host2", "127.0.0.2", new ResourceConfigs("two"))); List<TriStateSelection> selections = TriStateSelection.forAgentsResources(resourceConfigs, agents); assertThat(selections, hasItem(new TriStateSelection("none", TriStateSelection.Action.remove))); }
@Test public void shouldFindAgentByUuid() { Agents agents = new Agents(); agents.add(new AgentConfig("1", "localhost", "2")); assertThat(agents.getAgentByUuid("1").getHostname(), is("localhost")); }
@Test public void shouldInvalidateEmptyAddress() { AgentConfig agentConfig = new AgentConfig("uuid", "host", ""); agentConfig.validate(ConfigSaveValidationContext.forChain(agentConfig)); assertThat(agentConfig.errors().on(AgentConfig.IP_ADDRESS), is("IpAddress cannot be empty if it is present.")); }
@Test public void shouldBeAbleToDenyAgentThatIsRunningCancelledJob() { AgentConfig config = new AgentConfig("UUID", "A", "127.0.0.1"); AgentInstance agent = new AgentInstance(config, LOCAL, systemEnvironment, mock(AgentStatusChangeListener.class)); agent.cancel(); AgentBuildingInfo cancelled = agent.getBuildingInfo(); assertThat(agent.canDisable(), is(true)); agent.deny(); assertThat(config.isDisabled(), is(true)); assertThat(agent.getStatus(), is(AgentStatus.Disabled)); assertThat(agent.getBuildingInfo(), is(cancelled)); }
public static AgentInstance updateHostname(AgentInstance agentInstance, String hostname) { AgentConfig original = agentInstance.agentConfig(); agentInstance.syncConfig(new AgentConfig(original.getUuid(), hostname, original.getIpAddress(), original.getResourceConfigs())); return agentInstance; }
@Test public void shouldNotMatchJobPlanIfTheAgentIsElasticAndJobHasResourcesDefined() { AgentConfig agentConfig = new AgentConfig("uuid", "hostname", "11.1.1.1", new ResourceConfigs(new ResourceConfig("r1"))); agentConfig.setElasticAgentId("elastic-agent-id-1"); String elasticPluginId = "elastic-plugin-id-1"; agentConfig.setElasticPluginId(elasticPluginId); AgentInstance agentInstance = new AgentInstance(agentConfig, REMOTE, mock(SystemEnvironment.class), null); DefaultJobPlan jobPlan1 = new DefaultJobPlan(); jobPlan1.setResources(asList(new Resource("r1"))); List<JobPlan> jobPlans = asList(jobPlan1, new DefaultJobPlan()); assertThat(agentInstance.firstMatching(jobPlans), is(nullValue())); }
@Test public void shouldDenyAgentWhenAgentIsDeniedInConfigFile() throws Exception { AgentInstance original = AgentInstance.createFromConfig(agentConfig, systemEnvironment, mock(AgentStatusChangeListener.class)); original.update(buildingRuntimeInfo()); AgentConfig newAgentConfig = new AgentConfig(agentConfig.getUuid(), agentConfig.getHostname(), agentConfig.getIpAddress()); newAgentConfig.disable(); original.syncConfig(newAgentConfig); assertThat(original.getStatus(), is(AgentStatus.Disabled)); }
@Test public void shouldNotAllowResourcesElasticAgents() throws Exception { BasicCruiseConfig cruiseConfig = GoConfigMother.configWithPipelines("dev", "qa"); AgentConfig agentConfig = new AgentConfig("uuid", "hostname", "10.10.10.10"); cruiseConfig.agents().add(agentConfig); agentConfig.setElasticPluginId("com.example.foo"); agentConfig.setElasticAgentId("foobar"); agentConfig.addResourceConfig(new ResourceConfig("foo")); assertThat(cruiseConfig.validateAfterPreprocess().isEmpty(), is(false)); assertEquals(1, agentConfig.errors().size()); assertThat(agentConfig.errors().on("elasticAgentId"), is("Elastic agents cannot have resources.")); }
public static AgentInstance idle(final Date lastHeardAt, final String hostname, SystemEnvironment systemEnvironment) { AgentConfig idleAgentConfig = new AgentConfig("uuid2", hostname, "10.18.5.1"); AgentRuntimeInfo agentRuntimeInfo = new AgentRuntimeInfo(idleAgentConfig.getAgentIdentifier(), AgentRuntimeStatus.Idle, currentWorkingDirectory(), "cookie", false); agentRuntimeInfo.setLocation("/var/lib/foo"); agentRuntimeInfo.idle(); agentRuntimeInfo.setUsableSpace(10 * 1024l); AgentInstance agentInstance = AgentInstance.createFromLiveAgent(agentRuntimeInfo, systemEnvironment, mock(AgentStatusChangeListener.class)); agentInstance.idle(); agentInstance.update(agentRuntimeInfo); ReflectionUtil.setField(agentInstance, "lastHeardTime", lastHeardAt); return agentInstance; }
public static AgentInstance pending(SystemEnvironment systemEnvironment) { AgentRuntimeInfo runtimeInfo = AgentRuntimeInfo.fromServer(new AgentConfig("uuid4", "CCeDev03", "10.18.5.3", new ResourceConfigs(new ResourceConfig("db"), new ResourceConfig("web"))), false, "/var/lib", 0L, "linux", false); AgentInstance pending = AgentInstance.createFromLiveAgent(runtimeInfo, systemEnvironment, mock(AgentStatusChangeListener.class)); pending.pending(); pending.update(runtimeInfo); pending.pending(); return pending; }