【问题标题】:Run sql server real database tests on Azure DevOps在 Azure DevOps 上运行 sql server 真实数据库测试
【发布时间】:2021-03-10 08:37:55
【问题描述】:

我有一个玩具应用程序,其中包含一些涉及连接到真实 sql server 数据库的测试。测试在我的本地机器上通过。我想让他们传递 Azure Devops。这里的全部目的是让我们的真实应用程序的测试针对 ADO 上的真实数据库运行。我不想切换到内存数据库。

我设想的过程类似于以下内容。但如果它可以实现我的目的,我愿意接受不同的过程:

  1. 使用 sql server 启动 Docker 容器。
  2. 针对容器内的数据库运行我的测试。
  3. 删除容器。

我有一个启动 sql server 的 hello-world yml 脚本。这是我通过How do I get MSSQL service container working in Azure DevOps pipeline? 找到并发布在https://developercommunity.visualstudio.com/content/problem/1159426/working-examples-using-service-container-of-sql-se.html 的脚本的缩短版本。当我将它作为管道运行时,它会成功:

  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;"'

我还有一个脚本来运行我的 sql server 测试。当我运行这个脚本时,脚本成功了,测试失败了,这是意料之中的,因为没有他们可以指向的 sql server 数据库。这是脚本:

- 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)'

问题在于将两者结合起来。第一个脚本使用 vmImage 'ubuntu-16.04',第二个脚本使用 'windows-latest'。如果我将 Windows 更改为在 ubuntu-16.04 上运行,则 VsBuild 步骤将失败:

##[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.

如果我将 ubuntu 更改为使用“windows-latest”,它也会失败:

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

我该如何解决这个问题?

【问题讨论】:

  • 你知道linux上没有Visual Studio所以你不能在linux主机代理上运行VSBuild
  • 您的解决方案数据库中有项目吗?
  • 为了完整起见,仅包括它。这可能意味着我需要让 sql server 在 Windows 上运行,或者在其中的 linux 上运行。并且C#解决方案文件包括一个通过EntityFrameworkCore连接到sql server的项目。
  • 如果你在 dotnet core 中开发,你可以摆脱 VSbuild 和 VSTest 并使用 dotnet 命令行。请查看我的回复。

标签: sql-server windows azure-devops ubuntu-16.04


【解决方案1】:

您不能使用 VSBuild 或 VSTest 任务,因为没有 Visual Studio for Linux。

但是我注意到使用 dotnet core。因此,请将您的任务更改为使用dotnet command line tasks

所以你需要使用

  • dotnet 恢复
  • dotnet 构建
  • dotnet 测试

你已经准备好继续ubuntu主机代理。

【讨论】:

    猜你喜欢
    • 2022-08-16
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多