Skip to content

Commit

Permalink
fix: quoting defaults to previous behavior if configuration value is …
Browse files Browse the repository at this point in the history
…null

add tests
  • Loading branch information
gschueler committed Feb 8, 2024
1 parent 9d3bee2 commit 3f25da4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/main/groovy/com/rundeck/plugins/logging/JsonLogFilter.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ See [here](https://github.com/eiiches/jackson-jq#implementation-status-and-curre
)
Boolean logData

//Note: we don't set a defaultValue, so that we can detect a null value if the user has not set it
@PluginProperty(
title = 'Extra quotes',
description = '''If true, the result will be parse to string, that will add extra quotes to the result (compatible with rundeck 4.x)''',
defaultValue = 'false'
title = 'Extra quotes (Rundeck 4.x Compatibility)',
description = '''If true, the result will be parsed to a string with extra quotes to match Rundeck 4.x behavior.'''
)
Boolean extraQuotes=false
Boolean extraQuotes

private StringBuffer buffer;
OutputContext outputContext
Expand Down Expand Up @@ -82,6 +82,19 @@ See [here](https://github.com/eiiches/jackson-jq#implementation-status-and-curre
void complete(final PluginLoggingContext context) {

if(buffer.size()>0) {
if (extraQuotes == null) {
//fall back to previous behavior
extraQuotes = true
context.log(
1,
'JSON jq key/value mapper: defaulting to rundeck 4.x quoting behavior'
)
}else if(extraQuotes){
context.log(
1,
'JSON jq key/value mapper: Using rundeck 4.x quoting behavior'
)
}
//apply json transform
this.processJson(context)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class JsonLogFilterSpec extends Specification {
plugin.filter = filter
plugin.prefix = "result"
plugin.logData = dolog
plugin.extraQuotes = false
def sharedoutput = new DataOutput(ContextView.global())
def context = Mock(PluginLoggingContext) {
getOutputContext() >> sharedoutput
Expand Down Expand Up @@ -62,6 +63,55 @@ class JsonLogFilterSpec extends Specification {
false | ".id" | ['{"id":"abc12345"}'] | [result: 'abc12345']
}

def "simple test legacy quoting"() {
given:
def plugin = new JsonLogFilter()
plugin.filter = filter
plugin.prefix = "result"
plugin.logData = dolog
plugin.extraQuotes = quoteVal
def sharedoutput = new DataOutput(ContextView.global())
def context = Mock(PluginLoggingContext) {
getOutputContext() >> sharedoutput
}
def events = []
lines.each { line ->
events << Mock(LogEventControl) {
getMessage() >> line
getEventType() >> 'log'
getLoglevel() >> LogLevel.NORMAL
}
}
when:
plugin.init(context)
events.each {
plugin.handleEvent(context, it)
}
plugin.complete(context)
then:

sharedoutput.getSharedContext().getData(ContextView.global())?.getData() == (expect ? ['data': expect] : null)
if (expect) {
if (dolog) {
1 * context.log(2, _, _)
} else {
0 * context.log(*_)
}
}


where:

quoteVal | dolog | filter | lines | expect
false | true | "." | ['{"test":"value"}'] | [test: 'value']
null | true | "." | ['{"test":"value"}'] | [test: '"value"']
true | true | "." | ['{"test":"value"}'] | [test: '"value"']
true | true | "." | ['["value"]'] | ['result.0': '"value"']
null | true | "." | ['["value"]'] | ['result.0': '"value"']
false | true | "." | ['["value"]'] | ['result.0': 'value']

}

def "array test"() {
given:
def filter = ".[]"
Expand All @@ -70,6 +120,7 @@ class JsonLogFilterSpec extends Specification {
plugin.filter = filter
plugin.prefix = "result"
plugin.logData = dolog
plugin.extraQuotes = false
def sharedoutput = new DataOutput(ContextView.global())
def context = Mock(PluginLoggingContext) {
getOutputContext() >> sharedoutput
Expand Down

0 comments on commit 3f25da4

Please sign in to comment.