Skip to content

Commit

Permalink
[FIXED JENKINS-28913] write the id to the correct file.
Browse files Browse the repository at this point in the history
When sotring the ID write it to the correct file.
Add unit tests to check the uniqueness of the ID to catch regresssions.
Add some code to migrate an empty id. (this will only happen for new
jobs/builds created with 2.0 or 2.0.1 of the plugin.)

Migrations will be ok.
  • Loading branch information
jtnord committed Jun 15, 2015
1 parent 1a7b37a commit f79e4cd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
Expand Up @@ -42,7 +42,7 @@ public void make(PersistenceRoot object) {
File tmp = null;
try {
tmp = File.createTempFile(".unique-id_", ".tmp", object.getRootDir());
FileUtils.writeStringToFile(f, IdStore.generateUniqueID(), StandardCharsets.UTF_8);
FileUtils.writeStringToFile(tmp, IdStore.generateUniqueID(), StandardCharsets.UTF_8);
try {
Files.move(tmp.toPath(), f.toPath(), StandardCopyOption.ATOMIC_MOVE);
}
Expand All @@ -62,7 +62,13 @@ public String get(PersistenceRoot object) {
File f = new File(object.getRootDir(), ID_FILE);
if (f.exists() && f.canRead()) {
try {
return FileUtils.readFileToString(f, StandardCharsets.UTF_8);
String str = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
if (str.length() == 0) {
// regenerate it.
make(object);
str = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
}
return str;
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Failed to retrieve unique ID for " + object.toString(), ex);
}
Expand Down
39 changes: 36 additions & 3 deletions src/test/java/org/jenkinsci/plugins/uniqueid/IdTest.java
@@ -1,14 +1,24 @@
package org.jenkinsci.plugins.uniqueid;

import com.cloudbees.hudson.plugins.folder.Folder;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Project;

import java.util.HashSet;
import java.util.Set;

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.*;
import com.cloudbees.hudson.plugins.folder.Folder;

import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

public class IdTest {

Expand All @@ -22,6 +32,10 @@ public void project() throws Exception {
IdStore.makeId(p);
String id = IdStore.getId(p);
AbstractBuild build = jenkinsRule.buildAndAssertSuccess(p);

// to be unique we need not null and at least a minimum size.
assertThat("project id", id, notNullValue());
assertThat("project id", id.length(), greaterThan(20));

// a build will get an id computed from its parent.
String buildId = IdStore.getId(build);
Expand All @@ -44,8 +58,27 @@ public void folder() throws Exception {
assertNull(IdStore.getId(f));
IdStore.makeId(f);
String id = IdStore.getId(f);

// to be unique we need not null and at least a minimum size.
assertThat("folder id", id, notNullValue());
assertThat("folder id", id.length(), greaterThan(20));

jenkinsRule.jenkins.reload();
assertEquals(id, IdStore.getId(jenkinsRule.jenkins.getItemByFullName("folder", Folder.class)));

}

@Test
public void uniqueness() throws Exception {
Set<String> ids = new HashSet<String>();
for (int i=0;i<100;i++) {
Project p = jenkinsRule.createFreeStyleProject();
IdStore.makeId(p);
ids.add(IdStore.getId(p));

Folder f = jenkinsRule.jenkins.createProject(Folder.class, "Folder"+i);
IdStore.makeId(f);
ids.add(IdStore.getId(f));
}
assertThat(ids, hasSize(200));
}
}

0 comments on commit f79e4cd

Please sign in to comment.