Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer command #600

Merged
merged 3 commits into from
Sep 30, 2023
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
12 changes: 11 additions & 1 deletion src/task/batch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DownloadFile, Run, UploadData, UploadFile } from "../script";
import { DownloadFile, Run, Transfer, UploadData, UploadFile } from "../script";
import { Batch } from "./batch";
import { NullStorageProvider } from "../storage";
import { ActivityMock } from "../../tests/mock/activity.mock";
Expand Down Expand Up @@ -28,6 +28,16 @@ describe("Batch", () => {
});
});

describe("transfer()", () => {
it("should add transfer file command", async () => {
expect(batch.transfer("http://golem.network/test.txt", "/golem/file.txt")).toBe(batch);
const cmd = batch["script"]["commands"][0] as Transfer;
expect(cmd).toBeInstanceOf(Transfer);
expect(cmd["from"]).toBe("http://golem.network/test.txt");
expect(cmd["to"]).toBe("/golem/file.txt");
});
});

describe("uploadFile()", () => {
it("should add upload file command", async () => {
expect(batch.uploadFile("/tmp/file.txt", "/golem/file.txt")).toBe(batch);
Expand Down
7 changes: 6 additions & 1 deletion src/task/batch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DownloadFile, Run, Script, UploadFile } from "../script";
import { DownloadFile, Run, Script, Transfer, UploadFile } from "../script";
import { Activity, Result } from "../activity";
import { StorageProvider } from "../storage/provider";
import { Logger, sleep } from "../utils";
Expand Down Expand Up @@ -44,6 +44,11 @@ export class Batch {
return this;
}

transfer(from: string, to: string): Batch {
this.script.add(new Transfer(from, to));
return this;
}

uploadFile(src: string, dst: string): Batch {
this.script.add(new UploadFile(this.storageProvider, src, dst));
return this;
Expand Down
30 changes: 28 additions & 2 deletions src/task/work.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Batch, WorkContext } from "./index";
import { LoggerMock, YagnaMock } from "../../tests/mock";
import { ActivityStateEnum, ResultState } from "../activity";
import { DownloadData, DownloadFile, Run, Script, UploadData, UploadFile } from "../script";
import { DownloadData, DownloadFile, Run, Script, Transfer, UploadData, UploadFile } from "../script";
import { ActivityMock } from "../../tests/mock/activity.mock";

/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down Expand Up @@ -46,6 +46,19 @@ describe("Work Context", () => {
});
});

describe("transfer()", () => {
it("should execute transfer command", async () => {
const result = ActivityMock.createResult({ stdout: "Ok" });
runSpy.mockImplementation((cmd) => {
expect(cmd).toBeInstanceOf(Transfer);
expect(cmd["from"]).toBe("http://golem.network/test.txt");
expect(cmd["to"]).toBe("/golem/work/test.txt");
return Promise.resolve(result);
});
expect(await context.transfer("http://golem.network/test.txt", "/golem/work/test.txt")).toBe(result);
});
});

describe("uploadFile()", () => {
it("should execute upload file command", async () => {
const result = ActivityMock.createResult();
Expand Down Expand Up @@ -177,7 +190,7 @@ describe("Work Context", () => {

describe("getWebsocketUri()", () => {
it("should throw error if there is no network node", () => {
expect(context["networkNode"]).toBeUndefined();
expect(() => context.getIp()).toThrow(new Error("There is no network in this work context"));
});

it("should return websocket URI", () => {
Expand All @@ -190,6 +203,19 @@ describe("Work Context", () => {
});
});

describe("getIp()", () => {
it("should throw error if there is no network node", () => {
expect(() => context.getIp()).toThrow(new Error("There is no network in this work context"));
});

it("should return ip address of provider vpn network node", () => {
(context as any)["networkNode"] = {
ip: "192.168.0.2",
};
expect(context.getIp()).toEqual("192.168.0.2");
});
});

describe("beginBatch()", () => {
it("should create a batch object", () => {
const o = {};
Expand Down
19 changes: 18 additions & 1 deletion src/task/work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Run,
Script,
Start,
Transfer,
UploadData,
UploadFile,
} from "../script";
Expand Down Expand Up @@ -134,6 +135,17 @@ export class WorkContext {
return this.runOneCommand(run, runOptions);
}

/**
* Generic transfer command, requires the user to provide a publicly readable transfer source
*
* @param from - publicly available resource for reading. Supported protocols: file, http, ftp or gftp
* @param to - file path
* @param options Additional run options.
*/
async transfer(from: string, to: string, options?: CommandOptions): Promise<Result> {
return this.runOneCommand(new Transfer(from, to), options);
}

async uploadFile(src: string, dst: string, options?: CommandOptions): Promise<Result> {
return this.runOneCommand(new UploadFile(this.storageProvider, src, dst), options);
}
Expand Down Expand Up @@ -185,7 +197,12 @@ export class WorkContext {

getWebsocketUri(port: number): string {
if (!this.networkNode) throw new Error("There is no network in this work context");
return this.networkNode?.getWebsocketUri(port);
return this.networkNode.getWebsocketUri(port);
}

getIp(): string {
if (!this.networkNode) throw new Error("There is no network in this work context");
return this.networkNode.ip.toString();
}

async getState(): Promise<ActivityStateEnum> {
Expand Down
27 changes: 27 additions & 0 deletions tests/e2e/tasks.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LoggerMock } from "../mock";
import { readFileSync } from "fs";
import { TaskExecutor } from "../../src";
import fs from "fs";
const logger = new LoggerMock(false);

describe("Task Executor", function () {
Expand Down Expand Up @@ -153,4 +154,30 @@ describe("Task Executor", function () {
expect(result).toEqual("Ok");
expect(readFileSync(`${process.env.GOTH_GFTP_VOLUME || ""}new_test.json`, "utf-8")).toEqual('{"test":"1234"}');
});

it("should run transfer file via http", async () => {
executor = await TaskExecutor.create({
package: "golem/alpine:latest",
logger,
});
const result = await executor.run(async (ctx) => {
const res = await ctx.transfer(
"http://registry.golem.network/download/a2bb9119476179fac36149723c3ad4474d8d135e8d2d2308eb79907a6fc74dfa",
"/golem/work/alpine.gvmi",
);
return res.result;
});
expect(result).toEqual("Ok");
});

it("should get ip address", async () => {
executor = await TaskExecutor.create({
package: "golem/alpine:latest",
capabilities: ["vpn"],
networkIp: "192.168.0.0/24",
logger,
});
const result = await executor.run(async (ctx) => ctx.getIp());
expect(["192.168.0.2", "192.168.0.3"]).toContain(result);
});
});
Loading