@Test
  public void shouldCopyPackagesFromOldRepositoryToTheUpdatedRepository() throws Exception {
    PackageDefinition nodePackage = new PackageDefinition("foo", "bar", new Configuration());
    oldPackageRepo.setPackages(new Packages(nodePackage));
    UpdatePackageRepositoryCommand command =
        new UpdatePackageRepositoryCommand(
            goConfigService,
            packageRepositoryService,
            newPackageRepo,
            currentUser,
            "md5",
            entityHashingService,
            result,
            repoId);

    assertThat(cruiseConfig.getPackageRepositories().find(repoId), is(oldPackageRepo));
    assertThat(cruiseConfig.getPackageRepositories().find(repoId).getPackages().size(), is(1));
    assertThat(newPackageRepo.getPackages().size(), is(0));

    command.update(cruiseConfig);
    HttpLocalizedOperationResult expectedResult = new HttpLocalizedOperationResult();
    assertThat(result, is(expectedResult));
    assertThat(cruiseConfig.getPackageRepositories().find(repoId), is(newPackageRepo));
    assertThat(cruiseConfig.getPackageRepositories().find(repoId).getPackages().size(), is(1));
    assertThat(
        cruiseConfig.getPackageRepositories().find(repoId).getPackages().first(), is(nodePackage));
  }
예제 #2
0
 @Test
 public void shouldReturnGroupsOtherThanMain_WhenMerged() {
   BasicCruiseConfig mainCruiseConfig = new BasicCruiseConfig(pipelines);
   cruiseConfig =
       new BasicCruiseConfig(mainCruiseConfig, PartialConfigMother.withPipeline("pipe2"));
   assertNotSame(mainCruiseConfig.getGroups(), cruiseConfig.getGroups());
 }
예제 #3
0
  @Test
  public void shouldFailToAddDuplicatePipelineAlreadyDefinedInConfigRepo() {
    pipelines =
        new BasicPipelineConfigs(
            "group_main", new Authorization(), PipelineConfigMother.pipelineConfig("pipe1"));
    BasicCruiseConfig localCruiseConfig = new BasicCruiseConfig(pipelines);
    cruiseConfig =
        new BasicCruiseConfig(
            localCruiseConfig, PartialConfigMother.withPipelineInGroup("pipe2", "remote_group"));

    PipelineConfig pipe2Dup = PipelineConfigMother.pipelineConfig("pipe2");
    try {
      cruiseConfig.addPipeline("doesNotMatterWhichGroup", pipe2Dup);
    } catch (Exception ex) {
      assertThat(
          ex.getMessage(),
          is(
              "Pipeline called 'pipe2' is already defined in configuration repository http://some.git at 1234fed"));
    }

    assertThat(cruiseConfig.allPipelines().size(), is(2));
    assertThat(cruiseConfig.hasPipelineNamed(new CaseInsensitiveString("pipe2")), is(true));

    assertThat(localCruiseConfig.allPipelines().size(), is(1));
    assertThat(localCruiseConfig.hasPipelineNamed(new CaseInsensitiveString("pipe1")), is(true));
    assertThat(localCruiseConfig.hasPipelineNamed(new CaseInsensitiveString("pipe2")), is(false));
  }
 @Test
 public void shouldDeleteTemplateFromTheGivenConfig() throws Exception {
   cruiseConfig.addTemplate(pipelineTemplateConfig);
   DeleteTemplateConfigCommand command =
       new DeleteTemplateConfigCommand(
           pipelineTemplateConfig, result, goConfigService, currentUser);
   assertThat(cruiseConfig.getTemplates().contains(pipelineTemplateConfig), is(true));
   command.update(cruiseConfig);
   assertThat(cruiseConfig.getTemplates().contains(pipelineTemplateConfig), is(false));
 }
  @Test
  public void shouldCreatePackageRepository() throws Exception {
    PackageRepository repository =
        new PackageRepository("id", "name", new PluginConfiguration(), new Configuration());
    CreatePackageRepositoryCommand command =
        new CreatePackageRepositoryCommand(
            goConfigService, packageRepositoryService, repository, currentUser, result);

    assertNull(cruiseConfig.getPackageRepositories().find("id"));
    command.update(cruiseConfig);
    HttpLocalizedOperationResult expectedResult = new HttpLocalizedOperationResult();
    assertThat(result, is(expectedResult));
    assertThat(cruiseConfig.getPackageRepositories().find("id"), is(repository));
  }
예제 #6
0
  @Test
  public void addPipeline_shouldAddPipelineToMain() {
    pipelines =
        new BasicPipelineConfigs(
            "group_main", new Authorization(), PipelineConfigMother.pipelineConfig("pipe1"));
    pipelines.setOrigin(new FileConfigOrigin());
    BasicCruiseConfig mainCruiseConfig = new BasicCruiseConfig(pipelines);
    cruiseConfig =
        new BasicCruiseConfig(mainCruiseConfig, PartialConfigMother.withPipeline("pipe2"));
    cruiseConfig.addPipeline("group_main", PipelineConfigMother.pipelineConfig("pipe3"));

    assertThat(mainCruiseConfig.hasPipelineNamed(new CaseInsensitiveString("pipe3")), is(true));
    assertThat(cruiseConfig.hasPipelineNamed(new CaseInsensitiveString("pipe3")), is(true));
  }
예제 #7
0
  @Test
  public void shouldNotContinueWithConfigSaveIfRequestIsNotFresh() {
    when(goConfigService.isUserAdmin(currentUser)).thenReturn(true);
    when(goConfigService.isGroupAdministrator(currentUser.getUsername())).thenReturn(false);

    SCM updatedScm =
        new SCM(
            "id",
            new PluginConfiguration("plugin-id", "1"),
            new Configuration(
                new ConfigurationProperty(
                    new ConfigurationKey("key1"), new ConfigurationValue("value1"))));
    updatedScm.setName("material");
    when(entityHashingService.md5ForEntity(cruiseConfig.getSCMs().find("id"), "material"))
        .thenReturn("another-md5");
    UpdateSCMConfigCommand command =
        new UpdateSCMConfigCommand(
            updatedScm,
            pluggableScmService,
            goConfigService,
            currentUser,
            result,
            "md5",
            entityHashingService);

    assertThat(command.canContinue(cruiseConfig), is(false));
    assertThat(result.toString(), containsString("STALE_RESOURCE_CONFIG"));
    assertThat(result.toString(), containsString(updatedScm.getName()));
  }
 @Before
 public void setup() throws Exception {
   initMocks(this);
   currentUser = new Username(new CaseInsensitiveString("user"));
   cruiseConfig = GoConfigMother.defaultCruiseConfig();
   repoId = "npmOrg";
   newPackageRepo =
       new PackageRepository(
           repoId,
           repoId,
           new PluginConfiguration("npm", "1"),
           new Configuration(
               new ConfigurationProperty(
                   new ConfigurationKey("REPO_URL"), new ConfigurationValue("http://bar"))));
   oldPackageRepo =
       new PackageRepository(
           repoId,
           repoId,
           new PluginConfiguration("npm", "1"),
           new Configuration(
               new ConfigurationProperty(
                   new ConfigurationKey("REPO_URL"), new ConfigurationValue("http://foo"))));
   result = new HttpLocalizedOperationResult();
   cruiseConfig.setPackageRepositories(new PackageRepositories(oldPackageRepo));
 }
예제 #9
0
  @Test
  public void shouldGetUniqueMaterialsWithoutConfigRepos() {
    BasicCruiseConfig mainCruiseConfig = new BasicCruiseConfig(pipelines);
    ConfigReposConfig reposConfig = new ConfigReposConfig();
    GitMaterialConfig configRepo = new GitMaterialConfig("http://git");
    reposConfig.add(new ConfigRepoConfig(configRepo, "myplug"));
    mainCruiseConfig.setConfigRepos(reposConfig);

    PartialConfig partialConfig = PartialConfigMother.withPipeline("pipe2");
    MaterialConfig pipeRepo = partialConfig.getGroups().get(0).get(0).materialConfigs().get(0);

    cruiseConfig = new BasicCruiseConfig(mainCruiseConfig, partialConfig);

    Set<MaterialConfig> materials = cruiseConfig.getAllUniqueMaterialsBelongingToAutoPipelines();
    assertThat(materials, hasItem(pipeRepo));
    assertThat(materials.size(), is(1));
  }
예제 #10
0
  public ConfigUpdateResponse updateConfigFromUI(
      final UpdateConfigFromUI command,
      final String md5,
      Username username,
      final LocalizedOperationResult result) {
    UiBasedConfigUpdateCommand updateCommand =
        new UiBasedConfigUpdateCommand(md5, command, result, cachedGoPartials);
    UpdatedNodeSubjectResolver updatedConfigResolver = new UpdatedNodeSubjectResolver();
    try {
      ConfigSaveState configSaveState = updateConfig(updateCommand);
      return latestUpdateResponse(
          command, updateCommand, updatedConfigResolver, clonedConfigForEdit(), configSaveState);
    } catch (ConfigFileHasChangedException e) {
      CruiseConfig updatedConfig = handleMergeException(md5, updateCommand);
      result.conflict(LocalizedMessage.string("SAVE_FAILED_WITH_REASON", e.getMessage()));
      return latestUpdateResponse(
          command, updateCommand, new OldNodeSubjectResolver(), updatedConfig, null);
    } catch (ConfigUpdateCheckFailedException e) {
      // result is already set
    } catch (Exception e) {
      ConfigMergeException mergeException = ExceptionUtils.getCause(e, ConfigMergeException.class);
      ConfigMergePostValidationException mergePostValidationException =
          ExceptionUtils.getCause(e, ConfigMergePostValidationException.class);
      if (mergeException != null || mergePostValidationException != null) {
        CruiseConfig updatedConfig = handleMergeException(md5, updateCommand);
        result.conflict(LocalizedMessage.string("SAVE_FAILED_WITH_REASON", e.getMessage()));
        return latestUpdateResponse(
            command, updateCommand, new OldNodeSubjectResolver(), updatedConfig, null);
      }
      GoConfigInvalidException ex = ExceptionUtils.getCause(e, GoConfigInvalidException.class);
      if (ex != null) {
        CruiseConfig badConfig = ex.getCruiseConfig();
        setMD5(md5, badConfig);
        Validatable node = updatedConfigResolver.getNode(command, updateCommand.cruiseConfig());
        BasicCruiseConfig.copyErrors(command.updatedNode(badConfig), node);
        result.badRequest(LocalizedMessage.string("SAVE_FAILED"));
        return new ConfigUpdateResponse(
            badConfig,
            node,
            subjectFromNode(command, updatedConfigResolver, node),
            updateCommand,
            null);
      } else {
        result.badRequest(LocalizedMessage.string("SAVE_FAILED_WITH_REASON", e.getMessage()));
      }
    }

    CruiseConfig newConfigSinceNoOtherConfigExists = clonedConfigForEdit();
    setMD5(md5, newConfigSinceNoOtherConfigExists);
    return latestUpdateResponse(
        command,
        updateCommand,
        new OldNodeSubjectResolver(),
        newConfigSinceNoOtherConfigExists,
        null);
  }
예제 #11
0
 @Override
 public boolean isValid(CruiseConfig preprocessedConfig) {
   preprocessedPipelineConfig = preprocessedConfig.getPipelineConfigByName(pipelineConfig.name());
   boolean isValid =
       preprocessedPipelineConfig.validateTree(
           PipelineConfigSaveValidationContext.forChain(
               true, groupName, preprocessedConfig, preprocessedPipelineConfig));
   if (!isValid) BasicCruiseConfig.copyErrors(preprocessedPipelineConfig, pipelineConfig);
   return isValid;
 }
예제 #12
0
  @Test
  public void shouldReturnOriginAsASumOfAllOrigins() {
    BasicCruiseConfig mainCruiseConfig = new BasicCruiseConfig(pipelines);
    FileConfigOrigin fileOrigin = new FileConfigOrigin();
    mainCruiseConfig.setOrigins(fileOrigin);

    PartialConfig part = PartialConfigMother.withPipeline("pipe2");
    RepoConfigOrigin repoOrigin = new RepoConfigOrigin();
    part.setOrigin(repoOrigin);
    cruiseConfig = new BasicCruiseConfig(mainCruiseConfig, part);

    ConfigOrigin allOrigins = cruiseConfig.getOrigin();
    assertThat(allOrigins instanceof MergeConfigOrigin, is(true));

    MergeConfigOrigin mergeConfigOrigin = (MergeConfigOrigin) allOrigins;
    assertThat(mergeConfigOrigin.size(), is(2));
    assertThat(mergeConfigOrigin.contains(fileOrigin), is(true));
    assertThat(mergeConfigOrigin.contains(repoOrigin), is(true));
  }
예제 #13
0
 @Test
 public void
     shouldCollectPipelineNameConflictErrorsInTheChildren_InMergedConfig2_WhenPipelinesInDefaultGroup() {
   BasicCruiseConfig cruiseConfig = GoConfigMother.configWithPipelines("pipeline1");
   // pipeline1 is in xml and in config repo - this is an error at merged scope
   PartialConfig remotePart = PartialConfigMother.withPipelineInGroup("pipeline1", "defaultGroup");
   remotePart.setOrigin(new RepoConfigOrigin());
   BasicCruiseConfig merged = new BasicCruiseConfig((BasicCruiseConfig) cruiseConfig, remotePart);
   List<ConfigErrors> allErrors = merged.validateAfterPreprocess();
   assertThat(remotePart.getGroups().get(0).getPipelines().get(0).errors().size(), is(1));
   assertThat(allErrors.size(), is(2));
   assertThat(
       allErrors.get(0).on("name"),
       is(
           "You have defined multiple pipelines named 'pipeline1'. Pipeline names must be unique."));
   assertThat(
       allErrors.get(1).on("name"),
       is(
           "You have defined multiple pipelines named 'pipeline1'. Pipeline names must be unique."));
 }
  @Test
  public void shouldContinueWithConfigSaveIfUserIsAuthorized() {
    cruiseConfig.addTemplate(pipelineTemplateConfig);
    when(goConfigService.isUserAdmin(currentUser)).thenReturn(true);

    DeleteTemplateConfigCommand command =
        new DeleteTemplateConfigCommand(
            pipelineTemplateConfig, result, goConfigService, currentUser);

    assertThat(command.canContinue(cruiseConfig), is(true));
  }
 @Before
 public void setup() throws Exception {
   initMocks(this);
   currentUser = new Username(new CaseInsensitiveString("user"));
   cruiseConfig = GoConfigMother.defaultCruiseConfig();
   repoId = "npmOrg";
   packageRepository =
       new PackageRepository(
           repoId, repoId, new PluginConfiguration("npm", "1"), new Configuration());
   result = new HttpLocalizedOperationResult();
   cruiseConfig.getPackageRepositories().add(packageRepository);
 }
예제 #16
0
 @Override
 public boolean isValid(CruiseConfig preprocessedConfig) {
   PackageRepositories repositories = preprocessedConfig.getPackageRepositories();
   this.preprocessedRepository = repositories.find(this.repository.getRepoId());
   preprocessedRepository.validate(null);
   repositories.validate(null);
   boolean isValidConfiguration =
       packageRepositoryService.validatePluginId(preprocessedRepository)
           && packageRepositoryService.validateRepositoryConfiguration(preprocessedRepository);
   BasicCruiseConfig.copyErrors(preprocessedRepository, this.repository);
   return getAllErrors(this.repository).isEmpty() && isValidConfiguration && result.isSuccessful();
 }
예제 #17
0
  @Test
  public void shouldAddPipelineToExistingGroup_InMergeAndLocalScope() {
    pipelines =
        new BasicPipelineConfigs(
            "group_main", new Authorization(), PipelineConfigMother.pipelineConfig("pipe1"));
    BasicCruiseConfig localCruiseConfig = new BasicCruiseConfig(pipelines);
    cruiseConfig =
        new BasicCruiseConfig(
            localCruiseConfig, PartialConfigMother.withPipelineInGroup("pipe2", "remote_group"));

    PipelineConfig pipe3 = PipelineConfigMother.pipelineConfig("pipe3");
    cruiseConfig.addPipeline("remote_group", pipe3);

    assertThat(cruiseConfig.allPipelines().size(), is(3));
    assertThat(cruiseConfig.hasPipelineNamed(new CaseInsensitiveString("pipe3")), is(true));

    assertThat(localCruiseConfig.allPipelines().size(), is(2));
    assertThat(localCruiseConfig.hasPipelineNamed(new CaseInsensitiveString("pipe1")), is(true));
    assertThat(localCruiseConfig.hasPipelineNamed(new CaseInsensitiveString("pipe3")), is(true));
    assertThat(localCruiseConfig.pipelines("remote_group").contains(pipe3), is(true));
  }
  @Test
  public void shouldUpdatePackageRepository() throws Exception {
    UpdatePackageRepositoryCommand command =
        new UpdatePackageRepositoryCommand(
            goConfigService,
            packageRepositoryService,
            newPackageRepo,
            currentUser,
            "md5",
            entityHashingService,
            result,
            repoId);

    assertThat(cruiseConfig.getPackageRepositories().size(), is(1));
    assertThat(cruiseConfig.getPackageRepositories().find(repoId), is(oldPackageRepo));

    command.update(cruiseConfig);
    HttpLocalizedOperationResult expectedResult = new HttpLocalizedOperationResult();
    assertThat(result, is(expectedResult));
    assertThat(cruiseConfig.getPackageRepositories().size(), is(1));
    assertThat(cruiseConfig.getPackageRepositories().find(repoId), is(newPackageRepo));
  }
예제 #19
0
 @Test
 public void shouldUpdateAnExistingSCMWithNewValues() throws Exception {
   SCM updatedScm =
       new SCM(
           "id",
           new PluginConfiguration("plugin-id", "1"),
           new Configuration(
               new ConfigurationProperty(
                   new ConfigurationKey("key1"), new ConfigurationValue("value1"))));
   updatedScm.setName("material");
   UpdateSCMConfigCommand command =
       new UpdateSCMConfigCommand(
           updatedScm,
           pluggableScmService,
           goConfigService,
           currentUser,
           result,
           "md5",
           entityHashingService);
   assertThat(cruiseConfig.getSCMs().contains(scm), is(true));
   command.update(cruiseConfig);
   assertThat(cruiseConfig.getSCMs().contains(updatedScm), is(true));
 }
예제 #20
0
 @Before
 public void setup() throws Exception {
   initMocks(this);
   result = new HttpLocalizedOperationResult();
   currentUser = new Username(new CaseInsensitiveString("user"));
   cruiseConfig = new GoConfigMother().defaultCruiseConfig();
   scm =
       new SCM(
           "id",
           new PluginConfiguration("plugin-id", "1"),
           new Configuration(
               new ConfigurationProperty(
                   new ConfigurationKey("key"), new ConfigurationValue("value"))));
   scm.setName("material");
   scms = new SCMs();
   scms.add(scm);
   cruiseConfig.setSCMs(scms);
 }
  @Test
  public void shouldNotUpdatePackageRepositoryWhenRepositoryWithSpecifiedNameAlreadyExists()
      throws Exception {
    cruiseConfig.getPackageRepositories().add(newPackageRepo);
    UpdatePackageRepositoryCommand command =
        new UpdatePackageRepositoryCommand(
            goConfigService,
            packageRepositoryService,
            newPackageRepo,
            currentUser,
            "md5",
            entityHashingService,
            result,
            repoId);

    assertFalse(command.isValid(cruiseConfig));
    assertThat(
        newPackageRepo.errors().firstError(),
        is(
            "You have defined multiple repositories called 'npmOrg'. Repository names are case-insensitive and must be unique."));
  }
예제 #22
0
 @Override
 public void clearErrors() {
   BasicCruiseConfig.clearErrors(this.preprocessedRepository);
 }