Skip to content

Commit

Permalink
Add whatif
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-c-martin committed Aug 17, 2023
1 parent 0c12079 commit 26ff6c5
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 13 deletions.
13 changes: 7 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function validate(wrapper, params) {
...["--template-file", params.templateFile],
...(params.parametersFile ? ["--parameters", params.parametersFile] : []),
...["--output", "json"],
...['--only-show-errors'],
...["--only-show-errors"]
];
return yield wrapper.execute(parameters);
});
Expand All @@ -68,7 +68,8 @@ function whatif(wrapper, params) {
...["--template-file", params.templateFile],
...(params.parametersFile ? ["--parameters", params.parametersFile] : []),
...["--output", "json"],
...['--only-show-errors'],
...["--only-show-errors"],
...["--no-pretty-print"]
];
return yield wrapper.execute(parameters);
});
Expand Down Expand Up @@ -285,17 +286,17 @@ function whatIfAndGetMarkdown(azCli, parameters) {
}
exports.whatIfAndGetMarkdown = whatIfAndGetMarkdown;
function parseErrors(stderr) {
if (stderr.startsWith('ERROR: ')) {
stderr = stderr.substring('ERROR: '.length);
if (stderr.startsWith("ERROR: ")) {
stderr = stderr.substring("ERROR: ".length);
}
const errors = [];
const split = stderr.split(/\r?\n/);
for (const line of split) {
if (line.startsWith('{')) {
if (line.startsWith("{")) {
errors.push(JSON.parse(line));
}
else if (/^(.+)\((\d+),(\d+)\)\s:\s(Error|Warning|Info)\s(.+):\s(.+)$/.test(line)) {
errors.push({ code: 'BicepBuildError', message: line });
errors.push({ code: "BicepBuildError", message: line });
}
}
return errors;
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/azcli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export async function whatif(wrapper: AzCliWrapper, params: ActionParameters) {
...["--template-file", params.templateFile],
...(params.parametersFile ? ["--parameters", params.parametersFile] : []),
...["--output", "json"],
...["--only-show-errors"]
...["--only-show-errors"],
...["--no-pretty-print"]
];

return await wrapper.execute(parameters);
Expand Down
32 changes: 27 additions & 5 deletions test/scenarios.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
expectBaselineToMatch,
isBaselineRecordEnabled
} from "./utils";
import { validateAndGetMarkdown } from "../src/run";
import { validateAndGetMarkdown, whatIfAndGetMarkdown } from "../src/run";

const subscriptionId = "d08e1a72-8180-4ed3-8125-9dff7376b0bd";
const resourceGroup = "ant-test";
Expand All @@ -19,24 +19,46 @@ describe("scenarios", () => {
{ name: "preflight-error" }
];

// it.each(scenarios)(
// "validation produces the expected output ($name)",
// async (scenario) => {
// const basePath = `test/scenarios/${scenario.name}`;

// const azCli = new AzCliTestRecorder(
// `${basePath}/cli-validate.json`,
// recordMode
// );

// const markdown = await validateAndGetMarkdown(azCli, {
// subscriptionId: subscriptionId,
// resourceGroup: resourceGroup,
// templateFile: `${basePath}/main.bicep`,
// parametersFile: `${basePath}/main.bicepparam`
// });

// await expectBaselineToMatch(`${basePath}/validate.md`, markdown);
// },
// timeout
// );

it.each(scenarios)(
"validation produces the expected output ($name)",
"whatif produces the expected output ($name)",
async (scenario) => {
const basePath = `test/scenarios/${scenario.name}`;

const azCli = new AzCliTestRecorder(
`${basePath}/cli-validate.json`,
`${basePath}/cli-whatif.json`,
recordMode
);

const markdown = await validateAndGetMarkdown(azCli, {
const markdown = await whatIfAndGetMarkdown(azCli, {
subscriptionId: subscriptionId,
resourceGroup: resourceGroup,
templateFile: `${basePath}/main.bicep`,
parametersFile: `${basePath}/main.bicepparam`
});

await expectBaselineToMatch(`${basePath}/validate.md`, markdown);
await expectBaselineToMatch(`${basePath}/whatif.md`, markdown);
},
timeout
);
Expand Down
6 changes: 6 additions & 0 deletions test/scenarios/basic-success/cli-whatif.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"arguments": "deployment group what-if --subscription d08e1a72-8180-4ed3-8125-9dff7376b0bd --resource-group ant-test --template-file test/scenarios/basic-success/main.bicep --parameters test/scenarios/basic-success/main.bicepparam --output json --only-show-errors --no-pretty-print",
"exitCode": 0,
"stdout": "{\n \"changes\": [\n {\n \"after\": {\n \"id\": \"/subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Microsoft.Storage/storageAccounts/asdf9h238h\",\n \"kind\": \"StorageV2\",\n \"location\": \"westus\",\n \"name\": \"asdf9h238h\",\n \"resourceGroup\": \"ant-test\",\n \"sku\": {\n \"name\": \"Standard_LRS\",\n \"tier\": \"Standard\"\n },\n \"type\": \"Microsoft.Storage/storageAccounts\"\n },\n \"before\": {\n \"id\": \"/subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Microsoft.Storage/storageAccounts/asdf9h238h\",\n \"kind\": \"StorageV2\",\n \"location\": \"westus\",\n \"name\": \"asdf9h238h\",\n \"resourceGroup\": \"ant-test\",\n \"sku\": {\n \"name\": \"Standard_LRS\",\n \"tier\": \"Standard\"\n },\n \"type\": \"Microsoft.Storage/storageAccounts\"\n },\n \"changeType\": \"Ignore\",\n \"delta\": null,\n \"resourceId\": \"/subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Microsoft.Storage/storageAccounts/asdf9h238h\",\n \"unsupportedReason\": null\n },\n {\n \"after\": {\n \"id\": \"/subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Microsoft.Storage/storageAccounts/foo3462346\",\n \"kind\": \"StorageV2\",\n \"location\": \"westus\",\n \"name\": \"foo3462346\",\n \"resourceGroup\": \"ant-test\",\n \"sku\": {\n \"name\": \"Standard_LRS\",\n \"tier\": \"Standard\"\n },\n \"type\": \"Microsoft.Storage/storageAccounts\"\n },\n \"before\": {\n \"id\": \"/subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Microsoft.Storage/storageAccounts/foo3462346\",\n \"kind\": \"StorageV2\",\n \"location\": \"westus\",\n \"name\": \"foo3462346\",\n \"resourceGroup\": \"ant-test\",\n \"sku\": {\n \"name\": \"Standard_LRS\",\n \"tier\": \"Standard\"\n },\n \"type\": \"Microsoft.Storage/storageAccounts\"\n },\n \"changeType\": \"Ignore\",\n \"delta\": null,\n \"resourceId\": \"/subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Microsoft.Storage/storageAccounts/foo3462346\",\n \"unsupportedReason\": null\n },\n {\n \"after\": {\n \"apiVersion\": \"2023-01-01\",\n \"id\": \"/subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Microsoft.Storage/storageAccounts/v54obf2m3uexgasddsf\",\n \"kind\": \"StorageV2\",\n \"location\": \"West US\",\n \"name\": \"v54obf2m3uexgasddsf\",\n \"resourceGroup\": \"ant-test\",\n \"sku\": {\n \"name\": \"Standard_ZRS\"\n },\n \"type\": \"Microsoft.Storage/storageAccounts\"\n },\n \"before\": null,\n \"changeType\": \"Create\",\n \"delta\": null,\n \"resourceId\": \"/subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Microsoft.Storage/storageAccounts/v54obf2m3uexgasddsf\",\n \"unsupportedReason\": null\n },\n {\n \"after\": {\n \"id\": \"/subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Providers.Test/statelessResources/main-child0-child0-child0-child\",\n \"location\": \"westus\",\n \"name\": \"main-child0-child0-child0-child\",\n \"resourceGroup\": \"ant-test\",\n \"type\": \"Providers.Test/statelessResources\"\n },\n \"before\": {\n \"id\": \"/subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Providers.Test/statelessResources/main-child0-child0-child0-child\",\n \"location\": \"westus\",\n \"name\": \"main-child0-child0-child0-child\",\n \"resourceGroup\": \"ant-test\",\n \"type\": \"Providers.Test/statelessResources\"\n },\n \"changeType\": \"Ignore\",\n \"delta\": null,\n \"resourceId\": \"/subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Providers.Test/statelessResources/main-child0-child0-child0-child\",\n \"unsupportedReason\": null\n }\n ],\n \"error\": null,\n \"status\": \"Succeeded\"\n}\n",
"stderr": ""
}
9 changes: 9 additions & 0 deletions test/scenarios/basic-success/whatif.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## What-If Results
✅ Success!

| Resource Id | Change Type | Change |
|-|-|- |
| /subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Microsoft.Storage/storageAccounts/asdf9h238h | Ignore | <pre>null</pre> |
| /subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Microsoft.Storage/storageAccounts/foo3462346 | Ignore | <pre>null</pre> |
| /subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Microsoft.Storage/storageAccounts/v54obf2m3uexgasddsf | Create | <pre>null</pre> |
| /subscriptions/d08e1a72-8180-4ed3-8125-9dff7376b0bd/resourceGroups/ant-test/providers/Providers.Test/statelessResources/main-child0-child0-child0-child | Ignore | <pre>null</pre> |
6 changes: 6 additions & 0 deletions test/scenarios/bicep-error/cli-whatif.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"arguments": "deployment group what-if --subscription d08e1a72-8180-4ed3-8125-9dff7376b0bd --resource-group ant-test --template-file test/scenarios/bicep-error/main.bicep --parameters test/scenarios/bicep-error/main.bicepparam --output json --only-show-errors --no-pretty-print",
"exitCode": 1,
"stdout": "",
"stderr": "ERROR: /Users/ant/Desktop/bicep-action/test/scenarios/bicep-error/main.bicep(1,5) : Error BCP028: Identifier \"foo\" is declared multiple times. Remove or rename the duplicates.\n/Users/ant/Desktop/bicep-action/test/scenarios/bicep-error/main.bicep(1,8) : Error BCP018: Expected the \"=\" character at this location.\n/Users/ant/Desktop/bicep-action/test/scenarios/bicep-error/main.bicep(3,5) : Error BCP028: Identifier \"foo\" is declared multiple times. Remove or rename the duplicates.\n/Users/ant/Desktop/bicep-action/test/scenarios/bicep-error/main.bicep(3,21) : Error BCP057: The name \"a\" does not exist in the current context.\n/Users/ant/Desktop/bicep-action/test/scenarios/bicep-error/main.bicep(5,21) : Error BCP062: The referenced declaration with name \"foo\" is not valid.\n"
}
10 changes: 10 additions & 0 deletions test/scenarios/bicep-error/whatif.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## What-If Results
❌ Failure!

| Code | Message | Target |
|-|-|- |
| BicepBuildError | /Users/ant/Desktop/bicep-action/test/scenarios/bicep-error/main.bicep(1,5) : Error BCP028: Identifier "foo" is declared multiple times. Remove or rename the duplicates. | |
| BicepBuildError | /Users/ant/Desktop/bicep-action/test/scenarios/bicep-error/main.bicep(1,8) : Error BCP018: Expected the "=" character at this location. | |
| BicepBuildError | /Users/ant/Desktop/bicep-action/test/scenarios/bicep-error/main.bicep(3,5) : Error BCP028: Identifier "foo" is declared multiple times. Remove or rename the duplicates. | |
| BicepBuildError | /Users/ant/Desktop/bicep-action/test/scenarios/bicep-error/main.bicep(3,21) : Error BCP057: The name "a" does not exist in the current context. | |
| BicepBuildError | /Users/ant/Desktop/bicep-action/test/scenarios/bicep-error/main.bicep(5,21) : Error BCP062: The referenced declaration with name "foo" is not valid. | |
6 changes: 6 additions & 0 deletions test/scenarios/preflight-error/cli-whatif.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"arguments": "deployment group what-if --subscription d08e1a72-8180-4ed3-8125-9dff7376b0bd --resource-group ant-test --template-file test/scenarios/preflight-error/main.bicep --parameters test/scenarios/preflight-error/main.bicepparam --output json --only-show-errors --no-pretty-print",
"exitCode": 1,
"stdout": "",
"stderr": "ERROR: InvalidTemplateDeployment - The template deployment 'main' is not valid according to the validation procedure. The tracking id is '501d03f7-c844-4b86-a498-1de4d9fafdb4'. See inner errors for details.\nPreflightValidationCheckFailed - Preflight validation failed. Please refer to the details for the specific errors.\nStorageAccountAlreadyTaken - The storage account named stg is already taken.\n"
}
5 changes: 5 additions & 0 deletions test/scenarios/preflight-error/whatif.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## What-If Results
❌ Failure!

| Code | Message | Target |
|-|-|- |
6 changes: 6 additions & 0 deletions test/scenarios/static-error/cli-whatif.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"arguments": "deployment group what-if --subscription d08e1a72-8180-4ed3-8125-9dff7376b0bd --resource-group ant-test --template-file test/scenarios/static-error/main.bicep --parameters test/scenarios/static-error/main.bicepparam --output json --only-show-errors --no-pretty-print",
"exitCode": 1,
"stdout": "",
"stderr": "ERROR: LocationNotAvailableForResourceType - The provided location 'antarctica' is not available for resource type 'Microsoft.Storage/storageAccounts'. List of available regions for the resource type is 'eastus,eastus2,westus,westeurope,eastasia,southeastasia,japaneast,japanwest,northcentralus,southcentralus,centralus,northeurope,brazilsouth,australiaeast,australiasoutheast,southindia,centralindia,westindia,canadaeast,canadacentral,westus2,westcentralus,uksouth,ukwest,koreacentral,koreasouth,francecentral,australiacentral,southafricanorth,uaenorth,switzerlandnorth,germanywestcentral,norwayeast,westus3,jioindiawest,swedencentral,qatarcentral,polandcentral'.\n"
}
5 changes: 5 additions & 0 deletions test/scenarios/static-error/whatif.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## What-If Results
❌ Failure!

| Code | Message | Target |
|-|-|- |

0 comments on commit 26ff6c5

Please sign in to comment.