11

我正在为 asp.net 应用程序设置管道。在集成测试任务期间,我需要连接到 SQL 服务器。我如何对管道说我需要 sql 服务?

我尝试了多个 microsoft 托管代理池(Windows Server 1803、Hosted 2017 和 2019)我使用 Windows Server 1803,问题是:

The operating system of the container does not match the operating system of the host.

我想正确设置一个临时 sql 服务器来运行测试。

我改用了localdb。

我在我的集​​成测试任务之前运行这个脚本

SqlLocalDB.exe create "DeptLocalDB"  
SqlLocalDB.exe share "DeptLocalDB" "DeptSharedLocalDB"  
SqlLocalDB.exe start "DeptLocalDB"  
SqlLocalDB.exe info "DeptLocalDB"

要与 powershell 连接:Invoke-Sqlcmd -Query "SELECT GETDATE() AS TimeOfQuery;" -ServerInstance "(localdb)\.\DeptSharedLocalDB"

要连接 sqlcmd:sqlcmd -S (localdb)\.\DeptSharedLocalDB

要连接 ac# 应用程序(connectionString):"Data Source=(localdb)\.\DeptS haredLocalDB;Initial Catalog=DeptLocalDB;Integrated Security=True;"

如果有人知道如何在 azure 管道上的容器中安装 sql 服务器,将不胜感激。感谢阅读

4

5 回答 5

8

Chocolatey 安装在windows-latest.

因此,如果您在 YAML 文件中定义:

  pool:
    vmImage: windows-latest

然后,您可以使用 choco 安装 SQL Server Express:

 - script: choco install sql-server-express
于 2019-09-19T11:25:16.743 回答
5

天蓝色管道.yml:

pool:
  vmImage: 'windows-latest'

...

- task: PowerShell@2
  displayName: 'start mssqllocaldb'
  inputs:
    targetType: 'inline'
    script: 'sqllocaldb start mssqllocaldb'

以下连接字符串有效后:

Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=my-db;Integrated Security=True;

资源:

https://www.jannikbuschke.de/blog/azure-devops-enable-mssqllocaldb/

于 2020-12-16T09:57:09.173 回答
2

这是我如何在 Azure Devops 中使用 Sql Express 运行集成测试的完整示例。我必须使用基于 YAML 的管道,因此我可以使用 simonauner 的方法使用 Chocolatey 来安装 Sql Express。请注意,我必须安装 EF Core 工具,因为我在此管道中使用 .Net Core 3.1。另请注意,我动态生成了一个 EF Code First 迁移 SQL 文件,以便操纵的 SQL Express 实例充满内容。

在 Azure Devops 中部署 SQL Express 实例,安装并生成和运行 EF Code first 迁移 sql 脚本,以使用 EF Code First 工具使用架构和种子数据更新数据库。

# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
- feature/testability

pool:
  vmImage: 'windows-latest'

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

steps:
- script: choco install sql-server-express
- task: NuGetToolInstaller@1

- task: VisualStudioTestPlatformInstaller@1
  displayName: 'Visual Studio Test Platform Installer'
  inputs:
    versionSelector: latestStable

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

- task: DotNetCoreCLI@2
  displayName: Build
  inputs:
    command: build
    projects: '**/*.csproj'
    arguments: '--configuration Debug' # Update this to match your need

- script: 'dotnet tool install --global dotnet-ef'
  displayName: 'Generate EF Code First Migrations SQL Script Update script'

- script: 'dotnet ef migrations script -i -o %BUILD_ARTIFACTSTAGINGDIRECTORY%\migrate.sql --project .\SomeAcme\SomeAcme.csproj'
  displayName: 'Generate EF Code First Migrations migrate.sql'

- script: 'sqlcmd -S .\SQLEXPRESS -Q "CREATE DATABASE [SomeAcmeDb]"'
  displayName: 'Create database SomeAcmeDb in Azure Devops SQL EXPRESS'

- script: 'sqlcmd -i %BUILD_ARTIFACTSTAGINGDIRECTORY%\migrate.sql -S .\SQLEXPRESS -d SomeAcmeDb'
  displayName: ' Run migrate.sql on SQL EXPRESS in Azure Devops'

# PowerShell
# Run a PowerShell script on Linux, macOS, or Windows
- task: PowerShell@2
  inputs:
    targetType: 'inline' # Optional. Options: filePath, inline
    #filePath: # Required when targetType == FilePath
    #arguments: # Optional
    script: 'gci -recurse -filter *.dll' # Required when targetType == Inline
    #errorActionPreference: 'stop' # Optional. Options: stop, continue, silentlyContinue
    #failOnStderr: false # Optional
    #ignoreLASTEXITCODE: false # Optional
    #pwsh: false # Optional
    #workingDirectory: # Optional

- task: VSTest@2
  displayName: 'VsTest - testAssemblies'
  inputs:
    testAssemblyVer2: |
     **\*SomeAcme.Tests.dll
     !**\*TestAdapter.dll
     !**\obj\**
    vsTestVersion: toolsInstaller
    testFiltercriteria: 'Category=IntegrationTest'
    runInParallel: false
    codeCoverageEnabled: false
    testRunTitle: 'XUnit tests SomeAcme solution integration test starting'
    failOnMinTestsNotRun: true
    rerunFailedTests: false
于 2020-04-04T23:48:28.917 回答
2

在我的情况下(一些需要 LocalDB 的 .NET Core 集成测试),只需使用windows-latest图像就可以了:

pool:
  vmImage: 'windows-latest'

该图像带有 Visual Studio,其中包括 LocalDB

于 2019-10-11T01:20:33.467 回答
1

我需要 localdb 才能运行数据库单元测试,而且我还至少需要 SQL Server 2016 SP2,因为我的团队正在使用一些较新的语言功能。

windows-2019映像上,只需使用 choco 或 powershell 执行以下命令:

choco upgrade sqllocaldb
于 2021-06-16T17:41:01.843 回答