-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core-manager): implement
snapshots.delete
action (#3724)
- Loading branch information
1 parent
82382fc
commit f87e146
Showing
3 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
__tests__/unit/core-manager/actions/snapshots-delete.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import "jest-extended"; | ||
|
||
import { Container } from "@packages/core-kernel"; | ||
import { Action } from "@packages/core-manager/src/actions/snaphsots-delete"; | ||
import { Sandbox } from "@packages/core-test-framework"; | ||
|
||
let sandbox: Sandbox; | ||
let action: Action; | ||
|
||
let mockFilesystem; | ||
|
||
beforeEach(() => { | ||
mockFilesystem = { | ||
exists: jest.fn().mockResolvedValue(true), | ||
delete: jest.fn().mockResolvedValue(true), | ||
}; | ||
|
||
sandbox = new Sandbox(); | ||
|
||
sandbox.app.bind(Container.Identifiers.FilesystemService).toConstantValue(mockFilesystem); | ||
|
||
action = sandbox.app.resolve(Action); | ||
}); | ||
|
||
describe("Snapshots:Delete", () => { | ||
it("should have name", () => { | ||
expect(action.name).toEqual("snapshots.delete"); | ||
}); | ||
|
||
it("should delete snapshot", async () => { | ||
const result = await action.execute({ name: "1-10" }); | ||
|
||
expect(result).toEqual({}); | ||
}); | ||
|
||
it("should throw error if snapshot is not found", async () => { | ||
mockFilesystem.exists = jest.fn().mockResolvedValue(false); | ||
|
||
await expect(action.execute({ name: "1-10" })).rejects.toThrow("Snapshot not found"); | ||
}); | ||
|
||
it("should throw error if snapshot is not deleted", async () => { | ||
mockFilesystem.delete = jest.fn().mockResolvedValue(false); | ||
|
||
await expect(action.execute({ name: "1-10" })).rejects.toThrow("Cannot delete snapshot"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Container, Contracts } from "@arkecosystem/core-kernel"; | ||
import { join } from "path"; | ||
|
||
import { Actions } from "../contracts"; | ||
|
||
@Container.injectable() | ||
export class Action implements Actions.Action { | ||
public name = "snapshots.delete"; | ||
|
||
public schema = { | ||
type: "object", | ||
properties: { | ||
name: { | ||
type: "string", | ||
}, | ||
}, | ||
required: ["name"], | ||
}; | ||
|
||
@Container.inject(Container.Identifiers.FilesystemService) | ||
private readonly filesystem!: Contracts.Kernel.Filesystem; | ||
|
||
public async execute(params: { name: string }): Promise<any> { | ||
await this.deleteSnapshot(params.name); | ||
|
||
return {}; | ||
} | ||
|
||
public async deleteSnapshot(name: string): Promise<void> { | ||
const snapshotsDir = `${process.env.CORE_PATH_DATA}/snapshots/`; | ||
const snapshotPath = join(snapshotsDir, name); | ||
|
||
if (!(await this.filesystem.exists(snapshotPath))) { | ||
throw new Error("Snapshot not found"); | ||
} | ||
|
||
if (!(await this.filesystem.delete(snapshotPath))) { | ||
throw new Error("Cannot delete snapshot"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters