From 56a0d4d134a84ddb7bcba28c0f547c5b422e845c Mon Sep 17 00:00:00 2001 From: Bryan Lopez Date: Fri, 15 Mar 2024 17:29:21 -0400 Subject: [PATCH] adding tests, cleaning up files --- controller/integration/pewpew.spec.ts | 74 +++++++++++++++++++++++---- controller/pages/api/util/pewpew.ts | 2 +- controller/test/pewpew.spec.ts | 14 +++++ controller/tsconfig.json | 3 +- 4 files changed, 82 insertions(+), 11 deletions(-) diff --git a/controller/integration/pewpew.spec.ts b/controller/integration/pewpew.spec.ts index 75b85d64..01629553 100644 --- a/controller/integration/pewpew.spec.ts +++ b/controller/integration/pewpew.spec.ts @@ -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"; @@ -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 = {}; @@ -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; @@ -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; + } done(); }).catch((error) => { log("postPewPew error", LogLevel.ERROR, error); @@ -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; @@ -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); @@ -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) => { @@ -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", () => { diff --git a/controller/pages/api/util/pewpew.ts b/controller/pages/api/util/pewpew.ts index d36d3cfb..52b73db4 100644 --- a/controller/pages/api/util/pewpew.ts +++ b/controller/pages/api/util/pewpew.ts @@ -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). diff --git a/controller/test/pewpew.spec.ts b/controller/test/pewpew.spec.ts index 38a1d8a2..6b33b52f 100644 --- a/controller/test/pewpew.spec.ts +++ b/controller/test/pewpew.spec.ts @@ -15,7 +15,9 @@ import { } from "../pages/api/util/util"; import { PEWPEW_EXECUTABLE_NAME, + VERSION_TAG_NAME, deletePewPew, + getCurrentPewPewLatestVersion, getPewPewVersionsInS3, getPewpew, postPewPew @@ -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}` }))); diff --git a/controller/tsconfig.json b/controller/tsconfig.json index d9fbba37..9ceb92ae 100644 --- a/controller/tsconfig.json +++ b/controller/tsconfig.json @@ -31,5 +31,6 @@ "include": [ "next-env.d.ts", "**/*.ts", - "**/*.tsx"] + "**/*.tsx" + ] }