From f79e4cd342fb830c47adf3d3732e3988ac6c6018 Mon Sep 17 00:00:00 2001 From: James Nord Date: Mon, 15 Jun 2015 16:59:35 +0100 Subject: [PATCH] [FIXED JENKINS-28913] write the id to the correct file. 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. --- .../implv2/PersistenceRootIdStore.java | 10 ++++- .../jenkinsci/plugins/uniqueid/IdTest.java | 39 +++++++++++++++++-- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/uniqueid/implv2/PersistenceRootIdStore.java b/src/main/java/org/jenkinsci/plugins/uniqueid/implv2/PersistenceRootIdStore.java index 43068ee..e7815a1 100644 --- a/src/main/java/org/jenkinsci/plugins/uniqueid/implv2/PersistenceRootIdStore.java +++ b/src/main/java/org/jenkinsci/plugins/uniqueid/implv2/PersistenceRootIdStore.java @@ -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); } @@ -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); } diff --git a/src/test/java/org/jenkinsci/plugins/uniqueid/IdTest.java b/src/test/java/org/jenkinsci/plugins/uniqueid/IdTest.java index 3885c65..e58aecc 100644 --- a/src/test/java/org/jenkinsci/plugins/uniqueid/IdTest.java +++ b/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 { @@ -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); @@ -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 ids = new HashSet(); + 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)); } }