|
| 1 | +import * as assert from 'assert'; |
| 2 | +import path = require('path'); |
| 3 | +import { MockTestRunner } from 'azure-pipelines-task-lib/mock-test'; |
| 4 | + |
| 5 | +describe("Create Snapshot test", function () { |
| 6 | + this.timeout(30000); |
| 7 | + |
| 8 | + before(async () => { |
| 9 | + process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_SERVICEPRINCIPALID"] = "spId"; |
| 10 | + process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_SERVICEPRINCIPALKEY"] = "spKey"; |
| 11 | + process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_TENANTID"] = "tenant"; |
| 12 | + process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_AUTHENTICATIONTYPE"] = "spnKey"; |
| 13 | + process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONNAME"] = "sName"; |
| 14 | + process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONID"] = "sId"; |
| 15 | + process.env["ENDPOINT_DATA_AzureRMSpn_GRAPHURL"] = "https://graph.windows.net/"; |
| 16 | + process.env["ENDPOINT_DATA_AzureRMSpn_ENVIRONMENT"] = "AzureCloud"; |
| 17 | + process.env["ENDPOINT_URL_AzureRMSpn"] = "https://management.azure.com/"; |
| 18 | + process.env["SYSTEM_DEFAULTWORKINGDIRECTORY"] = "C:\\a\\w\\"; |
| 19 | + process.env["AGENT_TEMPDIRECTORY"] = process.cwd(); |
| 20 | + }); |
| 21 | + |
| 22 | + function runValidations(validator: () => void, testRunner) { |
| 23 | + try { |
| 24 | + validator(); |
| 25 | + } catch (error) { |
| 26 | + console.log("STDERR", testRunner.stderr); |
| 27 | + console.log("STDOUT", testRunner.stdout); |
| 28 | + console.log("Error", error); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + it("Successfully create a snapshot", async () => { |
| 33 | + const taskPath = path.join(__dirname, "createSnapshot.js"); |
| 34 | + const tr = new MockTestRunner(taskPath); |
| 35 | + |
| 36 | + await tr.runAsync(); |
| 37 | + runValidations(() => { |
| 38 | + assert.strictEqual(tr.succeeded, true, "should have succeeded"); |
| 39 | + assert.strictEqual(tr.warningIssues.length, 0, "should have no warnings"); |
| 40 | + assert.strictEqual(tr.errorIssues.length, 0, "should have no errors"); |
| 41 | + assert.strictEqual(tr.stdout.indexOf(`loc_mock_SnapshotCreatedSuccessfully`) >= 0, true, "should have printed snapshot created successfully"); |
| 42 | + }, tr); |
| 43 | + }); |
| 44 | + |
| 45 | + it("Handle create snapshot conflict request", async () => { |
| 46 | + const taskPath = path.join(__dirname, "createSnapshotWithConflict.js"); |
| 47 | + const tr = new MockTestRunner(taskPath); |
| 48 | + |
| 49 | + await tr.runAsync(); |
| 50 | + |
| 51 | + runValidations(() => { |
| 52 | + assert.strictEqual(tr.succeeded, false, "should not succeeded"); |
| 53 | + assert.strictEqual(tr.warningIssues.length, 0, "should have no warnings"); |
| 54 | + assert.strictEqual(tr.errorIssues.length, 1, "should have one error"); |
| 55 | + assert.strictEqual(tr.errorIssues[0], `loc_mock_SnapshotAlreadyExists TestSnapshot Status code: 409`); |
| 56 | + }, tr); |
| 57 | + }); |
| 58 | + |
| 59 | + it("Handle create snapshot forbidden request", async () => { |
| 60 | + const taskPath = path.join(__dirname, "createSnapshotWithForbidden.js"); |
| 61 | + const tr = new MockTestRunner(taskPath); |
| 62 | + |
| 63 | + await tr.runAsync(); |
| 64 | + runValidations(() => { |
| 65 | + assert.strictEqual(tr.succeeded, false, "should not succeeded"); |
| 66 | + assert.strictEqual(tr.warningIssues.length, 0, "should have no warnings"); |
| 67 | + assert.strictEqual(tr.errorIssues.length, 1, "should have one error"); |
| 68 | + assert.strictEqual(tr.errorIssues[0], "loc_mock_AccessDenied Status code: 403", "Should have error message"); |
| 69 | + }, tr); |
| 70 | + }); |
| 71 | + |
| 72 | + it("Handle empty filter", async () => { |
| 73 | + const taskPath = path.join(__dirname, "createSnapshotWithEmptyFilter.js"); |
| 74 | + const tr = new MockTestRunner(taskPath); |
| 75 | + |
| 76 | + await tr.runAsync(); |
| 77 | + |
| 78 | + runValidations(() => { |
| 79 | + assert.strictEqual(tr.succeeded, false, "should have failed"); |
| 80 | + assert.strictEqual(tr.warningIssues.length, 0, "should have no warnings"); |
| 81 | + assert.strictEqual(tr.errorIssues.length, 1, "should have one error"); |
| 82 | + assert.strictEqual(tr.errorIssues[0], "loc_mock_MaxAndMinFiltersRequired"); |
| 83 | + }, tr); |
| 84 | + }); |
| 85 | + |
| 86 | + it("Handle invalid composition type", async () => { |
| 87 | + const taskPath = path.join(__dirname, "createSnapshotWithInvalidCompositionType.js"); |
| 88 | + const tr = new MockTestRunner(taskPath); |
| 89 | + |
| 90 | + await tr.runAsync(); |
| 91 | + |
| 92 | + runValidations(() => { |
| 93 | + assert.strictEqual(tr.succeeded, false, "should have failed"); |
| 94 | + assert.strictEqual(tr.warningIssues.length, 0, "should have no warnings"); |
| 95 | + assert.strictEqual(tr.errorIssues.length, 1, "should have one error"); |
| 96 | + assert.strictEqual(tr.errorIssues[0], "loc_mock_InvalidCompositionTypeValue key key_label invalidCompositionType"); |
| 97 | + }, tr); |
| 98 | + }); |
| 99 | + |
| 100 | + it("Handle invalid filter type", async () => { |
| 101 | + const taskPath = path.join(__dirname, "createSnapshotWithInvalidFilter.js"); |
| 102 | + const tr = new MockTestRunner(taskPath); |
| 103 | + |
| 104 | + await tr.runAsync(); |
| 105 | + runValidations(() => { |
| 106 | + assert.strictEqual(tr.succeeded, false, "should have failed"); |
| 107 | + assert.strictEqual(tr.warningIssues.length, 0, "should have no warnings"); |
| 108 | + assert.strictEqual(tr.errorIssues.length, 1, "should have one error"); |
| 109 | + assert.strictEqual(tr.errorIssues[0], "loc_mock_InvalidFilterFormat"); |
| 110 | + }, tr); |
| 111 | + }); |
| 112 | + |
| 113 | + it("Handle invalid json filters", async () => { |
| 114 | + const taskPath = path.join(__dirname, "createSnapshotWithInvalidJsonFilter.js"); |
| 115 | + const tr = new MockTestRunner(taskPath); |
| 116 | + |
| 117 | + await tr.runAsync(); |
| 118 | + runValidations(() => { |
| 119 | + assert.strictEqual(tr.succeeded, false, "should have failed"); |
| 120 | + assert.strictEqual(tr.warningIssues.length, 0, "should have no warnings"); |
| 121 | + assert.strictEqual(tr.errorIssues.length, 1, "should have one error"); |
| 122 | + assert.strictEqual(tr.errorIssues[0], "loc_mock_InvalidFilterFormatJSONObjectExpected"); |
| 123 | + }, tr); |
| 124 | + }); |
| 125 | + |
| 126 | + it("Handle invalid key filter property", async () => { |
| 127 | + const taskPath = path.join(__dirname, "createSnapshotWithInvalidKeyFilter.js"); |
| 128 | + const tr = new MockTestRunner(taskPath); |
| 129 | + |
| 130 | + await tr.runAsync(); |
| 131 | + runValidations(() => { |
| 132 | + assert.strictEqual(tr.succeeded, false, "should have failed"); |
| 133 | + assert.strictEqual(tr.warningIssues.length, 0, "should have no warnings"); |
| 134 | + assert.strictEqual(tr.errorIssues.length, 1, "should have one error"); |
| 135 | + assert.strictEqual(tr.errorIssues[0], "loc_mock_InvalidFilterFormatKeyIsRequired"); |
| 136 | + }, tr); |
| 137 | + }); |
| 138 | + |
| 139 | + it("Handle invalid label filter property", async () => { |
| 140 | + const taskPath = path.join(__dirname, "createSnapshotWithInvalidLabelFilter.js"); |
| 141 | + const tr = new MockTestRunner(taskPath); |
| 142 | + |
| 143 | + await tr.runAsync(); |
| 144 | + runValidations(() => { |
| 145 | + assert.strictEqual(tr.succeeded, false, "should have failed"); |
| 146 | + assert.strictEqual(tr.warningIssues.length, 0, "should have no warnings"); |
| 147 | + assert.strictEqual(tr.errorIssues.length, 1, "should have one error"); |
| 148 | + assert.strictEqual(tr.errorIssues[0],`loc_mock_InvalidFilterFormatExpectedAllowedProperties {"key":"*","label_filter":"2.0.0"}`); |
| 149 | + }, tr); |
| 150 | + |
| 151 | + try { |
| 152 | + |
| 153 | + } catch (error) { |
| 154 | + console.log("STDERR", tr.stderr); |
| 155 | + console.log("STDOUT", tr.stdout); |
| 156 | + console.log("Error", error); |
| 157 | + } |
| 158 | + }); |
| 159 | + |
| 160 | + it("Handle invalid retention period", async () => { |
| 161 | + const taskPath = path.join(__dirname, "createSnapshotWithInvalidRetention.js"); |
| 162 | + const tr = new MockTestRunner(taskPath); |
| 163 | + |
| 164 | + await tr.runAsync(); |
| 165 | + }); |
| 166 | + |
| 167 | + it("Handle invalid tags", async () => { |
| 168 | + const taskPath = path.join(__dirname, "createSnapshotWithInvalidTags.js"); |
| 169 | + const tr = new MockTestRunner(taskPath); |
| 170 | + |
| 171 | + await tr.runAsync(); |
| 172 | + runValidations(() => { |
| 173 | + assert.strictEqual(tr.succeeded, false, "should have failed"); |
| 174 | + assert.strictEqual(tr.warningIssues.length, 0, "should have no warnings"); |
| 175 | + assert.strictEqual(tr.errorIssues.length, 1, "should have one error"); |
| 176 | + assert.strictEqual(tr.errorIssues[0], "loc_mock_InvalidTagFormatValidJSONStringExpected"); |
| 177 | + }, tr); |
| 178 | + }); |
| 179 | + |
| 180 | + it("Handle invalid tag type", async () => { |
| 181 | + const taskPath = path.join(__dirname, "createSnapshotWithInvalidTagType.js"); |
| 182 | + const tr = new MockTestRunner(taskPath); |
| 183 | + |
| 184 | + await tr.runAsync(); |
| 185 | + |
| 186 | + runValidations(() => { |
| 187 | + assert.strictEqual(tr.succeeded, false, "should have failed"); |
| 188 | + assert.strictEqual(tr.warningIssues.length, 0, "should have no warnings"); |
| 189 | + assert.strictEqual(tr.errorIssues.length, 1, "should have one error"); |
| 190 | + assert.strictEqual(tr.errorIssues[0], "loc_mock_InvalidTagFormatOnlyStringsSupported"); |
| 191 | + }, tr); |
| 192 | + }); |
| 193 | + |
| 194 | + it("Handle max filters", async () => { |
| 195 | + const taskPath = path.join(__dirname, "createSnapshotWithMaxFilters.js"); |
| 196 | + const tr = new MockTestRunner(taskPath); |
| 197 | + |
| 198 | + await tr.runAsync(); |
| 199 | + |
| 200 | + runValidations(() => { |
| 201 | + assert.strictEqual(tr.succeeded, false, "should have failed"); |
| 202 | + assert.strictEqual(tr.warningIssues.length, 0, "should have no warnings"); |
| 203 | + assert.strictEqual(tr.errorIssues.length, 1, "should have one error"); |
| 204 | + assert.strictEqual(tr.errorIssues[0], "loc_mock_MaxAndMinFiltersRequired"); |
| 205 | + }, tr); |
| 206 | + }); |
| 207 | + |
| 208 | + it("Warn for minimum retention period", async () => { |
| 209 | + const taskPath = path.join(__dirname, "createSnapshotWithMinRetention.js"); |
| 210 | + const tr = new MockTestRunner(taskPath); |
| 211 | + |
| 212 | + await tr.runAsync(); |
| 213 | + |
| 214 | + runValidations(() => { |
| 215 | + assert.strictEqual(tr.succeeded, true, "should have succeeded"); |
| 216 | + assert.strictEqual(tr.warningIssues.length, 1, "should have one warning"); |
| 217 | + assert.strictEqual(tr.errorIssues.length, 0, "should no error"); |
| 218 | + assert.strictEqual(tr.warningIssues[0], "loc_mock_MinRetentionAfterArchiveSnapshot"); |
| 219 | + }, tr); |
| 220 | + }); |
| 221 | + |
| 222 | +}); |
0 commit comments