Skip to content

Dockerized mysql test dependencies #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/Agent/IntegrationTests/Shared/MySqlConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class MySqlTestConfiguration
private static string _mySqlConnectionString;
private static string _mySqlServer;
private static string _mySqlPort;
private static string _mySqlDbName;

// example: "Network Address=1.2.3.4;Port=4444;Initial Catalog=CatalogName;Persist Security Info=no;User Name=root;Password=password"
public static string MySqlConnectionString
Expand Down Expand Up @@ -77,5 +78,27 @@ public static string MySqlPort
return _mySqlPort;
}
}

public static string MySqlDbName
{
get
{
if (_mySqlDbName == null)
{
try
{
var subParts = MySqlConnectionString.Split(';');
var index = subParts[2].IndexOf('=') + 1;
_mySqlDbName = subParts[2].Substring(index);
}
catch (Exception ex)
{
throw new Exception("MySqlDbName configuration is invalid.", ex);
}
}

return _mySqlDbName;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class MySqlController : Controller
[HttpGet]
public string MySql()
{
var teamMembers = new List<string>();
var dates = new List<string>();

using (var connection = new MySqlConnection(MySqlTestConfiguration.MySqlConnectionString))
using (var command = new MySqlCommand("SELECT _date FROM dates WHERE _date LIKE '2%' ORDER BY _date DESC LIMIT 1", connection))
Expand All @@ -26,18 +26,18 @@ public string MySql()
{
while (reader.Read())
{
teamMembers.Add(reader.GetString(reader.GetOrdinal("_date")));
dates.Add(reader.GetString(reader.GetOrdinal("_date")));
}
}
}

return string.Join(",", teamMembers);
return string.Join(",", dates);
}

[HttpGet]
public async Task<string> MySqlAsync()
{
var teamMembers = new List<string>();
var dates = new List<string>();

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

return string.Join(",", teamMembers);
}

[HttpGet]
public async Task<string> MySql_Parameterized_FindMembersByKeyword_StoredProcedure(string keyword, bool paramsWithAtSigns)
{
var teamMembers = new List<string>();

using (var connection = new MySqlConnection(MySqlTestConfiguration.MySqlConnectionString))
{
connection.Open();

using (var command = new MySqlCommand(@"CALL findmembersbykeyword(@keywordValue)", connection))
{

if (paramsWithAtSigns)
{
command.Parameters.Add(new MySqlParameter("@keywordValue", keyword));
}
else
{
command.Parameters.Add(new MySqlParameter("keywordValue", keyword));
}

using (var reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
teamMembers.Add(reader.GetString(reader.GetOrdinal("firstName")));
if (await reader.NextResultAsync())
{
teamMembers.Add(reader.GetString(reader.GetOrdinal("firstName")));
}
}
}
}

}

return string.Join(",", teamMembers);
return string.Join(",", dates);
}

[HttpGet]
Expand All @@ -118,12 +79,12 @@ public int MySqlParameterizedStoredProcedure(string procedureName, bool paramsWi
}


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

private void CreateProcedure(string procedureName)
{
var parameters = string.Join(", ", DbParameterData.MySqlParameters.Select(x => $"{x.ParameterName} {x.DbTypeName}"));
var statement = string.Format(CreateProcedureStatement, procedureName, parameters);
var statement = string.Format(CreateProcedureStatement, MySqlTestConfiguration.MySqlDbName, procedureName, parameters);
using (var connection = new MySqlConnection(MySqlTestConfiguration.MySqlConnectionString))
using (var command = new MySqlCommand(statement, connection))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void Test()
new Assertions.ExpectedMetric { metricName = @"Datastore/allWeb", callCount = 1 },
new Assertions.ExpectedMetric { metricName = @"Datastore/MySQL/all", callCount = 1 },
new Assertions.ExpectedMetric { metricName = @"Datastore/MySQL/allWeb", callCount = 1 },
new Assertions.ExpectedMetric { metricName = $@"Datastore/instance/MySQL/{MySqlTestConfiguration.MySqlServer}/{MySqlTestConfiguration.MySqlPort}", callCount = 1},
new Assertions.ExpectedMetric { metricName = $@"Datastore/instance/MySQL/{CommonUtils.NormalizeHostname(MySqlTestConfiguration.MySqlServer)}/{MySqlTestConfiguration.MySqlPort}", callCount = 1},
new Assertions.ExpectedMetric { metricName = @"Datastore/operation/MySQL/select", callCount = 1 },
new Assertions.ExpectedMetric { metricName = @"Datastore/statement/MySQL/dates/select", callCount = 1 },
new Assertions.ExpectedMetric { metricName = @"Datastore/statement/MySQL/dates/select", callCount = 1, metricScope = "WebTransaction/MVC/MySqlController/MySqlAsync"},
Expand Down Expand Up @@ -96,10 +96,10 @@ public void Test()
var expectedTransactionTraceSegmentParameters = new List<Assertions.ExpectedSegmentParameter>
{
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "sql", parameterValue = "SELECT _date FROM dates WHERE _date LIKE ? ORDER BY _date DESC LIMIT ?"},
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "host", parameterValue = MySqlTestConfiguration.MySqlServer},
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "host", parameterValue = CommonUtils.NormalizeHostname(MySqlTestConfiguration.MySqlServer)},
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "port_path_or_id", parameterValue = MySqlTestConfiguration.MySqlPort},
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "explain_plan"},
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "database_name", parameterValue = "NewRelic"}
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "database_name", parameterValue = MySqlTestConfiguration.MySqlDbName}

};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void Test()
new Assertions.ExpectedMetric { metricName = @"Datastore/allWeb", callCount = 1 },
new Assertions.ExpectedMetric { metricName = @"Datastore/MySQL/all", callCount = 1 },
new Assertions.ExpectedMetric { metricName = @"Datastore/MySQL/allWeb", callCount = 1 },
new Assertions.ExpectedMetric { metricName = $@"Datastore/instance/MySQL/{MySqlTestConfiguration.MySqlServer}/{MySqlTestConfiguration.MySqlPort}", callCount = 1},
new Assertions.ExpectedMetric { metricName = $@"Datastore/instance/MySQL/{CommonUtils.NormalizeHostname(MySqlTestConfiguration.MySqlServer)}/{MySqlTestConfiguration.MySqlPort}", callCount = 1},
new Assertions.ExpectedMetric { metricName = @"Datastore/operation/MySQL/select", callCount = 1 },
new Assertions.ExpectedMetric { metricName = @"Datastore/statement/MySQL/dates/select", callCount = 1 },
new Assertions.ExpectedMetric { metricName = @"Datastore/statement/MySQL/dates/select", callCount = 1, metricScope = "WebTransaction/MVC/MySqlController/MySql"},
Expand Down Expand Up @@ -100,9 +100,9 @@ public void Test()
var expectedTransactionTraceSegmentParameters = new List<Assertions.ExpectedSegmentParameter>
{
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "sql", parameterValue = "SELECT _date FROM dates WHERE _date LIKE ? ORDER BY _date DESC LIMIT ?"},
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "host", parameterValue = MySqlTestConfiguration.MySqlServer},
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "host", parameterValue = CommonUtils.NormalizeHostname(MySqlTestConfiguration.MySqlServer)},
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "port_path_or_id", parameterValue = MySqlTestConfiguration.MySqlPort},
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "database_name", parameterValue = "NewRelic"},
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "database_name", parameterValue = MySqlTestConfiguration.MySqlDbName},
new Assertions.ExpectedSegmentParameter { segmentName = "Datastore/statement/MySQL/dates/select", parameterName = "explain_plan"}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@ db2:
- SAMPLEDB=true
container_name: Db2Server

mysql:
build: ./mysql
command: mysqld --default-authentication-plugin=mysql_native_password
ports:
- "3306:3306"
environment:
# These credentials are only used to configure the MySql container
# for use by the integration tests. They must match the values from
# the connection string used by those tests
- MYSQL_ROOT_PASSWORD=password
container_name: MySqlServer

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM mysql:8.0

COPY database.sql /docker-entrypoint-initdb.d

Large diffs are not rendered by default.