使用不同的凭据运行NUnit集成测试

时间:2022-01-21 14:02:05

I am running integration tests with C#, NUnit, and SQL Server 2008 r2 dev edition database. Setting up my fixture includes creating a new database and loading test data, so I need dbo privileges for that.

我正在使用C#,NUnit和SQL Server 2008 r2 dev版数据库运行集成测试。设置我的夹具包括创建一个新数据库和加载测试数据,所以我需要dbo权限。

However, I want to run the tests themselves with less privileges. I have another AD account I can authenticate with, and I do can run some T-SQL using impersonation as described here: http://support.microsoft.com/?scid=306158, as follows:

但是,我希望以较少的权限运行测试。我有另一个我可以通过身份验证的AD帐户,我可以使用模拟运行一些T-SQL,如下所述:http://support.microsoft.com/?scid = 306158,如下所示:

public static bool ExecuteFileAs(string fileName, string connectionString, 
                                  string user, string domain, string password)
{
    using(new Impersonator(user, domain, password))
    {
        using(var connection = new SqlConnection(connectionString))
        {
            connection.Open();
            return SqlFileExecuter.RunSql(connection, fileName);
        }
    }
}

When I hit the breakpoint inside this code snippet, and start the Profiler, I see another connection open with the username I submitted to it, so impersonation really works. Unfortunately, I cannot run all the tests impersonating at the end of fixture set up, and ending it at fixture tear down. At the end of set up I execute the following:

当我点击此代码段内的断点并启动Profiler时,我看到另一个连接打开了我提交给它的用户名,因此模拟确实有效。不幸的是,我无法在夹具设置结束时运行所有测试模拟,并在夹具拆卸时结束它。在设置结束时,我执行以下操作:

        impersonator = new Impersonator("username", "DOMAIN", "pwd");

As soon as the first unit test begins, I am getting this error listing one of the dlls used in this test: System.IO.FileLoadException: Could not load file or assembly '...' or one of its dependencies. Access is denied. I have granted that other account full access to the directory with all my binaries, which did not help.

第一次单元测试开始后,我收到此错误列出了此测试中使用的一个dll:System.IO.FileLoadException:无法加载文件或程序集“...”或其依赖项之一。访问被拒绝。我已授予其他帐户对所有二进制文件的完全访问权限,这对我没有帮助。

Any suggestions are welcome.

欢迎任何建议。

Edit: my workstation is still running XP.

编辑:我的工作站仍在运行XP。

2 个解决方案

#1


3  

One possible solution is to not use Windows authentication, use a SQL user instead. This would just be a change of connection strings.

一种可能的解决方案是不使用Windows身份验证,而是使用SQL用户。这只是连接字符串的更改。

Additionally, if the error is related to IO permissions, you can try doing all the IO before you impersonate the user. Using http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.server.aspx instead of SqlFileExecuter you can directly execute blocks of T-SQL against a SQL Server (unlike ADO.NET). SMO is slightly different than how you're doing it above...

此外,如果错误与IO权限相关,则可以在模拟用户之前尝试执行所有IO。使用http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.server.aspx而不是SqlFileExecuter,您可以直接对SQL Server执行T-SQL块(与ADO.NET不同) 。 SMO与您上面的方式略有不同......

var s = new Server(connectionString);
s.ConnectionContext.ExecuteNonQuery(@"CREATE DATABASE test; GO; Use test; ...");

#2


2  

Try to check if nothing else is locking same file without share-read access. Also enable bind error logging. Type fusion in win7 start menu and run it as admin so that settings are not grayed out. Then set 'log bind failures to disk' and specify location for the log file. If You don't get any logs check in registry that HKLM\Software\Microsoft\Fusion\ForceLog = 1

尝试检查没有其他任何东西锁定相同的文件没有共享读取访问权限。还启用绑定错误日志记录。在win7开始菜单中键入fusion并以管理员身份运行,以便设置不会变灰。然后设置“将日志绑定失败到磁盘”并指定日志文件的位置。如果您没有获得任何日志,请在注册表中检查HKLM \ Software \ Microsoft \ Fusion \ ForceLog = 1

#1


3  

One possible solution is to not use Windows authentication, use a SQL user instead. This would just be a change of connection strings.

一种可能的解决方案是不使用Windows身份验证,而是使用SQL用户。这只是连接字符串的更改。

Additionally, if the error is related to IO permissions, you can try doing all the IO before you impersonate the user. Using http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.server.aspx instead of SqlFileExecuter you can directly execute blocks of T-SQL against a SQL Server (unlike ADO.NET). SMO is slightly different than how you're doing it above...

此外,如果错误与IO权限相关,则可以在模拟用户之前尝试执行所有IO。使用http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.server.aspx而不是SqlFileExecuter,您可以直接对SQL Server执行T-SQL块(与ADO.NET不同) 。 SMO与您上面的方式略有不同......

var s = new Server(connectionString);
s.ConnectionContext.ExecuteNonQuery(@"CREATE DATABASE test; GO; Use test; ...");

#2


2  

Try to check if nothing else is locking same file without share-read access. Also enable bind error logging. Type fusion in win7 start menu and run it as admin so that settings are not grayed out. Then set 'log bind failures to disk' and specify location for the log file. If You don't get any logs check in registry that HKLM\Software\Microsoft\Fusion\ForceLog = 1

尝试检查没有其他任何东西锁定相同的文件没有共享读取访问权限。还启用绑定错误日志记录。在win7开始菜单中键入fusion并以管理员身份运行,以便设置不会变灰。然后设置“将日志绑定失败到磁盘”并指定日志文件的位置。如果您没有获得任何日志,请在注册表中检查HKLM \ Software \ Microsoft \ Fusion \ ForceLog = 1