Skip to content
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
25 changes: 25 additions & 0 deletions app/client/packages/rts/src/ctl/backup/BackupState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getTimeStampInISO } from "./index";

export class BackupState {
readonly args: string[];
readonly initAt: string = getTimeStampInISO();
readonly errors: string[] = [];

backupRootPath: string = "";
archivePath: string = "";

encryptionPassword: string = "";

constructor(args: string[]) {
this.args = args;

// We seal `this` so that no link in the chain can "add" new properties to the state. This is intentional. If any
// link wants to save data in the `BackupState`, which shouldn't even be needed in most cases, it should do so by
// explicitly declaring a property in this class. No surprises.
Object.seal(this);
}

isEncryptionEnabled() {
return !!this.encryptionPassword;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
jest.mock("./utils", () => ({
...jest.requireActual("./utils"),
execCommand: jest.fn().mockImplementation(async (a) => a.join(" ")),
}));

import * as backup from "./backup";
import * as Constants from "./constants";
import os from "os";
import fsPromises from "fs/promises";
import * as utils from "./utils";
import * as backup from ".";
import * as Constants from "../constants";
import * as utils from "../utils";
import readlineSync from "readline-sync";

jest.mock("../utils", () => ({
...jest.requireActual("../utils"),
execCommand: jest.fn().mockImplementation(async (a) => a.join(" ")),
}));

describe("Backup Tests", () => {
test("Timestamp string in ISO format", () => {
console.log(backup.getTimeStampInISO());
Expand Down Expand Up @@ -46,14 +45,6 @@ describe("Backup Tests", () => {
);
});

it("Generates t", async () => {
os.tmpdir = jest.fn().mockReturnValue("temp/dir");
fsPromises.mkdtemp = jest.fn().mockImplementation((a) => a);
const res = await backup.generateBackupRootPath();

expect(res).toBe("temp/dir/appsmithctl-backup-");
});

test("Test backup contents path generation", () => {
const root = "/rootDir";
const timestamp = "0000-00-0T00-00-00.00Z";
Expand Down Expand Up @@ -136,67 +127,60 @@ describe("Backup Tests", () => {
});

test("Cleanup Backups when limit is 4 and there are 5 files", async () => {
const backupArchivesLimit = 4;

fsPromises.rm = jest.fn().mockImplementation(async (a) => console.log(a));
fsPromises.rm = jest.fn().mockImplementation();
const backupFiles = ["file1", "file2", "file3", "file4", "file5"];
const expectedBackupFiles = ["file2", "file3", "file4", "file5"];
const res = await backup.removeOldBackups(backupFiles, backupArchivesLimit);

console.log(res);
await backup.removeOldBackups(backupFiles, 4);

expect(res).toEqual(expectedBackupFiles);
expect(fsPromises.rm).toHaveBeenCalledTimes(1);
expect(fsPromises.rm).toHaveBeenCalledWith(
Constants.BACKUP_PATH + "/file1",
);
});

test("Cleanup Backups when limit is 2 and there are 5 files", async () => {
const backupArchivesLimit = 2;
fsPromises.rm = jest.fn().mockImplementation();
const backupFiles = ["file1", "file4", "file3", "file2", "file5"];

fsPromises.rm = jest.fn().mockImplementation(async (a) => console.log(a));
const backupFiles = ["file1", "file2", "file3", "file4", "file5"];
const expectedBackupFiles = ["file4", "file5"];
const res = await backup.removeOldBackups(backupFiles, backupArchivesLimit);
await backup.removeOldBackups(backupFiles, 2);

console.log(res);

expect(res).toEqual(expectedBackupFiles);
expect(fsPromises.rm).toHaveBeenCalledTimes(3);
expect(fsPromises.rm).toHaveBeenCalledWith(
Constants.BACKUP_PATH + "/file1",
);
expect(fsPromises.rm).toHaveBeenCalledWith(
Constants.BACKUP_PATH + "/file2",
);
expect(fsPromises.rm).toHaveBeenCalledWith(
Constants.BACKUP_PATH + "/file3",
);
});

test("Cleanup Backups when limit is 4 and there are 4 files", async () => {
const backupArchivesLimit = 4;

fsPromises.rm = jest.fn().mockImplementation(async (a) => console.log(a));
fsPromises.rm = jest.fn().mockImplementation();
const backupFiles = ["file1", "file2", "file3", "file4"];
const expectedBackupFiles = ["file1", "file2", "file3", "file4"];
const res = await backup.removeOldBackups(backupFiles, backupArchivesLimit);

console.log(res);
await backup.removeOldBackups(backupFiles, 4);

expect(res).toEqual(expectedBackupFiles);
expect(fsPromises.rm).not.toHaveBeenCalled();
});

test("Cleanup Backups when limit is 4 and there are 2 files", async () => {
const backupArchivesLimit = 4;

fsPromises.rm = jest.fn().mockImplementation(async (a) => console.log(a));
fsPromises.rm = jest.fn().mockImplementation();
const backupFiles = ["file1", "file2"];
const expectedBackupFiles = ["file1", "file2"];
const res = await backup.removeOldBackups(backupFiles, backupArchivesLimit);

console.log(res);
await backup.removeOldBackups(backupFiles, 4);

expect(res).toEqual(expectedBackupFiles);
expect(fsPromises.rm).not.toHaveBeenCalled();
});

test("Cleanup Backups when limit is 2 and there is 1 file", async () => {
const backupArchivesLimit = 4;

fsPromises.rm = jest.fn().mockImplementation(async (a) => console.log(a));
fsPromises.rm = jest.fn().mockImplementation();
const backupFiles = ["file1"];
const expectedBackupFiles = ["file1"];
const res = await backup.removeOldBackups(backupFiles, backupArchivesLimit);

console.log(res);
expect(res).toEqual(expectedBackupFiles);
await backup.removeOldBackups(backupFiles, 4);

expect(fsPromises.rm).not.toHaveBeenCalled();
});

test("Cleanup Backups when limit is 2 and there is no file", async () => {
Expand Down
Loading