Skip to content

🎥 Make videos programmatically with React

more and better tests

Jonny Burgerbe97a19deb4a
diff --git a/packages/lambda/src/api/__mocks__/bundle-lambda.ts b/packages/lambda/src/api/__mocks__/bundle-lambda.ts
new file mode 100644
index 0000000000..5b1a2ba22b
--- /dev/null
+++ b/packages/lambda/src/api/__mocks__/bundle-lambda.ts
@@ -0,0 +1,5 @@
+import {bundleLambda as original} from '../bundle-lambda';
+
+export const bundleLambda: typeof original = async () => {
+ return Promise.resolve('/path/to/functionbundle');
+};
diff --git a/packages/lambda/src/api/__mocks__/create-function.ts b/packages/lambda/src/api/__mocks__/create-function.ts
new file mode 100644
index 0000000000..1c817803ca
--- /dev/null
+++ b/packages/lambda/src/api/__mocks__/create-function.ts
@@ -0,0 +1,20 @@
+import {CURRENT_VERSION} from '../../defaults';
+import {createFunction as original} from '../create-function';
+import {addFunction} from './mock-functions';
+import {isLayerEnsured} from './mock-layers';
+
+export const createFunction: typeof original = async (input) => {
+ if (!isLayerEnsured(input.region)) {
+ throw new TypeError('did not ensure layer for ' + input.region);
+ }
+
+ addFunction({
+ functionName: input.functionName,
+ memorySizeInMb: 1024,
+ timeoutInSeconds: input.timeoutInSeconds,
+ version: CURRENT_VERSION,
+ });
+ return Promise.resolve({
+ FunctionName: input.functionName,
+ });
+};
diff --git a/packages/lambda/src/api/__mocks__/deploy-function.ts b/packages/lambda/src/api/__mocks__/deploy-function.ts
deleted file mode 100644
index ea1ad700f2..0000000000
--- a/packages/lambda/src/api/__mocks__/deploy-function.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import {deployFunction as original} from '../deploy-function';
-import {isLayerEnsured} from './mock-layers';
-
-export const deployFunction: typeof original = async (input) => {
- if (!isLayerEnsured(input.region)) {
- throw new TypeError('did not ensure layer for ' + input.region);
- }
-
- return Promise.resolve({
- functionName: 'hithere',
- });
-};
diff --git a/packages/lambda/src/api/__mocks__/get-functions.ts b/packages/lambda/src/api/__mocks__/get-functions.ts
new file mode 100644
index 0000000000..d69e326dcd
--- /dev/null
+++ b/packages/lambda/src/api/__mocks__/get-functions.ts
@@ -0,0 +1,6 @@
+import {getFunctions as original} from '../get-functions';
+import {getAllMockFunctions} from './mock-functions';
+
+export const getFunctions: typeof original = async () => {
+ return getAllMockFunctions();
+};
diff --git a/packages/lambda/src/api/__mocks__/mock-functions.ts b/packages/lambda/src/api/__mocks__/mock-functions.ts
new file mode 100644
index 0000000000..26061504a9
--- /dev/null
+++ b/packages/lambda/src/api/__mocks__/mock-functions.ts
@@ -0,0 +1,25 @@
+import {FunctionInfo} from '../get-function-info';
+
+export let mockFunctionsStore: FunctionInfo[] = [];
+
+export const addFunction = (fn: FunctionInfo) => {
+ mockFunctionsStore.push(fn);
+};
+
+export const deleteFunction = (name: string) => {
+ mockFunctionsStore = mockFunctionsStore.filter(
+ (fn) => fn.functionName !== name
+ );
+};
+
+export const findFunction = (name: string) => {
+ return mockFunctionsStore.find((n) => n.functionName === name);
+};
+
+export const getAllMockFunctions = () => {
+ return mockFunctionsStore;
+};
+
+export const cleanFnStore = () => {
+ mockFunctionsStore = [];
+};
diff --git a/packages/lambda/src/api/create-function.ts b/packages/lambda/src/api/create-function.ts
new file mode 100644
index 0000000000..550bdfcb9f
--- /dev/null
+++ b/packages/lambda/src/api/create-function.ts
@@ -0,0 +1,40 @@
+import {CreateFunctionCommand} from '@aws-sdk/client-lambda';
+import {readFileSync} from 'fs';
+import {AwsRegion} from '..';
+import {getLambdaClient} from '../shared/aws-clients';
+
+export const createFunction = async ({
+ region,
+ zipFile,
+ functionName,
+ accountId,
+ memorySizeInMb,
+ timeoutInSeconds,
+ layerArn,
+}: {
+ region: AwsRegion;
+ zipFile: string;
+ functionName: string;
+ accountId: string;
+ memorySizeInMb: number;
+ timeoutInSeconds: number;
+ layerArn: string;
+}) => {
+ const {FunctionName} = await getLambdaClient(region).send(
+ new CreateFunctionCommand({
+ Code: {
+ ZipFile: readFileSync(zipFile),
+ },
+ FunctionName: functionName,
+ Handler: 'index.handler',
+ // TODO: Give helpful suggestion if user did not create role
+ Role: `arn:aws:iam::${accountId[1]}:role/remotion-lambda-role`,
+ Runtime: 'nodejs14.x',
+ Description: 'Renders a Remotion video.',
+ MemorySize: memorySizeInMb,
+ Timeout: timeoutInSeconds,
+ Layers: [layerArn],
+ })
+ );
+ return {FunctionName};
+};
diff --git a/packages/lambda/src/api/deploy-function.ts b/packages/lambda/src/api/deploy-function.ts
index 33e66afb61..901be6a203 100644
--- a/packages/lambda/src/api/deploy-function.ts
+++ b/packages/lambda/src/api/deploy-function.ts
@@ -1,7 +1,4 @@
-import {CreateFunctionCommand} from '@aws-sdk/client-lambda';
-import {readFileSync} from 'fs';
import {AwsRegion} from '../pricing/aws-regions';
-import {getLambdaClient} from '../shared/aws-clients';
import {RENDER_FN_PREFIX} from '../shared/constants';
import {getAccountId} from '../shared/get-account-id';
import {randomHash} from '../shared/random-hash';
@@ -9,6 +6,7 @@ import {validateAwsRegion} from '../shared/validate-aws-region';
import {validateMemorySize} from '../shared/validate-memory-size';
import {validateTimeout} from '../shared/validate-timeout';
import {bundleLambda} from './bundle-lambda';
+import {createFunction} from './create-function';
/**
* @description Creates an AWS Lambda function in your account that will be able to render a video in the cloud.
@@ -35,22 +33,15 @@ export const deployFunction = async (options: {
getAccountId({region: options.region}),
]);
- const created = await getLambdaClient(options.region).send(
- new CreateFunctionCommand({
- Code: {
- ZipFile: readFileSync(renderOut),
- },
- FunctionName: fnNameRender,
- Handler: 'index.handler',
- // TODO: Give helpful suggestion if user did not create role
- Role: `arn:aws:iam::${accountId[1]}:role/remotion-lambda-role`,
- Runtime: 'nodejs14.x',
- Description: 'Renders a Remotion video.',
- MemorySize: options.memorySizeInMb,
- Timeout: options.timeoutInSeconds,
- Layers: [options.layerArn],
- })
- );
+ const created = await createFunction({
+ region: options.region,
+ zipFile: renderOut,
+ functionName: fnNameRender,
+ accountId: accountId[1],
+ memorySizeInMb: options.memorySizeInMb,
+ timeoutInSeconds: options.timeoutInSeconds,
+ layerArn: options.layerArn,
+ });
if (!created.FunctionName) {
throw new Error('Lambda was created but has no name');
diff --git a/packages/lambda/src/shared/__mocks__/get-account-id.ts b/packages/lambda/src/shared/__mocks__/get-account-id.ts
new file mode 100644
index 0000000000..2f99ad129f
--- /dev/null
+++ b/packages/lambda/src/shared/__mocks__/get-account-id.ts
@@ -0,0 +1,10 @@
+import {getAccountId as original} from '../get-account-id';
+
+export const getAccountId: typeof original = () => {
+ const accountId = 'aws:iam::123456789'.match(/aws:iam::([0-9]+)/);
+ if (!accountId) {
+ throw new Error('Cannot get account ID');
+ }
+
+ return Promise.resolve(accountId);
+};
diff --git a/packages/lambda/src/test/integration/deploy-function.test.ts b/packages/lambda/src/test/integration/deploy-function.test.ts
index 50aba95c40..014dfc82d5 100644
--- a/packages/lambda/src/test/integration/deploy-function.test.ts
+++ b/packages/lambda/src/test/integration/deploy-function.test.ts
@@ -1,5 +1,7 @@
import {deployFunction} from '../../api/deploy-function';
import {ensureLambdaBinaries} from '../../api/ensure-lambda-binaries';
+import {getFunctions} from '../../api/get-functions';
+import {cleanFnStore} from '../../api/__mocks__/mock-functions';
jest.mock('../../api/get-buckets');
jest.mock('../../functions/helpers/io');
@@ -10,8 +12,11 @@ jest.mock('../../api/create-bucket');
jest.mock('../../api/upload-dir');
jest.mock('../../api/bucket-exists');
jest.mock('../../api/clean-items');
-jest.mock('../../api/deploy-function');
jest.mock('../../api/ensure-lambda-binaries');
+jest.mock('../../api/create-function');
+jest.mock('../../api/bundle-lambda');
+jest.mock('../../api/get-functions');
+jest.mock('../../shared/get-account-id');
test('Should be able to deploy function', async () => {
const {layerArn} = await ensureLambdaBinaries('us-east-1');
@@ -22,7 +27,7 @@ test('Should be able to deploy function', async () => {
region: 'us-east-1',
timeoutInSeconds: 120,
});
- expect(functionName).toBe('hithere');
+ expect(functionName).toBe('remotion-render-abcdef');
});
test('Should not be able to deploy function if binaries were not ensured', async () => {
@@ -35,3 +40,28 @@ test('Should not be able to deploy function if binaries were not ensured', async
})
).rejects.toThrow(/did not ensure layer for/);
});
+
+test('Should be able to get the function afterwards', async () => {
+ cleanFnStore();
+ const {layerArn} = await ensureLambdaBinaries('us-east-1');
+
+ const {functionName} = await deployFunction({
+ layerArn,
+ memorySizeInMb: 2048,
+ region: 'us-east-1',
+ timeoutInSeconds: 120,
+ });
+ expect(functionName).toBe('remotion-render-abcdef');
+ const fns = await getFunctions({
+ region: 'us-east-1',
+ compatibleOnly: true,
+ });
+ expect(fns).toEqual([
+ {
+ functionName: 'remotion-render-abcdef',
+ memorySizeInMb: 1024,
+ timeoutInSeconds: 120,
+ version: '2021-09-15',
+ },
+ ]);
+});