Skip to content

Commit ea2c982

Browse files
committed
add test
1 parent cd6e512 commit ea2c982

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import static datadog.trace.api.config.TraceInstrumentationConfig.DB_CLIENT_INFO_FETCHING_ENABLED
2+
import static datadog.trace.api.config.TraceInstrumentationConfig.DB_METADATA_FETCHING_ENABLED
3+
4+
import datadog.trace.agent.test.InstrumentationSpecification
5+
import datadog.trace.bootstrap.instrumentation.jdbc.DBInfo
6+
import datadog.trace.instrumentation.jdbc.JDBCDecorator
7+
import java.sql.Connection
8+
import test.TestDatabaseMetaData
9+
10+
/**
11+
* Base test class for parseDBInfoFromConnection with different flag configurations
12+
*/
13+
abstract class JDBCDecoratorParseDBInfoTestBase extends InstrumentationSpecification {
14+
15+
def "test parseDBInfoFromConnection with null connection"() {
16+
when:
17+
def result = JDBCDecorator.parseDBInfoFromConnection(null)
18+
19+
then:
20+
result == DBInfo.DEFAULT
21+
}
22+
23+
def "test parseDBInfoFromConnection with null ClientInfo"() {
24+
setup:
25+
def metadata = new TestDatabaseMetaData()
26+
metadata.setURL("jdbc:postgresql://testhost:5432/testdb")
27+
def connection = Mock(Connection) {
28+
getMetaData() >> metadata
29+
getClientInfo() >> null
30+
}
31+
32+
when:
33+
def result = JDBCDecorator.parseDBInfoFromConnection(connection)
34+
35+
then:
36+
if (shouldFetchMetadata()) {
37+
result.type == "postgresql"
38+
result.host == "testhost"
39+
result.port == 5432
40+
result.db == "testdb"
41+
} else {
42+
result == DBInfo.DEFAULT
43+
}
44+
}
45+
46+
def "test parseDBInfoFromConnection regular case"() {
47+
setup:
48+
def metadata = new TestDatabaseMetaData()
49+
metadata.setURL("jdbc:postgresql://testhost:5432/testdb")
50+
def clientInfo = new Properties()
51+
clientInfo.setProperty("warehouse", "my-test-warehouse") // we'll check that property to know if clientInfo were used
52+
def connection = Mock(Connection) {
53+
getMetaData() >> (shouldFetchMetadata() ? metadata : { assert false })
54+
getClientInfo() >> (shouldFetchClientInfo() ? clientInfo : { assert false })
55+
}
56+
57+
when:
58+
def result = JDBCDecorator.parseDBInfoFromConnection(connection)
59+
60+
then:
61+
if (shouldFetchMetadata()) {
62+
result.type == "postgresql"
63+
result.host == "testhost"
64+
result.port == 5432
65+
result.db == "testdb"
66+
if (shouldFetchClientInfo()) { // only if we _also_ fetch metadata
67+
result.warehouse == "my-test-warehouse"
68+
}
69+
else {
70+
result.warehouse == null
71+
}
72+
} else {
73+
result == DBInfo.DEFAULT
74+
}
75+
}
76+
77+
abstract boolean shouldFetchMetadata()
78+
79+
abstract boolean shouldFetchClientInfo()
80+
}
81+
82+
/**
83+
* Test with both flags enabled (default behavior)
84+
*/
85+
class JDBCDecoratorParseDBInfoWithMetadataAndClientInfoForkedTest extends JDBCDecoratorParseDBInfoTestBase {
86+
87+
@Override
88+
void configurePreAgent() {
89+
super.configurePreAgent()
90+
injectSysConfig(DB_METADATA_FETCHING_ENABLED, "true")
91+
injectSysConfig(DB_CLIENT_INFO_FETCHING_ENABLED, "true")
92+
}
93+
94+
@Override
95+
boolean shouldFetchMetadata() {
96+
return true
97+
}
98+
99+
@Override
100+
boolean shouldFetchClientInfo() {
101+
return true
102+
}
103+
}
104+
105+
/**
106+
* Test with both flags disabled
107+
*/
108+
class JDBCDecoratorParseDBInfoWithoutCallsForkedTest extends JDBCDecoratorParseDBInfoTestBase {
109+
110+
@Override
111+
void configurePreAgent() {
112+
super.configurePreAgent()
113+
injectSysConfig(DB_METADATA_FETCHING_ENABLED, "false")
114+
injectSysConfig(DB_CLIENT_INFO_FETCHING_ENABLED, "false")
115+
}
116+
117+
@Override
118+
boolean shouldFetchMetadata() {
119+
return false
120+
}
121+
122+
@Override
123+
boolean shouldFetchClientInfo() {
124+
return false
125+
}
126+
}
127+
128+
/**
129+
* Test with metadata enabled but client info disabled
130+
*/
131+
class JDBCDecoratorParseDBInfoWithMetadataOnlyForkedTest extends JDBCDecoratorParseDBInfoTestBase {
132+
133+
@Override
134+
void configurePreAgent() {
135+
super.configurePreAgent()
136+
injectSysConfig(DB_METADATA_FETCHING_ENABLED, "true")
137+
injectSysConfig(DB_CLIENT_INFO_FETCHING_ENABLED, "false")
138+
}
139+
140+
@Override
141+
boolean shouldFetchMetadata() {
142+
return true
143+
}
144+
145+
@Override
146+
boolean shouldFetchClientInfo() {
147+
return false
148+
}
149+
}
150+
151+
class JDBCDecoratorParseDBInfoWithClientInfoOnlyForkedTest extends JDBCDecoratorParseDBInfoTestBase {
152+
153+
@Override
154+
void configurePreAgent() {
155+
super.configurePreAgent()
156+
injectSysConfig(DB_METADATA_FETCHING_ENABLED, "false")
157+
injectSysConfig(DB_CLIENT_INFO_FETCHING_ENABLED, "true")
158+
}
159+
160+
@Override
161+
boolean shouldFetchMetadata() {
162+
return false
163+
}
164+
165+
@Override
166+
boolean shouldFetchClientInfo() {
167+
return true
168+
}
169+
}
170+

0 commit comments

Comments
 (0)