Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions src/commands/infra/generate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fs from "fs";
import path from "path";
import { loadConfigurationFromLocalEnv, readYaml } from "../../config";
import { safeGitUrlForLogging } from "../../lib/gitutils";
import { removeDir } from "../../lib/ioUtil";
import {
disableVerboseLogging,
Expand All @@ -8,17 +10,28 @@ import {
} from "../../logger";
import { IInfraConfigYaml } from "../../types";
import {
checkRemoteGitExist,
createGenerated,
DefinitionYAMLExistence,
dirIteration,
execute,
fetchValues,
generateConfig,
generateTfvars,
gitCheckout,
gitFetchPull,
validateDefinition,
validateRemoteSource,
validateTemplateSources
} from "./generate";
import * as generate from "./generate";
import * as infraCommon from "./infra_common";

interface IGitTestData {
source: string;
sourcePath: string;
safeLoggingUrl: string;
}

beforeAll(() => {
enableVerboseLogging();
Expand Down Expand Up @@ -353,8 +366,124 @@ describe("Validate sources in definition.yaml files", () => {
});
});

const getMockedDataForGitTests = async (
positive: boolean
): Promise<IGitTestData> => {
const mockParentPath = "src/commands/infra/mocks/discovery-service";
const mockProjectPath = "src/commands/infra/mocks/discovery-service/west";
const sourceConfiguration = validateDefinition(
mockParentPath,
mockProjectPath
);
const sourceConfig = validateTemplateSources(
sourceConfiguration,
mockParentPath,
mockProjectPath
);
let source = sourceConfig.source!;
if (!positive) {
source += "dummy";
}

// Converting source name to storable folder name
const sourceFolder = await infraCommon.repoCloneRegex(source);
const sourcePath = path.join(infraCommon.spkTemplatesPath, sourceFolder);
const safeLoggingUrl = safeGitUrlForLogging(source);

return {
safeLoggingUrl,
source,
sourcePath
};
};

const testCheckRemoteGitExist = async (positive: boolean) => {
const { safeLoggingUrl, source, sourcePath } = await getMockedDataForGitTests(
positive
);
if (!fs.existsSync(sourcePath)) {
createGenerated(sourcePath);
}
await checkRemoteGitExist(sourcePath, source, safeLoggingUrl);
};

describe("test checkRemoteGitExist function", () => {
it("postive Test", async () => {
await testCheckRemoteGitExist(true);
// no exception thrown
});
// cannot do negative test because it will take too long
// and timeout
xit("negative Test", async () => {
try {
await testCheckRemoteGitExist(false);
expect(true).toBe(false);
} catch (e) {
expect(e).toBeDefined();
}
});
});

const testGitFetchPull = async (positive: boolean) => {
const { safeLoggingUrl, sourcePath } = await getMockedDataForGitTests(
positive
);
if (!positive || fs.existsSync(path.join(sourcePath, ".git"))) {
await gitFetchPull(sourcePath, safeLoggingUrl);
}
};

describe("test gitFetchPull function", () => {
it("postive Test", async () => {
await testGitFetchPull(true);
// no exception thrown
});
it("negative Test", async () => {
try {
await testGitFetchPull(false);
expect(true).toBe(false);
} catch (e) {
expect(e).toBeDefined();
}
});
});

const testGitCheckout = async (positive: boolean) => {
const { sourcePath } = await getMockedDataForGitTests(positive);
if (!positive || fs.existsSync(path.join(sourcePath, ".git"))) {
await gitCheckout(sourcePath, "v0.0.1");
}
};

describe("test gitCheckout function", () => {
it("postive Test", async () => {
await testGitCheckout(true);
// no exception thrown
});
it("negative Test", async () => {
try {
await testGitCheckout(false);
expect(true).toBe(false);
} catch (e) {
expect(e).toBeDefined();
}
});
});

describe("Validate remote git source", () => {
test("Validating that a git source is cloned to .spk/templates", async () => {
jest
.spyOn(generate, "checkRemoteGitExist")
.mockImplementationOnce(async () => {
return;
});
jest.spyOn(generate, "gitFetchPull").mockImplementationOnce(async () => {
return;
});
jest.spyOn(generate, "gitCheckout").mockImplementationOnce(async () => {
return;
});

const mockParentPath = "src/commands/infra/mocks/discovery-service";
const mockProjectPath = "src/commands/infra/mocks/discovery-service/west";
const sourceConfiguration = validateDefinition(
Expand Down
56 changes: 40 additions & 16 deletions src/commands/infra/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,43 @@ Template: ${source.template} source: ${source.source} version: ${source.version}
return source;
};

export const checkRemoteGitExist = async (
sourcePath: string,
source: string,
safeLoggingUrl: string
) => {
// Checking for git remote
if (!fs.existsSync(sourcePath)) {
throw new Error(`${sourcePath} does not exist`);
}
const result = await simpleGit(sourcePath).listRemote([source]);
if (!result) {
logger.error(result);
throw new Error(
`Unable to clone the source remote repository. \
The remote repo may not exist or you do not have the rights to access it`
);
}

logger.info(`Remote source repo: ${safeLoggingUrl} exists.`);
};

export const gitFetchPull = async (
sourcePath: string,
safeLoggingUrl: string
) => {
// Make sure we have the latest version of all releases cached locally
await simpleGit(sourcePath).fetch("all");
await simpleGit(sourcePath).pull("origin", "master");
logger.info(`${safeLoggingUrl} already cloned. Performing 'git pull'...`);
};

export const gitCheckout = async (sourcePath: string, version: string) => {
// Checkout tagged version
logger.info(`Checking out template version: ${version}`);
await simpleGit(sourcePath).checkout(version);
};

/**
* Checks if provided source, template and version are valid. TODO/ Private Repo, PAT, ssh-key agent
*
Expand Down Expand Up @@ -202,17 +239,8 @@ export const validateRemoteSource = async (
`Source template folder found. Validating existence of repository.`
);
}
// Checking for git remote
const result = await simpleGit(sourcePath).listRemote([source]);
if (!result) {
logger.error(result);
throw new Error(
`Unable to clone the source remote repository. \
The remote repo may not exist or you do not have the rights to access it`
);
}

logger.info(`Remote source repo: ${safeLoggingUrl} exists.`);
await checkRemoteGitExist(sourcePath, source, safeLoggingUrl);
logger.info(
`Checking if source repo: ${safeLoggingUrl} has been already cloned to: ${sourcePath}.`
);
Expand All @@ -221,17 +249,13 @@ The remote repo may not exist or you do not have the rights to access it`
// Check if .git folder exists in ${sourcePath}, if not, then clone
// if already cloned, 'git pull'
if (fs.existsSync(path.join(sourcePath, ".git"))) {
// Make sure we have the latest version of all releases cached locally
await simpleGit(sourcePath).fetch("all");
await simpleGit(sourcePath).pull("origin", "master");
logger.info(`${safeLoggingUrl} already cloned. Performing 'git pull'...`);
await gitFetchPull(sourcePath, safeLoggingUrl);
} else {
await gitClone(source, sourcePath);
}
// Checkout tagged version
logger.info(`Checking out template version: ${version}`);

await simpleGit(sourcePath).checkout(version);
await gitCheckout(sourcePath, version);
} catch (err) {
logger.error(err);
// TOFIX: this error should be rethrown
Expand Down