Search in sources :

Example 31 with ConfigErrors

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

the class ErrorCollector method collectFieldErrors.

public static void collectFieldErrors(Map<String, List<String>> errorBucket, String prefix, Object subject) {
    Field[] fields = subject.getClass().getDeclaredFields();
    for (Field field : fields) {
        String fieldName = field.getName();
        Object fieldValue = null;
        try {
            field.setAccessible(true);
            fieldValue = field.get(subject);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        // Object fieldValue = ReflectionUtil.getField(subject, fieldName);
        if (isAConstantField(field) || field.isAnnotationPresent(IgnoreTraversal.class)) {
            continue;
        }
        if (fieldValue != null && fieldValue instanceof Collection && isConfigObject(fieldValue)) {
            // collection to be walked
            Collection collection = (Collection) fieldValue;
            int index = 0;
            for (Object collectionItem : collection) {
                collectFieldErrors(errorBucket, prefix + "[" + fieldName + "][" + (index++) + "]", collectionItem);
            }
        } else if (isConfigObject(fieldValue)) {
            // object to be walked
            collectFieldErrors(errorBucket, prefix + "[" + fieldName + "]", fieldValue);
        } else {
            // basic field
            if (subject instanceof Validatable) {
                ConfigErrors configErrors = ((Validatable) subject).errors();
                if (configErrors != null && configErrors.getAllOn(fieldName) != null) {
                    errorBucket.put(prefix + "[" + fieldName + "]", configErrors.getAllOn(fieldName));
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) IgnoreTraversal(com.thoughtworks.go.config.IgnoreTraversal) Collection(java.util.Collection) Validatable(com.thoughtworks.go.config.Validatable) ConfigErrors(com.thoughtworks.go.domain.ConfigErrors)

Example 32 with ConfigErrors

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

the class CruiseConfigTestBase method shouldErrorOutWhenDependsOnItself.

@Test
public void shouldErrorOutWhenDependsOnItself() throws Exception {
    CruiseConfig cruiseConfig = createCruiseConfig();
    PipelineConfig pipelineConfig = goConfigMother.addPipeline(cruiseConfig, "pipeline1", "stage", "build");
    goConfigMother.addStageToPipeline(cruiseConfig, "pipeline1", "ft", "build");
    goConfigMother.setDependencyOn(cruiseConfig, pipelineConfig, "pipeline1", "ft");
    cruiseConfig.validate(null);
    ConfigErrors errors = pipelineConfig.materialConfigs().errors();
    assertThat(errors.on("base"), is("Circular dependency: pipeline1 <- pipeline1"));
}
Also used : ConfigErrors(com.thoughtworks.go.domain.ConfigErrors) Test(org.junit.jupiter.api.Test)

Example 33 with ConfigErrors

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

the class CruiseConfigTestBase method shouldBuildTheValidationContextForAnOnCancelTask.

@Test
public void shouldBuildTheValidationContextForAnOnCancelTask() {
    CruiseConfig config = GoConfigMother.configWithPipelines("pipeline-1");
    PipelineConfig pipelineConfig = config.pipelineConfigByName(new CaseInsensitiveString("pipeline-1"));
    StageConfig stageConfig = pipelineConfig.get(0);
    JobConfig jobConfig = stageConfig.getJobs().get(0);
    ExecTask execTask = new ExecTask("ls", "-la", "dir");
    Task mockTask = mock(Task.class);
    when(mockTask.errors()).thenReturn(new ConfigErrors());
    execTask.setCancelTask(mockTask);
    jobConfig.addTask(execTask);
    config.validateAfterPreprocess();
    verify(mockTask).validate(ConfigSaveValidationContext.forChain(config, config.getGroups(), config.getGroups().findGroup("defaultGroup"), pipelineConfig, stageConfig, stageConfig.getJobs(), jobConfig, jobConfig.getTasks(), execTask, execTask.onCancelConfig()));
}
Also used : Task(com.thoughtworks.go.domain.Task) ConfigErrors(com.thoughtworks.go.domain.ConfigErrors) Test(org.junit.jupiter.api.Test)

Example 34 with ConfigErrors

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

the class CruiseConfigTestBase method shouldNotDuplicateErrorWhenPipelineDoesnotExist.

@Test
public void shouldNotDuplicateErrorWhenPipelineDoesnotExist() throws Exception {
    CruiseConfig cruiseConfig = createCruiseConfig();
    PipelineConfig pipelineConfig = goConfigMother.addPipeline(cruiseConfig, "pipeline1", "stage", "build");
    PipelineConfig pipelineConfig2 = goConfigMother.addPipeline(cruiseConfig, "pipeline2", "stage", "build");
    goConfigMother.addStageToPipeline(cruiseConfig, "pipeline1", "ft", "build");
    goConfigMother.setDependencyOn(cruiseConfig, pipelineConfig2, "pipeline1", "ft");
    goConfigMother.setDependencyOn(cruiseConfig, pipelineConfig, "invalid", "invalid");
    cruiseConfig.validate(null);
    List<ConfigErrors> allErrors = cruiseConfig.getAllErrors();
    List<String> errors = new ArrayList<>();
    for (ConfigErrors allError : allErrors) {
        errors.addAll(allError.getAllOn("base"));
    }
    assertThat(errors.size(), is(1));
    assertThat(errors.get(0), is("Pipeline 'invalid' does not exist. It is used from pipeline 'pipeline1'."));
}
Also used : ConfigErrors(com.thoughtworks.go.domain.ConfigErrors) Test(org.junit.jupiter.api.Test)

Example 35 with ConfigErrors

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

the class CruiseConfigTestBase method shouldCollectAllTheErrorsInTheChildrenHelper.

protected void shouldCollectAllTheErrorsInTheChildrenHelper(CruiseConfig config) {
    SecurityAuthConfig ldapConfig = new SecurityAuthConfig("ldap", "cd.go.authorization.ldap");
    ldapConfig.errors().add("uri", "invalid ldap uri");
    ldapConfig.errors().add("searchBase", "invalid search base");
    config.server().security().securityAuthConfigs().add(ldapConfig);
    PipelineConfig pipelineConfig = config.pipelineConfigByName(new CaseInsensitiveString("pipeline-1"));
    pipelineConfig.errors().add("base", "Some base errors");
    P4MaterialConfig p4MaterialConfig = p4("localhost:1999", "view");
    p4MaterialConfig.setConfigAttributes(Collections.singletonMap(ScmMaterialConfig.FOLDER, "p4_folder"));
    pipelineConfig.addMaterialConfig(p4MaterialConfig);
    p4MaterialConfig.errors().add("materialName", "material name does not follow pattern");
    StageConfig stage = pipelineConfig.first();
    stage.errors().add("role", "Roles must be proper");
    List<ConfigErrors> allErrors = config.validateAfterPreprocess();
    assertThat(allErrors.size(), is(5));
    assertThat(allErrors.get(0).on("uri"), is("invalid ldap uri"));
    assertThat(allErrors.get(0).on("searchBase"), is("invalid search base"));
    assertThat(allErrors.get(1).on("base"), is("Some base errors"));
    assertThat(allErrors.get(2).on("role"), is("Roles must be proper"));
    assertThat(allErrors.get(3).on(ScmMaterialConfig.FOLDER), is("Destination directory is required when a pipeline has multiple SCM materials."));
    assertThat(allErrors.get(4).on("materialName"), is("material name does not follow pattern"));
}
Also used : P4MaterialConfig(com.thoughtworks.go.config.materials.perforce.P4MaterialConfig) ConfigErrors(com.thoughtworks.go.domain.ConfigErrors)

Aggregations

ConfigErrors (com.thoughtworks.go.domain.ConfigErrors)52 Test (org.junit.jupiter.api.Test)35 DependencyMaterialConfig (com.thoughtworks.go.config.materials.dependency.DependencyMaterialConfig)6 GoConfigInvalidException (com.thoughtworks.go.config.exceptions.GoConfigInvalidException)4 P4MaterialConfig (com.thoughtworks.go.config.materials.perforce.P4MaterialConfig)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 ConfigSaveValidationContext (com.thoughtworks.go.config.ConfigSaveValidationContext)3 GitMaterialConfig (com.thoughtworks.go.config.materials.git.GitMaterialConfig)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Cloner (com.rits.cloning.Cloner)1 AgentConfig (com.thoughtworks.go.config.AgentConfig)1 EnvironmentAgentConfig (com.thoughtworks.go.config.EnvironmentAgentConfig)1 EnvironmentConfig (com.thoughtworks.go.config.EnvironmentConfig)1 GoConfigDao (com.thoughtworks.go.config.GoConfigDao)1 IgnoreTraversal (com.thoughtworks.go.config.IgnoreTraversal)1 Validatable (com.thoughtworks.go.config.Validatable)1 ElasticProfiles (com.thoughtworks.go.config.elastic.ElasticProfiles)1 GoConfigInvalidMergeException (com.thoughtworks.go.config.exceptions.GoConfigInvalidMergeException)1 MaterialConfigs (com.thoughtworks.go.config.materials.MaterialConfigs)1