Run sql server real database tests on Azure DevOps

Viewed 718

I have a toy app with a couple of tests that involve connecting to a real sql server database. The tests pass on my local machine. I want to get them to pass on Azure Devops. The entire purpose here is to get the tests of our real app running against a real database on ADO. I do not want to switch to an in-memory database.

The process I am envisioning is something like the following. But I am open to a different process if it can accomplish my purpose:

  1. Start a Docker container with sql server.
  2. Run my tests pointing at the database inside the container.
  3. Delete the container.

I have a hello-world yml script that starts up sql server. It is a shortened version of a script I found via How do I get MSSQL service container working in Azure DevOps pipeline? and published at https://developercommunity.visualstudio.com/content/problem/1159426/working-examples-using-service-container-of-sql-se.html. When I run it as a pipeline, it succeeds:

  containers:
  - container: mssql
    image: mcr.microsoft.com/mssql/server:2017-latest
    env:
      ACCEPT_EULA: Y
      SA_PASSWORD: Aaaaaa1!
      MSSQL_PID: Express
    ports: 
      - 1433:1433
    options: --name mssql


trigger:
- none


variables:
  system.debug: true


pool:
  vmImage: 'ubuntu-16.04'


jobs:
- job: unit_test_db_mssql
  services:
    mssql: mssql
  steps:
  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: |
        # Write your PowerShell commands here.
        
        start-sleep -s 10
  - task: CmdLine@2
    inputs:
      script: 'docker logs mssql'
  - task: CmdLine@2
    inputs:
      script: 'npm install mssql'
      workingDirectory: '$(build.sourcesdirectory)'
  - task: CmdLine@2
    inputs:
      script: 'sqlcmd -S localhost -d master -U sa -P Aaaaaa1! -Q "SELECT @@version;"'

I also have a script to run my sql server tests. When I run this script, the script succeeds, and the tests fail, which is expected, as there is no sql server database they can point at. Here is the script:

- main

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      SqlServerApp.Tests\bin\Release\netcoreapp3.1\SqlServerApp.Tests.dll
      searchFolder: '$(System.DefaultWorkingDirectory)'

The problem is combining the two. The first script uses vmImage 'ubuntu-16.04' and the second one uses 'windows-latest'. If I change the windows one to run on ubuntu-16.04, the VsBuild step fails:

##[error]The current operating system is not capable of running this task. That typically means the task was written for Windows only. For example, written for Windows Desktop PowerShell.

If I change the ubuntu one to use 'windows-latest', it also fails:

2020-11-27T14:50:26.1749476Z 2017-latest: Pulling from mssql/server
2020-11-27T14:50:26.2353797Z image operating system "linux" cannot be used on this platform
2020-11-27T14:50:26.7933543Z ##[error]Docker pull failed with exit code 1

How can I work around this problem?

1 Answers

You can't use VSBuild or VSTest task as there is no Visual Studio for Linux.

However I noticed that use use dotnet core. So please change your task to use dotnet command line tasks.

So you need to use

  • dotnet restore
  • dotnet build
  • dotnet test

and they you are ready to go on ubuntu host agents.

Related