Skip to content

Commit

Permalink
Test fix for image-manager.spec.ts flakiness
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
myarmolinsky committed Oct 18, 2024
1 parent 0716544 commit 3a36f45
Showing 1 changed file with 49 additions and 48 deletions.
97 changes: 49 additions & 48 deletions tests/utils/image-manager/image-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as stream from 'stream';
import { AssertionError, expect } from 'chai';
import { stub } from 'sinon';
import * as tmp from 'tmp';
import { delay } from '../../utils';
// import { delay } from '. ./../utils';
import * as fs from 'fs';
import * as fsAsync from 'fs/promises';
import * as stringToStream from 'string-to-stream';
Expand Down Expand Up @@ -79,53 +79,53 @@ describe('image-manager', function () {
});

// Skipping test because we keep getting `Cache image` instead of `Download image`
describe.skip('given a valid download endpoint', function () {
beforeEach(function () {
this.osDownloadStub = stub(balena.models.os, 'download');
this.osDownloadStub.returns(
Promise.resolve(stringToStream('Download image')),
);
});

afterEach(function () {
this.osDownloadStub.restore();
});

it('should eventually become a readable stream of the download image and save a backup copy', function (done) {
void imageManager.getStream('raspberry-pi').then((stream) => {
let result = '';

stream.on('data', (chunk) => (result += chunk));

stream.on('end', async () => {
expect(result).to.equal('Download image');
const contents = await fsAsync.readFile(this.image.name, {
encoding: 'utf8',
});
expect(contents).to.equal('Download image');
done();
});
});
});

it('should be able to read from the stream after a slight delay', function (done) {
void imageManager.getStream('raspberry-pi').then(async (s) => {
await delay(200);

const pass = new stream.PassThrough();
s.pipe(pass);

let result = '';

pass.on('data', (chunk) => (result += chunk));

pass.on('end', function () {
expect(result).to.equal('Download image');
done();
});
});
});
});
// describe.skip('given a valid download endpoint', function () {
// beforeEach(function () {
// this.osDownloadStub = stub(balena.models.os, 'download');
// this.osDownloadStub.returns(
// Promise.resolve(stringToStream('Download image')),
// );
// });

// afterEach(function () {
// this.osDownloadStub.restore();
// });

// it('should eventually become a readable stream of the download image and save a backup copy', function (done) {
// void imageManager.getStream('raspberry-pi').then((stream) => {
// let result = '';

// stream.on('data', (chunk) => (result += chunk));

// stream.on('end', async () => {
// expect(result).to.equal('Download image');
// const contents = await fsAsync.readFile(this.image.name, {
// encoding: 'utf8',
// });
// expect(contents).to.equal('Download image');
// done();
// });
// });
// });

// it('should be able to read from the stream after a slight delay', function (done) {
// void imageManager.getStream('raspberry-pi').then(async (s) => {
// await delay(200);

// const pass = new stream.PassThrough();
// s.pipe(pass);

// let result = '';

// pass.on('data', (chunk) => (result += chunk));

// pass.on('end', function () {
// expect(result).to.equal('Download image');
// done();
// });
// });
// });
// });

describe('given a failing download', function () {
beforeEach(function () {
Expand Down Expand Up @@ -158,6 +158,7 @@ describe('image-manager', function () {
);
})
.catch((err) => {
console.info('*** err', err);
if (err.code !== 'ENOENT') {
throw err;
}
Expand Down

0 comments on commit 3a36f45

Please sign in to comment.