Skip to content

Commit 03d6ca8

Browse files
authored
Dockerized mysql test dependencies (#193)
* Some initial test app cleanup * Dockerized mysql server for int tests * Handle different database names in connection string
1 parent 368af3c commit 03d6ca8

File tree

7 files changed

+105
-53
lines changed

7 files changed

+105
-53
lines changed

tests/Agent/IntegrationTests/Shared/MySqlConfiguration.cs

+23
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class MySqlTestConfiguration
1111
private static string _mySqlConnectionString;
1212
private static string _mySqlServer;
1313
private static string _mySqlPort;
14+
private static string _mySqlDbName;
1415

1516
// example: "Network Address=1.2.3.4;Port=4444;Initial Catalog=CatalogName;Persist Security Info=no;User Name=root;Password=password"
1617
public static string MySqlConnectionString
@@ -77,5 +78,27 @@ public static string MySqlPort
7778
return _mySqlPort;
7879
}
7980
}
81+
82+
public static string MySqlDbName
83+
{
84+
get
85+
{
86+
if (_mySqlDbName == null)
87+
{
88+
try
89+
{
90+
var subParts = MySqlConnectionString.Split(';');
91+
var index = subParts[2].IndexOf('=') + 1;
92+
_mySqlDbName = subParts[2].Substring(index);
93+
}
94+
catch (Exception ex)
95+
{
96+
throw new Exception("MySqlDbName configuration is invalid.", ex);
97+
}
98+
}
99+
100+
return _mySqlDbName;
101+
}
102+
}
80103
}
81104
}

tests/Agent/IntegrationTests/UnboundedApplications/BasicMvcApplication/Controllers/MySqlController.cs

+8-47
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class MySqlController : Controller
1616
[HttpGet]
1717
public string MySql()
1818
{
19-
var teamMembers = new List<string>();
19+
var dates = new List<string>();
2020

2121
using (var connection = new MySqlConnection(MySqlTestConfiguration.MySqlConnectionString))
2222
using (var command = new MySqlCommand("SELECT _date FROM dates WHERE _date LIKE '2%' ORDER BY _date DESC LIMIT 1", connection))
@@ -26,18 +26,18 @@ public string MySql()
2626
{
2727
while (reader.Read())
2828
{
29-
teamMembers.Add(reader.GetString(reader.GetOrdinal("_date")));
29+
dates.Add(reader.GetString(reader.GetOrdinal("_date")));
3030
}
3131
}
3232
}
3333

34-
return string.Join(",", teamMembers);
34+
return string.Join(",", dates);
3535
}
3636

3737
[HttpGet]
3838
public async Task<string> MySqlAsync()
3939
{
40-
var teamMembers = new List<string>();
40+
var dates = new List<string>();
4141

4242
using (var connection = new MySqlConnection(MySqlTestConfiguration.MySqlConnectionString))
4343
using (var command = new MySqlCommand("SELECT _date FROM dates WHERE _date LIKE '2%' ORDER BY _date DESC LIMIT 10000", connection))
@@ -47,51 +47,12 @@ public async Task<string> MySqlAsync()
4747
{
4848
while (await reader.ReadAsync())
4949
{
50-
teamMembers.Add(reader.GetString(reader.GetOrdinal("_date")));
50+
dates.Add(reader.GetString(reader.GetOrdinal("_date")));
5151
}
5252
}
5353
}
5454

55-
return string.Join(",", teamMembers);
56-
}
57-
58-
[HttpGet]
59-
public async Task<string> MySql_Parameterized_FindMembersByKeyword_StoredProcedure(string keyword, bool paramsWithAtSigns)
60-
{
61-
var teamMembers = new List<string>();
62-
63-
using (var connection = new MySqlConnection(MySqlTestConfiguration.MySqlConnectionString))
64-
{
65-
connection.Open();
66-
67-
using (var command = new MySqlCommand(@"CALL findmembersbykeyword(@keywordValue)", connection))
68-
{
69-
70-
if (paramsWithAtSigns)
71-
{
72-
command.Parameters.Add(new MySqlParameter("@keywordValue", keyword));
73-
}
74-
else
75-
{
76-
command.Parameters.Add(new MySqlParameter("keywordValue", keyword));
77-
}
78-
79-
using (var reader = await command.ExecuteReaderAsync())
80-
{
81-
while (await reader.ReadAsync())
82-
{
83-
teamMembers.Add(reader.GetString(reader.GetOrdinal("firstName")));
84-
if (await reader.NextResultAsync())
85-
{
86-
teamMembers.Add(reader.GetString(reader.GetOrdinal("firstName")));
87-
}
88-
}
89-
}
90-
}
91-
92-
}
93-
94-
return string.Join(",", teamMembers);
55+
return string.Join(",", dates);
9556
}
9657

9758
[HttpGet]
@@ -118,12 +79,12 @@ public int MySqlParameterizedStoredProcedure(string procedureName, bool paramsWi
11879
}
11980

12081

121-
private static readonly string CreateProcedureStatement = @"CREATE PROCEDURE `NewRelic`.`{0}`({1}) BEGIN END;";
82+
private static readonly string CreateProcedureStatement = @"CREATE PROCEDURE `{0}`.`{1}`({2}) BEGIN END;";
12283

12384
private void CreateProcedure(string procedureName)
12485
{
12586
var parameters = string.Join(", ", DbParameterData.MySqlParameters.Select(x => $"{x.ParameterName} {x.DbTypeName}"));
126-
var statement = string.Format(CreateProcedureStatement, procedureName, parameters);
87+
var statement = string.Format(CreateProcedureStatement, MySqlTestConfiguration.MySqlDbName, procedureName, parameters);
12788
using (var connection = new MySqlConnection(MySqlTestConfiguration.MySqlConnectionString))
12889
using (var command = new MySqlCommand(statement, connection))
12990
{

tests/Agent/IntegrationTests/UnboundedIntegrationTests/MySqlAsyncTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void Test()
5757
new Assertions.ExpectedMetric { metricName = @"Datastore/allWeb", callCount = 1 },
5858
new Assertions.ExpectedMetric { metricName = @"Datastore/MySQL/all", callCount = 1 },
5959
new Assertions.ExpectedMetric { metricName = @"Datastore/MySQL/allWeb", callCount = 1 },
60-
new Assertions.ExpectedMetric { metricName = $@"Datastore/instance/MySQL/{MySqlTestConfiguration.MySqlServer}/{MySqlTestConfiguration.MySqlPort}", callCount = 1},
60+
new Assertions.ExpectedMetric { metricName = $@"Datastore/instance/MySQL/{CommonUtils.NormalizeHostname(MySqlTestConfiguration.MySqlServer)}/{MySqlTestConfiguration.MySqlPort}", callCount = 1},
6161
new Assertions.ExpectedMetric { metricName = @"Datastore/operation/MySQL/select", callCount = 1 },
6262
new Assertions.ExpectedMetric { metricName = @"Datastore/statement/MySQL/dates/select", callCount = 1 },
6363
new Assertions.ExpectedMetric { metricName = @"Datastore/statement/MySQL/dates/select", callCount = 1, metricScope = "WebTransaction/MVC/MySqlController/MySqlAsync"},
@@ -96,10 +96,10 @@ public void Test()
9696
var expectedTransactionTraceSegmentParameters = new List<Assertions.ExpectedSegmentParameter>
9797
{
9898
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "sql", parameterValue = "SELECT _date FROM dates WHERE _date LIKE ? ORDER BY _date DESC LIMIT ?"},
99-
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "host", parameterValue = MySqlTestConfiguration.MySqlServer},
99+
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "host", parameterValue = CommonUtils.NormalizeHostname(MySqlTestConfiguration.MySqlServer)},
100100
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "port_path_or_id", parameterValue = MySqlTestConfiguration.MySqlPort},
101101
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "explain_plan"},
102-
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "database_name", parameterValue = "NewRelic"}
102+
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "database_name", parameterValue = MySqlTestConfiguration.MySqlDbName}
103103

104104
};
105105

tests/Agent/IntegrationTests/UnboundedIntegrationTests/MySqlTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void Test()
5858
new Assertions.ExpectedMetric { metricName = @"Datastore/allWeb", callCount = 1 },
5959
new Assertions.ExpectedMetric { metricName = @"Datastore/MySQL/all", callCount = 1 },
6060
new Assertions.ExpectedMetric { metricName = @"Datastore/MySQL/allWeb", callCount = 1 },
61-
new Assertions.ExpectedMetric { metricName = $@"Datastore/instance/MySQL/{MySqlTestConfiguration.MySqlServer}/{MySqlTestConfiguration.MySqlPort}", callCount = 1},
61+
new Assertions.ExpectedMetric { metricName = $@"Datastore/instance/MySQL/{CommonUtils.NormalizeHostname(MySqlTestConfiguration.MySqlServer)}/{MySqlTestConfiguration.MySqlPort}", callCount = 1},
6262
new Assertions.ExpectedMetric { metricName = @"Datastore/operation/MySQL/select", callCount = 1 },
6363
new Assertions.ExpectedMetric { metricName = @"Datastore/statement/MySQL/dates/select", callCount = 1 },
6464
new Assertions.ExpectedMetric { metricName = @"Datastore/statement/MySQL/dates/select", callCount = 1, metricScope = "WebTransaction/MVC/MySqlController/MySql"},
@@ -100,9 +100,9 @@ public void Test()
100100
var expectedTransactionTraceSegmentParameters = new List<Assertions.ExpectedSegmentParameter>
101101
{
102102
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "sql", parameterValue = "SELECT _date FROM dates WHERE _date LIKE ? ORDER BY _date DESC LIMIT ?"},
103-
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "host", parameterValue = MySqlTestConfiguration.MySqlServer},
103+
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "host", parameterValue = CommonUtils.NormalizeHostname(MySqlTestConfiguration.MySqlServer)},
104104
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "port_path_or_id", parameterValue = MySqlTestConfiguration.MySqlPort},
105-
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "database_name", parameterValue = "NewRelic"},
105+
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "database_name", parameterValue = MySqlTestConfiguration.MySqlDbName},
106106
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "explain_plan"}
107107
};
108108

tests/Agent/IntegrationTests/UnboundedServices/docker-compose.yml

+12
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,15 @@ db2:
7676
- SAMPLEDB=true
7777
container_name: Db2Server
7878

79+
mysql:
80+
build: ./mysql
81+
command: mysqld --default-authentication-plugin=mysql_native_password
82+
ports:
83+
- "3306:3306"
84+
environment:
85+
# These credentials are only used to configure the MySql container
86+
# for use by the integration tests. They must match the values from
87+
# the connection string used by those tests
88+
- MYSQL_ROOT_PASSWORD=password
89+
container_name: MySqlServer
90+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM mysql:8.0
2+
3+
COPY database.sql /docker-entrypoint-initdb.d

tests/Agent/IntegrationTests/UnboundedServices/mysql/database.sql

+53
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)