Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit 8f45e74

Browse files
[FEATURE] have error code and chaining for deployment validate cmd (#490)
* [FEATURE] Added code to have error code and chaining for deployment validate cmd * Update validate.test.ts Co-authored-by: Samiya Akhtar <[email protected]>
1 parent 2758c12 commit 8f45e74

File tree

8 files changed

+115
-68
lines changed

8 files changed

+115
-68
lines changed

docs/commands/data.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@
224224
"description": "Run a test for the configured storage account. This will write test data and delete the test data. For more information on the behavior, please check the online documentation.",
225225
"defaultValue": false
226226
}
227-
]
227+
],
228+
"markdown": "## Description\n\nThis command validates the\n[requirements](https://github.com/CatalystCode/spk/blob/master/guides/service-introspection.md#requirements)\nand the onboard\n[prerequisites](https://github.com/CatalystCode/spk/blob/master/guides/service-introspection.md#prerequisites)\n\n## Note\n\nThe purpose of `--self-test` option is to make sure that `spk` is able to write\ndata to the provided storage account. Once the test ends, it will remove the\ntest data that was added.\n"
228229
},
229230
"hld append-variable-group": {
230231
"command": "append-variable-group <variable-group-name>",

src/commands/deployment/create.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const handlePipeline1 = async (
7979
) {
8080
throw buildError(
8181
errorStatusCode.VALIDATION_ERR,
82-
"introspect-create-cmd-cmd-p1-missing-values"
82+
"introspect-create-cmd-p1-missing-values"
8383
);
8484
}
8585
await addSrcToACRPipeline(
@@ -104,7 +104,7 @@ export const handlePipeline2 = async (
104104
) {
105105
throw buildError(
106106
errorStatusCode.VALIDATION_ERR,
107-
"introspect-create-cmd-cmd-p2-missing-values"
107+
"introspect-create-cmd-p2-missing-values"
108108
);
109109
}
110110
await updateACRToHLDPipeline(
File renamed without changes.

src/commands/deployment/validate.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ describe("test runSelfTest function", () => {
224224

225225
const config = deepClone(mockedValidateConfig);
226226
config.tableName = "";
227-
await runSelfTest(config);
227+
await expect(runSelfTest(config)).rejects.toThrow(
228+
"introspect-validate-cmd-valid-exception: Error was caught during validation."
229+
);
228230
});
229231
it("negative test: error thrown", async () => {
230232
jest

src/commands/deployment/validate.ts

+33-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
1212
import { logger } from "../../logger";
1313
import { ConfigYaml } from "../../types";
14-
import decorator from "./validator.decorator.json";
14+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
15+
import { errorStatusCode } from "../../lib/errorStatusCode";
16+
import decorator from "./validate.decorator.json";
1517

1618
const service = "spk-self-test";
1719

@@ -64,10 +66,10 @@ export const isValidConfig = (config: ConfigYaml): ValidateConfig => {
6466
}
6567

6668
if (missingConfig.length > 0) {
67-
logger.error(
68-
"Validation failed. Missing configuration: " + missingConfig.join(" ")
69-
);
70-
throw new Error("missing configuration in spk configuration");
69+
throw buildError(errorStatusCode.VALIDATION_ERR, {
70+
errorKey: "introspect-validate-cmd-valid-err",
71+
values: [missingConfig.join(" ")],
72+
});
7173
} else {
7274
logger.info("Configuration validation: SUCCEEDED");
7375
}
@@ -90,8 +92,10 @@ export const isValidConfig = (config: ConfigYaml): ValidateConfig => {
9092
key: config.introspection.azure.key,
9193
};
9294
}
93-
throw Error(
94-
"You need to specify configuration for your introspection storage account and DevOps pipeline to run this dashboard. Please initialize the spk tool with the right configuration"
95+
96+
throw buildError(
97+
errorStatusCode.VALIDATION_ERR,
98+
"introspect-validate-cmd-missing-vals"
9599
);
96100
};
97101

@@ -133,8 +137,11 @@ export const writeSelfTestData = async (
133137

134138
return buildId;
135139
} catch (err) {
136-
logger.error(err);
137-
throw new Error("Error writing data to service introspection.");
140+
throw buildError(
141+
errorStatusCode.ENV_SETTING_ERR,
142+
"introspect-validate-cmd-write-pipeline",
143+
err
144+
);
138145
}
139146
};
140147

@@ -216,12 +223,18 @@ export const runSelfTest = async (config: ValidateConfig): Promise<void> => {
216223

217224
if (!isVerified) {
218225
logger.error(statusMessage + "FAILED. Please try again.");
219-
} else {
220-
logger.info(statusMessage + "SUCCEEDED.");
226+
throw buildError(
227+
errorStatusCode.ENV_SETTING_ERR,
228+
"introspect-validate-cmd-valid-failed"
229+
);
221230
}
231+
logger.info(statusMessage + "SUCCEEDED.");
222232
} catch (err) {
223-
logger.error("Error running self-test.");
224-
throw err;
233+
throw buildError(
234+
errorStatusCode.EXE_FLOW_ERR,
235+
"introspect-validate-cmd-valid-exception",
236+
err
237+
);
225238
}
226239
};
227240

@@ -244,7 +257,13 @@ export const execute = async (
244257
}
245258
await exitFn(0);
246259
} catch (err) {
247-
logger.error(err);
260+
logError(
261+
buildError(
262+
errorStatusCode.CMD_EXE_ERR,
263+
"introspect-validate-cmd-failed",
264+
err
265+
)
266+
);
248267
await exitFn(1);
249268
}
250269
};

src/lib/azure/deploymenttable.ts

+63-47
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,28 @@ export const addSrcToACRPipeline = async (
148148
commitId: string,
149149
repository?: string
150150
): Promise<RowSrcToACRPipeline> => {
151-
const entry: RowSrcToACRPipeline = {
152-
PartitionKey: tableInfo.partitionKey,
153-
RowKey: getRowKey(),
154-
commitId,
155-
imageTag,
156-
p1: pipelineId,
157-
service: serviceName,
158-
};
159-
if (repository) {
160-
entry.sourceRepo = repository.toLowerCase();
151+
try {
152+
const entry: RowSrcToACRPipeline = {
153+
PartitionKey: tableInfo.partitionKey,
154+
RowKey: getRowKey(),
155+
commitId,
156+
imageTag,
157+
p1: pipelineId,
158+
service: serviceName,
159+
};
160+
if (repository) {
161+
entry.sourceRepo = repository.toLowerCase();
162+
}
163+
await insertToTable(tableInfo, entry);
164+
logger.info("Added first pipeline details to the database");
165+
return entry;
166+
} catch (err) {
167+
throw buildError(
168+
errorStatusCode.AZURE_STORAGE_OP_ERR,
169+
"deployment-table-add-src-to-acr-pipeline",
170+
err
171+
);
161172
}
162-
await insertToTable(tableInfo, entry);
163-
logger.info("Added first pipeline details to the database");
164-
return entry;
165173
};
166174

167175
/**
@@ -330,33 +338,48 @@ export const updateACRToHLDPipeline = async (
330338
pr?: string,
331339
repository?: string
332340
): Promise<RowACRToHLDPipeline> => {
333-
const entries = await findMatchingDeployments<EntryACRToHLDPipeline>(
334-
tableInfo,
335-
"imageTag",
336-
imageTag
337-
);
338-
339-
// 1. try to find the matching entry.
340-
if (entries && entries.length > 0) {
341-
const found = await updateMatchingArcToHLDPipelineEntry(
342-
entries,
341+
try {
342+
const entries = await findMatchingDeployments<EntryACRToHLDPipeline>(
343343
tableInfo,
344-
pipelineId,
345-
imageTag,
346-
hldCommitId,
347-
env,
348-
pr,
349-
repository
344+
"imageTag",
345+
imageTag
350346
);
351347

352-
if (found) {
353-
return found;
348+
// 1. try to find the matching entry.
349+
if (entries && entries.length > 0) {
350+
const found = await updateMatchingArcToHLDPipelineEntry(
351+
entries,
352+
tableInfo,
353+
pipelineId,
354+
imageTag,
355+
hldCommitId,
356+
env,
357+
pr,
358+
repository
359+
);
360+
361+
if (found) {
362+
return found;
363+
}
364+
365+
// 2. when cannot find the entry, we take the last row and INSERT it.
366+
// TODO: rethink this logic.
367+
return await updateLastRowOfArcToHLDPipelines(
368+
entries,
369+
tableInfo,
370+
pipelineId,
371+
imageTag,
372+
hldCommitId,
373+
env,
374+
pr,
375+
repository
376+
);
354377
}
355378

356-
// 2. when cannot find the entry, we take the last row and INSERT it.
379+
// Fallback: Ideally we should not be getting here, because there should
380+
// always be a p1 for any p2 being created.
357381
// TODO: rethink this logic.
358-
return await updateLastRowOfArcToHLDPipelines(
359-
entries,
382+
return await addNewRowToArcToHLDPipelines(
360383
tableInfo,
361384
pipelineId,
362385
imageTag,
@@ -365,20 +388,13 @@ export const updateACRToHLDPipeline = async (
365388
pr,
366389
repository
367390
);
391+
} catch (err) {
392+
throw buildError(
393+
errorStatusCode.AZURE_STORAGE_OP_ERR,
394+
"deployment-table-add-acr-to-hld-pipeline",
395+
err
396+
);
368397
}
369-
370-
// Fallback: Ideally we should not be getting here, because there should
371-
// always be a p1 for any p2 being created.
372-
// TODO: rethink this logic.
373-
return await addNewRowToArcToHLDPipelines(
374-
tableInfo,
375-
pipelineId,
376-
imageTag,
377-
hldCommitId,
378-
env,
379-
pr,
380-
repository
381-
);
382398
};
383399

384400
/**

src/lib/i18n.json

+12-3
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,15 @@
7575
"introspect-create-cmd-failed": "Deployment create command was not successfully executed.",
7676
"introspect-create-cmd-no-ops": "No action could be performed for specified arguments.",
7777
"introspect-create-cmd-missing-values": "Access key, storage account name, partition key and/or table name were not provided. Provide them.",
78-
"introspect-create-cmd-cmd-p1-missing-values": "Values for image-tag, commit-id and service options were missing. They are required for updating the details of source pipeline. Provide them.",
79-
"introspect-create-cmd-cmd-p2-missing-values": "Values for p2, hld-commit-id, image-tag and env options were missing. They are required For updating the details of image tag release pipeline. Provide them.",
78+
"introspect-create-cmd-p1-missing-values": "Values for image-tag, commit-id and service options were missing. They are required for updating the details of source pipeline. Provide them.",
79+
"introspect-create-cmd-p2-missing-values": "Values for p2, hld-commit-id, image-tag and env options were missing. They are required For updating the details of image tag release pipeline. Provide them.",
80+
81+
"introspect-validate-cmd-failed": "Deployment validate command was not successfully executed.",
82+
"introspect-validate-cmd-valid-err": "Validation failed. Missing configuration: {0}",
83+
"introspect-validate-cmd-missing-vals": "Configuration for introspection storage account and DevOps pipeline to execute this command were missing. Initialize the spk tool with the right configuration",
84+
"introspect-validate-cmd-valid-failed": "Validation was unsuccessful. Try again.",
85+
"introspect-validate-cmd-valid-exception": "Error was caught during validation.",
86+
"introspect-validate-cmd-write-pipeline": "Error writing data to service introspection.",
8087

8188
"introspect-dashboard-cmd-failed": "Deployment dashboard command was not successfully executed.",
8289
"introspect-dashboard-cmd-invalid-port": "value for port option has to be a valid port number. Enter a valid port number.",
@@ -88,6 +95,8 @@
8895

8996
"deployment-table-update-hld-manifest-pipeline-failed": "Could not update HLD to manifest pipeline.",
9097
"deployment-table-update-manifest-commit-id-failed": "Could not update manifest commit Id.",
91-
"deployment-table-update-manifest-commit-id-failed-no-generation": "No manifest generation found to update manifest commit {0}."
98+
"deployment-table-update-manifest-commit-id-failed-no-generation": "No manifest generation found to update manifest commit {0}.",
99+
"deployment-table-add-src-to-acr-pipeline": "Could not add source to ACR pipeline information to storage table.",
100+
"deployment-table-add-acr-to-hld-pipeline": "Could not add ACR to HLD pipeline information to storage table."
92101
}
93102
}

0 commit comments

Comments
 (0)