Skip to content

Commit

Permalink
adding tests, cleaning up files
Browse files Browse the repository at this point in the history
  • Loading branch information
bryan-e-lopez committed Mar 15, 2024
1 parent 13ca9c6 commit 56a0d4d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 11 deletions.
74 changes: 65 additions & 9 deletions controller/integration/pewpew.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AuthPermission, AuthPermissions, ErrorResponse, PewPewVersionsResponse, TestManagerError } from "../types";
import type { File, Files } from "formidable";
import { LogLevel, log, logger } from "@fs/ppaas-common";
import { ParsedForm, createFormidableFile, unzipFile } from "../pages/api/util/util";
import { deletePewPew, getPewPewVersionsInS3, getPewpew, postPewPew } from "../pages/api/util/pewpew";
import { LogLevel, PpaasS3File, log, logger } from "@fs/ppaas-common";
import { PEWPEW_BINARY_FOLDER, ParsedForm, createFormidableFile, unzipFile } from "../pages/api/util/util";
import { VERSION_TAG_NAME, deletePewPew, getCurrentPewPewLatestVersion, getPewPewVersionsInS3, getPewpew, postPewPew } from "../pages/api/util/pewpew";
import { expect } from "chai";
import { latestPewPewVersion } from "../pages/api/util/clientutil";
import path from "path";
Expand All @@ -28,6 +28,7 @@ const pewpewZipFile: File = createFormidableFile(
);
let sharedPewPewVersions: string[] | undefined;
let uploadedPewPewVersion: string | undefined; // Used for delete
let currentPewPewLatestVersion: string | undefined;

describe("PewPew Util Integration", () => {
let files: Files = {};
Expand Down Expand Up @@ -75,7 +76,7 @@ describe("PewPew Util Integration", () => {
files
};
log("postPewPew parsedForm", LogLevel.DEBUG, parsedForm);
postPewPew(parsedForm, authAdmin).then((res: ErrorResponse) => {
postPewPew(parsedForm, authAdmin).then(async (res: ErrorResponse) => {
log("postPewPew res", LogLevel.DEBUG, res);
expect(res.status, JSON.stringify(res.json)).to.equal(200);
const body: TestManagerError = res.json;
Expand All @@ -97,6 +98,21 @@ describe("PewPew Util Integration", () => {
sharedPewPewVersions.push(version);
}
log("sharedPewPewVersions: " + sharedPewPewVersions, LogLevel.DEBUG);
try {
const pewpewFiles = await PpaasS3File.getAllFilesInS3({
s3Folder: `${PEWPEW_BINARY_FOLDER}/${version}`,
localDirectory: process.env.TEMP || "/tmp"
});
expect(pewpewFiles).to.not.equal(undefined);
expect(pewpewFiles.length).to.be.greaterThan(0);
const pewpewFile = pewpewFiles.find( (file) => file.filename === "pewpew");
expect(pewpewFile).to.not.equal(undefined);
expect(pewpewFile?.tags).to.not.equal(undefined);
expect(pewpewFile?.tags?.get(VERSION_TAG_NAME)).to.equal(version);
} catch (error) {
log("postPewPew error while checking latest tag: ", LogLevel.ERROR, error);
throw error;
}

Check warning on line 115 in controller/integration/pewpew.spec.ts

View workflow job for this annotation

GitHub Actions / Build project (18.x)

Trailing spaces not allowed

Check warning on line 115 in controller/integration/pewpew.spec.ts

View workflow job for this annotation

GitHub Actions / Build project (20.x)

Trailing spaces not allowed
done();
}).catch((error) => {
log("postPewPew error", LogLevel.ERROR, error);
Expand All @@ -112,7 +128,7 @@ describe("PewPew Util Integration", () => {
files
};
log("postPewPew parsedForm", LogLevel.DEBUG, parsedForm);
postPewPew(parsedForm, authAdmin).then((res: ErrorResponse) => {
postPewPew(parsedForm, authAdmin).then(async (res: ErrorResponse) => {
log("postPewPew res", LogLevel.DEBUG, res);
expect(res.status, JSON.stringify(res.json)).to.equal(200);
const body: TestManagerError = res.json;
Expand All @@ -121,14 +137,34 @@ describe("PewPew Util Integration", () => {
expect(body.message).to.not.equal(undefined);
expect(body.message).to.include("PewPew uploaded, version");
expect(body.message).to.include("as latest");
const version = latestPewPewVersion;
const match: RegExpMatchArray | null = body.message.match(/PewPew uploaded, version: (\d+\.\d+\.\d+)/);
log(`pewpew match: ${match}`, LogLevel.DEBUG, match);
expect(match, "pewpew match").to.not.equal(null);
expect(match!.length, "pewpew match.length").to.be.greaterThan(1);
const version: string = match![1];
// If this runs before the other acceptance tests populate the shared pewpew versions
if (!sharedPewPewVersions) {
sharedPewPewVersions = [version];
} else if (!sharedPewPewVersions.includes(version)) {
sharedPewPewVersions.push(version);
sharedPewPewVersions = [latestPewPewVersion];
} else if (!sharedPewPewVersions.includes(latestPewPewVersion)) {
sharedPewPewVersions.push(latestPewPewVersion);
}
log("sharedPewPewVersions: " + sharedPewPewVersions, LogLevel.DEBUG);
try {
const pewpewFiles = await PpaasS3File.getAllFilesInS3({
s3Folder: `${PEWPEW_BINARY_FOLDER}/${latestPewPewVersion}`,
localDirectory: process.env.TEMP || "/tmp"
});
expect(pewpewFiles).to.not.equal(undefined);
expect(pewpewFiles.length).to.be.greaterThan(0);
const pewpewFile = pewpewFiles.find( (file) => file.filename === "pewpew");
expect(pewpewFile).to.not.equal(undefined);
expect(pewpewFile?.tags).to.not.equal(undefined);
expect(pewpewFile?.tags?.get(VERSION_TAG_NAME)).to.equal(version);
} catch (error) {
log("postPewPew error while checking latest tag: ", LogLevel.ERROR, error);
throw error;
}
currentPewPewLatestVersion = version;
done();
}).catch((error) => {
log("postPewPew error", LogLevel.ERROR, error);
Expand All @@ -138,6 +174,20 @@ describe("PewPew Util Integration", () => {
});

describe("getPewpew", () => {
before(async () => {
try {
const pewpewFiles = await PpaasS3File.getAllFilesInS3({
s3Folder: `${PEWPEW_BINARY_FOLDER}/${latestPewPewVersion}`,
localDirectory: process.env.TEMP || "/tmp"
});
expect(pewpewFiles).to.not.equal(undefined);
const pewpewFile = pewpewFiles.find( (file) => file.filename === "pewpew");
expect(pewpewFile).to.not.equal(undefined);
} catch (error) {
log("getCurrentPewPewLatest Version failed: ", LogLevel.ERROR, error);
throw error;
}
});
it("getPewpew should respond 200 OK", (done: Mocha.Done) => {
expect(sharedPewPewVersions, "sharedPewPewVersions").to.not.equal(undefined);
getPewpew().then((res: ErrorResponse | PewPewVersionsResponse) => {
Expand All @@ -153,6 +203,12 @@ describe("PewPew Util Integration", () => {
done();
}).catch((error) => done(error));
});

it("getCurrentPewPewLatestVersion", async () => {
expect(currentPewPewLatestVersion, "currentPewPewLatestVersion").to.not.equal(undefined);
const result = await getCurrentPewPewLatestVersion();
expect(result, "result").to.equal(currentPewPewLatestVersion);
});
});

describe("deletePewPew", () => {
Expand Down
2 changes: 1 addition & 1 deletion controller/pages/api/util/pewpew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ logger.config.LogFileName = "ppaas-controller";
const deleteS3 = s3.deleteObject;
export const PEWPEW_EXECUTABLE_NAME: string = "pewpew";
const PEWPEW_EXECUTABLE_NAME_WINDOWS: string = "pewpew.exe";
const VERSION_TAG_NAME: string = "version";
export const VERSION_TAG_NAME: string = "version";

/**
* Queries S3 for all objects in the pewpew/ folder that end with pewpew (not pewpew.exe).
Expand Down
14 changes: 14 additions & 0 deletions controller/test/pewpew.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
} from "../pages/api/util/util";
import {
PEWPEW_EXECUTABLE_NAME,
VERSION_TAG_NAME,
deletePewPew,
getCurrentPewPewLatestVersion,
getPewPewVersionsInS3,
getPewpew,
postPewPew
Expand Down Expand Up @@ -106,6 +108,18 @@ describe("PewPew Util", () => {
resetMockS3();
});

describe("getCurrentPewPewLatestVersion", () => {
it("getCurrentPewPewLatestVersion should return version with latest tag from S3", (done: Mocha.Done) => {
const expected = "0.5.13";
mockGetObjectTagging(new Map([[VERSION_TAG_NAME, expected]]));
getCurrentPewPewLatestVersion().then((result: string | undefined) => {
log("getPewPewVersionsInS3()", LogLevel.DEBUG, result);
expect(result).to.equal(expected);
done();
}).catch((error) => done(error));
});
});

describe("getPewPewVersionsInS3", () => {
it("getPewPewVersionsInS3() should return array with elements", (done: Mocha.Done) => {
mockListObjects(versions.map((version): S3Object => ({ ...s3Object, Key: `${pewpewS3Folder}/${version}/${pewpewFilename}` })));
Expand Down
3 changes: 2 additions & 1 deletion controller/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"]
"**/*.tsx"
]
}

0 comments on commit 56a0d4d

Please sign in to comment.