|
1 | 1 | import { promises as fspromises } from 'fs'; |
2 | 2 | import md5file from 'md5-file'; |
| 3 | +import * as mkdirp from 'mkdirp'; |
3 | 4 | import { DryMongoBinary } from '../DryMongoBinary'; |
4 | 5 | import MongoBinaryDownload from '../MongoBinaryDownload'; |
| 6 | +import MongoBinaryDownloadUrl from '../MongoBinaryDownloadUrl'; |
5 | 7 | import { envName, ResolveConfigVariables } from '../resolveConfig'; |
6 | 8 | import * as utils from '../utils'; |
7 | 9 |
|
8 | 10 | jest.mock('md5-file'); |
| 11 | +jest.mock('mkdirp'); |
9 | 12 |
|
10 | 13 | describe('MongoBinaryDownload', () => { |
11 | 14 | afterEach(() => { |
@@ -222,4 +225,38 @@ describe('MongoBinaryDownload', () => { |
222 | 225 | du.getPath |
223 | 226 | ).toHaveBeenCalledTimes(1); |
224 | 227 | }); |
| 228 | + |
| 229 | + it('should return the mongodb archive path (startDownload)', async () => { |
| 230 | + const downloadUrl = 'https://fastdl.mongodb.org/linux/mongod-something-something.tgz'; |
| 231 | + const archivePath = '/path/to/archive.tgz'; |
| 232 | + jest.spyOn(mkdirp, 'default').mockResolvedValue(void 0); |
| 233 | + jest.spyOn(fspromises, 'access').mockResolvedValue(void 0); |
| 234 | + jest.spyOn(MongoBinaryDownloadUrl.prototype, 'getDownloadUrl').mockResolvedValue(downloadUrl); |
| 235 | + |
| 236 | + const du = new MongoBinaryDownload({ downloadDir: '/' }); |
| 237 | + jest.spyOn(du, 'download').mockResolvedValue(archivePath); |
| 238 | + jest.spyOn(du, 'makeMD5check'); |
| 239 | + |
| 240 | + const returnValue = await du.startDownload(); |
| 241 | + expect(returnValue).toEqual(archivePath); |
| 242 | + expect(du.makeMD5check).toHaveBeenCalledWith(`${downloadUrl}.md5`, archivePath); |
| 243 | + expect(du.download).toHaveBeenCalledWith(downloadUrl); |
| 244 | + }); |
| 245 | + |
| 246 | + it('should return the mongodb archive path (startDownload)', async () => { |
| 247 | + const customError = new Error('custom fs error'); |
| 248 | + jest.spyOn(mkdirp, 'default').mockResolvedValue(void 0); |
| 249 | + jest.spyOn(fspromises, 'access').mockRejectedValue(customError); |
| 250 | + jest.spyOn(console, 'error').mockImplementation(() => void 0); |
| 251 | + |
| 252 | + const du = new MongoBinaryDownload({ downloadDir: '/' }); |
| 253 | + |
| 254 | + try { |
| 255 | + await du.startDownload(); |
| 256 | + fail('Expected "startDownload" to fail'); |
| 257 | + } catch (err) { |
| 258 | + expect(err.message).toEqual(customError.message); |
| 259 | + expect(console.error).toHaveBeenCalledTimes(1); |
| 260 | + } |
| 261 | + }); |
225 | 262 | }); |
0 commit comments