From 72f359ed4bbde75c7adeb7f7d98568e5df4b2c89 Mon Sep 17 00:00:00 2001 From: changlong-liu <59815250+changlong-liu@users.noreply.github.com> Date: Mon, 11 May 2020 13:00:20 +0800 Subject: [PATCH] don't use default rg in test (#354) * WIP * gather command params * format Co-authored-by: Qiaoqiao Zhang <55688292+qiaozha@users.noreply.github.com> --- src/plugins/azgenerator/CodeModelAz.ts | 3 +- src/plugins/azgenerator/CodeModelAzImpl.ts | 35 +++++++++++++++---- .../TemplateAzureCliTestScenario.ts | 6 ++-- .../tests/latest/test_attestation_scenario.py | 3 +- .../tests/latest/test_datafactory_scenario.py | 3 +- .../latest/test_managed_network_scenario.py | 3 +- 6 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/plugins/azgenerator/CodeModelAz.ts b/src/plugins/azgenerator/CodeModelAz.ts index 0c5b3fe66..e8103f51d 100644 --- a/src/plugins/azgenerator/CodeModelAz.ts +++ b/src/plugins/azgenerator/CodeModelAz.ts @@ -50,6 +50,7 @@ export class CommandExample { public HttpMethod: string; // Get, Post, Put ... public MethodResponses: any[]; public Method_IsLongRun: boolean; + public MethodParams: Map; } export interface CodeModelAz @@ -161,7 +162,7 @@ export interface CodeModelAz SelectFirstExample(): boolean; SelectNextExample(): boolean; - FindExampleById(id: string): string[][]; + FindExampleById(id: string, commandParams: any): string[][]; Example_Body: string[]; Example_Title: string; Example_Params: any; diff --git a/src/plugins/azgenerator/CodeModelAzImpl.ts b/src/plugins/azgenerator/CodeModelAzImpl.ts index e8b9663a2..e4f38ec95 100644 --- a/src/plugins/azgenerator/CodeModelAzImpl.ts +++ b/src/plugins/azgenerator/CodeModelAzImpl.ts @@ -1394,13 +1394,13 @@ export class CodeModelCliImpl implements CodeModelAz { return method_param_dict; } - public GetExampleParameters(example_obj): ExampleParam[] { + public GetExampleParameters(example_obj): [ExampleParam[], Map] { let parameters: ExampleParam[] = []; let method_param_dict: Map = this.GetMethodParametersDict(); Object.entries(example_obj.parameters).forEach(([param_name, param_value]) => { this.FlattenExampleParameter(method_param_dict, parameters, param_name, param_value, []); }) - return parameters; + return [parameters, method_param_dict]; } private isDiscriminator(param: any): boolean { @@ -1690,8 +1690,9 @@ export class CodeModelCliImpl implements CodeModelAz { example.Path = this.Method_Path; example.HttpMethod = this.Method_HttpMethod; example.ResourceClassName = this.CommandGroup_Key; - let params: ExampleParam[] = this.GetExampleParameters(example_obj); + const [params, methodParams] = this.GetExampleParameters(example_obj); example.Parameters = this.ConvertToCliParameters(params); + example.MethodParams = methodParams; example.MethodResponses = this.Method.responses || []; example.Method_IsLongRun = this.Method.extensions?.['x-ms-long-running-operation'] ? true : false; if (this.filterExampleByPoly(example_obj, example)) { @@ -1709,10 +1710,11 @@ export class CodeModelCliImpl implements CodeModelAz { return examples; } - public GetExampleItems(example: CommandExample, isTest: boolean): string[] { + public GetExampleItems(example: CommandExample, isTest: boolean, commandParams: any): string[] { let parameters: string[] = []; parameters.push("az " + this.Command_Name); + let hasRG = false; for (let param of example.Parameters) { let param_value = param.value; if (isTest) { @@ -1727,6 +1729,14 @@ export class CodeModelCliImpl implements CodeModelAz { slp = ToJsonString(slp); } parameters.push(param.name + " " + slp); + + if (["--resource-group", "-g"].indexOf(param.name) >=0) { + hasRG = true; + } + } + + if (isTest && !hasRG && commandParams && commandParams[this.Command_Name] && commandParams[this.Command_Name].has("resourceGroupName")) { + parameters.push('-g ""'); } return parameters; @@ -1776,10 +1786,10 @@ export class CodeModelCliImpl implements CodeModelAz { } } - public FindExampleById(id: string): string[][] { + public FindExampleById(id: string, commandParams?: any): string[][] { let ret: string[][] = []; this.GetAllExamples(id, (example) => { - ret.push(this.GetExampleItems(example, true)); + ret.push(this.GetExampleItems(example, true, commandParams)); }); return ret; } @@ -1861,6 +1871,19 @@ export class CodeModelCliImpl implements CodeModelAz { this._testScenario = GenerateDefaultTestScenarioByDependency(this.GetAllExamples(), this.resource_pool, this._testScenario); this.SortExamplesByDependency(); } + + let commandParams = {}; + this.GetAllMethods(null, () => { + if (!commandParams[this.Command_Name]) commandParams[this.Command_Name] = new Set(); + if (this.SelectFirstMethodParameter()) { + do { + if ((this.MethodParameter.implementation == 'Method' || (this.MethodParameter as any).polyBaseParam) && !this.MethodParameter_IsFlattened && this.MethodParameter?.schema?.type != 'constant') { + commandParams[this.Command_Name].add(this.MethodParameter.language['cli'].cliKey); + } + } while (this.SelectNextMethodParameter()); + } + }); + return commandParams; } public SortExamplesByDependency() { diff --git a/src/plugins/azgenerator/TemplateAzureCliTestScenario.ts b/src/plugins/azgenerator/TemplateAzureCliTestScenario.ts index c07f2b429..485dc4479 100644 --- a/src/plugins/azgenerator/TemplateAzureCliTestScenario.ts +++ b/src/plugins/azgenerator/TemplateAzureCliTestScenario.ts @@ -17,7 +17,7 @@ export function GenerateAzureCliTestScenario(model: CodeModelAz): string[] { let body: string[] = []; let funcScenario: string[] = []; - model.GatherInternalResource(); + let commandParams = model.GatherInternalResource(); let config: any = deepCopy(model.Extension_TestScenario); config.unshift({ function: "setup" }); config.push({ function: "cleanup" }); @@ -46,7 +46,7 @@ export function GenerateAzureCliTestScenario(model: CodeModelAz): string[] { for (var ci = 0; ci < config.length; ci++) { let exampleId: string = config[ci].name; if (exampleId) { - model.FindExampleById(exampleId); + model.FindExampleById(exampleId, commandParams); } } @@ -84,7 +84,7 @@ export function GenerateAzureCliTestScenario(model: CodeModelAz): string[] { steps.push(...ToMultiLine(`def ${functionName}(test${parameterLine()}):`)); // find example by name let found = false; - for (let exampleCmd of model.FindExampleById(exampleId)) { + for (let exampleCmd of model.FindExampleById(exampleId, commandParams)) { if (exampleCmd && exampleCmd.length > 0) { found = true; for (let idx = 0; idx < exampleCmd.length; idx++) { diff --git a/src/test/scenarios/attestation/output/src/attestation/azext_attestation/tests/latest/test_attestation_scenario.py b/src/test/scenarios/attestation/output/src/attestation/azext_attestation/tests/latest/test_attestation_scenario.py index aa0736fab..937a2f6d4 100644 --- a/src/test/scenarios/attestation/output/src/attestation/azext_attestation/tests/latest/test_attestation_scenario.py +++ b/src/test/scenarios/attestation/output/src/attestation/azext_attestation/tests/latest/test_attestation_scenario.py @@ -55,7 +55,8 @@ def mytest(test, rg, rg_2, rg_3): # EXAMPLE: AttestationProviders_List @try_manual def step_attestationproviders_list(test, rg, rg_2, rg_3): - test.cmd('az attestation attestation-provider list-attestation', + test.cmd('az attestation attestation-provider list-attestation ' + '-g ""', checks=[]) diff --git a/src/test/scenarios/datafactory/output/src/datafactory/azext_datafactory/tests/latest/test_datafactory_scenario.py b/src/test/scenarios/datafactory/output/src/datafactory/azext_datafactory/tests/latest/test_datafactory_scenario.py index 0c7cf59ac..eb6857957 100644 --- a/src/test/scenarios/datafactory/output/src/datafactory/azext_datafactory/tests/latest/test_datafactory_scenario.py +++ b/src/test/scenarios/datafactory/output/src/datafactory/azext_datafactory/tests/latest/test_datafactory_scenario.py @@ -44,7 +44,8 @@ def step__factories_get_factories_get(test, rg): # EXAMPLE: /Factories/get/Factories_List @try_manual def step__factories_get_factories_list(test, rg): - test.cmd('az datafactory list', + test.cmd('az datafactory list ' + '-g ""', checks=[]) diff --git a/src/test/scenarios/managed-network/output/src/managed-network/azext_managed_network/tests/latest/test_managed_network_scenario.py b/src/test/scenarios/managed-network/output/src/managed-network/azext_managed_network/tests/latest/test_managed_network_scenario.py index b9422578b..9a61ad866 100644 --- a/src/test/scenarios/managed-network/output/src/managed-network/azext_managed_network/tests/latest/test_managed_network_scenario.py +++ b/src/test/scenarios/managed-network/output/src/managed-network/azext_managed_network/tests/latest/test_managed_network_scenario.py @@ -108,7 +108,8 @@ def step_managednetworkslistbyresourcegroup(test, rg): # EXAMPLE: ManagedNetworksListBySubscription @try_manual def step_managednetworkslistbysubscription(test, rg): - test.cmd('az managed-network mn list', + test.cmd('az managed-network mn list ' + '-g ""', checks=[])