How to use extractTar method in Puppeteer

Best JavaScript code snippet using puppeteer

download-image.js

Source:download-image.js Github

copy

Full Screen

1/*2 * (C) Copyright 2017 o2r project3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *16 */17/* eslint-env mocha */18const chai = require('chai');19const assert = chai.assert;20const request = require('request');21const fs = require('fs');22const tmp = require('tmp');23const AdmZip = require('adm-zip');24const tarStream = require('tar-stream');25const gunzip = require('gunzip-maybe');26const stream = require('stream');27const exec = require('child_process').exec;28const createCompendiumPostRequest = require('./util').createCompendiumPostRequest;29const publishCandidate = require('./util').publishCandidate;30const startJob = require('./util').startJob;31const waitForJob = require('./util').waitForJob;32require("./setup");33const cookie = 's:C0LIrsxGtHOGHld8Nv2jedjL4evGgEHo.GMsWD5Vveq0vBt7/4rGeoH5Xx7Dd2pgZR9DvhKCyDTY';34describe('Image download', function () {35 var compendium_id, job_id = null;36 before(function (done) {37 this.timeout(720000);38 createCompendiumPostRequest('./test/workspace/dummy', cookie, 'workspace', (req) => {39 request(req, (err, res, body) => {40 assert.ifError(err);41 compendium_id = JSON.parse(body).id;42 publishCandidate(compendium_id, cookie, () => {43 startJob(compendium_id, id => {44 assert.ok(id);45 job_id = id;46 waitForJob(job_id, (finalStatus) => {47 done();48 });49 })50 });51 });52 });53 });54 describe('downloading a compendium', function () {55 it('should contain a tarball of Docker image in zip archive by default', (done) => {56 let tmpfile = tmp.tmpNameSync() + '.zip';57 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.zip';58 request.get(url)59 .on('error', function (err) {60 done(err);61 })62 .pipe(fs.createWriteStream(tmpfile))63 .on('finish', function () {64 let zip = new AdmZip(tmpfile);65 let zipEntries = zip.getEntries();66 let filenames = [];67 zipEntries.forEach(function (entry) {68 filenames.push(entry.entryName);69 });70 assert.oneOf('image.tar', filenames);71 assert.oneOf('main.Rmd', filenames);72 assert.oneOf('.erc/metadata_o2r_1.json', filenames);73 done();74 });75 }).timeout(10000);76 it('should contain a tarball of Docker image in gzipped .tar archive', (done) => {77 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?gzip';78 let filenames = [];79 let extractTar = tarStream.extract();80 extractTar.on('entry', function (header, stream, next) {81 filenames.push(header.name);82 stream.on('end', function () {83 next();84 })85 stream.resume();86 });87 extractTar.on('finish', function () {88 assert.oneOf('image.tar', filenames);89 assert.oneOf('.erc/metadata_o2r_1.json', filenames);90 done();91 });92 extractTar.on('error', function (e) {93 done(e);94 });95 request.get(url)96 .on('error', function (err) {97 done(err);98 })99 .pipe(gunzip())100 .pipe(extractTar);101 }).timeout(10000);102 it('should contain a tarball of Docker image in zip archive when explicitly asking for it', (done) => {103 let tmpfile = tmp.tmpNameSync() + '.zip';104 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.zip?image=true';105 request.get(url)106 .on('error', function (err) {107 done(err);108 })109 .pipe(fs.createWriteStream(tmpfile))110 .on('finish', function () {111 let zip = new AdmZip(tmpfile);112 let zipEntries = zip.getEntries();113 let filenames = [];114 zipEntries.forEach(function (entry) {115 filenames.push(entry.entryName);116 });117 assert.oneOf('image.tar', filenames);118 assert.oneOf('.erc/metadata_o2r_1.json', filenames);119 done();120 });121 }).timeout(10000);122 it('should contain a tarball of Docker image in tar.gz archive when explicitly asking for it', (done) => {123 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?gzip&image=true';124 let filenames = [];125 let extractTar = tarStream.extract();126 extractTar.on('entry', function (header, stream, next) {127 filenames.push(header.name);128 stream.on('end', function () {129 next();130 })131 stream.resume();132 });133 extractTar.on('finish', function () {134 assert.oneOf('image.tar', filenames);135 done();136 });137 extractTar.on('error', function (e) {138 done(e);139 });140 request.get(url)141 .on('error', function (err) {142 done(err);143 })144 .pipe(gunzip())145 .pipe(extractTar);146 }).timeout(10000);147 it('should not have a tarball of Docker image in zip archive when explicitly not asking for it', (done) => {148 let tmpfile = tmp.tmpNameSync() + '.zip';149 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.zip?image=false';150 request.get(url)151 .on('error', function (err) {152 done(err);153 })154 .pipe(fs.createWriteStream(tmpfile))155 .on('finish', function () {156 let zip = new AdmZip(tmpfile);157 let zipEntries = zip.getEntries();158 let filenames = [];159 zipEntries.forEach(function (entry) {160 filenames.push(entry.entryName);161 });162 assert.notInclude(filenames, 'image.tar');163 assert.oneOf('.erc/metadata_o2r_1.json', filenames);164 done();165 });166 });167 it('should not have a tarball of Docker image in tar.gz archive when explicitly not asking for it', (done) => {168 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.tar.gz?image=false';169 let filenames = [];170 let extractTar = tarStream.extract();171 extractTar.on('entry', function (header, stream, next) {172 filenames.push(header.name);173 stream.on('end', function () {174 next();175 })176 stream.resume();177 });178 extractTar.on('finish', function () {179 assert.notInclude(filenames, 'image.tar');180 assert.oneOf('erc.yml', filenames);181 done();182 });183 extractTar.on('error', function (e) {184 done(e);185 });186 request.get(url)187 .on('error', function (err) {188 done(err);189 })190 .pipe(gunzip())191 .pipe(extractTar);192 });193 it('should not have a tarball of Docker image in gzipped tar archive when explicitly not asking for it', (done) => {194 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?image=false&gzip';195 let filenames = [];196 let extractTar = tarStream.extract();197 extractTar.on('entry', function (header, stream, next) {198 filenames.push(header.name);199 stream.on('end', function () {200 next();201 })202 stream.resume();203 });204 extractTar.on('finish', function () {205 assert.notInclude(filenames, 'image.tar');206 assert.oneOf('erc.yml', filenames);207 done();208 });209 extractTar.on('error', function (e) {210 done(e);211 });212 request.get(url)213 .on('error', function (err) {214 done(err);215 })216 .pipe(gunzip())217 .pipe(extractTar);218 });219 it('should contain expected files in tarball', (done) => {220 let tmpfile = tmp.tmpNameSync() + '.zip';221 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.zip';222 request.get(url)223 .on('error', function (err) {224 done(err);225 })226 .pipe(fs.createWriteStream(tmpfile))227 .on('finish', function () {228 let zip = new AdmZip(tmpfile);229 zip.getEntries().forEach(function (entry) {230 if (entry.entryName === 'image.tar') {231 let filenames = [];232 let manifestJson = null;233 let extractTar = tarStream.extract();234 extractTar.on('entry', function (header, stream, next) {235 filenames.push(header.name);236 if (header.name == 'manifest.json') {237 const chunks = [];238 stream.on('data', function (chunk) {239 chunks.push(chunk);240 });241 stream.on('end', function () {242 manifestJson = JSON.parse(chunks)[0];243 next();244 });245 } else {246 stream.on('end', function () {247 next();248 })249 }250 stream.resume();251 });252 extractTar.on('finish', function () {253 assert.oneOf('manifest.json', filenames);254 assert.oneOf('repositories', filenames);255 assert.include(filenames.toString(), '/VERSION');256 assert.include(filenames.toString(), '/layer.tar');257 assert.property(manifestJson, 'RepoTags');258 done();259 });260 extractTar.on('error', function (e) {261 done(e);262 });263 let bufferStream = new stream.PassThrough();264 bufferStream.end(new Buffer.from(entry.getData()));265 bufferStream.pipe(extractTar);266 }267 });268 });269 }).timeout(60000);270 it('should have the correct job tag on the image in tarball', (done) => {271 let tmpfile = tmp.tmpNameSync() + '.zip';272 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.zip';273 request.get(url)274 .on('error', function (err) {275 done(err);276 })277 .pipe(fs.createWriteStream(tmpfile))278 .on('finish', function () {279 let zip = new AdmZip(tmpfile);280 zip.getEntries().forEach(function (entry) {281 if (entry.entryName === 'image.tar') {282 let manifestJson = null;283 let extractTar = tarStream.extract();284 extractTar.on('entry', function (header, stream, next) {285 if (header.name == 'manifest.json') {286 const chunks = [];287 stream.on('data', function (chunk) {288 chunks.push(chunk);289 });290 stream.on('end', function () {291 manifestJson = JSON.parse(chunks)[0];292 next();293 });294 } else {295 stream.on('end', function () {296 next();297 })298 }299 stream.resume();300 });301 extractTar.on('finish', function () {302 assert.oneOf('job:' + job_id, manifestJson.RepoTags, '"job:<job_id>" tag is in RepoTags list');303 done();304 });305 extractTar.on('error', function (e) {306 done(e);307 });308 let bufferStream = new stream.PassThrough();309 bufferStream.end(new Buffer.from(entry.getData()));310 bufferStream.pipe(extractTar);311 }312 });313 });314 }).timeout(60000);315 it('should have the correct compendium tag on the image in tarball', (done) => {316 let tmpfile = tmp.tmpNameSync() + '.zip';317 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.zip';318 request.get(url)319 .on('error', function (err) {320 done(err);321 })322 .pipe(fs.createWriteStream(tmpfile))323 .on('finish', function () {324 let zip = new AdmZip(tmpfile);325 zip.getEntries().forEach(function (entry) {326 if (entry.entryName === 'image.tar') {327 let manifestJson = null;328 let extractTar = tarStream.extract();329 extractTar.on('entry', function (header, stream, next) {330 if (header.name == 'manifest.json') {331 const chunks = [];332 stream.on('data', function (chunk) {333 chunks.push(chunk);334 });335 stream.on('end', function () {336 manifestJson = JSON.parse(chunks)[0];337 next();338 });339 } else {340 stream.on('end', function () {341 next();342 })343 }344 stream.resume();345 });346 extractTar.on('finish', function () {347 assert.oneOf('erc:' + compendium_id, manifestJson.RepoTags, '"erc:<erc_id>" tag is in RepoTags list');348 done();349 });350 extractTar.on('error', function (e) {351 done(e);352 });353 let bufferStream = new stream.PassThrough();354 bufferStream.end(new Buffer.from(entry.getData()));355 bufferStream.pipe(extractTar);356 }357 });358 });359 }).timeout(60000);360 });361 describe('tinkering with local images outside of transporter', function () {362 it('should return an error (HTTP 400, error message in JSON response) when no job was started', (done) => {363 let compendium_id = null;364 createCompendiumPostRequest('./test/workspace/dummy', cookie, 'workspace', (req) => {365 request(req, (err, res, body) => {366 assert.ifError(err);367 compendium_id = JSON.parse(body).id;368 publishCandidate(compendium_id, cookie, () => {369 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.zip', (err, res, body) => {370 assert.ifError(err);371 let response = JSON.parse(body);372 console.log(response);373 assert.equal(res.statusCode, 400);374 assert.isObject(response);375 assert.property(response, 'error');376 assert.include(response.error, 'successful job execution');377 done();378 });379 });380 });381 });382 }).timeout(30000);383 it('should not fail if image got additional tag', (done) => {384 let compendium_id_tag = null;385 createCompendiumPostRequest('./test/workspace/dummy', cookie, 'workspace', (req) => {386 request(req, (err, res, body) => {387 assert.ifError(err);388 compendium_id_tag = JSON.parse(body).id;389 publishCandidate(compendium_id_tag, cookie, () => {390 startJob(compendium_id_tag, id => {391 assert.ok(id);392 waitForJob(id, (finalStatus) => {393 exec('docker tag job:' + id + ' another:tag', (err, stdout, stderr) => {394 if (err || stderr) {395 assert.ifError(err);396 assert.ifError(stderr);397 } else {398 let tmpfile = tmp.tmpNameSync() + '.zip';399 request.get(global.test_host + '/api/v1/compendium/' + compendium_id_tag + '.zip')400 .on('error', function (err) {401 done(err);402 })403 .pipe(fs.createWriteStream(tmpfile))404 .on('finish', function () {405 let zip = new AdmZip(tmpfile);406 let zipEntries = zip.getEntries();407 let filenames = [];408 zipEntries.forEach(function (entry) {409 filenames.push(entry.entryName);410 });411 assert.oneOf('image.tar', filenames);412 assert.oneOf('erc.yml', filenames);413 done();414 });415 }416 });417 });418 });419 });420 });421 });422 }).timeout(30000);423 });...

Full Screen

Full Screen

image.js

Source:image.js Github

copy

Full Screen

1/*2 * (C) Copyright 2017 o2r project3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *16 */17/* eslint-env mocha */18const chai = require('chai');19const assert = chai.assert;20const request = require('request');21const fs = require('fs');22const tmp = require('tmp');23const AdmZip = require('adm-zip');24const tarStream = require('tar-stream');25const gunzip = require('gunzip-maybe');26const stream = require('stream');27const exec = require('child_process').exec;28const createCompendiumPostRequest = require('./util').createCompendiumPostRequest;29const publishCandidate = require('./util').publishCandidate;30const startJob = require('./util').startJob;31const waitForJob = require('./util').waitForJob;32const config = require('../config/config');33require("./setup");34const cookie = 's:C0LIrsxGtHOGHld8Nv2jedjL4evGgEHo.GMsWD5Vveq0vBt7/4rGeoH5Xx7Dd2pgZR9DvhKCyDTY';35describe('Image download', function () {36 var compendium_id, job_id = null;37 before(function (done) {38 this.timeout(30000);39 let req = createCompendiumPostRequest('./test/workspace/with_metadata', cookie);40 request(req, (err, res, body) => {41 assert.ifError(err);42 compendium_id = JSON.parse(body).id;43 publishCandidate(compendium_id, cookie, () => {44 startJob(compendium_id, id => {45 assert.ok(id);46 job_id = id;47 waitForJob(job_id, (finalStatus) => {48 done();49 });50 })51 });52 });53 });54 describe('downloading a compendium', function () {55 it('should contain a tarball of Docker image in zip archive by default', (done) => {56 let tmpfile = tmp.tmpNameSync() + '.zip';57 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.zip';58 request.get(url)59 .on('error', function (err) {60 done(err);61 })62 .pipe(fs.createWriteStream(tmpfile))63 .on('finish', function () {64 let zip = new AdmZip(tmpfile);65 let zipEntries = zip.getEntries();66 let filenames = [];67 zipEntries.forEach(function (entry) {68 filenames.push(entry.entryName);69 });70 assert.oneOf('image.tar', filenames);71 assert.oneOf('main.Rmd', filenames);72 assert.oneOf('.erc/metadata_o2r_1.json', filenames);73 done();74 });75 });76 it('should contain a tarball of Docker image in gzipped .tar archive', (done) => {77 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?gzip';78 let filenames = [];79 let extractTar = tarStream.extract();80 extractTar.on('entry', function (header, stream, next) {81 filenames.push(header.name);82 stream.on('end', function () {83 next();84 })85 stream.resume();86 });87 extractTar.on('finish', function () {88 assert.oneOf('image.tar', filenames);89 assert.oneOf('.erc/metadata_o2r_1.json', filenames);90 done();91 });92 extractTar.on('error', function (e) {93 done(e);94 });95 request.get(url)96 .on('error', function (err) {97 done(err);98 })99 .pipe(gunzip())100 .pipe(extractTar);101 });102 it('should contain a tarball of Docker image in zip archive when explicitly asking for it', (done) => {103 let tmpfile = tmp.tmpNameSync() + '.zip';104 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.zip?image=true';105 request.get(url)106 .on('error', function (err) {107 done(err);108 })109 .pipe(fs.createWriteStream(tmpfile))110 .on('finish', function () {111 let zip = new AdmZip(tmpfile);112 let zipEntries = zip.getEntries();113 let filenames = [];114 zipEntries.forEach(function (entry) {115 filenames.push(entry.entryName);116 });117 assert.oneOf('image.tar', filenames);118 assert.oneOf('.erc/metadata_o2r_1.json', filenames);119 done();120 });121 });122 it('should contain a tarball of Docker image in tar.gz archive when explicitly asking for it', (done) => {123 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?gzip&image=true';124 let filenames = [];125 let extractTar = tarStream.extract();126 extractTar.on('entry', function (header, stream, next) {127 filenames.push(header.name);128 stream.on('end', function () {129 next();130 })131 stream.resume();132 });133 extractTar.on('finish', function () {134 assert.oneOf('image.tar', filenames);135 done();136 });137 extractTar.on('error', function (e) {138 done(e);139 });140 request.get(url)141 .on('error', function (err) {142 done(err);143 })144 .pipe(gunzip())145 .pipe(extractTar);146 });147 it('should not have a tarball of Docker image in zip archive when explicitly not asking for it', (done) => {148 let tmpfile = tmp.tmpNameSync() + '.zip';149 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.zip?image=false';150 request.get(url)151 .on('error', function (err) {152 done(err);153 })154 .pipe(fs.createWriteStream(tmpfile))155 .on('finish', function () {156 let zip = new AdmZip(tmpfile);157 let zipEntries = zip.getEntries();158 let filenames = [];159 zipEntries.forEach(function (entry) {160 filenames.push(entry.entryName);161 });162 assert.notInclude(filenames, 'image.tar');163 assert.oneOf('.erc/metadata_o2r_1.json', filenames);164 done();165 });166 });167 it('should not have a tarball of Docker image in tar.gz archive when explicitly not asking for it', (done) => {168 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.tar.gz?image=false';169 let filenames = [];170 let extractTar = tarStream.extract();171 extractTar.on('entry', function (header, stream, next) {172 filenames.push(header.name);173 stream.on('end', function () {174 next();175 })176 stream.resume();177 });178 extractTar.on('finish', function () {179 assert.notInclude(filenames, 'image.tar');180 assert.oneOf('erc.yml', filenames);181 done();182 });183 extractTar.on('error', function (e) {184 done(e);185 });186 request.get(url)187 .on('error', function (err) {188 done(err);189 })190 .pipe(gunzip())191 .pipe(extractTar);192 });193 it('should not have a tarball of Docker image in gzipped tar archive when explicitly not asking for it', (done) => {194 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?image=false&gzip';195 let filenames = [];196 let extractTar = tarStream.extract();197 extractTar.on('entry', function (header, stream, next) {198 filenames.push(header.name);199 stream.on('end', function () {200 next();201 })202 stream.resume();203 });204 extractTar.on('finish', function () {205 assert.notInclude(filenames, 'image.tar');206 assert.oneOf('erc.yml', filenames);207 done();208 });209 extractTar.on('error', function (e) {210 done(e);211 });212 request.get(url)213 .on('error', function (err) {214 done(err);215 })216 .pipe(gunzip())217 .pipe(extractTar);218 });219 it('should contain expected files in tarball', (done) => {220 let tmpfile = tmp.tmpNameSync() + '.zip';221 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.zip';222 request.get(url)223 .on('error', function (err) {224 done(err);225 })226 .pipe(fs.createWriteStream(tmpfile))227 .on('finish', function () {228 let zip = new AdmZip(tmpfile);229 zip.getEntries().forEach(function (entry) {230 if (entry.entryName === 'image.tar') {231 let filenames = [];232 let manifestJson = null;233 let extractTar = tarStream.extract();234 extractTar.on('entry', function (header, stream, next) {235 filenames.push(header.name);236 if (header.name == 'manifest.json') {237 const chunks = [];238 stream.on('data', function (chunk) {239 chunks.push(chunk);240 });241 stream.on('end', function () {242 manifestJson = JSON.parse(chunks)[0];243 next();244 });245 } else {246 stream.on('end', function () {247 next();248 })249 }250 stream.resume();251 });252 extractTar.on('finish', function () {253 assert.oneOf('manifest.json', filenames);254 assert.oneOf('repositories', filenames);255 assert.include(filenames.toString(), '/VERSION');256 assert.include(filenames.toString(), '/layer.tar');257 assert.property(manifestJson, 'RepoTags');258 done();259 });260 extractTar.on('error', function (e) {261 done(e);262 });263 let bufferStream = new stream.PassThrough();264 bufferStream.end(new Buffer(entry.getData()));265 bufferStream.pipe(extractTar);266 }267 });268 });269 }).timeout(60000);270 it('should have the correct job tag on the image in tarball', (done) => {271 let tmpfile = tmp.tmpNameSync() + '.zip';272 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.zip';273 request.get(url)274 .on('error', function (err) {275 done(err);276 })277 .pipe(fs.createWriteStream(tmpfile))278 .on('finish', function () {279 let zip = new AdmZip(tmpfile);280 zip.getEntries().forEach(function (entry) {281 if (entry.entryName === 'image.tar') {282 let manifestJson = null;283 let extractTar = tarStream.extract();284 extractTar.on('entry', function (header, stream, next) {285 if (header.name == 'manifest.json') {286 const chunks = [];287 stream.on('data', function (chunk) {288 chunks.push(chunk);289 });290 stream.on('end', function () {291 manifestJson = JSON.parse(chunks)[0];292 next();293 });294 } else {295 stream.on('end', function () {296 next();297 })298 }299 stream.resume();300 });301 extractTar.on('finish', function () {302 assert.oneOf('job:' + job_id, manifestJson.RepoTags, '"job:<job_id>" tag is in RepoTags list');303 done();304 });305 extractTar.on('error', function (e) {306 done(e);307 });308 let bufferStream = new stream.PassThrough();309 bufferStream.end(new Buffer(entry.getData()));310 bufferStream.pipe(extractTar);311 }312 });313 });314 }).timeout(60000);315 it('should have the correct compendium tag on the image in tarball', (done) => {316 let tmpfile = tmp.tmpNameSync() + '.zip';317 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.zip';318 request.get(url)319 .on('error', function (err) {320 done(err);321 })322 .pipe(fs.createWriteStream(tmpfile))323 .on('finish', function () {324 let zip = new AdmZip(tmpfile);325 zip.getEntries().forEach(function (entry) {326 if (entry.entryName === 'image.tar') {327 let manifestJson = null;328 let extractTar = tarStream.extract();329 extractTar.on('entry', function (header, stream, next) {330 if (header.name == 'manifest.json') {331 const chunks = [];332 stream.on('data', function (chunk) {333 chunks.push(chunk);334 });335 stream.on('end', function () {336 manifestJson = JSON.parse(chunks)[0];337 next();338 });339 } else {340 stream.on('end', function () {341 next();342 })343 }344 stream.resume();345 });346 extractTar.on('finish', function () {347 assert.oneOf('erc:' + compendium_id, manifestJson.RepoTags, '"erc:<erc_id>" tag is in RepoTags list');348 done();349 });350 extractTar.on('error', function (e) {351 done(e);352 });353 let bufferStream = new stream.PassThrough();354 bufferStream.end(new Buffer(entry.getData()));355 bufferStream.pipe(extractTar);356 }357 });358 });359 }).timeout(60000);360 });361 describe('tinkering with local images outside of transporter', function () {362 it('should return an error (HTTP 400, error message in JSON response) when no job was started', (done) => {363 let compendium_id = null;364 let req = createCompendiumPostRequest('./test/workspace/with_metadata', cookie);365 request(req, (err, res, body) => {366 assert.ifError(err);367 compendium_id = JSON.parse(body).id;368 publishCandidate(compendium_id, cookie, () => {369 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.zip', (err, res, body) => {370 assert.ifError(err);371 let response = JSON.parse(body);372 console.log(response);373 assert.equal(res.statusCode, 400);374 assert.isObject(response);375 assert.property(response, 'error');376 assert.include(response.error, 'successful job execution');377 done();378 });379 });380 });381 }).timeout(30000);382 it('should not fail if image got additional tag', (done) => {383 let compendium_id_tag = null;384 let req = createCompendiumPostRequest('./test/workspace/with_metadata', cookie);385 request(req, (err, res, body) => {386 assert.ifError(err);387 compendium_id_tag = JSON.parse(body).id;388 publishCandidate(compendium_id_tag, cookie, () => {389 startJob(compendium_id_tag, id => {390 assert.ok(id);391 waitForJob(id, (finalStatus) => {392 exec('docker tag job:' + id + ' another:tag', (err, stdout, stderr) => {393 if (err || stderr) {394 assert.ifError(err);395 assert.ifError(stderr);396 } else {397 let tmpfile = tmp.tmpNameSync() + '.zip';398 request.get(global.test_host + '/api/v1/compendium/' + compendium_id_tag + '.zip')399 .on('error', function (err) {400 done(err);401 })402 .pipe(fs.createWriteStream(tmpfile))403 .on('finish', function () {404 let zip = new AdmZip(tmpfile);405 let zipEntries = zip.getEntries();406 let filenames = [];407 zipEntries.forEach(function (entry) {408 filenames.push(entry.entryName);409 });410 assert.oneOf('image.tar', filenames);411 assert.oneOf('erc.yml', filenames);412 done();413 });414 }415 });416 });417 });418 });419 });420 }).timeout(30000);421 });...

Full Screen

Full Screen

job-images.js

Source:job-images.js Github

copy

Full Screen

1/*2 * (C) Copyright 2017 o2r project3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *16 */17/* eslint-env mocha */18const assert = require('chai').assert;19const request = require('request');20const config = require('../config/config');21const createCompendiumPostRequest = require('./util').createCompendiumPostRequest;22const publishCandidate = require('./util').publishCandidate;23const waitForJob = require('./util').waitForJob;24const startJob = require('./util').startJob;25const mongojs = require('mongojs');26const fs = require('fs');27const path = require('path');28const debug = require('debug')('test:job-images');29const tmp = require('tmp');30const AdmZip = require('adm-zip');31const tarStream = require('tar-stream');32const stream = require('stream');33require("./setup");34const cookie_o2r = 's:C0LIrsxGtHOGHld8Nv2jedjL4evGgEHo.GMsWD5Vveq0vBt7/4rGeoH5Xx7Dd2pgZR9DvhKCyDTY';35let Docker = require('dockerode');36let docker = new Docker();37describe('Images in uploads and downloads', () => {38 var db = mongojs('localhost/muncher', ['compendia', 'jobs']);39 before(function (done) {40 db.compendia.drop(function (err, doc) {41 db.jobs.drop(function (err, doc) {42 done();43 });44 });45 });46 after(function (done) {47 db.close();48 done();49 });50 describe('Find image tarball in compendium if it is exists and use it for running the job', () => {51 let job_id, compendium_id, job_id2 = '';52 workspacePath = './test/workspace/with-image-tarball';53 let imageTag = 'erc:12345';54 imageTarballFile = path.join(workspacePath, config.bagtainer.imageTarballFile);55 before(function (done) {56 this.timeout(720000);57 uploadCompendiumWithImageTarball = function() {58 createCompendiumPostRequest(workspacePath, cookie_o2r, 'workspace', (requestData) => {59 request(requestData, (err, res, body) => {60 if (err) throw err;61 compendium_id = JSON.parse(body).id;62 publishCandidate(compendium_id, cookie_o2r, () => {63 startJob(compendium_id, id => {64 job_id = id;65 waitForJob(job_id, (finalStatus) => {66 done();67 });68 });69 });70 });71 });72 }73 fs.access(imageTarballFile, (err) => {74 if (err) {75 debug('build local image tarball at %s', imageTarballFile);76 docker.buildImage({77 context: workspacePath,78 src: ['Dockerfile']79 }, { t: imageTag }, function (err, stream) {80 if (err) throw err;81 stream.on('data', function (data) {82 s = JSON.parse(data.toString('utf8'));83 if (s.stream) debug(s.stream.substring(0, 100).trim());84 });85 stream.on('end', function () {86 debug('built image %s, now saving to %s', imageTag, imageTarballFile);87 fileStream = fs.createWriteStream(imageTarballFile);88 fileStream.on('finish', function () {89 debug('Image saved');90 uploadCompendiumWithImageTarball();91 });92 image = docker.getImage(imageTag);93 image.get((err, imageStream) => {94 if (err) throw err;95 imageStream.pipe(fileStream);96 });97 });98 });99 } else {100 debug('local image tarball file found as expected at %s. YOU MUST MANUALLY DELETE THIS FILE AND the upload cache file' +101 ', see test:util log below) if the content at %s changed. Uploading workspace now...',102 imageTarballFile, workspacePath);103 uploadCompendiumWithImageTarball();104 }105 });106 });107 it('should complete most steps and skip bag validation, manifest generation', (done) => {108 request(global.test_host + '/api/v1/job/' + job_id, (err, res, body) => {109 if (err) throw err;110 assert.ifError(err);111 let response = JSON.parse(body);112 assert.propertyVal(response.steps.validate_bag, 'status', 'skipped', 'validate bag');113 assert.propertyVal(response.steps.validate_compendium, 'status', 'success', 'validate compendium');114 assert.propertyVal(response.steps.image_prepare, 'status', 'success', 'image prepare');115 assert.propertyVal(response.steps.image_build, 'status', 'success', 'image build');116 assert.propertyVal(response.steps.image_execute, 'status', 'success', 'image execute');117 assert.propertyVal(response.steps.cleanup, 'status', 'success', 'cleanup');118 done();119 });120 });121 it('should not have a reference to the image digest in step image_build', function (done) {122 request(global.test_host + '/api/v1/job/' + job_id + '?steps=all', (err, res, body) => {123 assert.ifError(err);124 let response = JSON.parse(body);125 assert.notProperty(response.steps.image_save, 'imageId');126 done();127 });128 });129 it('should not have a reference to the image file in step image_save', function (done) {130 request(global.test_host + '/api/v1/job/' + job_id + '?steps=all', (err, res, body) => {131 assert.ifError(err);132 let response = JSON.parse(body);133 assert.notProperty(response.steps.image_save, 'file');134 done();135 });136 });137 it('should mention loading the existing tarball', function (done) {138 request(global.test_host + '/api/v1/job/' + job_id + '?steps=image_build', (err, res, body) => {139 assert.ifError(err);140 let response = JSON.parse(body);141 assert.property(response.steps.image_build, 'text');142 assert.include(JSON.stringify(response.steps.image_build.text), 'Image tarball found');143 assert.include(JSON.stringify(response.steps.image_build.text), 'Loaded image tarball from file ' + config.bagtainer.imageTarballFile);144 done();145 });146 });147 it('should have tagged an image for both the compendium and the job and have the original image tag (skipped if images are not kept)', function (done) {148 if (config.bagtainer.keepImages) {149 docker.listImages(function (err, images) {150 assert.ifError(err);151 let names = new Set();152 images.forEach(function (image) {153 if (image.RepoTags) {154 image.RepoTags.forEach(function (tag) {155 names.add(tag);156 });157 }158 });159 assert.include(names, config.bagtainer.image.prefix.compendium + compendium_id);160 assert.include(names, config.bagtainer.image.prefix.job + job_id);161 assert.include(names, imageTag);162 done();163 });164 } else {165 this.skip();166 }167 }).timeout(30000);168 });169 describe('Image tags tarballs', function () {170 before(function (done) {171 this.timeout(30000);172 createCompendiumPostRequest('./test/workspace/dummy', cookie_o2r, 'workspace', (requestData) => {173 request(requestData, (err, res, body) => {174 assert.ifError(err);175 compendium_id = JSON.parse(body).id;176 publishCandidate(compendium_id, cookie_o2r, () => {177 startJob(compendium_id, id => {178 assert.ok(id);179 job_id = id;180 waitForJob(job_id, (finalStatus) => {181 done();182 });183 })184 });185 });186 });187 });188 it('should have the correct job tag on the image in tarball', (done) => {189 let tmpfile = tmp.tmpNameSync() + '.zip';190 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.zip';191 request.get(url)192 .on('error', function (err) {193 done(err);194 })195 .pipe(fs.createWriteStream(tmpfile))196 .on('finish', function () {197 let zip = new AdmZip(tmpfile);198 zip.getEntries().forEach(function (entry) {199 if (entry.entryName === 'image.tar') {200 let manifestJson = null;201 let extractTar = tarStream.extract();202 let manifests = 0;203 extractTar.on('entry', function (header, stream, next) {204 if (header.name == 'manifest.json') {205 manifests++;206 const chunks = [];207 stream.on('data', function (chunk) {208 chunks.push(chunk);209 });210 stream.on('end', function () {211 manifestJson = JSON.parse(chunks)[0];212 next();213 });214 } else {215 stream.on('end', function () {216 next();217 })218 }219 stream.resume();220 });221 extractTar.on('finish', function () {222 assert.oneOf('job:' + job_id, manifestJson.RepoTags, '"job:<job_id>" tag is in RepoTags list');223 assert.equal(manifests, 2, 'two manifest files, only the newest will be extracted'); // see also https://github.com/npm/node-tar/issues/149224 done();225 });226 extractTar.on('error', function (e) {227 done(e);228 });229 let bufferStream = new stream.PassThrough();230 bufferStream.end(new Buffer(entry.getData()));231 bufferStream.pipe(extractTar);232 }233 });234 });235 }).timeout(60000);236 it('should have the correct compendium tag on the image in tarball', (done) => {237 let tmpfile = tmp.tmpNameSync() + '.zip';238 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.zip';239 request.get(url)240 .on('error', function (err) {241 done(err);242 })243 .pipe(fs.createWriteStream(tmpfile))244 .on('finish', function () {245 let zip = new AdmZip(tmpfile);246 zip.getEntries().forEach(function (entry) {247 if (entry.entryName === 'image.tar') {248 let manifestJson = null;249 let extractTar = tarStream.extract();250 extractTar.on('entry', function (header, stream, next) {251 if (header.name == 'manifest.json') {252 const chunks = [];253 stream.on('data', function (chunk) {254 chunks.push(chunk);255 });256 stream.on('end', function () {257 manifestJson = JSON.parse(chunks)[0];258 next();259 });260 } else {261 stream.on('end', function () {262 next();263 })264 }265 stream.resume();266 });267 extractTar.on('finish', function () {268 assert.oneOf('erc:' + compendium_id, manifestJson.RepoTags, '"erc:<erc_id>" tag is in RepoTags list');269 done();270 });271 extractTar.on('error', function (e) {272 done(e);273 });274 let bufferStream = new stream.PassThrough();275 bufferStream.end(new Buffer(entry.getData()));276 bufferStream.pipe(extractTar);277 }278 });279 });280 }).timeout(60000);281 });...

Full Screen

Full Screen

setup-conftest.test.js

Source:setup-conftest.test.js Github

copy

Full Screen

1/**2 * @jest-environment node3 */4// Mock external modules by default5jest.mock('@actions/core');6jest.mock('@actions/tool-cache');7// Mock Node.js core modules8jest.mock('os');9const os = require('os');10const path = require('path');11const io = require('@actions/io');12const core = require('@actions/core');13const tc = require('@actions/tool-cache');14const nock = require('nock');15const json = require('./releases.json');16const setup = require('../lib/setup-conftest');17// Overwrite defaults18// core.debug = jest19// .fn(console.log);20// core.error = jest21// .fn(console.error);22describe('Setup Conftest', () => {23 const HOME = process.env.HOME;24 const APPDATA = process.env.APPDATA;25 beforeEach(() => {26 process.env.HOME = '/tmp/asdf';27 process.env.APPDATA = '/tmp/asdf';28 });29 afterEach(async () => {30 await io.rmRF(process.env.HOME);31 process.env.HOME = HOME;32 process.env.APPDATA = APPDATA;33 });34 test('gets specific version on linux, amd64', async () => {35 const version = '0.20.0';36 core.getInput = jest37 .fn()38 .mockReturnValueOnce(version);39 tc.downloadTool = jest40 .fn()41 .mockReturnValueOnce('file.tar.gz');42 tc.extractTar = jest43 .fn()44 .mockReturnValueOnce('file');45 os.platform = jest46 .fn()47 .mockReturnValue('linux');48 os.arch = jest49 .fn()50 .mockReturnValue('amd64');51 nock('https://api.github.com')52 .get('/repos/open-policy-agent/conftest/releases')53 .reply(200, json);54 const versionString = await setup();55 expect(versionString).toEqual(`v${version}`);56 // downloaded CLI has been added to path57 expect(core.addPath).toHaveBeenCalled();58 });59 test('gets specific version on windows, 386', async () => {60 const version = '0.20.0';61 core.getInput = jest62 .fn()63 .mockReturnValueOnce(version);64 tc.downloadTool = jest65 .fn()66 .mockReturnValueOnce('file.tar.gz');67 tc.extractTar = jest68 .fn()69 .mockReturnValueOnce('file');70 os.platform = jest71 .fn()72 .mockReturnValue('win32');73 os.arch = jest74 .fn()75 .mockReturnValue('386');76 nock('https://api.github.com')77 .get('/repos/open-policy-agent/conftest/releases')78 .reply(200, json);79 const versionString = await setup();80 expect(versionString).toEqual(`v${version}`);81 // downloaded CLI has been added to path82 expect(core.addPath).toHaveBeenCalled();83 });84 test('gets latest version on linux, amd64', async () => {85 const version = 'latest';86 core.getInput = jest87 .fn()88 .mockReturnValueOnce(version);89 tc.downloadTool = jest90 .fn()91 .mockReturnValueOnce('file.tar.gz');92 tc.extractTar = jest93 .fn()94 .mockReturnValueOnce('file');95 os.platform = jest96 .fn()97 .mockReturnValue('linux');98 os.arch = jest99 .fn()100 .mockReturnValue('amd64');101 nock('https://api.github.com')102 .get('/repos/open-policy-agent/conftest/releases')103 .reply(200, json);104 const versionString = await setup();105 expect(versionString).toEqual('v0.21.0');106 // downloaded CLI has been added to path107 expect(core.addPath).toHaveBeenCalled();108 });109 test('fails when metadata cannot be downloaded', async () => {110 const version = 'latest';111 core.getInput = jest112 .fn()113 .mockReturnValueOnce(version);114 nock('https://api.github.com')115 .get('/repos/open-policy-agent/conftest/releases')116 .reply(404);117 try {118 await setup();119 } catch (e) {120 expect(core.error).toHaveBeenCalled();121 }122 });123 test('fails when specific version cannot be found', async () => {124 const version = '0.9.9';125 core.getInput = jest126 .fn()127 .mockReturnValueOnce(version);128 nock('https://api.github.com')129 .get('/repos/open-policy-agent/conftest/releases')130 .reply(200, json);131 try {132 await setup();133 } catch (e) {134 expect(core.error).toHaveBeenCalled();135 }136 });137 test('fails when CLI for os and architecture cannot be found', async () => {138 const version = '0.21.0';139 core.getInput = jest140 .fn()141 .mockReturnValueOnce(version);142 nock('https://api.github.com')143 .get('/repos/open-policy-agent/conftest/releases')144 .reply(200, json);145 tc.downloadTool = jest146 .fn()147 .mockReturnValueOnce('file.tar.gz');148 tc.extractTar = jest149 .fn()150 .mockReturnValueOnce('file');151 os.platform = jest152 .fn()153 .mockReturnValue('madeupplat');154 os.arch = jest155 .fn()156 .mockReturnValue('madeuparch');157 try {158 await setup();159 } catch (e) {160 expect(core.error).toHaveBeenCalled();161 }162 });163 test('fails when CLI cannot be downloaded', async () => {164 const version = '0.21.0';165 core.getInput = jest166 .fn()167 .mockReturnValueOnce(version);168 nock('https://api.github.com')169 .get('/repos/open-policy-agent/conftest/releases')170 .reply(200, json);171 tc.downloadTool = jest172 .fn()173 .mockReturnValueOnce('');174 tc.extractTar = jest175 .fn()176 .mockReturnValueOnce('');177 os.platform = jest178 .fn()179 .mockReturnValue('linux');180 os.arch = jest181 .fn()182 .mockReturnValue('amd64');183 try {184 await setup();185 } catch (e) {186 expect(core.error).toHaveBeenCalled();187 }188 });189 test('installs wrapper on linux', async () => {190 const version = '0.21.0';191 const wrapperPath = path.resolve([__dirname, '..', 'wrapper', 'dist', 'index.js'].join(path.sep));192 const ioMv = jest.spyOn(io, 'mv')193 .mockImplementation(() => {});194 const ioCp = jest.spyOn(io, 'cp')195 .mockImplementation(() => {});196 core.getInput = jest197 .fn()198 .mockReturnValueOnce(version)199 .mockReturnValueOnce('true');200 tc.downloadTool = jest201 .fn()202 .mockReturnValueOnce('file.tar.gz');203 tc.extractTar = jest204 .fn()205 .mockReturnValueOnce('file');206 os.platform = jest207 .fn()208 .mockReturnValue('linux');209 os.arch = jest210 .fn()211 .mockReturnValue('amd64');212 nock('https://api.github.com')213 .get('/repos/open-policy-agent/conftest/releases')214 .reply(200, json);215 await setup();216 expect(ioMv).toHaveBeenCalledWith(`file${path.sep}conftest`, `file${path.sep}conftest-bin`);217 expect(ioCp).toHaveBeenCalledWith(wrapperPath, `file${path.sep}conftest`);218 });219 test('installs wrapper on windows', async () => {220 const version = '0.21.0';221 const wrapperPath = path.resolve([__dirname, '..', 'wrapper', 'dist', 'index.js'].join(path.sep));222 const ioMv = jest.spyOn(io, 'mv')223 .mockImplementation(() => {});224 const ioCp = jest.spyOn(io, 'cp')225 .mockImplementation(() => {});226 core.getInput = jest227 .fn()228 .mockReturnValueOnce(version)229 .mockReturnValueOnce('true');230 tc.downloadTool = jest231 .fn()232 .mockReturnValueOnce('file.tar.gz');233 tc.extractTar = jest234 .fn()235 .mockReturnValueOnce('file');236 os.platform = jest237 .fn()238 .mockReturnValue('win32');239 os.arch = jest240 .fn()241 .mockReturnValue('386');242 nock('https://api.github.com')243 .get('/repos/open-policy-agent/conftest/releases')244 .reply(200, json);245 await setup();246 expect(ioMv).toHaveBeenCalledWith(`file${path.sep}conftest.exe`, `file${path.sep}conftest-bin.exe`);247 expect(ioCp).toHaveBeenCalledWith(wrapperPath, `file${path.sep}conftest`);248 });...

Full Screen

Full Screen

tar.js

Source:tar.js Github

copy

Full Screen

1/*2 * (C) Copyright 2017 o2r project3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *16 */17/* eslint-env mocha */18const assert = require('chai').assert;19const request = require('request');20const tarStream = require('tar-stream');21const gunzip = require('gunzip-maybe');22const createCompendiumPostRequest = require('./util').createCompendiumPostRequest;23const publishCandidate = require('./util').publishCandidate;24const startJob = require('./util').startJob;25const waitForJob = require('./util').waitForJob;26require("./setup")27const cookie = 's:C0LIrsxGtHOGHld8Nv2jedjL4evGgEHo.GMsWD5Vveq0vBt7/4rGeoH5Xx7Dd2pgZR9DvhKCyDTY';28describe('TAR downloading', function () {29 let compendium_id = null;30 before(function (done) {31 this.timeout(30000);32 let req = createCompendiumPostRequest('./test/workspace/with_metadata', cookie);33 request(req, (err, res, body) => {34 compendium_id = JSON.parse(body).id;35 publishCandidate(compendium_id, cookie, () => {36 startJob(compendium_id, job_id => {37 assert.ok(job_id);38 waitForJob(job_id, (finalStatus) => {39 done();40 });41 })42 });43 });44 });45 describe('Download compendium using .tar', function () {46 it('should respond with HTTP 200 in response', (done) => {47 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?image=false', (err, res) => {48 assert.ifError(err);49 assert.equal(res.statusCode, 200);50 done();51 });52 });53 it('content-type should be application/x-tar', (done) => {54 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?image=false', (err, res) => {55 assert.ifError(err);56 assert.equal(res.headers['content-type'], 'application/x-tar');57 done();58 });59 });60 it('content disposition is set to file name attachment', (done) => {61 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?image=false', (err, res) => {62 assert.ifError(err);63 assert.equal(res.headers['content-disposition'], 'attachment; filename="' + compendium_id + '.tar"');64 done();65 });66 });67 it('downloaded file is a tar archive (can be extracted, files exist)', (done) => {68 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?image=false';69 let filenames = [];70 let extractTar = tarStream.extract();71 extractTar.on('entry', function (header, stream, next) {72 filenames.push(header.name);73 stream.on('end', function () {74 next();75 })76 stream.resume();77 });78 extractTar.on('finish', function () {79 assert.oneOf('erc.yml', filenames);80 assert.oneOf('main.Rmd', filenames);81 assert.oneOf('.erc/metadata_o2r_1.json', filenames);82 done();83 });84 extractTar.on('error', function (e) {85 done(e);86 });87 request.get(url)88 .on('error', function (err) {89 done(err);90 })91 // NOT using gunzip() here, should not be needed!92 .pipe(extractTar);93 });94 });95 describe('Download compendium using .tar with gzip', function () {96 it('should respond with HTTP 200 in response for .gz', (done) => {97 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.tar.gz?image=false', (err, res) => {98 assert.ifError(err);99 assert.equal(res.statusCode, 200);100 done();101 });102 });103 it('should respond with HTTP 200 in response for ?gzip', (done) => {104 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?gzip&image=false', (err, res) => {105 assert.ifError(err);106 assert.equal(res.statusCode, 200);107 done();108 });109 });110 it('content-type should be application/gzip', (done) => { // https://superuser.com/a/960710111 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?gzip&image=false', (err, res) => {112 assert.ifError(err);113 assert.equal(res.headers['content-type'], 'application/gzip');114 done();115 });116 });117 it('content disposition is set to file name attachment', (done) => {118 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?gzip&image=false', (err, res) => {119 assert.ifError(err);120 assert.equal(res.headers['content-disposition'], 'attachment; filename="' + compendium_id + '.tar.gz"');121 done();122 });123 });124 it('downloaded file is a gzipped tar archive (can be extracted, files exist)', (done) => {125 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?gzip&image=false';126 let filenames = [];127 let extractTar = tarStream.extract();128 extractTar.on('entry', function (header, stream, next) {129 filenames.push(header.name);130 stream.on('end', function () {131 next();132 })133 stream.resume();134 });135 extractTar.on('finish', function () {136 assert.oneOf('erc.yml', filenames);137 assert.oneOf('main.Rmd', filenames);138 assert.oneOf('.erc/metadata_o2r_1.json', filenames);139 done();140 });141 extractTar.on('error', function (e) {142 done(e);143 });144 request.get(url)145 .on('error', function (err) {146 done(err);147 })148 .pipe(gunzip())149 .pipe(extractTar);150 });151 });...

Full Screen

Full Screen

download-tar.js

Source:download-tar.js Github

copy

Full Screen

1/*2 * (C) Copyright 2017 o2r project3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *16 */17/* eslint-env mocha */18const assert = require('chai').assert;19const request = require('request');20const tarStream = require('tar-stream');21const gunzip = require('gunzip-maybe');22const createCompendiumPostRequest = require('./util').createCompendiumPostRequest;23const publishCandidate = require('./util').publishCandidate;24const startJob = require('./util').startJob;25const waitForJob = require('./util').waitForJob;26require("./setup")27const cookie = 's:C0LIrsxGtHOGHld8Nv2jedjL4evGgEHo.GMsWD5Vveq0vBt7/4rGeoH5Xx7Dd2pgZR9DvhKCyDTY';28describe('TAR downloading', function () {29 let compendium_id = null;30 before(function (done) {31 this.timeout(720000);32 createCompendiumPostRequest('./test/workspace/dummy', cookie, 'workspace', (req) => {33 request(req, (err, res, body) => {34 compendium_id = JSON.parse(body).id;35 publishCandidate(compendium_id, cookie, () => {36 startJob(compendium_id, job_id => {37 assert.ok(job_id);38 waitForJob(job_id, (finalStatus) => {39 done();40 });41 })42 });43 });44 });45 });46 describe('Download compendium using .tar', function () {47 it('should respond with HTTP 200 in response', (done) => {48 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?image=false', (err, res) => {49 assert.ifError(err);50 assert.equal(res.statusCode, 200);51 done();52 });53 });54 it('should have correct response headers', (done) => {55 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?image=false', (err, res) => {56 assert.ifError(err);57 assert.equal(res.headers['content-type'], 'application/x-tar');58 assert.equal(res.headers['content-disposition'], 'attachment; filename="' + compendium_id + '.tar"');59 done();60 });61 });62 it('downloaded file is a tar archive (can be extracted, files exist)', (done) => {63 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?image=false';64 let filenames = [];65 let extractTar = tarStream.extract();66 extractTar.on('entry', function (header, stream, next) {67 filenames.push(header.name);68 stream.on('end', function () {69 next();70 })71 stream.resume();72 });73 extractTar.on('finish', function () {74 assert.oneOf('erc.yml', filenames);75 assert.oneOf('main.Rmd', filenames);76 assert.oneOf('.erc/metadata_o2r_1.json', filenames);77 done();78 });79 extractTar.on('error', function (e) {80 done(e);81 });82 request.get(url)83 .on('error', function (err) {84 done(err);85 })86 // NOT using gunzip() here, should not be needed!87 .pipe(extractTar);88 });89 });90 describe('Download compendium using .tar with gzip', function () {91 it('should respond with HTTP 200 in response for .gz', (done) => {92 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.tar.gz?image=false', (err, res) => {93 assert.ifError(err);94 assert.equal(res.statusCode, 200);95 done();96 });97 });98 it('should respond with HTTP 200 in response for ?gzip', (done) => {99 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?gzip&image=false', (err, res) => {100 assert.ifError(err);101 assert.equal(res.statusCode, 200);102 done();103 });104 });105 it('content-type should be application/gzip', (done) => { // https://superuser.com/a/960710106 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?gzip&image=false', (err, res) => {107 assert.ifError(err);108 assert.equal(res.headers['content-type'], 'application/gzip');109 done();110 });111 });112 it('content disposition is set to file name attachment', (done) => {113 request(global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?gzip&image=false', (err, res) => {114 assert.ifError(err);115 assert.equal(res.headers['content-disposition'], 'attachment; filename="' + compendium_id + '.tar.gz"');116 done();117 });118 });119 it('downloaded file is a gzipped tar archive (can be extracted, files exist)', (done) => {120 let url = global.test_host + '/api/v1/compendium/' + compendium_id + '.tar?gzip&image=false';121 let filenames = [];122 let extractTar = tarStream.extract();123 extractTar.on('entry', function (header, stream, next) {124 filenames.push(header.name);125 stream.on('end', function () {126 next();127 })128 stream.resume();129 });130 extractTar.on('finish', function () {131 assert.oneOf('erc.yml', filenames);132 assert.oneOf('main.Rmd', filenames);133 assert.oneOf('.erc/metadata_o2r_1.json', filenames);134 done();135 });136 extractTar.on('error', function (e) {137 done(e);138 });139 request.get(url)140 .on('error', function (err) {141 done(err);142 })143 .pipe(gunzip())144 .pipe(extractTar);145 });146 });...

Full Screen

Full Screen

untar.js

Source:untar.js Github

copy

Full Screen

1const path = require('path');2const tar = require('tar');3const fs = require('fs');4const extractTar = async (file, targetPath, entry) => {//, onClose5 let entry_type = -1;6 await new Promise((resolve, reject) => {7 let handleError = (err) => { reject(err); };8 // 首先检测入口9 tar.t({10 file: file,11 onentry: e => {12 if(entry_type == -1) {13 var entryName = e.path;14 if(/\/index\.html$/.test(entryName)){15 entry = entryName.split('/').shift();16 entry_type = 1;17 } else if(entryName == 'index.html') {18 entry_type = 0;19 }20 }21 }22 }, null, cb => {23 if(entry_type !== -1) {24 resolve();25 } else {26 reject();27 }28 });29 })30 .then(() => {31 const cwd = path.resolve(targetPath);32 fs.mkdir(cwd, { recursive: true }, function(err) {33 if(err) {34 Promise.reject(err);35 return;36 }37 // 解压文件38 tar.x({39 file: file,40 cwd: cwd41 }).then(() => Promise.resolve()42 ).catch(err => Promise.reject(err));43 });44 })45 .catch((err) => {46 console.error(err);47 });48 if(entry_type !== -1) {49 return entry_type == 1 ? entry : '';50 } else {51 return null;52 }53};54module.exports = {55 extractTar: extractTar...

Full Screen

Full Screen

extract-tar-spec.js

Source:extract-tar-spec.js Github

copy

Full Screen

...18 afterEach(() => {19 fsUtil.rmDir(workingdir);20 });21 it('unpacks an archive into a destination folder', done => {22 extractTar(path.join(__dirname, 'test-projects', 'tar-gz-example.tgz'), workingdir)23 .then(() => {24 expect(fs.readFileSync(path.join(workingdir, 'root.txt'), 'utf8')).toEqual('root\n');25 expect(fs.readFileSync(path.join(workingdir, 'subdir', 'sub.txt'), 'utf8')).toEqual('sub\n');26 })27 .then(done, done.fail);28 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const fs = require('fs');2const puppeteer = require('puppeteer');3(async () => {4 const browser = await puppeteer.launch();5 const page = await browser.newPage();6 const buffer = fs.readFileSync('test.tar');7 const files = await page.evaluate(buffer => {8 return window.extractTar(buffer);9 }, buffer);10 console.log(files);11 await browser.close();12})();13window.extractTar = function(buffer) {14 const files = [];15 const tar = new Tar(buffer);16 while (tar.hasNext()) {17 const file = tar.next();18 files.push(file);19 }20 return files;21};22class Tar {23 constructor(buffer) {24 this.buffer = buffer;25 this.offset = 0;26 }27 hasNext() {28 return this.offset < this.buffer.byteLength;29 }30 next() {31 const header = this.readHeader();32 const file = {33 body: this.readBody(header.size),34 };35 return file;36 }37 readHeader() {38 const header = {};39 header.name = this.read(100).replace(/\0.*$/g, '');40 header.mode = parseInt(this.read(8), 8);41 header.uid = parseInt(this.read(8), 8);42 header.gid = parseInt(this.read(8), 8);43 header.size = parseInt(this.read(12), 8);44 header.mtime = parseInt(this.read(12), 8);45 header.checksum = parseInt(this.read(8), 8);46 header.type = this.read(1);47 header.linkName = this.read(100).replace(/\0.*$/g, '');48 header.ustar = this.read(6

Full Screen

Using AI Code Generation

copy

Full Screen

1const { extractTar } = require('puppeteer');2(async () => {3 const result = await extractTar({4 });5 console.log(result);6})();7{ files: [ '/tmp/folder/file1.txt', '/tmp/folder/file2.txt' ] }

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful