forked from jenkinsci/configuration-as-code-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorPageTest.java
More file actions
135 lines (105 loc) · 4.85 KB
/
ErrorPageTest.java
File metadata and controls
135 lines (105 loc) · 4.85 KB
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
package io.jenkins.plugins.casc;
import static hudson.model.ManagementLink.Category.CONFIGURATION;
import static jenkins.model.Jenkins.ADMINISTER;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.htmlunit.html.HtmlButton;
import org.htmlunit.html.HtmlElementUtil;
import org.htmlunit.html.HtmlForm;
import org.htmlunit.html.HtmlPage;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.MockAuthorizationStrategy;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
@WithJenkins
class ErrorPageTest {
private JenkinsRule r;
@TempDir
public File folder;
private Path cascFile;
@BeforeEach
void setup(JenkinsRule r) throws IOException {
this.r = r;
cascFile = File.createTempFile("jenkins.yaml", null, folder).toPath();
}
@Test
void reloadSimple() throws Exception {
Files.write(cascFile, "jenkins:\n systemMessage2: Hello World\n".getBytes());
String pageContent = reloadConfiguration();
assertThat(pageContent, containsString("Invalid configuration elements for type"));
assertThat(pageContent, containsString("systemMessage2"));
assertThat(pageContent, containsString("systemMessage"));
}
private String reloadConfiguration() throws Exception {
try (WebClient webClient = r.createWebClient().withThrowExceptionOnFailingStatusCode(false)) {
System.setProperty(ConfigurationAsCode.CASC_JENKINS_CONFIG_PROPERTY, cascFile.toString());
HtmlPage htmlPage = webClient.goTo("manage/configuration-as-code/");
HtmlButton button = (HtmlButton) htmlPage.getElementById("btn-open-apply-configuration");
HtmlElementUtil.click(button);
HtmlForm reload = htmlPage.getFormByName("replace");
HtmlPage submit = r.submit(reload);
return submit.asNormalizedText();
} finally {
System.clearProperty(ConfigurationAsCode.CASC_JENKINS_CONFIG_PROPERTY);
}
}
@Test
void noConfigurator() throws Exception {
Files.write(cascFile, "invalid:\n systemMessage2: Hello World\n".getBytes());
String pageContent = reloadConfiguration();
assertThat(pageContent, containsString("No configurator for the following root elements"));
assertThat(pageContent, containsString("invalid"));
}
@Test
void noImplementationFoundForSymbol() throws Exception {
String content = """
jenkins:
securityRealm:
unknown:
username: "world"
""";
Files.write(cascFile, content.getBytes());
String pageContent = reloadConfiguration();
assertThat(pageContent, containsString("No implementation found for:"));
assertThat(pageContent, containsString("securityRealm"));
assertThat(pageContent, containsString("Attribute was:"));
assertThat(pageContent, containsString("unknown"));
}
@Test
void replaceWithInvalidSource() throws Exception {
String pageContent = replaceConfiguration();
assertThat(
pageContent, containsString("Source non-existent-file.yaml could not be applied or does not exist."));
}
private String replaceConfiguration() throws Exception {
r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
r.jenkins.setAuthorizationStrategy(
new MockAuthorizationStrategy().grant(ADMINISTER).everywhere().to("admin"));
try (WebClient webClient = r.createWebClient().withThrowExceptionOnFailingStatusCode(false)) {
webClient.login("admin", "admin");
HtmlPage htmlPage = webClient.goTo("manage/configuration-as-code/");
HtmlButton button = (HtmlButton) htmlPage.getElementById("btn-open-apply-configuration");
HtmlElementUtil.click(button);
HtmlForm replaceForm = htmlPage.getFormByName("replace");
replaceForm.getInputByName("_.newSource").setValue("non-existent-file.yaml");
HtmlPage submit = r.submit(replaceForm);
return submit.asNormalizedText();
}
}
@Test
void verifyManagementLinkProperties() {
ConfigurationAsCode casc = ConfigurationAsCode.get();
assertEquals(CONFIGURATION, casc.getCategory());
assertEquals("configuration-as-code", casc.getUrlName());
assertEquals("Configuration as Code", casc.getDisplayName());
assertEquals("symbol-logo plugin-configuration-as-code", casc.getIconFileName());
}
}