Skip to content

Commit

Permalink
Trigger Az.Predictor CI (#18067)
Browse files Browse the repository at this point in the history
* Trigger Az.Predictor CI

* Fix the issue when running the unit tests (#18300)

* Fix the issue in unit tests.

- In the module we create a minimum runspace for PowerShell, and use it to parse the command line.
  The mini runspace contains the built-in core modules for PowerShell. We don't need them when we
  package and publish our module because the runtime will have them. That'll reduce the module size.
  So in the module project we exclude `contentfiles` when we referece Microsoft.PowerShell.SDK.
- The issue in the unit tests is that it doesn't find the built-in core module when we create the runspace.
  So we have a reference to Microsoft.PowerShell.SDK in the test project to include `contentfiles` to provide
  the built-in core modules. The unit tests can run successfully.

* Test

* Update the data in the test cases (#18496)

* Fix the unit tests.

- Parsing the command parameter requires the module to be loaded. On the machine (e.g. build machine) where the Az modules are not installed, the
  parsing doesn't return the parameters names as expected. Some test cases thus fail. The fix is to remove Az cmdlets from the test cases. Instead, we
  use the cmdlet from the built-in modules in the test cases.

* Replace the way of getting the data.

- We used to zip the whole model and read from it. Since we don't need to use Az cmdlet in the test cases, we don't really need to zip the model.
  So we use the json file with crafted built-in PowerShell cmdlet as the data used in the test cases. With that, it's also clear to show the changes
  to the test cases and the data.

Add Dotnet Core 6 in pipeline.

Add logic for global.json

Add logic for global.json

* Move AzPredictor from msbuild to task

* Revert empty lines

Co-authored-by: kceiw <mahuang@microsoft.com>
  • Loading branch information
wyunchi-ms and kceiw authored Jun 23, 2022
1 parent 5d9e687 commit 13312f8
Show file tree
Hide file tree
Showing 19 changed files with 322 additions and 168 deletions.
1 change: 1 addition & 0 deletions .azure-pipelines/powershell-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ variables:
BuildTimeoutInMinutes: 120
AnalysisTimeoutInMinutes: 120
TestTimeoutInMinutes: 180
BuildAzPredictor: false

trigger: none

Expand Down
23 changes: 22 additions & 1 deletion .azure-pipelines/util/build-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,39 @@ steps:
filePath: tools/CheckIgnoredFile.ps1

- task: UseDotNet@2
displayName: 'Use .NET Core sdk'
displayName: 'Use .NET Core sdk 3.1.x'
inputs:
packageType: sdk
version: 3.1.x

- task: UseDotNet@2
displayName: 'Use .NET Core sdk 6.0.x'
inputs:
packageType: sdk
version: 6.0.x

- task: PowerShell@2
displayName: Setup global.json
inputs:
targetType: inline
script: "$SdkVersion=(dotnet --list-sdks | Select-String '[3,4,5].\\d.\\d{3}').Matches[0].Value; dotnet new globaljson --sdk-version $SdkVersion --force"
pwsh: true

- task: DotNetCoreCLI@2
displayName: Build
inputs:
command: custom
custom: msbuild
arguments: 'build.proj /t:Build /p:Configuration=${{ parameters.configuration }};TestFramework=${{ parameters.testFramework }};PullRequestNumber=$(System.PullRequest.PullRequestNumber)'

- task: PowerShell@2
displayName: Build-AzPredictor
condition: eq(variables.BuildAzPredictor, 'true')
inputs:
targetType: inline
script: "$SdkVersion=(dotnet --list-sdks | Select-String '6.0.\\d{3}').Matches[0].Value; dotnet new globaljson --sdk-version $SdkVersion --force;dotnet msbuild tools/Az.Tools.Predictor/build.proj /t:\"clean;build;test\""
pwsh: true

- template: publish-artifacts-steps.yml
parameters:
artifactName: build-${{ parameters.testFramework }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ SecurityTmp/
tmp/
FilesChanged.txt
CsprojMappings.json
global.json

obj
bin
Expand Down
8 changes: 2 additions & 6 deletions build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@
<Exec Command="dotnet new sln -n Azure.PowerShell -o $(RepoArtifacts) --force" />
<CallTarget Targets="BuildNormalModules" />

<CallTarget Targets="AzToolsPredictor" Condition="$(SubTasks.Contains('predictor'))" />
<CallTarget Targets="AzToolsInstaller" Condition="$(SubTasks.Contains('installer'))" />
<Exec Command="echo ##vso[task.setvariable variable=BuildAzPredictor;]true" Condition="$(SubTasks.Contains('Predictor')) OR $(SubTasks.Contains('all'))" />
<CallTarget Targets="AzToolsInstaller" Condition="$(SubTasks.Contains('Installer')) OR $(SubTasks.Contains('all'))" />
</Target>

<Target Name="BuildNormalModules">
Expand Down Expand Up @@ -290,10 +290,6 @@
<Exec Command='dotnet msbuild $(RepoTools)/Az.Tools.Installer/build.proj /t:"clean;build;test"' />
</Target>

<Target Name="AzToolsPredictor">
<Exec Command='dotnet msbuild $(RepoTools)/Az.Tools.Predictor/build.proj /t:"clean;build;test"' />
</Target>

<Target Name="ChangeLogErrorMessage">
<Error Text="Modified files were found with no update to their change log. Please add a snippet to the affected modules' change log." />
</Target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<ProjectReference Include="..\Az.Tools.Predictor\Az.Tools.Predictor.csproj" />
<!-- The test project doesn't reference to it directly. Az.Tools.Predictor has a reference to Microsoft.PowerShell.SDK.
- But it's excluding content files to reduce the size of the module. The content files are not needed since they're in the runtime (PowerShell)
- The content files are needed to set up the runspace to parse the command line. So we need to include them when we run the unit tests.
-->
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.0" />
</ItemGroup>

<ItemGroup>
<None Include="Data\CommandsModel.zip" CopyToOutputDirectory="PreserveNewest" />
<None Include="Data\PredictionsModel.zip" CopyToOutputDirectory="PreserveNewest" />
<None Include="Data\CommandsModel.json" CopyToOutputDirectory="PreserveNewest" />
<None Include="Data\PredictionsModel.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void Dispose()
[Fact]
public void VerifyParameterValues()
{
var predictionContext = PredictionContext.Create("Get-AzContext");
var predictionContext = PredictionContext.Create("Set-Content");

Action actual = () => this._service.GetSuggestion(null, 1, 1, CancellationToken.None);
Assert.Throws<ArgumentNullException>(actual);
Expand All @@ -116,14 +116,14 @@ public void VerifyParameterValues()
/// Verifies that the prediction comes from the command based list, not the fallback list.
/// </summary>
[Theory]
[InlineData("CONNECT-AZACCOUNT")]
[InlineData("set-azstorageaccount ")]
[InlineData("Get-AzResourceG")]
[InlineData("Get-AzStorageAcco")]
[InlineData("Get-AzKeyVault -VaultName")]
[InlineData("GET-AZSTORAGEACCOUNTKEY -NAME ")]
[InlineData("new-azresourcegroup -name hello")]
[InlineData("new-azresourcegroup hello")]
[InlineData("SET-CONTENT")]
[InlineData("get-logproperties ")]
[InlineData("Clear-C")]
[InlineData("compare-")]
[InlineData("Clear-Variable -Name")]
[InlineData("CLEAR-CONTENT -PATH test")]
[InlineData("Clear-content -path test")]
[InlineData("clear-content ./Test.log")]
public void VerifyUsingCommandBasedPredictor(string userInput)
{
var predictionContext = PredictionContext.Create(userInput);
Expand Down Expand Up @@ -165,8 +165,8 @@ public void VerifyUsingCommandBasedPredictor(string userInput)
/// Verifies that when no prediction is in the command based list, we'll use the fallback list.
/// </summary>
[Theory]
[InlineData("New-AzApiManagementContext -ResourceGroupName hello -Serv")]
[InlineData("Get-AzAlert -TimeRange '1h' -Incl")]
[InlineData("Set-Variable -Name desc -Value ")]
[InlineData("remove-ite")]
public void VerifyUsingFallbackPredictor(string userInput)
{
var predictionContext = PredictionContext.Create(userInput);
Expand Down Expand Up @@ -209,12 +209,13 @@ public void VerifyUsingFallbackPredictor(string userInput)
/// </summary>
[Theory]
[InlineData(AzPredictorConstants.CommandPlaceholder)]
[InlineData("Get-Help")]
[InlineData("Get-ChildItem")]
[InlineData("new-azresourcegroup -NoExistingParam")]
[InlineData("get-azaccount ")]
[InlineData("NEW-AZCONTEXT")]
[InlineData("Remove-Item -NoExistingParam")]
[InlineData("get-childitem ")]
[InlineData("NEW-CHILDITEM ")]
[InlineData("git status")]
[InlineData("Get-AzContext Name")]
[InlineData("get-item name")]
public void VerifyNoPrediction(string userInput)
{
var predictionContext = PredictionContext.Create(userInput);
Expand All @@ -235,7 +236,7 @@ public void VerifyNoPrediction(string userInput)
/// Verify that it returns null when we cannot parse the user input.
/// </summary>
[Theory]
[InlineData("New-AzVM -Name A $Location")]
[InlineData("Remove-Item -Name A $Location")]
public void VerifyFailToParseUserInput(string userInput)
{
var predictionContext = PredictionContext.Create(userInput);
Expand Down
Loading

0 comments on commit 13312f8

Please sign in to comment.