From 9484399eda0215fad872fc0e5d9e514debca2929 Mon Sep 17 00:00:00 2001 From: scbedd <45376673+scbedd@users.noreply.github.com> Date: Fri, 19 May 2023 17:04:46 -0700 Subject: [PATCH] adding sample configs --- .../CLITests.cs | 59 ++++++++++++ .../ScanTests.cs | 2 +- .../sample-repo-configuration.json | 17 ++++ ...re.Sdk.Tools.Assets.MaintenanceTool.csproj | 4 + .../Model/RunConfiguration.cs | 8 +- .../Options/BaseOptions.cs | 46 ++++++++++ .../Program.cs | 92 ++++++++++++++----- .../assets-maintenance-tool/README.md | 10 ++ .../integration-test-repo-configuration.yml | 0 tools/assets-automation/tests.yml | 35 ++++++- 10 files changed, 246 insertions(+), 27 deletions(-) create mode 100644 tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool.Tests/CLITests.cs create mode 100644 tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool.Tests/TestResources/configurations/sample-repo-configuration.json create mode 100644 tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Options/BaseOptions.cs create mode 100644 tools/assets-automation/assets-maintenance-tool/integration-test-repo-configuration.yml diff --git a/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool.Tests/CLITests.cs b/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool.Tests/CLITests.cs new file mode 100644 index 00000000000..ad8e35cd420 --- /dev/null +++ b/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool.Tests/CLITests.cs @@ -0,0 +1,59 @@ +global using NUnit.Framework; +using Azure.Sdk.Tools.Assets.MaintenanceTool.Model; +using Azure.Sdk.Tools.Assets.MaintenanceTool.Scan; +using Microsoft.AspNetCore.Http; +using Newtonsoft.Json; + +namespace Azure.Sdk.Tools.Assets.MaintenanceTool.Tests +{ + public class CLITests + { + public string TestDirectory { get; protected set; } + + [SetUp] + public void Setup() + { + var workingDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + + if (!Directory.Exists(workingDirectory)) + { + Directory.CreateDirectory(workingDirectory); + } + + // copy our static test files there + var source = Path.Combine(Directory.GetCurrentDirectory(), "TestResources"); + var target = Path.Combine(workingDirectory, "TestResources"); + + Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(source, target); + + TestDirectory = workingDirectory; + } + + [TearDown] + public void TearDown() + { + Directory.Delete(TestDirectory, true); + } + + [Test] + [TestCase("scan", "-c", "")] + [TestCase("scan", "--config", "")] + public void TestScanOptions(params string[] args) + { + } + + [Test] + [TestCase("scan")] + [TestCase("scan", "--config")] + public void TestInvalidScanOptions(params string[] args) + { + var obj = new object(); + + var rootCommand = Program.InitializeCommandOptions((DefaultOptions) => + { + obj = DefaultOptions; + }); + + } + } +} diff --git a/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool.Tests/ScanTests.cs b/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool.Tests/ScanTests.cs index cc6e3078f56..4e9780df877 100644 --- a/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool.Tests/ScanTests.cs +++ b/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool.Tests/ScanTests.cs @@ -6,7 +6,7 @@ namespace Azure.Sdk.Tools.Assets.MaintenanceTool.Tests { - public class Tests + public class ScanTests { // These tests assume the presence of three integration branches for the purposes of testing. // Azure/azure-sdk-tools diff --git a/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool.Tests/TestResources/configurations/sample-repo-configuration.json b/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool.Tests/TestResources/configurations/sample-repo-configuration.json new file mode 100644 index 00000000000..48e43a6f333 --- /dev/null +++ b/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool.Tests/TestResources/configurations/sample-repo-configuration.json @@ -0,0 +1,17 @@ +{ + "Repos": [ + { + "Repo": "azure/azure-sdk-tools", + "Branches": [ + "integration/assets-test-branch" + ] + }, + { + "Repo": "azure/azure-sdk-assets-integration", + "Branches": [ + "integration/assets-branch-1", + "integration/assets-branch-2" + ] + } + ] +} \ No newline at end of file diff --git a/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Azure.Sdk.Tools.Assets.MaintenanceTool.csproj b/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Azure.Sdk.Tools.Assets.MaintenanceTool.csproj index f3dd4e5ea26..34948679dc5 100644 --- a/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Azure.Sdk.Tools.Assets.MaintenanceTool.csproj +++ b/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Azure.Sdk.Tools.Assets.MaintenanceTool.csproj @@ -7,6 +7,10 @@ enable + + + + diff --git a/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Model/RunConfiguration.cs b/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Model/RunConfiguration.cs index e1655fbd097..69594213581 100644 --- a/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Model/RunConfiguration.cs +++ b/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Model/RunConfiguration.cs @@ -7,7 +7,8 @@ namespace Azure.Sdk.Tools.Assets.MaintenanceTool.Model { /// - /// A given RunConfiguration contains multiple repo configurations. + /// A given RunConfiguration contains multiple repo configurations. This class provides such a container as well as + /// mappings to pick up configurations from an incoming path. /// public class RunConfiguration { @@ -15,6 +16,11 @@ public RunConfiguration() { Repos = new List(); } + public RunConfiguration(string configPath) + { + + } + public List Repos { get; set; } } } diff --git a/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Options/BaseOptions.cs b/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Options/BaseOptions.cs new file mode 100644 index 00000000000..91b4b3b0607 --- /dev/null +++ b/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Options/BaseOptions.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.CommandLine; +using System.CommandLine.Binding; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Azure.Sdk.Tools.TestProxy.CommandOptions; + +namespace Azure.Sdk.Tools.Assets.MaintenanceTool.Options +{ + public class BaseOptions + { + public string ConfigLocation { get; set; } = string.Empty; + } + + public class BaseOptionsBinder : BinderBase + { + private readonly Option _configLocationOption; + + public BaseOptionsBinder(Option configLocationOption) + { + _configLocationOption = configLocationOption; + } + + protected override BaseOptions GetBoundValue(BindingContext bindingContext) + { + var result = bindingContext.ParseResult.GetValueForOption(_configLocationOption); + + if (result != null) + { + return new BaseOptions + { + ConfigLocation = result + }; + } + else + { + return new BaseOptions + { + ConfigLocation = string.Empty + }; + } + } + } +} diff --git a/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Program.cs b/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Program.cs index 1f9a0ebe9bc..e924daf9e5b 100644 --- a/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Program.cs +++ b/tools/assets-automation/assets-maintenance-tool/Azure.Sdk.Tools.Assets.MaintenanceTool/Program.cs @@ -1,39 +1,83 @@ +using System.CommandLine; using Azure.Sdk.Tools.Assets.MaintenanceTool.Model; +using Azure.Sdk.Tools.Assets.MaintenanceTool.Options; +using Azure.Sdk.Tools.Assets.MaintenanceTool.Scan; namespace Azure.Sdk.Tools.Assets.MaintenanceTool { - internal class Program + public class Program { - public static void Main(string[] args) - { - // working directory is where we will look for scan_results.json + public static string[] StoredArgs = new string[] { }; - // all should honor - // --ScanData <-- results from previous scans + // working directory is where we will look for previous results "output.json" + + // all should honor + // --ScanData <-- results from previous scans (todo, currently checks working directory) - // SCAN - // --configuration: -> path to file + // SCAN + // --configuration: -> path to file + + // BACKUP + // --configuration provided? + // SCAN + // BACKUP + // as each tag is backed up, it is saved with suffix _backup - // BACKUP - // --configuration provided? - // SCAN - // BACKUP - // as each tag is backed up, it is saved with suffix _backup + // RESTORE + // --input-tag - // RESTORE - // --input-tag + // CLEANUP + // --configuration provided? + // SCAN + // BACKUP + // CLEANUP + // each tag as found by configuration - // CLEANUP - // --configuration provided? - // SCAN - // BACKUP - // CLEANUP - // each tag as found by configuration + // --input-tag ? + // SCAN, BACKUP, and CLEANUP individual tag + public static void Main(string[] args) + { + StoredArgs = args; + + var rootCommand = InitializeCommandOptions(Run); + var resultCode = rootCommand.Invoke(args); + Environment.Exit(resultCode); + } + + public static void Run(object commandObj) + { + switch (commandObj) + { + case BaseOptions configOptions: + AssetsScanner scanner = new AssetsScanner(); + var runConfig = new RunConfiguration(configOptions.ConfigLocation); + scanner.Scan(runConfig); + break; + default: + throw new ArgumentException($"Unable to parse the argument set: {string.Join(" ", StoredArgs)}"); + } + } + + public static RootCommand InitializeCommandOptions(Action action) + { + var root = new RootCommand(); + var configOption = new Option( + name: "--config", + description: "The path to the json file containing the repo configuration. A sample repo configuration can be seen under /integration-test-repo-configuration.yml." + ) { + IsRequired = true + }; + configOption.AddAlias("-c"); - // --input-tag ? - // SCAN, BACKUP, and CLEANUP individual tag + var scanCommand = new Command("scan", "Scan the repositories as configured within the file provided to input argument --config ."); + scanCommand.AddOption(configOption); + scanCommand.SetHandler( + (configOpts) => action(configOpts), + new BaseOptionsBinder(configOption) + ); + root.Add(scanCommand); - Console.WriteLine("Hello, World!"); + return root; } } } diff --git a/tools/assets-automation/assets-maintenance-tool/README.md b/tools/assets-automation/assets-maintenance-tool/README.md index 0bec10ce95b..0d9865d0b1a 100644 --- a/tools/assets-automation/assets-maintenance-tool/README.md +++ b/tools/assets-automation/assets-maintenance-tool/README.md @@ -7,6 +7,16 @@ The tool contained within this directory is intended two fulfill three main task 2. Use the map of data created in step 1 to individually backup tags from the assets repository to a target that we can retrieve later. 3. Use the map of data created in step 1 to clean up _unnecessary_ tags in the assets repository. +## Usage + +### Installation + +`` + +### Scanning + +`` + ## What does the process look like? ![example processing layout](processing_layout.png) \ No newline at end of file diff --git a/tools/assets-automation/assets-maintenance-tool/integration-test-repo-configuration.yml b/tools/assets-automation/assets-maintenance-tool/integration-test-repo-configuration.yml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tools/assets-automation/tests.yml b/tools/assets-automation/tests.yml index d279b426f4a..7bece9c6dba 100644 --- a/tools/assets-automation/tests.yml +++ b/tools/assets-automation/tests.yml @@ -8,6 +8,7 @@ stages: displayName: "Integration Tests" jobs: - job: Solution_Integration_Test + name: Run Solution Tests strategy: matrix: @@ -38,6 +39,38 @@ stages: condition: succeededOrFailed() inputs: testResultsFiles: '**/*.trx' - testRunTitle: '$(OS) AssetSync tests against .NET' + testRunTitle: '$(OS) Maintenance tool tests against .NET' + testResultsFormat: 'VSTest' + mergeTestResults: true + + - job: CLI_Integration_Test + name: Invoke CLI Tool + strategy: + matrix: + Linux: + Pool: azsdk-pool-mms-ubuntu-2204-general + OS: 'Linux' + + pool: + name: $(Pool) + + steps: + - template: /eng/pipelines/templates/steps/install-dotnet.yml + + - script: 'dotnet test /p:ArtifactsPackagesDir=$(Build.ArtifactStagingDirectory) --logger trx $(Build.SourcesDirectory)/tools/assets-automation/assets-maintenance-tool/' + displayName: 'Test' + env: + DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1 + DOTNET_CLI_TELEMETRY_OPTOUT: 1 + DOTNET_MULTILEVEL_LOOKUP: 0 + GIT_TOKEN: $(azuresdk-github-pat) + GIT_COMMIT_OWNER: azure-sdk + GIT_COMMIT_EMAIL: azuresdk@microsoft.com + + - task: PublishTestResults@2 + condition: succeededOrFailed() + inputs: + testResultsFiles: '**/*.trx' + testRunTitle: '$(OS) Maintenance tool tests against .NET' testResultsFormat: 'VSTest' mergeTestResults: true