ConfigurationManager { public IJobConfiguration validateAndPopulate(IJobConfiguration job) throws TaskDescriptionException { requireNonNull(job); if (!job.isSetTaskConfig()) { throw new TaskDescriptionException("Job configuration must have taskConfig set."); } if (job.getInstanceCount() <= 0) { throw new TaskDescriptionException("Instance count must be positive."); } JobConfiguration builder = job.newBuilder(); if (!JobKeys.isValid(job.getKey())) { throw new TaskDescriptionException("Job key " + job.getKey() + " is invalid."); } if (job.isSetOwner() && !UserProvidedStrings.isGoodIdentifier(job.getOwner().getUser())) { throw new TaskDescriptionException( "Job user contains illegal characters: " + job.getOwner().getUser()); } builder.setTaskConfig( validateAndPopulate(ITaskConfig.build(builder.getTaskConfig())).newBuilder()); if (!Strings.isNullOrEmpty(job.getCronSchedule()) && builder.getTaskConfig().isIsService()) { throw new TaskDescriptionException( "A service task may not be run on a cron schedule: " + builder); } return IJobConfiguration.build(builder); } @Inject  ConfigurationManager(ConfigurationManagerSettings settings, TierManager tierManager); static boolean isDedicated(Iterable<IConstraint> taskConstraints); IJobConfiguration validateAndPopulate(IJobConfiguration job); ITaskConfig validateAndPopulate(ITaskConfig config); static Predicate<IConstraint> getConstraintByName(final String name); static final String DEDICATED_ATTRIBUTE; }
@Test public void testBadContainerConfig() throws TaskDescriptionException { TaskConfig taskConfig = CONFIG_WITH_CONTAINER.newBuilder(); taskConfig.getContainer().getDocker().setImage(null); expectTaskDescriptionException("A container must specify an image"); CONFIGURATION_MANAGER.validateAndPopulate(ITaskConfig.build(taskConfig)); }
@Test public void testDisallowedDockerParameters() throws TaskDescriptionException { TaskConfig taskConfig = CONFIG_WITH_CONTAINER.newBuilder(); taskConfig.getContainer().getDocker().addToParameters(new DockerParameter("foo", "bar")); expectTaskDescriptionException(ConfigurationManager.NO_DOCKER_PARAMETERS); CONFIGURATION_MANAGER.validateAndPopulate(ITaskConfig.build(taskConfig)); }
@Test public void testDisallowNoExecutorDockerTask() throws TaskDescriptionException { TaskConfig builder = CONFIG_WITH_CONTAINER.newBuilder(); builder.getContainer().getDocker().unsetParameters(); builder.unsetExecutorConfig(); expectTaskDescriptionException(ConfigurationManager.EXECUTOR_REQUIRED_WITH_DOCKER); CONFIGURATION_MANAGER.validateAndPopulate(ITaskConfig.build(builder)); }
@Test public void testAllowNoExecutorDockerTask() throws TaskDescriptionException { TaskConfig builder = CONFIG_WITH_CONTAINER.newBuilder(); builder.unsetExecutorConfig(); DOCKER_CONFIGURATION_MANAGER.validateAndPopulate(ITaskConfig.build(builder)); }
@Test public void testInvalidTier() throws TaskDescriptionException { ITaskConfig config = ITaskConfig.build(UNSANITIZED_JOB_CONFIGURATION.deepCopy().getTaskConfig() .setTier("pr/d")); expectTaskDescriptionException("Tier contains illegal characters"); CONFIGURATION_MANAGER.validateAndPopulate(config); }
@Test public void testDefaultDockerParameters() throws TaskDescriptionException { TaskConfig builder = CONFIG_WITH_CONTAINER.newBuilder(); builder.getContainer().getDocker().setParameters(ImmutableList.of()); ITaskConfig result = DOCKER_CONFIGURATION_MANAGER.validateAndPopulate(ITaskConfig.build(builder)); List<IDockerParameter> params = result.getContainer().getDocker().getParameters(); assertThat( params, is(ImmutableList.of(IDockerParameter.build(new DockerParameter("foo", "bar"))))); }
@Test public void testPassthroughDockerParameters() throws TaskDescriptionException { TaskConfig taskConfig = CONFIG_WITH_CONTAINER.newBuilder(); DockerParameter userParameter = new DockerParameter("bar", "baz"); taskConfig.getContainer().getDocker().getParameters().clear(); taskConfig.getContainer().getDocker().addToParameters(userParameter); ITaskConfig result = DOCKER_CONFIGURATION_MANAGER.validateAndPopulate( ITaskConfig.build(taskConfig)); List<IDockerParameter> params = result.getContainer().getDocker().getParameters(); assertThat(params, is(ImmutableList.of(IDockerParameter.build(userParameter)))); }
@Test public void testNonExclusiveDedicatedAllowed() throws Exception { TaskConfig builder = UNSANITIZED_JOB_CONFIGURATION.deepCopy().getTaskConfig(); builder.setConstraints(ImmutableSet.of(new Constraint() .setName(DEDICATED_ATTRIBUTE) .setConstraint(TaskConstraint.value(new ValueConstraint(false, ImmutableSet.of("*/f")))))); CONFIGURATION_MANAGER.validateAndPopulate(ITaskConfig.build(builder)); }
@Test public void testExclusiveDedicatedRoleMismatch() throws Exception { TaskConfig builder = UNSANITIZED_JOB_CONFIGURATION.deepCopy().getTaskConfig(); builder.setConstraints(ImmutableSet.of(new Constraint() .setName(DEDICATED_ATTRIBUTE) .setConstraint(TaskConstraint.value(new ValueConstraint(false, ImmutableSet.of("r/f")))))); expectTaskDescriptionException("Only r may use hosts dedicated for that role."); CONFIGURATION_MANAGER.validateAndPopulate(ITaskConfig.build(builder)); }
@Test public void testImageAndDockerContainerConfigurationAreMutuallyExclusive() throws Exception { TaskConfig builder = CONFIG_WITH_CONTAINER.newBuilder(); builder.getContainer().getDocker().unsetParameters(); Image image = new Image(); image.setDocker(new DockerImage().setName("my-container").setTag("tag")); builder.setImage(image); expectTaskDescriptionException(ConfigurationManager.CONTAINER_AND_IMAGE_ARE_MUTUALLY_EXCLUSIVE); CONFIGURATION_MANAGER.validateAndPopulate(ITaskConfig.build(builder)); }
@Test public void testImageWithoutContainerIsAllowed() throws Exception { TaskConfig builder = UNSANITIZED_JOB_CONFIGURATION.deepCopy().getTaskConfig(); builder.unsetConstraints(); Image image = new Image(); image.setDocker(new DockerImage().setName("my-container").setTag("tag")); builder.setImage(image); CONFIGURATION_MANAGER.validateAndPopulate(ITaskConfig.build(builder)); }