Skip to content

Commit

Permalink
chore: Reduce lint exceptions in ctl (#37643)
Browse files Browse the repository at this point in the history
Fix linting exceptions in `ctl`.


## Automation

/test sanity

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!WARNING]
> Tests have not run on the HEAD
1f2242a yet
> <hr>Fri, 22 Nov 2024 10:06:23 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [x] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

## Release Notes

- **New Features**
- Enhanced backup and restore processes with improved user prompts and
error handling.
- Added support for optional command-line flags during database imports.

- **Bug Fixes**
- Improved error handling for various operations, including database
exports and imports.
	- Enhanced logging for backup errors to provide more context.

- **Documentation**
- Updated user prompts and error messages for clarity during backup and
restore operations.

- **Tests**
- Expanded test coverage for backup functionalities and utility
functions to ensure robust error handling and output validation.

- **Chores**
	- Updated dependencies to enhance TypeScript development experience.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
sharat87 authored Nov 22, 2024
1 parent d87f7cc commit 0685d33
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 25 deletions.
1 change: 1 addition & 0 deletions app/client/packages/rts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"devDependencies": {
"@types/express": "^4.17.14",
"@types/jest": "^29.2.3",
"@types/node": "*",
"@types/nodemailer": "^6.4.17",
"@types/readline-sync": "^1.4.8",
"jest": "^29.3.1",
Expand Down
14 changes: 2 additions & 12 deletions app/client/packages/rts/src/ctl/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
{
"extends": ["../../../../.eslintrc.base.json"],
"extends": ["../../.eslintrc.json"],
"rules": {
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/prefer-nullish-coalescing": "off",
"@typescript-eslint/strict-boolean-expressions": "off",
"@typescript-eslint/no-explicit-any": "off",
"testing-library/no-debugging-utils": "off",
"@typescript-eslint/no-var-requires": "off",
"padding-line-between-statements": "off",
"no-console": "off",
"@typescript-eslint/promise-function-async": "off",
"@typescript-eslint/no-unused-vars": "off",
"sort-destructure-keys/sort-destructure-keys": "off"
"no-console": "off"
}
}
29 changes: 28 additions & 1 deletion app/client/packages/rts/src/ctl/backup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ jest.mock("./utils", () => ({
import * as backup from "./backup";
import * as Constants from "./constants";
import os from "os";
// @ts-ignore
import fsPromises from "fs/promises";
import * as utils from "./utils";
import readlineSync from "readline-sync";
Expand All @@ -21,16 +20,19 @@ describe("Backup Tests", () => {

test("Available Space in /appsmith-stacks volume in Bytes", async () => {
const res = expect(await backup.getAvailableBackupSpaceInBytes("/"));

res.toBeGreaterThan(1024 * 1024);
});

it("Check the constant is 2 GB", () => {
const size = 2 * 1024 * 1024 * 1024;

expect(Constants.MIN_REQUIRED_DISK_SPACE_IN_BYTES).toBe(size);
});

it("Should throw Error when the available size is below MIN_REQUIRED_DISK_SPACE_IN_BYTES", () => {
const size = Constants.MIN_REQUIRED_DISK_SPACE_IN_BYTES - 1;

expect(() => backup.checkAvailableBackupSpace(size)).toThrow();
});

Expand All @@ -48,12 +50,14 @@ describe("Backup Tests", () => {
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";

expect(backup.getBackupContentsPath(root, timestamp)).toBe(
"/rootDir/appsmith-backup-0000-00-0T00-00-00.00Z",
);
Expand All @@ -65,6 +69,7 @@ describe("Backup Tests", () => {
const cmd =
"mongodump --uri=mongodb://username:password@host/appsmith --archive=/dest/mongodb-data.gz --gzip";
const res = await backup.executeMongoDumpCMD(dest, appsmithMongoURI);

expect(res).toBe(cmd);
console.log(res);
});
Expand All @@ -86,6 +91,7 @@ describe("Backup Tests", () => {
const dest = "/destdir";
const cmd = "ln -s /appsmith-stacks/git-storage /destdir/git-storage";
const res = await backup.executeCopyCMD(gitRoot, dest);

expect(res).toBe(cmd);
console.log(res);
});
Expand All @@ -99,6 +105,7 @@ describe("Backup Tests", () => {
}
});
const res = await utils.getCurrentAppsmithVersion();

expect(res).toBe("v0.0.0-SNAPSHOT");
});

Expand Down Expand Up @@ -130,70 +137,83 @@ 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));
const backupFiles = ["file1", "file2", "file3", "file4", "file5"];
const expectedBackupFiles = ["file2", "file3", "file4", "file5"];
const res = await backup.removeOldBackups(backupFiles, backupArchivesLimit);

console.log(res);

expect(res).toEqual(expectedBackupFiles);
});

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

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);

console.log(res);

expect(res).toEqual(expectedBackupFiles);
});

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));
const backupFiles = ["file1", "file2", "file3", "file4"];
const expectedBackupFiles = ["file1", "file2", "file3", "file4"];
const res = await backup.removeOldBackups(backupFiles, backupArchivesLimit);

console.log(res);

expect(res).toEqual(expectedBackupFiles);
});

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));
const backupFiles = ["file1", "file2"];
const expectedBackupFiles = ["file1", "file2"];
const res = await backup.removeOldBackups(backupFiles, backupArchivesLimit);

console.log(res);

expect(res).toEqual(expectedBackupFiles);
});

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));
const backupFiles = ["file1"];
const expectedBackupFiles = ["file1"];
const res = await backup.removeOldBackups(backupFiles, backupArchivesLimit);

console.log(res);
expect(res).toEqual(expectedBackupFiles);
});

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

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

console.log(res);
expect(res).toEqual(expectedBackupFiles);
});

test("Test get encryption password from user prompt when both passwords are the same", async () => {
const password = "password#4321";

readlineSync.question = jest.fn().mockImplementation(() => password);
const password_res = backup.getEncryptionPasswordFromUser();

Expand All @@ -202,10 +222,12 @@ describe("Backup Tests", () => {

test("Test get encryption password from user prompt when both passwords are the different", async () => {
const password = "password#4321";

readlineSync.question = jest.fn().mockImplementation((a) => {
if (a == "Enter the above password again: ") {
return "pass";
}

return password;
});
const password_res = backup.getEncryptionPasswordFromUser();
Expand Down Expand Up @@ -233,6 +255,7 @@ describe("Backup Tests", () => {
archivePath,
encryptionPassword,
);

console.log(res);
expect(res).toEqual("/rootDir/appsmith-backup-0000-00-0T00-00-00.00Z.enc");
});
Expand All @@ -243,6 +266,7 @@ test("Get DB name from Mongo URI 1", async () => {
"mongodb+srv://admin:password@test.cluster.mongodb.net/my_db_name?retryWrites=true&minPoolSize=1&maxPoolSize=10&maxIdleTimeMS=900000&authSource=admin";
const expectedDBName = "my_db_name";
const dbName = utils.getDatabaseNameFromMongoURI(mongodb_uri);

expect(dbName).toEqual(expectedDBName);
});

Expand All @@ -251,6 +275,7 @@ test("Get DB name from Mongo URI 2", async () => {
"mongodb+srv://admin:password@test.cluster.mongodb.net/test123?retryWrites=true&minPoolSize=1&maxPoolSize=10&maxIdleTimeMS=900000&authSource=admin";
const expectedDBName = "test123";
const dbName = utils.getDatabaseNameFromMongoURI(mongodb_uri);

expect(dbName).toEqual(expectedDBName);
});

Expand All @@ -259,12 +284,14 @@ test("Get DB name from Mongo URI 3", async () => {
"mongodb+srv://admin:password@test.cluster.mongodb.net/test123";
const expectedDBName = "test123";
const dbName = utils.getDatabaseNameFromMongoURI(mongodb_uri);

expect(dbName).toEqual(expectedDBName);
});

test("Get DB name from Mongo URI 4", async () => {
const mongodb_uri = "mongodb://appsmith:pAssW0rd!@localhost:27017/appsmith";
const expectedDBName = "appsmith";
const dbName = utils.getDatabaseNameFromMongoURI(mongodb_uri);

expect(dbName).toEqual(expectedDBName);
});
Loading

0 comments on commit 0685d33

Please sign in to comment.