-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
/
JenkinsReloadConfigurationTest.java
147 lines (111 loc) · 4.23 KB
/
JenkinsReloadConfigurationTest.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
package jenkins.model;
import static org.junit.Assert.assertEquals;
import hudson.Util;
import hudson.model.FreeStyleProject;
import hudson.model.ListView;
import hudson.model.Node;
import hudson.model.User;
import hudson.tasks.Mailer;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
/**
* Ensure direct configuration change on disk is reflected after reload.
*
* @author ogondza
*/
public class JenkinsReloadConfigurationTest {
@Rule public JenkinsRule j = new JenkinsRule();
@Test
public void reloadBuiltinNodeConfig() throws Exception {
Node node = j.jenkins;
node.setLabelString("oldLabel");
modifyNode(node);
assertEquals("newLabel", node.getLabelString());
}
@Test
public void reloadAgentConfig() throws Exception {
Node node = j.createSlave("an_agent", "oldLabel", null);
modifyNode(node);
node = j.jenkins.getNode("an_agent");
assertEquals("newLabel", node.getLabelString());
}
private void modifyNode(Node node) throws Exception {
replace(node.getNodeName().isEmpty() ? "config.xml" : String.format("nodes/%s/config.xml", node.getNodeName()), "oldLabel", "newLabel");
assertEquals("oldLabel", node.getLabelString());
j.jenkins.reload();
}
@Test
public void reloadUserConfigUsingGlobalReload() throws Exception {
String originalName = "oldName";
String temporaryName = "newName";
{
User user = User.get("some_user", true, null);
user.setFullName(originalName);
user.save();
assertEquals(originalName, user.getFullName());
user.setFullName(temporaryName);
assertEquals(temporaryName, user.getFullName());
}
j.jenkins.reload();
{
assertEquals(originalName, User.getById("some_user", false).getFullName());
}
}
@Test
public void reloadJobConfig() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("a_project");
project.setDescription("oldDescription");
replace("jobs/a_project/config.xml", "oldDescription", "newDescription");
assertEquals("oldDescription", project.getDescription());
j.jenkins.reload();
project = j.jenkins.getItem("a_project", j.jenkins, FreeStyleProject.class);
assertEquals("newDescription", project.getDescription());
}
@Test
public void reloadViewConfig() throws Exception {
ListView view = new ListView("a_view");
j.jenkins.addView(view);
view.setIncludeRegex("oldIncludeRegex");
view.save();
replace("config.xml", "oldIncludeRegex", "newIncludeRegex");
assertEquals("oldIncludeRegex", view.getIncludeRegex());
j.jenkins.reload();
view = (ListView) j.jenkins.getView("a_view");
assertEquals("newIncludeRegex", view.getIncludeRegex());
}
@Test
public void reloadDescriptorConfig() {
Mailer.DescriptorImpl desc = mailerDescriptor();
desc.setDefaultSuffix("@oldSuffix");
desc.save();
replace("hudson.tasks.Mailer.xml", "@oldSuffix", "@newSuffix");
assertEquals("@oldSuffix", desc.getDefaultSuffix());
desc.load();
assertEquals("@newSuffix", desc.getDefaultSuffix());
}
private Mailer.DescriptorImpl mailerDescriptor() {
return j.jenkins.getExtensionList(Mailer.DescriptorImpl.class).get(0);
}
private void replace(String path, String search, String replace) {
File configFile = new File(j.jenkins.getRootDir(), path);
String oldConfig;
try {
oldConfig = Util.loadFile(configFile, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
String newConfig = oldConfig.replaceAll(search, replace);
try (Writer writer = Files.newBufferedWriter(configFile.toPath(), StandardCharsets.UTF_8)) {
writer.write(newConfig);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}