Issue when running tests as part of Team Services build process

72 views Asked by At

I have setup a build server at the company I work for. This build server interactively works with Visual Studio Team Services.

Building works great and so does publish. The issue I am running in to is the ability to run "dotnet test" as a different user.

This is needed because currently the user the agent runs under is a service account. It has access to IIS and has the ability to move files where they need to be. But it does not have access to the database.

Since we have a few integration tests that we utilize, it errors out when connecting to the database because it is trying to connect as the service user.

So far I have not found a way to run "dotnet test" as a different user, specifically one that has access to query the database.

I tried utilizing the VSTS Task "Run Powershell on Remote Machines" since it lets me supply a username and password. But it seems to have issues trying to remotely connect to itself (which is probably understandable).

I am at a loss. I have no idea how to get this to work. Except giving the service user the ability to run those queries on the database.

Any help is greatly appreciated!

2

There are 2 answers

2
starian chen-MSFT On BEST ANSWER

SQL authentication is the better way. So change connectionstring to use SQL authentication.

Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;

Authentication article: Choose an Authentication Mode

1
Luca Cappa On

You could start a process with the desired identity by passing appropriate credentials, e.g.

param($user, $pwd)
Start-Process $DOTNET_TEST_COMMAND -WorkingDirectory $DESIREDCURRENTWORKINGDIR -Credential (New-Object System.Management.Automation.PSCredential -ArgumentList @($user,(ConvertTo-SecureString -String $pwd -AsPlainText -Force)))

My opinion is that during a build only unit tests should be executed, as you could have side effects on the shared build machine if you execute more convoluted tests as functional tests. Instead than running the functional tests on the build machine, I would suggest to use the Run Functional Tests task during a Release of VSTS, as it would allow you to:

  • distribute the tests on a pool of machines where you installed the test agent by means of the Deploy Test Agent task);
  • provide the credentials of the identity the tests run for, this functionality is present out of the box in the task, i.e. solve your problem at the root;