forked from jenkinsci/unique-id-plugin
/
IdStoreMigratorV1ToV2Test.java
140 lines (109 loc) · 5.38 KB
/
IdStoreMigratorV1ToV2Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package org.jenkinsci.plugins.uniqueid.impl;
import hudson.model.ItemGroup;
import hudson.model.PersistenceRoot;
import hudson.model.Job;
import hudson.model.Run;
import jenkins.model.Jenkins;
import java.io.File;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;
import org.jenkinsci.plugins.uniqueid.IdStore;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;
import com.cloudbees.hudson.plugins.folder.Folder;
import hudson.Extension;
import hudson.model.TopLevelItem;
import hudson.model.TopLevelItemDescriptor;
import java.io.IOException;
import jenkins.model.AbstractTopLevelItem;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
public class IdStoreMigratorV1ToV2Test {
@Rule
public JenkinsRule jenkinsRule = new JenkinsRule();
@Test
@Issue("JENKINS-28843")
@LocalData
public void testMigration() throws Exception {
Jenkins jenkins = jenkinsRule.jenkins;
assertThat("All jobs loaded correctly", jenkins.getAllItems(), hasSize(4));
Folder folderNoID = jenkins.getItem("folderNoID", jenkins, Folder.class);
Folder folderWithID = jenkins.getItem("folderWithID", jenkins, Folder.class);
Job jobNoID = jenkins.getItem("jobNoID", (ItemGroup)folderWithID, Job.class);
Job jobWithID = jenkins.getItem("jobWithID", (ItemGroup)folderWithID, Job.class);
assertThat(folderNoID, notNullValue());
assertThat(folderWithID, notNullValue());
assertThat(jobNoID, notNullValue());
assertThat(jobWithID, notNullValue());
checkID(folderNoID, null);
checkID(folderWithID, "YzUxN2JiZTYtNGVhZS00NDQxLTg5NT");
checkID(jobNoID, null);
checkID(jobWithID, "ZGQxMDNhYzUtMTJlOC00YTc4LTgzOT");
checkID(jobNoID.getBuildByNumber(1), null);
checkID(jobNoID.getBuildByNumber(2), null);
// build 1 had no id so its generated on the fly from the parent
checkID(jobWithID.getBuildByNumber(1), "ZGQxMDNhYzUtMTJlOC00YTc4LTgzOT_" + jobWithID.getBuildByNumber(1).getId());
checkID(jobWithID.getBuildByNumber(2), "NGQ0ODM2NjktZGM0OS00MjdkLWE3NT");
}
@Test
@Issue("JENKINS-30424")
@LocalData
public void testMigrationLongLoadingBuilds() throws Exception {
Jenkins jenkins = jenkinsRule.jenkins;
assertThat("All jobs loaded correctly", jenkins.getAllItems(), hasSize(2));
Folder folderWithID = jenkins.getItem("folderWithID", jenkins, Folder.class);
Job jobWithID = jenkins.getItem("jobWithID", (ItemGroup)folderWithID, Job.class);
// Build #2 has a LongLoadingAction which simulates an action that blocks in the onLoad method
// This kind on delays causes issues on startup with version 2.1.0 (see issue linked to this test)
checkID(jobWithID.getBuildByNumber(2), "NGQ0ODM2NjktZGM0OS00MjdkLWE3NT");
}
@Test
@Issue("JENKINS-28883")
public void migrateUnsupportedType() throws Exception {
// Add new item, which is not supported by LegacyIdStore
jenkinsRule.jenkins.putItem(new PersistedRootItem(jenkinsRule.jenkins, "Hello"));
// Restart and perform migration w/o checking "already migrated" tokens, there should not be failures
IdStoreMigratorV1ToV2.performMigration(jenkinsRule.jenkins);
}
private static void checkID(PersistenceRoot obj, String expectedID) throws Exception {
assertThat("Checking " + obj.toString(), IdStore.getId(obj), is(expectedID));
if (expectedID != null) {
File f = new File(obj.getRootDir(), "config.xml");
String string = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
// main config should not contain a reference to the unique ID any more.
assertThat("config.xml for " + obj.toString() + " still contains the ID", string, not(containsString(expectedID)));
}
}
private static void checkID(Run obj, String expectedID) throws Exception {
assertThat("Checking " + obj.toString(), IdStore.getId(obj), is(expectedID));
if (expectedID != null) {
File f = new File(obj.getRootDir(), "build.xml");
String string = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
// main config should not contain a reference to the unique ID any more.
assertThat("build.xml for " + obj.toString() + " still contains the ID", string, not(containsString(expectedID)));
}
}
public static class PersistedRootItem extends AbstractTopLevelItem {
public PersistedRootItem(ItemGroup parent, String name) {
super(parent, name);
}
@Extension
public static class DescriptorImpl extends TopLevelItemDescriptor {
@Override
public String getDisplayName() {
return "Stub item";
}
@Override
public TopLevelItem newInstance(ItemGroup parent, String name) {
return new PersistedRootItem(parent, name);
}
}
}
}