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

Commit

Permalink
20210426 swagger scenario (#838)
Browse files Browse the repository at this point in the history
* use oav 2.3.2

* use oav 2.3.2

* handle wrong test scenario

* remove old scenario

* gen rest call

* fix rest call body

* test output

* clear code

* rename swagger test filename

* clean code

* output

* scenario steps file

* use swagger_steps

* fix spell issue

* configurable use-swagger-scenario and location

* spell fix
  • Loading branch information
changlong-liu authored Jun 10, 2021
1 parent e57ffff commit a573427
Show file tree
Hide file tree
Showing 31 changed files with 1,147 additions and 507 deletions.
15 changes: 15 additions & 0 deletions doc/04-scenario-test-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,18 @@ or configure in readme.az.md:
gen-cmdlet-test: true
~~~

## How to customize test location
The default test location is 'westus', you can change it with below configuration:
~~~
az:
...
test-location: eastus
~~~

## How to disable swagger test scenario
The swagger test scenario can be disabled with below configuration:
~~~
az:
...
use-swagger-test-scenario: false
~~~
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@
"mkdirp": "1.0.4",
"node-yaml": "^3.2.0",
"nunjucks": "^3.2.2",
"oav": "2.2.0",
"oav": "2.3.2",
"prettier": "2.2.1",
"reflect-metadata": "^0.1.13",
"request": "^2.87.0",
"request-promise-native": "1.0.8",
"typescript": "^3.7.5"
Expand Down
1 change: 1 addition & 0 deletions readme.az.common.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cli:
cli-flatten-all-overwrite-swagger: false

az:
use-swagger-test-scenario: true
preparers:
virtualNetworks:
abbr: vn
Expand Down
2 changes: 1 addition & 1 deletion src/generate/codemodel/CodeModelAz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*-------------------------------------------------------------------------------------------- */

import { Operation, OperationGroup, Parameter } from '@azure-tools/codemodel';
import { Operation, OperationGroup, Parameter, CodeModel } from '@azure-tools/codemodel';
import { CodeModelTypes, DataGraph, RenderInput } from '../../utils/models';
import { ExtensionModel } from './Extension';
import { CommandGroupModel } from './CommandGroup';
Expand Down
76 changes: 37 additions & 39 deletions src/generate/codemodel/Example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import {
ObjectStatus,
GroupTestScenario,
LoadPreparesConfig,
matchExample,
} from '../renders/tests/ScenarioTool';
import { TestStepExampleFileRestCall } from 'oav/dist/lib/testScenario/testResourceTypes';
import { TestStepRestCall } from 'oav/dist/lib/testScenario/testResourceTypes';
import { CodeModelCliImpl } from './CodeModelAzImpl';
import { CommandModel } from './Command';
import { CommandGroupModel } from './CommandGroup';
Expand All @@ -31,7 +32,7 @@ import { AzConfiguration, CodeGenConstants } from '../../utils/models';
import { readFile } from '@azure-tools/async-io';
import * as process from 'process';
import * as path from 'path';
import { Property } from '@azure-tools/codemodel';
import { Operation, Property } from '@azure-tools/codemodel';

export class MethodParam {
public value: any;
Expand Down Expand Up @@ -107,6 +108,7 @@ export class CommandExample {
public ExampleObj: any;
public commandStringItems: string[];
public CommandString: string;
public MethodObj: Operation;
}

export interface ExampleModel {
Expand All @@ -115,7 +117,7 @@ export interface ExampleModel {
GetSubscriptionKey(): string;
GetPreparerEntities(): any[];
GatherInternalResource();
FindExampleWaitById(id: string, step?: TestStepExampleFileRestCall): string[][];
FindExampleWaitById(id: string, step?: TestStepRestCall): string[][];
GetExampleItems(example: CommandExample, isTest: boolean, commandParams: any): string[];
GetExampleChecks(example: CommandExample): string[];

Expand All @@ -124,10 +126,9 @@ export interface ExampleModel {
FindExampleById(
id: string,
commandParams: any,
examples: any[],
minimum: boolean,
step?: TestStepExampleFileRestCall,
): string[][];
step?: TestStepRestCall,
): [string[], CommandExample];
GenerateTestInit(): void;
Example_TestScenario: any;
Example_DefaultTestScenario: any;
Expand Down Expand Up @@ -231,13 +232,7 @@ export class ExampleModelImpl implements ExampleModel {
}

public GenerateTestInit(): void {
if (this.GetResourcePool().hasTestResourceScenario) {
this._testScenario = GroupTestScenario(
this.GetResourcePool().generateTestScenario(),
this.extensionHandler.Extension_NameUnderscored,
);
this._configuredScenario = true;
} else if (this.baseHandler.codeModel['test-scenario']) {
if (this.baseHandler.codeModel['test-scenario']) {
this._testScenario = GroupTestScenario(
this.baseHandler.codeModel['test-scenario'],
this.extensionHandler.Extension_NameUnderscored,
Expand All @@ -248,7 +243,18 @@ export class ExampleModelImpl implements ExampleModel {
this._configuredScenario = false;
}
this.GatherInternalResource();
this.GetAllExamples();
const allExamples = this.GetAllExamples();
if (this.GetResourcePool().hasTestResourceScenario) {
this._testScenario = GroupTestScenario(
this.GetResourcePool().generateTestScenario(
allExamples,
this.baseHandler.codeModel,
),
this.extensionHandler.Extension_NameUnderscored,
true,
);
// this._configuredScenario = true;
}
}
/**
* Gets method parameters dict
Expand Down Expand Up @@ -957,6 +963,7 @@ export class ExampleModelImpl implements ExampleModel {
'x-ms-long-running-operation'
];
example.ExampleObj = exampleObj;
example.MethodObj = this.methodHandler.Method;
if (this.methodHandler.Method_GetSplitOriginalOperation) {
// filter example by name for generic createorupdate
if (
Expand Down Expand Up @@ -988,7 +995,7 @@ export class ExampleModelImpl implements ExampleModel {

public GetExampleChecks(example: CommandExample): string[] {
const ret: string[] = [];
if (!this.configHandler.GenChecks) return ret;
if (!this.configHandler.GenChecks || isNullOrUndefined(example)) return ret;
let resourceObjectName = undefined;
for (const param of example.Parameters) {
if (
Expand Down Expand Up @@ -1202,34 +1209,31 @@ export class ExampleModelImpl implements ExampleModel {
public FindExampleById(
id: string,
commandParams: any,
examples: CommandExample[],
minimum = false,
step: TestStepExampleFileRestCall = undefined,
): string[][] {
const ret: string[][] = [];
step: TestStepRestCall = undefined,
): [string[], CommandExample] {
let commandExample: CommandExample = undefined;
let commandString: string[] = [];
this.GetAllExamples(
id,
(example) => {
examples.push(example);
ret.push(this.GetExampleItems(example, true, commandParams, minimum));
commandExample = example;
commandString = this.GetExampleItems(example, true, commandParams, minimum);
},
step?.exampleTemplate,
this.resourcePool.genExampleTemplate(step),
);
return ret;
return [commandString, commandExample];
}

public FindExampleWaitById(
id: string,
step: TestStepExampleFileRestCall = undefined,
): string[][] {
public FindExampleWaitById(id: string, step: TestStepRestCall = undefined): string[][] {
const ret: string[][] = [];
this.GetAllExamples(
id,
(example) => {
const waitCmd = this.GetExampleWait(example);
if (waitCmd.length > 0) ret.push(waitCmd);
},
step?.exampleTemplate,
this.resourcePool.genExampleTemplate(step),
);
return ret;
}
Expand Down Expand Up @@ -1351,7 +1355,9 @@ export class ExampleModelImpl implements ExampleModel {
this._defaultTestScenario,
);
this.SortExamplesByDependency();
PrintTestScenario(this._defaultTestScenario);
if (!this.GetResourcePool().hasTestResourceScenario) {
PrintTestScenario(this._defaultTestScenario);
}
}

if (!this._configuredScenario && isNullOrUndefined(this._testScenario)) {
Expand Down Expand Up @@ -1440,7 +1446,7 @@ export class ExampleModelImpl implements ExampleModel {
const commandExamples = this.GetAllExamples();
for (let i = 0; i < this._defaultTestScenario.length; i++) {
for (const commandExample of commandExamples) {
if (this.matchExample(commandExample, this._defaultTestScenario[i].name)) {
if (matchExample(commandExample, this._defaultTestScenario[i].name)) {
scenarioExamples.set(this._defaultTestScenario[i].name, commandExample);
break;
}
Expand All @@ -1455,14 +1461,6 @@ export class ExampleModelImpl implements ExampleModel {
});
}

private matchExample(example: CommandExample, id: string) {
if (!id) return false;
return (
example.Id.toLowerCase() === id.toLowerCase() ||
example.Id.toLowerCase().endsWith(`/${id.toLowerCase()}`)
);
}

public GetAllExamples(
id?: string,
callback?: (example) => void,
Expand All @@ -1481,7 +1479,7 @@ export class ExampleModelImpl implements ExampleModel {
examples = this.GetExamples(true);
}
for (const example of examples) {
if (id && !this.matchExample(example, id)) continue;
if (id && !matchExample(example, id)) continue;
if (callback) {
callback(example);
}
Expand Down
6 changes: 5 additions & 1 deletion src/generate/generators/CoreFull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,17 @@ export class AzCoreFullGenerator extends GeneratorBase {

await this.generateFullSingleAndAddtoOutput(new CliTestInit(model));
await this.generateFullSingleAndAddtoOutput(new CliTestStep(model), true, true);
const hasTestResourceScenario = model.GetHandler().exampleHandler.GetResourcePool()
.hasTestResourceScenario;
for (const testGroup of exampleHandler.Example_TestScenario
? Object.getOwnPropertyNames(exampleHandler.Example_TestScenario)
: []) {
await this.generateFullSingleAndAddtoOutput(
new CliTestScenario(
model,
PathConstants.fullTestSceanrioFile(testGroup),
hasTestResourceScenario
? PathConstants.testSwaggerScenarioFile(testGroup)
: PathConstants.fullTestSceanrioFile(testGroup),
exampleHandler.Example_TestScenario[testGroup],
testGroup,
),
Expand Down
6 changes: 5 additions & 1 deletion src/generate/generators/CoreIncre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,17 @@ export class AzCoreIncrementalGenerator extends GeneratorBase {

await this.generateIncrementalSingleAndAddtoOutput(new CliTestInit(this.model));
await this.generateIncrementalSingleAndAddtoOutput(new CliTestStep(this.model), true);
const hasTestResourceScenario = this.model.GetHandler().exampleHandler.GetResourcePool()
.hasTestResourceScenario;
for (const testGroup of exampleHandler.Example_TestScenario
? Object.getOwnPropertyNames(exampleHandler.Example_TestScenario)
: []) {
await this.generateIncrementalSingleAndAddtoOutput(
new CliTestScenario(
this.model,
PathConstants.incTestScenarioFile(testGroup),
hasTestResourceScenario
? PathConstants.testSwaggerScenarioFile(testGroup)
: PathConstants.incTestScenarioFile(testGroup),
exampleHandler.Example_TestScenario[testGroup],
testGroup,
),
Expand Down
6 changes: 5 additions & 1 deletion src/generate/generators/ExtensionFull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,17 @@ export class AzExtensionFullGenerator extends GeneratorBase {

await this.generateFullSingleAndAddtoOutput(new CliTestInit(this.model));
await this.generateFullSingleAndAddtoOutput(new CliTestStep(this.model), true, true);
const hasTestResourceScenario = this.model.GetHandler().exampleHandler.GetResourcePool()
.hasTestResourceScenario;
for (const testGroup of exampleHandler.Example_TestScenario
? Object.getOwnPropertyNames(exampleHandler.Example_TestScenario)
: []) {
await this.generateFullSingleAndAddtoOutput(
new CliTestScenario(
this.model,
PathConstants.fullTestSceanrioFile(testGroup),
hasTestResourceScenario
? PathConstants.testSwaggerScenarioFile(testGroup)
: PathConstants.fullTestSceanrioFile(testGroup),
exampleHandler.Example_TestScenario[testGroup],
testGroup,
),
Expand Down
6 changes: 5 additions & 1 deletion src/generate/generators/ExtensionIncre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,17 @@ export class AzExtensionIncrementalGenerator extends GeneratorBase {

await this.generateIncrementalSingleAndAddtoOutput(new CliTestInit(this.model));
await this.generateIncrementalSingleAndAddtoOutput(new CliTestStep(this.model), true);
const hasTestResourceScenario = this.model.GetHandler().exampleHandler.GetResourcePool()
.hasTestResourceScenario;
for (const testGroup of exampleHandler.Example_TestScenario
? Object.getOwnPropertyNames(exampleHandler.Example_TestScenario)
: []) {
await this.generateIncrementalSingleAndAddtoOutput(
new CliTestScenario(
this.model,
PathConstants.incTestScenarioFile(testGroup),
hasTestResourceScenario
? PathConstants.testSwaggerScenarioFile(testGroup)
: PathConstants.incTestScenarioFile(testGroup),
exampleHandler.Example_TestScenario[testGroup],
testGroup,
),
Expand Down
Loading

0 comments on commit a573427

Please sign in to comment.