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

Commit 6a50ef9

Browse files
[FIX} Issue 935: Intermittent SPK unit test failure related to SPK infra (#241)
Co-authored-by: Nate <[email protected]>
1 parent 7ffaca9 commit 6a50ef9

File tree

2 files changed

+169
-16
lines changed

2 files changed

+169
-16
lines changed

src/commands/infra/generate.test.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import fs from "fs";
12
import path from "path";
23
import { loadConfigurationFromLocalEnv, readYaml } from "../../config";
4+
import { safeGitUrlForLogging } from "../../lib/gitutils";
35
import { removeDir } from "../../lib/ioUtil";
46
import {
57
disableVerboseLogging,
@@ -8,17 +10,28 @@ import {
810
} from "../../logger";
911
import { IInfraConfigYaml } from "../../types";
1012
import {
13+
checkRemoteGitExist,
14+
createGenerated,
1115
DefinitionYAMLExistence,
1216
dirIteration,
1317
execute,
1418
fetchValues,
1519
generateConfig,
1620
generateTfvars,
21+
gitCheckout,
22+
gitFetchPull,
1723
validateDefinition,
1824
validateRemoteSource,
1925
validateTemplateSources
2026
} from "./generate";
2127
import * as generate from "./generate";
28+
import * as infraCommon from "./infra_common";
29+
30+
interface IGitTestData {
31+
source: string;
32+
sourcePath: string;
33+
safeLoggingUrl: string;
34+
}
2235

2336
beforeAll(() => {
2437
enableVerboseLogging();
@@ -353,8 +366,124 @@ describe("Validate sources in definition.yaml files", () => {
353366
});
354367
});
355368

369+
const getMockedDataForGitTests = async (
370+
positive: boolean
371+
): Promise<IGitTestData> => {
372+
const mockParentPath = "src/commands/infra/mocks/discovery-service";
373+
const mockProjectPath = "src/commands/infra/mocks/discovery-service/west";
374+
const sourceConfiguration = validateDefinition(
375+
mockParentPath,
376+
mockProjectPath
377+
);
378+
const sourceConfig = validateTemplateSources(
379+
sourceConfiguration,
380+
mockParentPath,
381+
mockProjectPath
382+
);
383+
let source = sourceConfig.source!;
384+
if (!positive) {
385+
source += "dummy";
386+
}
387+
388+
// Converting source name to storable folder name
389+
const sourceFolder = await infraCommon.repoCloneRegex(source);
390+
const sourcePath = path.join(infraCommon.spkTemplatesPath, sourceFolder);
391+
const safeLoggingUrl = safeGitUrlForLogging(source);
392+
393+
return {
394+
safeLoggingUrl,
395+
source,
396+
sourcePath
397+
};
398+
};
399+
400+
const testCheckRemoteGitExist = async (positive: boolean) => {
401+
const { safeLoggingUrl, source, sourcePath } = await getMockedDataForGitTests(
402+
positive
403+
);
404+
if (!fs.existsSync(sourcePath)) {
405+
createGenerated(sourcePath);
406+
}
407+
await checkRemoteGitExist(sourcePath, source, safeLoggingUrl);
408+
};
409+
410+
describe("test checkRemoteGitExist function", () => {
411+
it("postive Test", async () => {
412+
await testCheckRemoteGitExist(true);
413+
// no exception thrown
414+
});
415+
// cannot do negative test because it will take too long
416+
// and timeout
417+
xit("negative Test", async () => {
418+
try {
419+
await testCheckRemoteGitExist(false);
420+
expect(true).toBe(false);
421+
} catch (e) {
422+
expect(e).toBeDefined();
423+
}
424+
});
425+
});
426+
427+
const testGitFetchPull = async (positive: boolean) => {
428+
const { safeLoggingUrl, sourcePath } = await getMockedDataForGitTests(
429+
positive
430+
);
431+
if (!positive || fs.existsSync(path.join(sourcePath, ".git"))) {
432+
await gitFetchPull(sourcePath, safeLoggingUrl);
433+
}
434+
};
435+
436+
describe("test gitFetchPull function", () => {
437+
it("postive Test", async () => {
438+
await testGitFetchPull(true);
439+
// no exception thrown
440+
});
441+
it("negative Test", async () => {
442+
try {
443+
await testGitFetchPull(false);
444+
expect(true).toBe(false);
445+
} catch (e) {
446+
expect(e).toBeDefined();
447+
}
448+
});
449+
});
450+
451+
const testGitCheckout = async (positive: boolean) => {
452+
const { sourcePath } = await getMockedDataForGitTests(positive);
453+
if (!positive || fs.existsSync(path.join(sourcePath, ".git"))) {
454+
await gitCheckout(sourcePath, "v0.0.1");
455+
}
456+
};
457+
458+
describe("test gitCheckout function", () => {
459+
it("postive Test", async () => {
460+
await testGitCheckout(true);
461+
// no exception thrown
462+
});
463+
it("negative Test", async () => {
464+
try {
465+
await testGitCheckout(false);
466+
expect(true).toBe(false);
467+
} catch (e) {
468+
expect(e).toBeDefined();
469+
}
470+
});
471+
});
472+
356473
describe("Validate remote git source", () => {
357474
test("Validating that a git source is cloned to .spk/templates", async () => {
475+
jest
476+
.spyOn(generate, "checkRemoteGitExist")
477+
.mockImplementationOnce(async () => {
478+
return;
479+
});
480+
jest.spyOn(generate, "gitFetchPull").mockImplementationOnce(async () => {
481+
return;
482+
});
483+
jest.spyOn(generate, "gitCheckout").mockImplementationOnce(async () => {
484+
return;
485+
});
486+
358487
const mockParentPath = "src/commands/infra/mocks/discovery-service";
359488
const mockProjectPath = "src/commands/infra/mocks/discovery-service/west";
360489
const sourceConfiguration = validateDefinition(

src/commands/infra/generate.ts

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,43 @@ Template: ${source.template} source: ${source.source} version: ${source.version}
174174
return source;
175175
};
176176

177+
export const checkRemoteGitExist = async (
178+
sourcePath: string,
179+
source: string,
180+
safeLoggingUrl: string
181+
) => {
182+
// Checking for git remote
183+
if (!fs.existsSync(sourcePath)) {
184+
throw new Error(`${sourcePath} does not exist`);
185+
}
186+
const result = await simpleGit(sourcePath).listRemote([source]);
187+
if (!result) {
188+
logger.error(result);
189+
throw new Error(
190+
`Unable to clone the source remote repository. \
191+
The remote repo may not exist or you do not have the rights to access it`
192+
);
193+
}
194+
195+
logger.info(`Remote source repo: ${safeLoggingUrl} exists.`);
196+
};
197+
198+
export const gitFetchPull = async (
199+
sourcePath: string,
200+
safeLoggingUrl: string
201+
) => {
202+
// Make sure we have the latest version of all releases cached locally
203+
await simpleGit(sourcePath).fetch("all");
204+
await simpleGit(sourcePath).pull("origin", "master");
205+
logger.info(`${safeLoggingUrl} already cloned. Performing 'git pull'...`);
206+
};
207+
208+
export const gitCheckout = async (sourcePath: string, version: string) => {
209+
// Checkout tagged version
210+
logger.info(`Checking out template version: ${version}`);
211+
await simpleGit(sourcePath).checkout(version);
212+
};
213+
177214
/**
178215
* Checks if provided source, template and version are valid. TODO/ Private Repo, PAT, ssh-key agent
179216
*
@@ -202,17 +239,8 @@ export const validateRemoteSource = async (
202239
`Source template folder found. Validating existence of repository.`
203240
);
204241
}
205-
// Checking for git remote
206-
const result = await simpleGit(sourcePath).listRemote([source]);
207-
if (!result) {
208-
logger.error(result);
209-
throw new Error(
210-
`Unable to clone the source remote repository. \
211-
The remote repo may not exist or you do not have the rights to access it`
212-
);
213-
}
214242

215-
logger.info(`Remote source repo: ${safeLoggingUrl} exists.`);
243+
await checkRemoteGitExist(sourcePath, source, safeLoggingUrl);
216244
logger.info(
217245
`Checking if source repo: ${safeLoggingUrl} has been already cloned to: ${sourcePath}.`
218246
);
@@ -221,17 +249,13 @@ The remote repo may not exist or you do not have the rights to access it`
221249
// Check if .git folder exists in ${sourcePath}, if not, then clone
222250
// if already cloned, 'git pull'
223251
if (fs.existsSync(path.join(sourcePath, ".git"))) {
224-
// Make sure we have the latest version of all releases cached locally
225-
await simpleGit(sourcePath).fetch("all");
226-
await simpleGit(sourcePath).pull("origin", "master");
227-
logger.info(`${safeLoggingUrl} already cloned. Performing 'git pull'...`);
252+
await gitFetchPull(sourcePath, safeLoggingUrl);
228253
} else {
229254
await gitClone(source, sourcePath);
230255
}
231256
// Checkout tagged version
232257
logger.info(`Checking out template version: ${version}`);
233-
234-
await simpleGit(sourcePath).checkout(version);
258+
await gitCheckout(sourcePath, version);
235259
} catch (err) {
236260
logger.error(err);
237261
// TOFIX: this error should be rethrown

0 commit comments

Comments
 (0)