Skip to content

Commit

Permalink
Merge pull request #1709 from snyk/refactor/iac-remove-writeable-stre…
Browse files Browse the repository at this point in the history
…am-bundle

refactor: Remove writeable stream to stop writing tarball to the disk [IaC]
  • Loading branch information
ipapast authored Mar 11, 2021
2 parents cabe568 + c1ca6d4 commit 0b0d28c
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 17 deletions.
7 changes: 1 addition & 6 deletions src/cli/commands/test/iac-local-execution/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@ export function createIacDir(): void {
}
}

export function extractBundle(
response,
bundlePath: fs.PathLike,
): Promise<void> {
export function extractBundle(response): Promise<void> {
return new Promise((resolve, reject) => {
fs.createWriteStream(bundlePath).on('error', (err) => reject(err));

response
.pipe(
tar.x({
Expand Down
3 changes: 1 addition & 2 deletions src/cli/commands/test/iac-local-execution/local-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ export async function initLocalCache(): Promise<void> {
if (!process.env.SNYK_IAC_SKIP_BUNDLE_DOWNLOAD) {
const preSignedUrl =
'https://cloud-config-policy-bundles.s3-eu-west-1.amazonaws.com/bundle.tar.gz';
const fileName: fs.PathLike = path.join('.iac-data/bundle.tar.gz');

createIacDir();
const response: ReadableStream = needle.get(preSignedUrl);
await extractBundle(response, fileName);
await extractBundle(response);
}
if (!doesLocalCacheExist()) {
throw Error(
Expand Down
10 changes: 2 additions & 8 deletions test/iac-unit-tests/file-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { extractBundle } from '../../src/cli/commands/test/iac-local-execution/file-utils';

describe('extractBundle', () => {
const fs = require('fs');
const { PassThrough } = require('stream');
jest.mock('fs');
const mockFilePath = '.iac-data/bundle.tar.gz';

it('fails to write the file on disk', () => {
const mockReadable = new PassThrough();
const mockWriteable = new PassThrough();
const mockError = new Error('A stream error');
fs.createWriteStream.mockReturnValueOnce(mockWriteable);

const actualPromise = extractBundle(mockReadable, mockFilePath);
const actualPromise = extractBundle(mockReadable);
setTimeout(() => {
mockReadable.emit('error', mockError);
}, 100);
Expand All @@ -22,10 +18,8 @@ describe('extractBundle', () => {

it('resolves data successfully', () => {
const mockReadable = new PassThrough();
const mockWriteable = new PassThrough();
fs.createWriteStream.mockReturnValueOnce(mockWriteable);

const actualPromise = extractBundle(mockReadable, mockFilePath);
const actualPromise = extractBundle(mockReadable);

setTimeout(() => {
mockReadable.emit('data', 'this-is-a-file-chunk');
Expand Down
2 changes: 1 addition & 1 deletion test/iac-unit-tests/local-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('initLocalCache - SNYK_IAC_SKIP_BUNDLE_DOWNLOAD is not set', () => {

localCacheModule.initLocalCache();

expect(spy).toHaveBeenCalledWith(mockReadable, '.iac-data/bundle.tar.gz');
expect(spy).toHaveBeenCalledWith(mockReadable);
});
});

Expand Down

0 comments on commit 0b0d28c

Please sign in to comment.