add test container for s3 / minio

This commit is contained in:
Peter Clement 2024-03-15 11:59:01 +00:00
parent c9d88e7d26
commit 04895fc921
4 changed files with 62 additions and 32 deletions

View File

@ -136,8 +136,13 @@ class RestIntegration implements IntegrationBase {
const contentType = response.headers.get("content-type") || ""
const contentDisposition = response.headers.get("content-disposition") || ""
filename =
path.basename(parse(contentDisposition).parameters?.filename) || ""
if (
contentDisposition.includes("attachment") ||
contentDisposition.includes("form-data")
) {
filename =
path.basename(parse(contentDisposition).parameters?.filename) || ""
}
try {
if (filename) {

View File

@ -17,15 +17,6 @@ jest.mock("@budibase/backend-core", () => {
const core = jest.requireActual("@budibase/backend-core")
return {
...core,
objectStore: {
...core.objectStore,
ObjectStoreBuckets: {
APPS: "apps",
TEMP: "tmp-file-attachments",
},
upload: jest.fn(),
getPresignedUrl: jest.fn(() => "https://presigned-url.com"),
},
context: {
...core.context,
getProdAppId: jest.fn(() => "app-id"),
@ -36,8 +27,8 @@ jest.mock("uuid", () => ({ v4: () => "00000000-0000-0000-0000-000000000000" }))
import { default as RestIntegration } from "../rest"
import { RestAuthType } from "@budibase/types"
import { objectStore } from "@budibase/backend-core"
import fetch from "node-fetch"
import { databaseTestProviders } from "./utils"
const FormData = require("form-data")
const { URLSearchParams } = require("url")
@ -59,6 +50,13 @@ describe("REST Integration", () => {
const BASE_URL = "https://myapi.com"
let config: any
beforeAll(async () => {
await databaseTestProviders.s3.start()
})
afterAll(async () => {
await databaseTestProviders.s3.stop()
})
beforeEach(() => {
config = new TestConfiguration({
url: BASE_URL,
@ -635,7 +633,7 @@ describe("REST Integration", () => {
})
it("uploads file to object store and returns signed URL", async () => {
const responseData = Buffer.from("test file content")
const responseData = Buffer.from("teest file contnt")
const filename = "test.tar.gz"
const contentType = "application/gzip"
@ -662,27 +660,14 @@ describe("REST Integration", () => {
const response = await config.integration.read(query)
expect(objectStore.upload).toHaveBeenCalledWith({
bucket: objectStore.ObjectStoreBuckets.TEMP,
filename: expect.stringContaining(
"app-id/00000000-0000-0000-0000-000000000000.tar.gz"
),
addTTL: true,
body: responseData,
})
expect(objectStore.getPresignedUrl).toHaveBeenCalledWith(
objectStore.ObjectStoreBuckets.TEMP,
expect.stringContaining(`app-id/`),
600
)
expect(response.data).toEqual({
size: responseData.byteLength,
name: expect.stringContaining(".tar.gz"),
url: "https://presigned-url.com",
name: "00000000-0000-0000-0000-000000000000.tar.gz",
url: "/files/signed/tmp-file-attachments/app-id/00000000-0000-0000-0000-000000000000.tar.gz",
extension: "tar.gz",
key: expect.stringContaining(`app-id/`),
key: expect.stringContaining(
"app-id/00000000-0000-0000-0000-000000000000.tar.gz"
),
})
})
})

View File

@ -4,6 +4,7 @@ import { Datasource } from "@budibase/types"
import * as postgres from "./postgres"
import * as mongodb from "./mongodb"
import * as mysql from "./mysql"
import * as s3 from "./s3"
import { StartedTestContainer } from "testcontainers"
jest.setTimeout(30000)
@ -14,4 +15,4 @@ export interface DatabaseProvider {
datasource(): Promise<Datasource>
}
export const databaseTestProviders = { postgres, mongodb, mysql }
export const databaseTestProviders = { postgres, mongodb, mysql, s3 }

View File

@ -0,0 +1,39 @@
import { Datasource } from "@budibase/types"
import { GenericContainer, Wait, StartedTestContainer } from "testcontainers"
import { AbstractWaitStrategy } from "testcontainers/build/wait-strategies/wait-strategy"
let container: StartedTestContainer | undefined
class LocalstackS3WaitStrategy extends AbstractWaitStrategy {
async waitUntilReady(container: any, boundPorts: any, startTime?: Date) {
const logs = Wait.forLogMessage("Ready.", 1)
await logs.waitUntilReady(container, boundPorts, startTime)
const command = Wait.forSuccessfulCommand(
`aws --endpoint-url=http://localhost:4566 s3 ls`
)
await command.waitUntilReady(container)
}
}
export async function start(): Promise<StartedTestContainer> {
container = await new GenericContainer("localstack/localstack")
.withExposedPorts(9000)
.withEnvironment({
SERVICES: "s3",
DEFAULT_REGION: "eu-west-1",
AWS_ACCESS_KEY_ID: "testkey",
AWS_SECRET_ACCESS_KEY: "testsecret",
})
.withWaitStrategy(new LocalstackS3WaitStrategy().withStartupTimeout(10000))
.start()
return container
}
export async function stop() {
if (container) {
await container.stop()
container = undefined
}
}