/*
Copyright 2025 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { test, describe, mock } from 'node:test';
import assert from 'node:assert/strict';
import esmock from 'esmock';
import { DEPLOYMENT_CONFIG } from '../../lib/deployment/constants.js';
describe('Deployment Helpers', () => {
const fsMock = {
statSync: mock.fn(),
existsSync: mock.fn(),
readFileSync: mock.fn(),
};
test('makeFileDeploymentMetadata correctly identifies Dockerfile in folder', async () => {
const deploymentHelpers = await esmock('../../lib/deployment/helpers.js', {
fs: fsMock,
});
const folderPath = '/absolute/path/to/folder';
// Mock isFolder logic
fsMock.statSync.mock.mockImplementation(() => ({
isDirectory: () => true,
}));
// Mock Dockerfile check
fsMock.existsSync.mock.mockImplementation((filePath) => {
if (filePath.endsWith('Dockerfile') || filePath.endsWith('dockerfile'))
return true;
return false;
});
const result = deploymentHelpers.makeFileDeploymentMetadata([folderPath]);
assert.equal(result.hasDockerfile, true);
});
test('makeFileDeploymentMetadata correctly identifies Node.js project', async () => {
const deploymentHelpers = await esmock('../../lib/deployment/helpers.js', {
fs: fsMock,
});
const folderPath = '/absolute/path/to/node-app';
// Mock isFolder
fsMock.statSync.mock.mockImplementation(() => ({
isDirectory: () => true,
}));
// Mock existsSync
fsMock.existsSync.mock.mockImplementation((p) => {
if (p.endsWith('Dockerfile')) return false;
if (p.endsWith('package.json')) return true;
return false;
});
const result = deploymentHelpers.makeFileDeploymentMetadata([folderPath]);
assert.equal(result.hasDockerfile, false);
assert.equal(result.deploymentAttrs.runtime, 'nodejs');
});
test('canDeployWithoutBuild returns true for valid Node.js project', async () => {
const deploymentHelpers = await esmock('../../lib/deployment/helpers.js', {
fs: fsMock,
});
const metadata = {
hasDockerfile: false,
deploymentAttrs: {
runtime: 'nodejs',
},
};
assert.equal(deploymentHelpers.canDeployWithoutBuild(metadata), true);
});
test('canDeployWithoutBuild returns false if Dockerfile exists', async () => {
const deploymentHelpers = await esmock('../../lib/deployment/helpers.js', {
fs: fsMock,
});
const metadata = {
hasDockerfile: true,
deploymentAttrs: {
runtime: 'nodejs',
},
};
assert.equal(deploymentHelpers.canDeployWithoutBuild(metadata), false);
});
test('createDirectSourceDeploymentContainer creates correct object without envVars', async () => {
const deploymentHelpers = await esmock('../../lib/deployment/helpers.js', {
fs: fsMock,
});
const input = {
bucketName: 'test-bucket',
fileName: 'source.tar.gz',
deploymentAttrs: {
cmd: ['node'],
args: ['server.js'],
baseImage: 'gcr.io/google-appengine/nodejs',
},
};
const result =
deploymentHelpers.createDirectSourceDeploymentContainer(input);
assert.deepEqual(result, {
image: DEPLOYMENT_CONFIG.NO_BUILD_IMAGE_TYPE,
baseImageUri: 'gcr.io/google-appengine/nodejs',
sourceCode: {
cloudStorageSource: {
bucket: 'test-bucket',
object: 'source.tar.gz',
},
},
command: ['node'],
args: ['server.js'],
});
});
test('createDirectSourceDeploymentContainer creates correct object with envVars', async () => {
const deploymentHelpers = await esmock('../../lib/deployment/helpers.js', {
fs: fsMock,
});
const input = {
bucketName: 'test-bucket',
fileName: 'source.tar.gz',
deploymentAttrs: {
cmd: ['node'],
args: ['server.js'],
baseImage: 'gcr.io/google-appengine/nodejs',
envVars: {
PORT: '8080',
NODE_ENV: 'production',
},
},
};
const result =
deploymentHelpers.createDirectSourceDeploymentContainer(input);
assert.deepEqual(result, {
image: DEPLOYMENT_CONFIG.NO_BUILD_IMAGE_TYPE,
baseImageUri: 'gcr.io/google-appengine/nodejs',
sourceCode: {
cloudStorageSource: {
bucket: 'test-bucket',
object: 'source.tar.gz',
},
},
command: ['node'],
args: ['server.js'],
env: [
{ name: 'PORT', value: '8080' },
{ name: 'NODE_ENV', value: 'production' },
],
});
});
});