Fix MongoDB testLatestDeps (#1703)
* Fix MongoDB testLatestDeps * Codenarc
This commit is contained in:
parent
7bfb6b6dbd
commit
c3e1a321d2
|
@ -3,6 +3,8 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import static java.util.Arrays.asList
|
||||||
|
|
||||||
import io.opentelemetry.javaagent.instrumentation.mongo.MongoClientTracer
|
import io.opentelemetry.javaagent.instrumentation.mongo.MongoClientTracer
|
||||||
import org.bson.BsonArray
|
import org.bson.BsonArray
|
||||||
import org.bson.BsonDocument
|
import org.bson.BsonDocument
|
||||||
|
@ -10,27 +12,25 @@ import org.bson.BsonInt32
|
||||||
import org.bson.BsonString
|
import org.bson.BsonString
|
||||||
import spock.lang.Specification
|
import spock.lang.Specification
|
||||||
|
|
||||||
import static java.util.Arrays.asList
|
|
||||||
|
|
||||||
class MongoClientTracerTest extends Specification {
|
class MongoClientTracerTest extends Specification {
|
||||||
def 'should normalize queries to json'() {
|
def 'should normalize queries to json'() {
|
||||||
setup:
|
setup:
|
||||||
def tracer = new MongoClientTracer()
|
def tracer = new MongoClientTracer()
|
||||||
|
|
||||||
expect:
|
expect:
|
||||||
tracer.normalizeQuery(
|
normalizeQueryAcrossVersions(tracer,
|
||||||
new BsonDocument("cmd", new BsonInt32(1))) ==
|
new BsonDocument("cmd", new BsonInt32(1))) ==
|
||||||
'{ "cmd" : "?" }'
|
'{"cmd": "?"}'
|
||||||
|
|
||||||
tracer.normalizeQuery(
|
normalizeQueryAcrossVersions(tracer,
|
||||||
new BsonDocument("cmd", new BsonInt32(1))
|
new BsonDocument("cmd", new BsonInt32(1))
|
||||||
.append("sub", new BsonDocument("a", new BsonInt32(1)))) ==
|
.append("sub", new BsonDocument("a", new BsonInt32(1)))) ==
|
||||||
'{ "cmd" : "?", "sub" : { "a" : "?" } }'
|
'{"cmd": "?", "sub": {"a": "?"}}'
|
||||||
|
|
||||||
tracer.normalizeQuery(
|
normalizeQueryAcrossVersions(tracer,
|
||||||
new BsonDocument("cmd", new BsonInt32(1))
|
new BsonDocument("cmd", new BsonInt32(1))
|
||||||
.append("sub", new BsonArray(asList(new BsonInt32(1))))) ==
|
.append("sub", new BsonArray(asList(new BsonInt32(1))))) ==
|
||||||
'{ "cmd" : "?", "sub" : ["?"] }'
|
'{"cmd": "?", "sub": ["?"]}'
|
||||||
}
|
}
|
||||||
|
|
||||||
def 'should only preserve string value if it is the value of the first top-level key'() {
|
def 'should only preserve string value if it is the value of the first top-level key'() {
|
||||||
|
@ -38,34 +38,47 @@ class MongoClientTracerTest extends Specification {
|
||||||
def tracer = new MongoClientTracer()
|
def tracer = new MongoClientTracer()
|
||||||
|
|
||||||
expect:
|
expect:
|
||||||
tracer.normalizeQuery(
|
normalizeQueryAcrossVersions(tracer,
|
||||||
new BsonDocument("cmd", new BsonString("c"))
|
new BsonDocument("cmd", new BsonString("c"))
|
||||||
.append("f", new BsonString("c"))
|
.append("f", new BsonString("c"))
|
||||||
.append("sub", new BsonString("c"))) ==
|
.append("sub", new BsonString("c"))) ==
|
||||||
'{ "cmd" : "c", "f" : "?", "sub" : "?" }'
|
'{"cmd": "c", "f": "?", "sub": "?"}'
|
||||||
}
|
}
|
||||||
|
|
||||||
def 'should truncate simple command'() {
|
def 'should truncate simple command'() {
|
||||||
setup:
|
setup:
|
||||||
def tracer = new MongoClientTracer(20)
|
def tracer = new MongoClientTracer(20)
|
||||||
|
|
||||||
expect:
|
def normalized = normalizeQueryAcrossVersions(tracer,
|
||||||
tracer.normalizeQuery(
|
|
||||||
new BsonDocument("cmd", new BsonString("c"))
|
new BsonDocument("cmd", new BsonString("c"))
|
||||||
.append("f1", new BsonString("c1"))
|
.append("f1", new BsonString("c1"))
|
||||||
.append("f2", new BsonString("c2"))) ==
|
.append("f2", new BsonString("c2")))
|
||||||
'{ "cmd" : "c", "f1" '
|
expect:
|
||||||
|
// this can vary because of different whitespace for different mongo versions
|
||||||
|
normalized == '{"cmd": "c", "f1": "' || normalized == '{"cmd": "c", "f1" '
|
||||||
}
|
}
|
||||||
|
|
||||||
def 'should truncate array'() {
|
def 'should truncate array'() {
|
||||||
setup:
|
setup:
|
||||||
def tracer = new MongoClientTracer(27)
|
def tracer = new MongoClientTracer(27)
|
||||||
|
|
||||||
expect:
|
def normalized = normalizeQueryAcrossVersions(tracer,
|
||||||
tracer.normalizeQuery(
|
|
||||||
new BsonDocument("cmd", new BsonString("c"))
|
new BsonDocument("cmd", new BsonString("c"))
|
||||||
.append("f1", new BsonArray(asList(new BsonString("c1"), new BsonString("c2"))))
|
.append("f1", new BsonArray(Arrays.asList(new BsonString("c1"), new BsonString("c2"))))
|
||||||
.append("f2", new BsonString("c3"))) ==
|
.append("f2", new BsonString("c3")))
|
||||||
'{ "cmd" : "c", "f1" : ["?",'
|
expect:
|
||||||
|
// this can vary because of different whitespace for different mongo versions
|
||||||
|
normalized == '{"cmd": "c", "f1": ["?", "?' || normalized == '{"cmd": "c", "f1": ["?",'
|
||||||
|
}
|
||||||
|
|
||||||
|
def normalizeQueryAcrossVersions(MongoClientTracer tracer, BsonDocument query) {
|
||||||
|
return normalizeAcrossVersions(tracer.normalizeQuery(query))
|
||||||
|
}
|
||||||
|
|
||||||
|
def normalizeAcrossVersions(String json) {
|
||||||
|
json = json.replaceAll('\\{ ', '{')
|
||||||
|
json = json.replaceAll(' }', '}')
|
||||||
|
json = json.replaceAll(' :', ':')
|
||||||
|
return json
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import static java.util.Arrays.asList
|
||||||
|
|
||||||
import io.opentelemetry.javaagent.instrumentation.mongo.MongoClientTracer
|
import io.opentelemetry.javaagent.instrumentation.mongo.MongoClientTracer
|
||||||
import org.bson.BsonArray
|
import org.bson.BsonArray
|
||||||
import org.bson.BsonDocument
|
import org.bson.BsonDocument
|
||||||
|
@ -10,27 +12,25 @@ import org.bson.BsonInt32
|
||||||
import org.bson.BsonString
|
import org.bson.BsonString
|
||||||
import spock.lang.Specification
|
import spock.lang.Specification
|
||||||
|
|
||||||
import static java.util.Arrays.asList
|
|
||||||
|
|
||||||
class MongoClientTracerTest extends Specification {
|
class MongoClientTracerTest extends Specification {
|
||||||
def 'should normalize queries to json'() {
|
def 'should normalize queries to json'() {
|
||||||
setup:
|
setup:
|
||||||
def tracer = new MongoClientTracer()
|
def tracer = new MongoClientTracer()
|
||||||
|
|
||||||
expect:
|
expect:
|
||||||
tracer.normalizeQuery(
|
normalizeQueryAcrossVersions(tracer,
|
||||||
new BsonDocument("cmd", new BsonInt32(1))) ==
|
new BsonDocument("cmd", new BsonInt32(1))) ==
|
||||||
'{ "cmd" : "?" }'
|
'{"cmd": "?"}'
|
||||||
|
|
||||||
tracer.normalizeQuery(
|
normalizeQueryAcrossVersions(tracer,
|
||||||
new BsonDocument("cmd", new BsonInt32(1))
|
new BsonDocument("cmd", new BsonInt32(1))
|
||||||
.append("sub", new BsonDocument("a", new BsonInt32(1)))) ==
|
.append("sub", new BsonDocument("a", new BsonInt32(1)))) ==
|
||||||
'{ "cmd" : "?", "sub" : { "a" : "?" } }'
|
'{"cmd": "?", "sub": {"a": "?"}}'
|
||||||
|
|
||||||
tracer.normalizeQuery(
|
normalizeQueryAcrossVersions(tracer,
|
||||||
new BsonDocument("cmd", new BsonInt32(1))
|
new BsonDocument("cmd", new BsonInt32(1))
|
||||||
.append("sub", new BsonArray(asList(new BsonInt32(1))))) ==
|
.append("sub", new BsonArray(asList(new BsonInt32(1))))) ==
|
||||||
'{ "cmd" : "?", "sub" : ["?"] }'
|
'{"cmd": "?", "sub": ["?"]}'
|
||||||
}
|
}
|
||||||
|
|
||||||
def 'should only preserve string value if it is the value of the first top-level key'() {
|
def 'should only preserve string value if it is the value of the first top-level key'() {
|
||||||
|
@ -38,34 +38,47 @@ class MongoClientTracerTest extends Specification {
|
||||||
def tracer = new MongoClientTracer()
|
def tracer = new MongoClientTracer()
|
||||||
|
|
||||||
expect:
|
expect:
|
||||||
tracer.normalizeQuery(
|
normalizeQueryAcrossVersions(tracer,
|
||||||
new BsonDocument("cmd", new BsonString("c"))
|
new BsonDocument("cmd", new BsonString("c"))
|
||||||
.append("f", new BsonString("c"))
|
.append("f", new BsonString("c"))
|
||||||
.append("sub", new BsonString("c"))) ==
|
.append("sub", new BsonString("c"))) ==
|
||||||
'{ "cmd" : "c", "f" : "?", "sub" : "?" }'
|
'{"cmd": "c", "f": "?", "sub": "?"}'
|
||||||
}
|
}
|
||||||
|
|
||||||
def 'should truncate simple command'() {
|
def 'should truncate simple command'() {
|
||||||
setup:
|
setup:
|
||||||
def tracer = new MongoClientTracer(20)
|
def tracer = new MongoClientTracer(20)
|
||||||
|
|
||||||
expect:
|
def normalized = normalizeQueryAcrossVersions(tracer,
|
||||||
tracer.normalizeQuery(
|
|
||||||
new BsonDocument("cmd", new BsonString("c"))
|
new BsonDocument("cmd", new BsonString("c"))
|
||||||
.append("f1", new BsonString("c1"))
|
.append("f1", new BsonString("c1"))
|
||||||
.append("f2", new BsonString("c2"))) ==
|
.append("f2", new BsonString("c2")))
|
||||||
'{ "cmd" : "c", "f1" '
|
expect:
|
||||||
|
// this can vary because of different whitespace for different mongo versions
|
||||||
|
normalized == '{"cmd": "c", "f1": "' || normalized == '{"cmd": "c", "f1" '
|
||||||
}
|
}
|
||||||
|
|
||||||
def 'should truncate array'() {
|
def 'should truncate array'() {
|
||||||
setup:
|
setup:
|
||||||
def tracer = new MongoClientTracer(27)
|
def tracer = new MongoClientTracer(27)
|
||||||
|
|
||||||
expect:
|
def normalized = normalizeQueryAcrossVersions(tracer,
|
||||||
tracer.normalizeQuery(
|
|
||||||
new BsonDocument("cmd", new BsonString("c"))
|
new BsonDocument("cmd", new BsonString("c"))
|
||||||
.append("f1", new BsonArray(asList(new BsonString("c1"), new BsonString("c2"))))
|
.append("f1", new BsonArray(Arrays.asList(new BsonString("c1"), new BsonString("c2"))))
|
||||||
.append("f2", new BsonString("c3"))) ==
|
.append("f2", new BsonString("c3")))
|
||||||
'{ "cmd" : "c", "f1" : ["?",'
|
expect:
|
||||||
|
// this can vary because of different whitespace for different mongo versions
|
||||||
|
normalized == '{"cmd": "c", "f1": ["?", "?' || normalized == '{"cmd": "c", "f1": ["?",'
|
||||||
|
}
|
||||||
|
|
||||||
|
def normalizeQueryAcrossVersions(MongoClientTracer tracer, BsonDocument query) {
|
||||||
|
return normalizeAcrossVersions(tracer.normalizeQuery(query))
|
||||||
|
}
|
||||||
|
|
||||||
|
def normalizeAcrossVersions(String json) {
|
||||||
|
json = json.replaceAll('\\{ ', '{')
|
||||||
|
json = json.replaceAll(' }', '}')
|
||||||
|
json = json.replaceAll(' :', ':')
|
||||||
|
return json
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue