public ResourceConfigs toResourceConfigs() { final ResourceConfigs resourceConfigs = new ResourceConfigs(); for (Resource resource : this) { resourceConfigs.add(new ResourceConfig(resource.getName())); } return resourceConfigs; } }
@Test //This is to work around a bug caused by MagicalCruiseConfigLoader, // since it uses direct field access public void shouldUseTrimmedNameInEquals() throws NoSuchFieldException, IllegalAccessException { ResourceConfig resourceConfig = new ResourceConfig(); Field field = resourceConfig.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(resourceConfig, "resource1 "); assertThat(new ResourceConfig("resource1"), is(resourceConfig)); }
@Before public void before() { resourceConfigs = new HashSet<>(); resourceConfigs.add(new ResourceConfig("one")); resourceConfigs.add(new ResourceConfig("two")); agents = new Agents(); }
private static void setResources(JobConfig jobConfig) { ResourceConfigs resourceConfigs = new ResourceConfigs(); jsonReader.readArrayIfPresent("resources", resources -> { resources.forEach(resource -> { resourceConfigs.add(new ResourceConfig(resource.getAsString())); }); }); jobConfig.setResourceConfigs(resourceConfigs); }
@Test public void shouldListResourceSelectionInAlhpaOrder() { HashSet<ResourceConfig> resourceConfigs = new HashSet<>(); resourceConfigs.add(new ResourceConfig("B02")); resourceConfigs.add(new ResourceConfig("b01")); resourceConfigs.add(new ResourceConfig("a01")); List<TriStateSelection> selections = TriStateSelection.forAgentsResources(resourceConfigs, agents); assertThat(selections.get(0), Matchers.is(new TriStateSelection("a01", TriStateSelection.Action.remove))); assertThat(selections.get(1), Matchers.is(new TriStateSelection("b01", TriStateSelection.Action.remove))); assertThat(selections.get(2), Matchers.is(new TriStateSelection("B02", TriStateSelection.Action.remove))); }
public static StageConfig twoBuildPlansWithResourcesAndMaterials(String stageName) { JobConfig windoze = new JobConfig( new CaseInsensitiveString("WinBuild"), new ResourceConfigs(new ResourceConfig("Windows"), new ResourceConfig(".NET")), new ArtifactConfigs(Arrays.asList(new TestArtifactConfig("junit", "junit"))) ); JobConfig linux = new JobConfig( new CaseInsensitiveString("NixBuild"), new ResourceConfigs(new ResourceConfig("Linux"), new ResourceConfig("java")), new ArtifactConfigs(Arrays.asList(new TestArtifactConfig("junit", "junit"))) ); JobConfigs jobConfigs = new JobConfigs(windoze, linux); return stageConfig(stageName, jobConfigs); }
@Test public void shouldGetAllResourcesNames() { ResourceConfigs resourceConfigs = new ResourceConfigs(); resourceConfigs.add(new ResourceConfig("Eoo")); resourceConfigs.add(new ResourceConfig("Poo")); List<String> names = new ArrayList<>(); names.add("Eoo"); names.add("Poo"); List<String> resourceNames = resourceConfigs.resourceNames(); assertThat(resourceNames, is(names)); }
public static ResourceConfigs resources(String... names) { ResourceConfigs resourceConfigs = new ResourceConfigs(); for (String name : names) { resourceConfigs.add(new ResourceConfig(name)); } return resourceConfigs; } }
public static AgentConfig localAgentWithResources(String... resources) { AgentConfig agentConfig = localAgent(); for (String resource : resources) { agentConfig.addResourceConfig(new ResourceConfig(resource)); } return agentConfig; } }
@Test public void shouldReturnSortedResourceNamesAsStringRepresention() { ResourceConfigs actual = new ResourceConfigs(); actual.add(new ResourceConfig("jdk1.4")); actual.add(new ResourceConfig("linux")); actual.add(new ResourceConfig("gentoo")); actual.add(new ResourceConfig("jdk1.5")); actual.add(new ResourceConfig("Jdk1.5")); assertThat(actual.size(), is(4)); assertThat(actual.toString(), is("gentoo | jdk1.4 | jdk1.5 | linux")); }
@Test public void shouldCompareBasedOnName() { ResourceConfig resourceConfigA = new ResourceConfig("aaa"); ResourceConfig resourceConfigB = new ResourceConfig("bbb"); assertThat(resourceConfigA.compareTo(resourceConfigB), is(org.hamcrest.Matchers.lessThan(0))); assertThat(resourceConfigB.compareTo(resourceConfigA), is(greaterThan(0))); assertThat(resourceConfigA.compareTo(resourceConfigA), is(0)); } }
@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())); }
public static AgentInstance agentWithConfigErrors() { ResourceConfig resourceConfig1 = new ResourceConfig("foo%"); ResourceConfig resourceConfig2 = new ResourceConfig("bar$"); AgentConfig agentConfig = new AgentConfig("uuid", "host", "IP", new ResourceConfigs(resourceConfig1, resourceConfig2)); agentConfig.validateTree(ConfigSaveValidationContext.forChain(new BasicCruiseConfig())); return AgentInstance.createFromConfig(agentConfig, new SystemEnvironment(), mock(AgentStatusChangeListener.class)); } }
@Test public void shouldNotBeAbleToAddResourceWithWhiteSpaceAsName() { ResourceConfigs actual = new ResourceConfigs(); actual.add(new ResourceConfig(" ")); assertThat(actual.size(), is(0)); }
@Test public void shouldPopulateResourcesFromAttributeMap() { HashMap map = new HashMap(); String value = "a, b,c ,d,e"; map.put(JobConfig.RESOURCES, value); ResourceConfigs resourceConfigs = new ResourceConfigs(); resourceConfigs.add(new ResourceConfig("z")); JobConfig jobConfig = new JobConfig(new CaseInsensitiveString("job-name"), resourceConfigs, null); jobConfig.setConfigAttributes(map); assertThat(jobConfig.resourceConfigs().size(), is(5)); }
@Test public void shouldFindAllAgentResources() { cruiseConfig.agents().add(new AgentConfig("uuid", "host1", "127.0.0.1", new ResourceConfigs("from-agent"))); assertThat(cruiseConfig.getAllResources(), hasItem(new ResourceConfig("from-agent"))); }
private StageConfig stageWithJobResource(String resourceName) { StageConfig stage = StageConfigMother.custom("stage", "job"); JobConfigs configs = stage.allBuildPlans(); ResourceConfig resourceConfig = new ResourceConfig(); resourceConfig.setName(resourceName); configs.get(0).resourceConfigs().add(resourceConfig); return stage; } }
@Test public void shouldHaveActionAddIfAllAgentsHaveThatResource() { resourceConfigs.add(new ResourceConfig("all")); agents.add(new AgentConfig("uuid1", "host1", "127.0.0.1", new ResourceConfigs("all"))); agents.add(new AgentConfig("uuid2", "host2", "127.0.0.2", new ResourceConfigs("all"))); List<TriStateSelection> selections = TriStateSelection.forAgentsResources(resourceConfigs, agents); assertThat(selections, hasItem(new TriStateSelection("all", TriStateSelection.Action.add))); }
@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.")); }
@Test public void shouldAddErrorTheMessageOnTheRightFieldOfTheRightElement() throws NoSuchFieldException { ResourceConfig resourceConfig = new ResourceConfig(); resourceConfig.setName("#{not-found}"); PipelineConfig pipelineConfig = PipelineConfigMother.createPipelineConfig("cruise", "dev", "ant"); pipelineConfig.setLabelTemplate("#a"); pipelineConfig.get(0).getJobs().addJobWithoutValidityAssertion(new JobConfig(new CaseInsensitiveString("another"), new ResourceConfigs(resourceConfig), new ArtifactConfigs())); new ParamResolver(new ParamSubstitutionHandlerFactory(params(param("foo", "pavan"), param("bar", "jj"))), fieldCache).resolve(pipelineConfig); assertThat(pipelineConfig.errors().on("labelTemplate"), is("Error when processing params for '#a' used in field 'labelTemplate', # must be followed by a parameter pattern or escaped by another #")); assertThat(resourceConfig.errors().on(JobConfig.RESOURCES), is("Parameter 'not-found' is not defined. All pipelines using this parameter directly or via a template must define it.")); }