gpt4 book ai didi

sql-server - 无法通过Azure DevOps Pipeline集成测试查询容器化的SQL Server数据库

转载 作者:行者123 更新时间:2023-12-02 19:47:50 25 4
gpt4 key购买 nike

我有一个集成测试,该测试使用SQL Server和已还原的数据库启动基于Linux的Docker容器。该测试运行一个简单的select count(*)查询,并在本地运行时每次都通过。当测试作为Azure DevOps Pipeline的一部分运行时,测试失败并显示以下错误:

System.Data.SqlClient.SqlException : Cannot open database "A" requested by the login. The login failed. Login failed for user 'sa'.
Stack Trace:
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.WaitForPendingOpen()
当我更改测试以查询 master数据库而不是我们自己的数据库时,查询成功运行并且测试通过。
测试的核心如下:
using (var connection = new SqlConnection(connectionString.ConnectionString))
{
await connection.OpenAsync().ConfigureAwait(false);

using (var command = new SqlCommand("select count(*) from [table];", connection))
{
var rowCount = (int)await command.ExecuteScalarAsync();

rowCount.Should().BeGreaterThan(0);
}

connection.Close();
}
此连接字符串有效:
var connectionString = new SqlConnectionStringBuilder
{
DataSource = "localhost,32808",
UserID = "sa",
Password = "P@ssword!23",
InitialCatalog = "master",
ConnectTimeout = 120,
ConnectRetryCount = 3
};
此失败:
var connectionString = new SqlConnectionStringBuilder
{
DataSource = "localhost,32808",
UserID = "sa",
Password = "P@ssword!23",
InitialCatalog = "A",
ConnectTimeout = 120,
ConnectRetryCount = 3
};
最初,测试因连接超时而失败。我添加了一个更长的超时,并且异常更改为此。安全设置没有什么明显的缺失: sadbo数据库的 A。此外,测试使用同一Docker镜像在本地通过。
感激收到有关如何解决此问题的任何想法,或通过日志记录等获得其他反馈。
SQL Server 15.0.4023.6; Ubuntu 18.04.4;完整的Azure构建代理规范 here;

最佳答案

事实证明,解决此问题的关键是测试代码如何检查容器中SQL Server实例的准备情况。
该代码尝试使用master数据库创建到容器中运行的SQL Server实例的连接。我不知道的是,虽然master数据库可能已经准备就绪并且可以接受连接,但其他数据库可能还没有。我更改了代码,以尝试使用测试要使用的实际数据库而不是master进行连接,这似乎可行。
本地测试必须已经在本地通过,因为与Azure DevOps环境相比,容器在该环境中能够更快地一致地完成初始化。

关于sql-server - 无法通过Azure DevOps Pipeline集成测试查询容器化的SQL Server数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62656139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com