-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
103 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 56 additions & 38 deletions
94
src/test/java/hudson/plugins/audit_trail/ConfigurationAsCodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,73 @@ | ||
package hudson.plugins.audit_trail; | ||
|
||
import hudson.ExtensionList; | ||
import io.jenkins.plugins.casc.ConfigurationContext; | ||
import io.jenkins.plugins.casc.ConfiguratorRegistry; | ||
import io.jenkins.plugins.casc.misc.ConfiguredWithCode; | ||
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule; | ||
import io.jenkins.plugins.casc.model.CNode; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.Issue; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static io.jenkins.plugins.casc.misc.Util.*; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Created by Pierre Beitz | ||
* on 2019-07-20. | ||
*/ | ||
public class ConfigurationAsCodeTest { | ||
|
||
@ClassRule | ||
@ConfiguredWithCode("jcasc.yml") | ||
public static JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule(); | ||
|
||
@Issue("JENKINS-57232") | ||
@Test | ||
public void should_support_configuration_as_code() { | ||
ExtensionList<AuditTrailPlugin> extensionList = r.jenkins.getExtensionList(AuditTrailPlugin.class); | ||
AuditTrailPlugin plugin = extensionList.get(0); | ||
assertEquals(".*/(?:configSubmit|doUninstall|doDelete|postBuildResult|enable|disable|cancelQueue|stop|toggleLogKeep|doWipeOutWorkspace|createItem|createView|toggleOffline|cancelQuietDown|quietDown|restart|exit|safeExit)", plugin.getPattern()); | ||
assertTrue(plugin.getLogBuildCause()); | ||
assertEquals(3, plugin.getLoggers().size()); | ||
|
||
//first logger | ||
AuditLogger logger = plugin.getLoggers().get(0); | ||
assertTrue(logger instanceof ConsoleAuditLogger); | ||
assertEquals("test", ((ConsoleAuditLogger) logger).getLogPrefix()); | ||
assertEquals(ConsoleAuditLogger.Output.STD_OUT, ((ConsoleAuditLogger) logger).getOutput()); | ||
assertEquals("yyyy-MM-dd HH:mm:ss:SSS", ((ConsoleAuditLogger)logger).getDateFormat()); | ||
|
||
//second logger | ||
logger = plugin.getLoggers().get(1); | ||
assertTrue(logger instanceof LogFileAuditLogger); | ||
assertEquals(10, ((LogFileAuditLogger)logger).getCount()); | ||
assertEquals(666, ((LogFileAuditLogger)logger).getLimit()); | ||
assertEquals("/log/location", ((LogFileAuditLogger)logger).getLog()); | ||
|
||
//third logger | ||
logger = plugin.getLoggers().get(2); | ||
assertEquals("jenkins", ((SyslogAuditLogger)logger).getAppName()); | ||
assertEquals("DAEMON", ((SyslogAuditLogger)logger).getFacility()); | ||
assertEquals("RFC_5424", ((SyslogAuditLogger)logger).getMessageFormat()); | ||
assertEquals("hostname", ((SyslogAuditLogger)logger).getMessageHostname()); | ||
assertEquals("syslog-server", ((SyslogAuditLogger)logger).getSyslogServerHostname()); | ||
assertEquals(514, ((SyslogAuditLogger)logger).getSyslogServerPort()); | ||
} | ||
@ClassRule | ||
@ConfiguredWithCode("jcasc.yml") | ||
public static JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule(); | ||
|
||
@Issue("JENKINS-57232") | ||
@Test | ||
public void should_support_configuration_as_code() { | ||
ExtensionList<AuditTrailPlugin> extensionList = r.jenkins.getExtensionList(AuditTrailPlugin.class); | ||
AuditTrailPlugin plugin = extensionList.get(0); | ||
assertEquals(".*/(?:configSubmit|doUninstall|doDelete|postBuildResult|enable|disable|cancelQueue|stop|toggleLogKeep|doWipeOutWorkspace|createItem|createView|toggleOffline|cancelQuietDown|quietDown|restart|exit|safeExit)", plugin.getPattern()); | ||
assertTrue(plugin.getLogBuildCause()); | ||
assertEquals(3, plugin.getLoggers().size()); | ||
|
||
//first logger | ||
AuditLogger logger = plugin.getLoggers().get(0); | ||
assertTrue(logger instanceof ConsoleAuditLogger); | ||
assertEquals("test", ((ConsoleAuditLogger) logger).getLogPrefix()); | ||
assertEquals(ConsoleAuditLogger.Output.STD_OUT, ((ConsoleAuditLogger) logger).getOutput()); | ||
assertEquals("yyyy-MM-dd HH:mm:ss:SSS", ((ConsoleAuditLogger) logger).getDateFormat()); | ||
|
||
//second logger | ||
logger = plugin.getLoggers().get(1); | ||
assertTrue(logger instanceof LogFileAuditLogger); | ||
assertEquals(10, ((LogFileAuditLogger) logger).getCount()); | ||
assertEquals(666, ((LogFileAuditLogger) logger).getLimit()); | ||
assertEquals("/log/location", ((LogFileAuditLogger) logger).getLog()); | ||
|
||
//third logger | ||
logger = plugin.getLoggers().get(2); | ||
assertEquals("jenkins", ((SyslogAuditLogger) logger).getAppName()); | ||
assertEquals("DAEMON", ((SyslogAuditLogger) logger).getFacility()); | ||
assertEquals("RFC_5424", ((SyslogAuditLogger) logger).getMessageFormat()); | ||
assertEquals("hostname", ((SyslogAuditLogger) logger).getMessageHostname()); | ||
assertEquals("syslog-server", ((SyslogAuditLogger) logger).getSyslogServerHostname()); | ||
assertEquals(514, ((SyslogAuditLogger) logger).getSyslogServerPort()); | ||
} | ||
|
||
@Issue("JENKINS-57232") | ||
@Test | ||
public void should_support_configuration_export() throws Exception { | ||
ConfiguratorRegistry registry = ConfiguratorRegistry.get(); | ||
ConfigurationContext context = new ConfigurationContext(registry); | ||
CNode auditTrailAttribute = getUnclassifiedRoot(context).get("audit-trail"); | ||
|
||
String exported = toYamlString(auditTrailAttribute); | ||
|
||
String expected = toStringFromYamlFile(this, "expected.yml"); | ||
|
||
assertThat(exported, is(expected)); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/resources/hudson/plugins/audit_trail/expected.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
logBuildCause: true | ||
loggers: | ||
- console: | ||
dateFormat: "yyyy-MM-dd HH:mm:ss:SSS" | ||
logPrefix: "test" | ||
output: STD_OUT | ||
- logFile: | ||
count: 10 | ||
limit: 666 | ||
log: "/log/location" | ||
- syslog: | ||
appName: "jenkins" | ||
facility: "DAEMON" | ||
messageFormat: "RFC_5424" | ||
messageHostname: "hostname" | ||
syslogServerHostname: "syslog-server" | ||
syslogServerPort: 514 | ||
pattern: ".*/(?:configSubmit|doUninstall|doDelete|postBuildResult|enable|disable|cancelQueue|stop|toggleLogKeep|doWipeOutWorkspace|createItem|createView|toggleOffline|cancelQuietDown|quietDown|restart|exit|safeExit)" |