首页 > 解决方案 > Azure - 从命令行在代理上运行测试项目

问题描述

我正在研究 Azure 管道,在 Windows 自托管代理上运行,在防火墙后面运行自动化测试。

去年 12 月,我在有关错误的 Dotnet 测试问题What .Net Core version to use on agent链接到我收到的错误消息:'<em>{"code":"UNABLE_TO_GET_ISSUER_CERT_LOCALLY"}'</p>

我仍然有这个错误,我相信它可能与代理的防火墙有关,但我试图从不同的角度看待这个问题。

我想在 Dotnet Core 3.1 项目上运行一个测试项目,在自托管代理上,我有 Dotnet SDK 5.0,我看到一条关于 3.1 接近不再受支持的消息,但我无法迁移应用程序,我有3.1 以下的其他人需要构建测试管道。

我在代理上安装了 .Net Core 3.1,但作为 .Net 2 开发人员,这对我来说是新的,我发现代理上有这些版本:

C:\>dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.100
 Commit:    5044b93829

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17763
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\...\sdk\5.0.100\

Host (useful for support):
  Version: 5.0.0
  Commit:  cf258a14b7

.NET SDKs installed:
  3.1.301 [C:\...\sdk]
  5.0.100 [C:\...\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.23 [C:\...\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.23 [C:\...\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.5 [C:\...\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.9 [C:\...\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0 [C:\...\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.23 [C:\...\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.5 [C:\...\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.9 [C:\...\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0 [C:\...\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.5 [C:\...\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.9 [C:\...\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0 [C:\...\Microsoft.WindowsDesktop.App]

我认为如果管道中的任务无法运行我的测试项目,我应该从代理服务器上的命令行尝试,但是我不知道是否应该设置要使用的版本?

我试过这个命令:

E:\test>dotnet test E:\Agent\_work\01\s\src\SolutionFolder\ProjectFolder\TestProject.csproj

要获得此输出:

C:\dotnet\sdk\5.0.100\NuGet.targets(131,5): error : Unable to load the service index for source https://ourFeed.pkgs.visualstudio.com/33706eec-...4c4/_packaging/UpstreamFeed/nuget/v3/index.json. 
C:\dotnet\sdk\5.0.100\NuGet.targets(131,5): error :   Response status code does not indicate success: 401 (Unauthorized). 

要连接到 Nuget,我们使用配置了 nuget 的提要,因此管道应该使用此提要连接到它,防火墙阻止直接链接到 Nuget。这适用于构建管道。

在解决方案的根目录下,我有一个 nuget.config 文件,我还将该文件复制到测试项目中作为测试:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="ArtifactRepo" value=" https://ourFeed.pkgs.visualstudio.com/33706eec-...4c4/_packaging/UpstreamFeed/nuget/v3/index.json " />
  </packageSources>
  <config>
        <add key="http_proxy" value="http://proxy.xxx:443" />
        <add key="https_proxy" value="https://proxy.xxx:443" />
  </config>
</configuration> 

我对吗?我应该首先能够从命令行运行它以使管道正常工作?我可以将其设置为使用 Dotnet Core 3.1 吗?

运行管道时,我也得到了这个:#[warning].NET 5 has some compatibility issues with older Nuget versions(<=5.7), so if you are using an older Nuget version(and not dotnet cli) to restore, then the dotnet cli commands (e.g. dotnet build) which rely on such restored packages might fail. To mitigate such error, you can either: (1) - Use dotnet cli to restore, (2) - Use Nuget version 5.8 to restore, (3) - Use global.json using an older sdk version(<=3) to build

这就是我想在代理上使用 Dotnet Core 3 的原因,我不确定我使用的是什么 Nuget 版本以及是否可以轻松更改。

是不是,当从命令行本地运行它时,我没有使用 PAT 令牌连接到提要?

如果您有任何想法可以帮助我,非常感谢。克劳德

----------- 更新 1 -----------

我检查了系统环境变量是否存在并且设置正确:HTTP_PROXY、HTTPS_PROXY 以及:内部调用的 NO_PROXY

在代理服务器上,我使用了您的脚本,但将版本更改为安装在我的开发人员机器上的版本:

dotnet new globaljson --sdk-version 3.1.301 --force

我运行了命令:“<code>dotnet test [path to csproj file]”并得到了一个我没有写下来的错误,它要么是'401(未经授权)'要么是'UNABLE_TO_GET_ISSUER_CERT_LOCALLY'</p>

我添加了上面添加的 nuget.config 文件。

我在浏览器中测试了提要的 URL 并被安全阻止,我将 URL 添加到受信任的站点,再次在浏览器中测试提要 URL,但出现相同的错误。

我再次测试了“<code>dotnet test [csproj 文件的路径]”,现在它工作了!!
我删除了 nuget.config 和 global.json 文件,它仍然有效!!!!
我不明白这一点,是受信任的站点还是曾经从文件中读取并将所需数据保存在某处的事实……也许您会理解这一点。

测试本身失败,但 dotnet test 命令行有效:

失败:29,通过:0,跳过:0,总计:29,持续时间:1 s - MyProject.Test.Core.dll (netcoreapp3.1)

现在,运行管道,在使用您的 powershell 脚本添加一个步骤后,将创建 global.json 文件,但我仍然有问题:

Results File: E:\Agent\_work\_temp\[***]_2021-02-05_11_53_38.trx
Failed!  - Failed:    29, Passed:     0, Skipped:     0, Total:    29, Duration: 1 s - [***].Core.dll (netcoreapp3.1)

项目中的 29 个错误可能与安全有关,但我应该在 Azure 中得到错误报告,对吗?

我可以看到在 E:\Agent_work_temp\ 中生成的 TRX 文件,但随后它们消失并出现更多错误,这可能是安全的吗?他们告诉我他们打开了那台服务器和 Azure 之间的通道……你知道为什么我没有得到测试报告和代码覆盖率报告吗?

仍然有关于 NET 5 存在一些兼容性问题的警告。以下是它开始失败的日志:

##[debug]Exit code 1 received from tool 'C:\Program Files\dotnet\dotnet.exe'
##[debug]Processed: ##vso[task.issue type=error;]Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1
##[debug]BuildConfiguration=undefined
##[debug]BuildPlatform=undefined
##[debug]testRunTitle=ProjectTests
##[debug]defaultRoot: 'E:\Agent\_work\_temp'
##[debug]findOptions.allowBrokenSymbolicLinks: 'false'
##[debug]findOptions.followSpecifiedSymbolicLink: 'true'
##[debug]findOptions.followSymbolicLinks: 'true'
##[debug]matchOptions.debug: 'false'
##[debug]matchOptions.nobrace: 'true'
##[debug]matchOptions.noglobstar: 'false'
##[debug]matchOptions.dot: 'true'
##[debug]matchOptions.noext: 'false'
##[debug]matchOptions.nocase: 'true'
##[debug]matchOptions.nonull: 'false'
##[debug]matchOptions.matchBase: 'false'
##[debug]matchOptions.nocomment: 'false'
##[debug]matchOptions.nonegate: 'false'
##[debug]matchOptions.flipNegate: 'false'
##[debug]pattern: '**/*.trx'
##[debug]findPath: 'E:\Agent\_work\_temp'
##[debug]statOnly: 'false'
##[debug]findPath: 'E:\Agent\_work\_temp'
##[debug]findOptions.allowBrokenSymbolicLinks: 'false'
##[debug]findOptions.followSpecifiedSymbolicLink: 'true'
##[debug]findOptions.followSymbolicLinks: 'true'
##[debug]  E:\Agent\_work\_temp (directory)
##[debug]  E:\Agent\_work\_temp\.taskkey (file)
##[debug]  E:\Agent\_work\_temp\1a672b68-2897-472e-8574-05442c3ded4d.ps1 (file)
##[debug]  E:\Agent\_work\_temp\[***]_2021-02-05_11_53_38.trx (file)
##[debug]  E:\Agent\_work\_temp\Deploy_[***] 2021-02-05 11_53_37 (directory)
##[debug]  E:\Agent\_work\_temp\Deploy_[***] 2021-02-05 11_53_37\In (directory)
##[debug]  E:\Agent\_work\_temp\Deploy_[***] 2021-02-05 11_53_37\In\[***] (directory)
##[debug]7 results
##[debug]found 7 paths
##[debug]applying include pattern
##[debug]adjustedPattern: 'E:\Agent\_work\_temp\**/*.trx'
##[debug]1 matches
##[debug]1 final results
##[debug]Failed to get FF TestManagement.Server.UsePublishTestResultsLibInAgent Value.
Unable to get the FF: TestManagement.Server.EnablePublishToTcmServiceDirectlyFromTask. Reason: One or more errors occurred. (Moved Temporarily)
##[debug]Failed to get FF TestManagement.PTR.CalculateTestRunSummary Value.
##[debug]Reading test results from file 'E:\Agent\_work\_temp\[***]_2021-02-05_11_53_38.trx'
##[debug]Setting run start and finish times.
##[debug]Attachment location: E:\Agent\_work\_temp\[***]_2021-02-05_11_53_38\In
##[debug]Total test results: 29
##[debug]Processed: ##vso[results.publish type=VSTest;mergeResults=false;runTitle=[***];publishRunAttachments=true;resultFiles=E:\Agent\_work\_temp\[***]_2021-02-05_11_53_38.trx;testRunSystem=VSTS - dotnet;]
##[warning].NET 5 has some compatibility issues with older Nuget versions(<=5.7), so if you are using an older Nuget version(and not dotnet cli) to restore, then the dotnet cli commands (e.g. dotnet build) which rely on such restored packages might fail. To mitigate such error, you can either: (1) - Use dotnet cli to restore, (2) - Use Nuget version 5.8 to restore, (3) - Use global.json using an older sdk version(<=3) to build
##[debug]Processed: ##vso[task.issue type=warning;].NET 5 has some compatibility issues with older Nuget versions(<=5.7), so if you are using an older Nuget version(and not dotnet cli) to restore, then the dotnet cli commands (e.g. dotnet build) which rely on such restored packages might fail. To mitigate such error, you can either: (1) - Use dotnet cli to restore, (2) - Use Nuget version 5.8 to restore, (3) - Use global.json using an older sdk version(<=3) to build
Info: Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core version which are currently lts. Unless you have locked down a SDK version for your project(s), 5.x SDK might be picked up which might have breaking behavior as compared to previous versions. You can learn more about the breaking changes here: https://docs.microsoft.com/en-us/dotnet/core/tools/ and https://docs.microsoft.com/en-us/dotnet/core/compatibility/ . To learn about more such changes and troubleshoot, refer here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#troubleshooting
##[debug]task result: Failed
##[error]Dotnet command failed with non-zero exit code on the following projects : E:\Agent\_work\31\s\src\[***]Core\[***].Core.csproj
##[debug]Processed: ##vso[task.issue type=error;]Dotnet command failed with non-zero exit code on the following projects : E:\Agent\_work\31\s\src\[***].Core\[***].Core.csproj
##[debug]Processed: ##vso[task.complete result=Failed;]Dotnet command failed with non-zero exit code on the following projects : E:\Agent\_work\31\s\src\[***].Core\[***].Core.csproj
##[section]Async Command Start: Publish test results
##[warning]Failed to publish test results: Moved Temporarily
##[section]Async Command End: Publish test results
##[section]Finishing: Tests

再次感谢您的帮助,克劳德

标签: .net-coreautomated-testsazure-pipelinesazure-devops-self-hosted-agent

解决方案


我检查了由于代理位于代理后面而Use dotnet task无法安装3.1 的其他线程。dotnet core如下文所述

请注意,许多工具不会自动使用代理配置的代理设置。例如,curl 和 dotnet 等工具可能需要在机器上也设置代理环境变量,例如 http_proxy

请尝试在您的自托管代理机器中添加两个名为HTTP_PROXY和的新环境变量。HTTPS_PROXY并设置这些变量的值指向您的代理。

如果上述解决方法没有奏效。请尝试以下解决方法,将您的代理设置为dotnet core 3.1专门使用

正如它在本文档中描述的那样。CLI 在项目目录中搜索 global.json 文件。并使用此文件中定义的 dotnet 版本。

您可以在管道中运行以下 dotnet 命令以在当前目录文件夹中创建“global.json”文件并设置要使用的 dotnet 版本。请参见下面的示例:

steps:
- powershell: |
   
   dotnet new globaljson --sdk-version 3.1.9 --force 
   
  displayName: 'PowerShell Script'

然后,您可以从命令行和 dotnet 任务运行测试。您将看到 dotnet 3.1 将用于运行您的测试。


推荐阅读