@webda/gcp
Version:
Webda GCP Services implementation
194 lines • 7.21 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Storage as GCS } from "@google-cloud/storage";
import { suite, test } from "@testdeck/mocha";
import { getCommonJS } from "@webda/core";
import { BinaryTest } from "@webda/core/lib/services/binary.spec.js";
import * as assert from "assert";
import * as path from "path";
import * as sinon from "sinon";
const { __dirname } = getCommonJS(import.meta.url);
class MockFile {
constructor(path) { }
async delete() { }
async getFiles() { }
async exists() { }
async save() { }
async setMetadata() { }
async createWriteStream() { }
async getSignedUrl() { }
async publicUrl() { }
async getMetadata() { }
async createReadStream() { }
async move() { }
}
class MockBucket {
file(path) {
return new MockFile(path);
}
}
const BUCKET = "webda-dev";
let StorageTest = class StorageTest extends BinaryTest {
async before() {
this.buildWebda();
await this.install();
await super.before();
this.prefix = this.getBinary().getWebda().getUuid();
this.getBinary().getParameters().prefix = this.prefix;
// Prepare for Mocked Version
if (!process.env.NO_MOCK && false) {
sinon.stub(this.getBinary(), "getStorageBucket").callsFake(() => {
return new MockBucket();
});
}
}
async after() {
await this.cleanData();
}
async install() {
var storage = new GCS();
try {
let [metadata] = await storage.bucket(BUCKET).getMetadata();
if (!metadata) {
await storage.createBucket(BUCKET, {
location: "US-WEST2",
storageClass: "COLDLINE"
});
await storage.bucket(BUCKET).getMetadata();
}
//this.webda.log("INFO", `Google cloud storage bucket '${BUCKET}' exists`);
}
catch (e) {
this.webda.log("ERROR", `Google cloud storage error (${e.message})`);
}
}
async cleanData() {
var storage = new GCS();
const [files] = await storage.bucket(BUCKET).getFiles({
prefix: this.prefix
});
for (let file of files) {
await file.delete();
}
}
// Override getNotFound as exception is raised after
// @test
// async getNotFound() {}
async putObject() {
const body = `RAW Body: ${new Date()}`;
const name = `${this.prefix}/plop/test`;
await this.getBinary().putObject(name, Buffer.from(body), { meta1: "meta1" }, BUCKET);
let getFile = this.getBinary().getStorageBucket().file(name);
assert.deepStrictEqual((await getFile.getMetadata())[0].metadata, {
meta1: "meta1"
});
}
// Override getNotFound as exception is raised after
async getNotFound() { }
async httpGetError() {
// GET is not through classic binary
// Skip it
}
async redirectUrl() {
let { user1, ctx } = await this.setupDefault();
// Making sure we are redirected on GET
let executor = this.getExecutor(ctx, "test.webda.io", "GET", `/binary/users/${user1.getUuid()}/images/0`, {});
await executor.execute(ctx);
assert.ok(ctx.getResponseHeaders().Location !== undefined);
}
async challenge() {
await this.testChallenge(false);
}
async redirectUrlInfo() {
let { user1, ctx } = await this.setupDefault();
// Making sure we are redirected on GET
let executor = this.getExecutor(ctx, "test.webda.io", "GET", `/binary/users/${user1.getUuid()}/images/0/url`, {});
await executor.execute(ctx);
assert.ok(ctx.getResponseHeaders().Location === undefined);
assert.notStrictEqual(JSON.parse(ctx.getResponseBody()).Location, undefined);
}
async cascadeDelete() {
let stubDelete = sinon.stub(this.getBinary(), "_cleanUsage").callsFake(() => {
throw new Error();
});
try {
// @ts-ignore
await this.getBinary().cascadeDelete({ hash: "pp" }, "pp");
}
finally {
stubDelete.restore();
}
}
async defaultGCS() {
let { user1, ctx } = await this.setupDefault();
// COV for double
const binary = this.getBinary();
const from = `${this.prefix}/rawAccess`;
const to = `${this.prefix}/movedAccess`;
await binary.putObject(from, path.join(__dirname, "..", "..", "test", "Dockerfile.txt"));
await binary.moveObject({ key: from }, { key: to });
assert.deepStrictEqual(binary.getSignedUrlHeaders(), {});
const meta = await binary.getMeta({ bucket: "webda-dev", key: to });
assert.deepStrictEqual(meta, { size: 311, contentType: "text/plain" });
assert.ok(/https:\/\/storage\.googleapis\.com\/webda-dev\/webda-dev/.exec(await binary.getPublicUrl({ key: "webda-dev" })));
await binary.deleteObject({ key: to });
}
async bucketSize() {
const binary = this.getBinary();
sinon.stub(binary["storage"], "bucket").callsFake(() => {
return {
getFiles: ({ pageToken }) => {
let res = [];
let offset = pageToken ? parseInt(pageToken) : 1;
for (let i = offset; i < 100 + offset; i++) {
res.push({ name: `a${i}`, getMetadata: async () => [{ size: pageToken ? "20" : "100" }] });
}
return [res, pageToken ? { pageToken: "" } : { pageToken: "666" }];
}
};
});
assert.deepStrictEqual(await binary.getBucketSize(), {
size: 12000,
count: 200
});
assert.deepStrictEqual(await binary.getBucketSize("plop", undefined, /a1/), {
size: 1200,
count: 12
});
}
};
__decorate([
test
], StorageTest.prototype, "putObject", null);
__decorate([
test
], StorageTest.prototype, "getNotFound", null);
__decorate([
test
], StorageTest.prototype, "httpGetError", null);
__decorate([
test
], StorageTest.prototype, "redirectUrl", null);
__decorate([
test
], StorageTest.prototype, "challenge", null);
__decorate([
test
], StorageTest.prototype, "redirectUrlInfo", null);
__decorate([
test
], StorageTest.prototype, "cascadeDelete", null);
__decorate([
test
], StorageTest.prototype, "defaultGCS", null);
__decorate([
test
], StorageTest.prototype, "bucketSize", null);
StorageTest = __decorate([
suite
], StorageTest);
//# sourceMappingURL=storage.spec.js.map