support validate-traffic <traffic-path> <spec-path> command in OAV cli (#633)

* support validate-traffic <traffic-path> <spec-path> command

* fix format

* update command describe

* update

* update log type

* format

* update readme

* delete semicolon

* test ci

* check file path before validate

* resolve comment

* resovle comment

* Update

* add missing dependency files

* fix lint

* remove files

* resolve comment

* fix lint

* Bump version and update changelog

* update

Co-authored-by: Ray Chen <raychen@microsoft.com>
This commit is contained in:
Ligengxin 2021-07-13 11:36:21 +08:00 коммит произвёл GitHub
Родитель 56886b0e5e
Коммит 51e2e4247b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 124 добавлений и 14 удалений

13
.vscode/launch.json поставляемый
Просмотреть файл

@ -28,6 +28,19 @@
],
"env": {}
},
{
"type": "node",
"request": "launch",
"name": "Validate Traffic",
"program": "${workspaceRoot}/dist/cli.js",
"cwd": "${workspaceRoot}",
"args": [
"validate-traffic",
"${workspaceRoot}/test/liveValidation/payloads/test-validate-traffic.json",
"${workspaceRoot}/test/liveValidation/swaggers/specification/dns/resource-manager/Microsoft.Network/stable/2018-05-01/dns.json"
],
"env": {}
},
{
"type": "node",
"request": "launch",

Просмотреть файл

@ -1,5 +1,9 @@
# Change Log - oav
## 07/08/2021 2.6.0
- Support `validate-traffic <traffic-path> <spec-path>` command in OAV cli
## 07/05/2021 2021 2.5.9
- Ignore LRO_RESPONSE_HEADER rule check in case of synchronous api call

Просмотреть файл

@ -42,6 +42,8 @@ Commands:
and examples present in the spec.
validate-spec <spec-path> Performs semantic validation of the
spec.
validate-traffic <traffic-path> Validate traffic payload against
<spec-path> the spec.
Options:
--version Show version number [boolean]

Просмотреть файл

@ -0,0 +1,23 @@
import * as yargs from "yargs";
import { cliSuppressExceptions } from "../cliSuppressExceptions";
import { log } from "../util/logging";
import * as validate from "../validate";
export const command = "validate-traffic <traffic-path> <spec-path>";
export const describe = "Validate traffic payload against the spec.";
export async function handler(argv: yargs.Arguments): Promise<void> {
await cliSuppressExceptions(async () => {
log.debug(argv.toString());
const specPath = argv.specPath;
const trafficPath = argv.trafficPath;
const vOptions: validate.Options = {
consoleLogLevel: argv.logLevel,
logFilepath: argv.f,
pretty: argv.p,
};
const errors = await validate.validateTrafficAgainstSpec(specPath, trafficPath, vOptions);
return errors.length > 0 ? 1 : 0;
});
}

Просмотреть файл

@ -30,6 +30,8 @@ import { getErrorsFromModelValidation } from "./util/getErrorsFromModelValidatio
import { getSuppressions } from "./validators/suppressions";
import { log } from "./util/logging";
import { getInputFiles } from "./generator/util";
import { LiveValidator } from "../lib/liveValidation/liveValidator"
import { LiveValidationIssue } from "../lib/liveValidation/liveValidator"
export interface Options extends specResolver.Options, umlGeneratorLib.Options {
consoleLogLevel?: unknown;
@ -276,6 +278,72 @@ export async function validateExamplesInCompositeSpec(
});
}
export async function validateTrafficAgainstSpec(
specPath: string,
trafficPath: string,
options: Options
): Promise<Array<LiveValidationIssue>>{
if (!fs.existsSync(specPath)) {
const error = new Error(`Can not find spec file, please check your specPath parameter.`);
log.error(JSON.stringify(error));
throw error;
}
if (!fs.existsSync(trafficPath)) {
const error = new Error(`Can not find traffic file, please check your trafficPath parameter.`);
log.error(JSON.stringify(error));
throw error;
}
try {
const trafficFile = require(trafficPath);
const specFileDirectory = path.dirname(specPath);
const swaggerPathsPattern = path.basename(specPath);
return validate(options, async (o) => {
o.consoleLogLevel = log.consoleLogLevel;
o.logFilepath = log.filepath;
const liveValidationOptions = {
directory: specFileDirectory,
swaggerPathsPattern: [swaggerPathsPattern],
git: {
shouldClone: false
}
}
const validator = new LiveValidator(liveValidationOptions);
const errors: Array<LiveValidationIssue> = [];
await validator.initialize();
const result = await validator.validateLiveRequestResponse(trafficFile);
if (!result.requestValidationResult.isSuccessful) {
errors.push(...result.requestValidationResult.errors);
}
if (!result.responseValidationResult.isSuccessful) {
errors.push(...result.responseValidationResult.errors);
}
if (errors.length > 0) {
if (o.pretty) {
prettyPrint(errors, "error");
} else {
for (let error of errors) {
log.error(JSON.stringify(error));
}
}
} else {
log.info('No errors were found.');
}
return errors;
})
} catch (error) {
log.error(JSON.stringify(error));
throw error;
}
}
export async function resolveSpec(
specPath: string,
outputDir: string,

26
package-lock.json сгенерированный
Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "oav",
"version": "2.5.9",
"version": "2.6.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -3334,7 +3334,7 @@
},
"buffer-equal-constant-time": {
"version": "1.0.1",
"resolved": "https://pkgs.dev.azure.com/devdiv/_packaging/openapi-platform/npm/registry/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
"resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
"integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk="
},
"buffer-from": {
@ -4168,7 +4168,7 @@
},
"difflib": {
"version": "0.2.4",
"resolved": "https://pkgs.dev.azure.com/devdiv/_packaging/openapi-platform/npm/registry/difflib/-/difflib-0.2.4.tgz",
"resolved": "https://registry.npmjs.org/difflib/-/difflib-0.2.4.tgz",
"integrity": "sha1-teMDYabbAjF21WKJLbhZQKcY9H4=",
"requires": {
"heap": ">= 0.2.0"
@ -5929,7 +5929,7 @@
},
"heap": {
"version": "0.2.6",
"resolved": "https://pkgs.dev.azure.com/devdiv/_packaging/openapi-platform/npm/registry/heap/-/heap-0.2.6.tgz",
"resolved": "https://registry.npmjs.org/heap/-/heap-0.2.6.tgz",
"integrity": "sha1-CH4fELBGky/IWU3Z5tN4r8nR5aw="
},
"hosted-git-info": {
@ -10175,12 +10175,12 @@
},
"lodash.includes": {
"version": "4.3.0",
"resolved": "https://pkgs.dev.azure.com/devdiv/_packaging/openapi-platform/npm/registry/lodash.includes/-/lodash.includes-4.3.0.tgz",
"resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz",
"integrity": "sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8="
},
"lodash.isboolean": {
"version": "3.0.3",
"resolved": "https://pkgs.dev.azure.com/devdiv/_packaging/openapi-platform/npm/registry/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz",
"resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz",
"integrity": "sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY="
},
"lodash.isequal": {
@ -10190,22 +10190,22 @@
},
"lodash.isinteger": {
"version": "4.0.4",
"resolved": "https://pkgs.dev.azure.com/devdiv/_packaging/openapi-platform/npm/registry/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz",
"resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz",
"integrity": "sha1-YZwK89A/iwTDH1iChAt3sRzWg0M="
},
"lodash.isnumber": {
"version": "3.0.3",
"resolved": "https://pkgs.dev.azure.com/devdiv/_packaging/openapi-platform/npm/registry/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz",
"resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz",
"integrity": "sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w="
},
"lodash.isplainobject": {
"version": "4.0.6",
"resolved": "https://pkgs.dev.azure.com/devdiv/_packaging/openapi-platform/npm/registry/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
"resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
"integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs="
},
"lodash.isstring": {
"version": "4.0.1",
"resolved": "https://pkgs.dev.azure.com/devdiv/_packaging/openapi-platform/npm/registry/lodash.isstring/-/lodash.isstring-4.0.1.tgz",
"resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz",
"integrity": "sha1-1SfftUVuynzJu5XV2ur4i6VKVFE="
},
"lodash.merge": {
@ -10221,7 +10221,7 @@
},
"lodash.once": {
"version": "4.1.1",
"resolved": "https://pkgs.dev.azure.com/devdiv/_packaging/openapi-platform/npm/registry/lodash.once/-/lodash.once-4.1.1.tgz",
"resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz",
"integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w="
},
"lodash.sortby": {
@ -11218,7 +11218,7 @@
},
"object-assign": {
"version": "4.1.1",
"resolved": "https://pkgs.dev.azure.com/devdiv/_packaging/openapi-platform/npm/registry/object-assign/-/object-assign-4.1.1.tgz",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
"integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM="
},
"object-copy": {
@ -11998,7 +11998,7 @@
},
"process": {
"version": "0.11.10",
"resolved": "https://pkgs.dev.azure.com/devdiv/_packaging/openapi-platform/npm/registry/process/-/process-0.11.10.tgz",
"resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz",
"integrity": "sha1-czIwDoQBYb2j5podHZGn1LwW8YI="
},
"process-nextick-args": {

Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "oav",
"version": "2.5.9",
"version": "2.6.0",
"author": {
"name": "Microsoft Corporation",
"email": "azsdkteam@microsoft.com",