Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
don't use default rg in test (#354)
Browse files Browse the repository at this point in the history
* WIP

* gather command params

* format

Co-authored-by: Qiaoqiao Zhang <55688292+qiaozha@users.noreply.github.com>
  • Loading branch information
changlong-liu and qiaozha authored May 11, 2020
1 parent 14c1a60 commit 72f359e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/plugins/azgenerator/CodeModelAz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class CommandExample {
public HttpMethod: string; // Get, Post, Put ...
public MethodResponses: any[];
public Method_IsLongRun: boolean;
public MethodParams: Map<string, MethodParam>;
}

export interface CodeModelAz
Expand Down Expand Up @@ -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;
Expand Down
35 changes: 29 additions & 6 deletions src/plugins/azgenerator/CodeModelAzImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1394,13 +1394,13 @@ export class CodeModelCliImpl implements CodeModelAz {
return method_param_dict;
}

public GetExampleParameters(example_obj): ExampleParam[] {
public GetExampleParameters(example_obj): [ExampleParam[], Map<string, MethodParam>] {
let parameters: ExampleParam[] = [];
let method_param_dict: Map<string, MethodParam> = 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 {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/azgenerator/TemplateAzureCliTestScenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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=[])


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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=[])


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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=[])


Expand Down

0 comments on commit 72f359e

Please sign in to comment.