-
Notifications
You must be signed in to change notification settings - Fork 83
/
TestHelper.java
200 lines (165 loc) · 7.59 KB
/
TestHelper.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package org.jvnet.hudson.plugins.thinbackup;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import hudson.Util;
import hudson.util.StreamTaskListener;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import org.jvnet.hudson.plugins.thinbackup.backup.HudsonBackup;
import org.mortbay.io.ByteArrayBuffer;
public class TestHelper {
public static final String CONFIG_XML_CONTENTS = "FILLED WITH DATA... ";
public static final String CONCRET_BUILD_DIRECTORY_NAME = "2011-01-08_22-26-40";
public static final String TEST_JOB_NAME = "test";
public static File createBasicFolderStructure(File base) throws IOException {
File root = new File(base, "RootDirForHudsonBackupTest");
root.mkdir();
new File(root, "config.xml").createNewFile();
new File(root, "thinBackup.xml").createNewFile();
new File(root, "secret.key").createNewFile();
new File(root, "nodeMonitors.xml").createNewFile();
new File(root, "hudson.model.UpdateCenter.xml").createNewFile();
new File(root, HudsonBackup.JOBS_DIR_NAME).mkdir();
new File(root, HudsonBackup.USERS_DIR_NAME).mkdir();
new File(root, HudsonBackup.USERSCONTENTS_DIR_NAME).mkdir();
new File(root, "plugins").mkdir();
return root;
}
public static File createBackupFolder(File base) {
File backupDir = new File(base, "BackupDirForHudsonBackupTest");
backupDir.mkdir();
return backupDir;
}
public static File createCloudBeesFolder(File jenkinsHome, String folderName) throws FileNotFoundException, IOException {
final File folderDir = new File(new File(jenkinsHome, HudsonBackup.JOBS_DIR_NAME), folderName);
folderDir.mkdirs();
final File config = new File(folderDir, "config.xml");
config.createNewFile();
final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(config));
out.write(new ByteArrayBuffer(CONFIG_XML_CONTENTS).array());
out.close();
return folderDir;
}
public static File createJob(File jenkinsHome, String jobName) throws IOException {
final File testJob = createJobsFolderWithConfiguration(jenkinsHome, jobName);
final File nextBuildNumberFile = new File(testJob, HudsonBackup.NEXT_BUILD_NUMBER_FILE_NAME);
nextBuildNumberFile.createNewFile();
addBuildNumber(nextBuildNumberFile);
File workspace = new File(testJob, "workspace");
workspace.mkdir();
new File(workspace, "neverBackupMe.txt").createNewFile();
return testJob;
}
private static File createJobsFolderWithConfiguration(File jenkinsHome, String jobName) throws IOException, FileNotFoundException {
final File testJob = new File(new File(jenkinsHome, HudsonBackup.JOBS_DIR_NAME), jobName);
testJob.mkdirs();
final File config = new File(testJob, "config.xml");
config.createNewFile();
final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(config));
out.write(new ByteArrayBuffer(CONFIG_XML_CONTENTS).array());
out.close();
return testJob;
}
public static File addNewBuildToJob(File job) throws IOException, InterruptedException {
final File builds = new File(job, HudsonBackup.BUILDS_DIR_NAME);
builds.mkdir();
final File build = new File(builds, CONCRET_BUILD_DIRECTORY_NAME);
build.mkdir();
final File changelogDir = new File(build, HudsonBackup.CHANGELOG_HISTORY_PLUGIN_DIR_NAME);
changelogDir.mkdir();
new File(changelogDir, "1.xml").createNewFile();
new File(changelogDir, "2.xml").createNewFile();
new File(build, "build.xml").createNewFile();
new File(build, "changelog.xml").createNewFile();
new File(build, "log").createNewFile();
new File(build, "revision.txt").createNewFile();
new File(build, "logfile.log").createNewFile();
new File(build, "logfile.xlog").createNewFile();
final File archiveDir = new File(build, HudsonBackup.ARCHIVE_DIR_NAME);
archiveDir.mkdir();
new File(archiveDir, "someFile.log").createNewFile();
Util.createSymlink(job, HudsonBackup.BUILDS_DIR_NAME+"/"+CONCRET_BUILD_DIRECTORY_NAME, "lastSuccessful", new StreamTaskListener(new StringWriter()));
Util.createSymlink(job, HudsonBackup.BUILDS_DIR_NAME+"/"+CONCRET_BUILD_DIRECTORY_NAME, "lastStable", new StreamTaskListener(new StringWriter()));
return build;
}
public static void addSingleConfigurationResult(File job) throws IOException, InterruptedException {
File configurations = new File(job, HudsonBackup.CONFIGURATIONS_DIR_NAME);
configurations.mkdir();
File axis_x = new File(configurations, "axis-x");
axis_x.mkdir();
File xValueA = new File(axis_x, "a");
xValueA.mkdir();
File xValueB = new File(axis_x, "b");
xValueB.mkdir();
addNewBuildToJob(xValueA);
addNewBuildToJob(xValueB);
new File(xValueA, "config.xml").createNewFile();
File nextBuildnumber = new File(xValueA, "nextBuildNumber");
nextBuildnumber.createNewFile();
addBuildNumber(nextBuildnumber);
new File(xValueB, "config.xml").createNewFile();
nextBuildnumber = new File(xValueB, "nextBuildNumber");
nextBuildnumber.createNewFile();
addBuildNumber(nextBuildnumber);
}
public static void addSinglePromotionResult(File job) throws IOException, InterruptedException {
File promotions = new File(job, HudsonBackup.PROMOTIONS_DIR_NAME);
promotions.mkdir();
File promotion_x = new File(promotions, "promotion-x");
promotion_x.mkdir();
addNewBuildToJob(promotion_x);
new File(promotion_x, "config.xml").createNewFile();
File nextBuildnumber = new File(promotion_x, "nextBuildNumber");
nextBuildnumber.createNewFile();
addBuildNumber(nextBuildnumber);
}
public static boolean containsStringEndingWith(final List<String> strings, final String pattern) {
boolean contains = false;
for (final String string : strings) {
if (string.endsWith(pattern)) {
contains = true;
break;
}
}
return contains;
}
private static void addBuildNumber(final File nextBuildNumberFile) {
Writer w = null;
try {
w = new FileWriter(nextBuildNumberFile);
w.write("1234");
} catch (final IOException e) {
// catch me if you can!
} finally {
try {
if (w != null) {
w.close();
}
} catch (final IOException e) {
// catch me if you can!
}
}
}
public static ThinBackupPluginImpl createMockPlugin(File jenkinsHome, File backupDir) {
final ThinBackupPluginImpl mockPlugin = mock(ThinBackupPluginImpl.class);
when(mockPlugin.getHudsonHome()).thenReturn(jenkinsHome);
when(mockPlugin.getFullBackupSchedule()).thenReturn("");
when(mockPlugin.getDiffBackupSchedule()).thenReturn("");
when(mockPlugin.getExpandedBackupPath()).thenReturn(backupDir.getAbsolutePath());
when(mockPlugin.getNrMaxStoredFull()).thenReturn(-1);
when(mockPlugin.isCleanupDiff()).thenReturn(false);
when(mockPlugin.isMoveOldBackupsToZipFile()).thenReturn(false);
when(mockPlugin.isBackupBuildResults()).thenReturn(true);
when(mockPlugin.isBackupBuildArchive()).thenReturn(false);
when(mockPlugin.isBackupNextBuildNumber()).thenReturn(false);
when(mockPlugin.getExcludedFilesRegex()).thenReturn("");
return mockPlugin;
}
}