Skip to content

Commit

Permalink
[FAB-11059] unit test for pacakger
Browse files Browse the repository at this point in the history
- unit tests for packager main with 100% coverage
- linting fixes for associated files

Change-Id: I913dea276be158e1e17552c471b1d4162d4a8f9d
Signed-off-by: [email protected] <[email protected]>
  • Loading branch information
nklincoln committed Aug 29, 2018
1 parent 3c0f31a commit f48cb3c
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 48 deletions.
31 changes: 19 additions & 12 deletions fabric-client/lib/Packager.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
/*
Copyright 2017, 2018 IBM All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
* 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
*
* http://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.
*/

'use strict';

var Golang = require('./packager/Golang.js');
var Car = require('./packager/Car.js');
var Node = require('./packager/Node.js');
var utils = require('./utils.js');
const Golang = require('./packager/Golang.js');
const Car = require('./packager/Car.js');
const Node = require('./packager/Node.js');
const utils = require('./utils.js');

var logger = utils.getLogger('packager');
const logger = utils.getLogger('packager');

/**
* Utility function to package a chaincode. The contents will be returned as a byte array.
Expand All @@ -29,7 +36,7 @@ var logger = utils.getLogger('packager');
module.exports.package = function(chaincodePath, chaincodeType, devmode, metadataPath) {
logger.debug('packager: chaincodePath: %s, chaincodeType: %s, devmode: %s, metadataPath: %s',
chaincodePath,chaincodeType,devmode, metadataPath);
return new Promise(function(resolve, reject) {
return new Promise((resolve, reject) => {
if (devmode) {
logger.debug('packager: Skipping chaincode packaging due to devmode configuration');
return resolve(null);
Expand All @@ -40,7 +47,7 @@ module.exports.package = function(chaincodePath, chaincodeType, devmode, metadat
return reject(new Error('Missing chaincodePath parameter'));
}

let type = chaincodeType ? chaincodeType : 'golang';
const type = chaincodeType ? chaincodeType : 'golang';
logger.debug('packager: type %s ',type);

let handler;
Expand Down
34 changes: 17 additions & 17 deletions fabric-client/lib/packager/BasePackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

'use strict';

var fs = require('fs-extra');
var klaw = require('klaw');
var tar = require('tar-stream');
var path = require('path');
var zlib = require('zlib');
const fs = require('fs-extra');
const klaw = require('klaw');
const tar = require('tar-stream');
const path = require('path');
const zlib = require('zlib');
const utils = require('../utils.js');

let logger = utils.getLogger('packager/BasePackager.js');
const logger = utils.getLogger('packager/BasePackager.js');

var BasePackager = class {
const BasePackager = class {

/**
* Constructor
Expand Down Expand Up @@ -68,11 +68,11 @@ var BasePackager = class {
findMetadataDescriptors (filePath) {
return new Promise((resolve, reject) => {
logger.debug('findMetadataDescriptors : start');
var descriptors = [];
const descriptors = [];
klaw(filePath).on('data', (entry) => {
if (entry.stats.isFile() && this.isMetadata(entry.path)) {

var desc = {
const desc = {
name: path.join('META-INF', path.relative(filePath, entry.path).split('\\').join('/')), // for windows style paths
fqp: entry.path
};
Expand All @@ -98,7 +98,7 @@ var BasePackager = class {
* @returns {boolean} Returns true for valid metadata descriptors.
*/
isMetadata (filePath) {
var extensions = ['.json'];
const extensions = ['.json'];
return (extensions.indexOf(path.extname(filePath)) != -1);
}

Expand All @@ -125,13 +125,13 @@ var BasePackager = class {
packEntry (pack, desc) {
return new Promise((resolve, reject) => {
// Use a synchronous read to reduce non-determinism
var content = fs.readFileSync(desc.fqp);
const content = fs.readFileSync(desc.fqp);
if (!content) {
reject(new Error('failed to read ' + desc.fqp));
} else {
// Use a deterministic "zero-time" for all date fields
var zeroTime = new Date(0);
var header = {
const zeroTime = new Date(0);
const header = {
name: desc.name,
size: content.size,
mode: 0o100644,
Expand Down Expand Up @@ -159,7 +159,7 @@ var BasePackager = class {
*/
generateTarGz (descriptors, dest) {
return new Promise((resolve, reject) => {
var pack = tar.pack();
const pack = tar.pack();

// Setup the pipeline to compress on the fly and resolve/reject the promise
pack.pipe(zlib.createGzip()).pipe(dest).on('finish', () => {
Expand All @@ -171,9 +171,9 @@ var BasePackager = class {
// Iterate through each descriptor in the order it was provided and resolve
// the entry asynchronously. We will gather results below before
// finalizing the tarball
var tasks = [];
for (let desc of descriptors) {
var task = this.packEntry(pack, desc);
const tasks = [];
for (const desc of descriptors) {
const task = this.packEntry(pack, desc);
tasks.push(task);
}

Expand Down
22 changes: 11 additions & 11 deletions fabric-client/lib/packager/Golang.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

'use strict';

var klaw = require('klaw');
var path = require('path');
var sbuf = require('stream-buffers');
var utils = require('../utils.js');
var BasePackager = require('./BasePackager');
const klaw = require('klaw');
const path = require('path');
const sbuf = require('stream-buffers');
const utils = require('../utils.js');
const BasePackager = require('./BasePackager');

var logger = utils.getLogger('packager/Golang.js');
const logger = utils.getLogger('packager/Golang.js');

class GolangPackager extends BasePackager {

Expand All @@ -28,17 +28,17 @@ class GolangPackager extends BasePackager {
logger.debug('packaging GOLANG from %s', chaincodePath);

// Determine the user's $GOPATH
let goPath = process.env['GOPATH'];
const goPath = process.env['GOPATH'];

// Compose the path to the chaincode project directory
let projDir = path.join(goPath, 'src', chaincodePath);
const projDir = path.join(goPath, 'src', chaincodePath);

// We generate the tar in two phases: First grab a list of descriptors,
// and then pack them into an archive. While the two phases aren't
// strictly necessary yet, they pave the way for the future where we
// will need to assemble sources from multiple packages

var buffer = new sbuf.WritableStreamBuffer();
const buffer = new sbuf.WritableStreamBuffer();

return this.findSource(goPath, projDir).then((srcDescriptors) => {
if (metadataPath){
Expand Down Expand Up @@ -67,12 +67,12 @@ class GolangPackager extends BasePackager {
*/
findSource (goPath, filePath) {
return new Promise((resolve, reject) => {
var descriptors = [];
const descriptors = [];
klaw(filePath).on('data', (entry) => {

if (entry.stats.isFile() && super.isSource(entry.path)) {

var desc = {
const desc = {
name: path.relative(goPath, entry.path).split('\\').join('/'), // for windows style paths
fqp: entry.path
};
Expand Down
12 changes: 6 additions & 6 deletions fabric-client/lib/packager/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const sbuf = require('stream-buffers');
const utils = require('../utils.js');
const walk = require('ignore-walk');

let logger = utils.getLogger('packager/Node.js');
const logger = utils.getLogger('packager/Node.js');

let BasePackager = require('./BasePackager');
const BasePackager = require('./BasePackager');

class NodePackager extends BasePackager {

Expand All @@ -29,14 +29,14 @@ class NodePackager extends BasePackager {
logger.debug('packaging Node from %s', chaincodePath);

// Compose the path to the chaincode project directory
let projDir = chaincodePath;
const projDir = chaincodePath;

// We generate the tar in two phases: First grab a list of descriptors,
// and then pack them into an archive. While the two phases aren't
// strictly necessary yet, they pave the way for the future where we
// will need to assemble sources from multiple packages

let buffer = new sbuf.WritableStreamBuffer();
const buffer = new sbuf.WritableStreamBuffer();
return this.findSource(projDir).then((srcDescriptors) => {
if (metadataPath){
return super.findMetadataDescriptors(metadataPath)
Expand Down Expand Up @@ -69,7 +69,7 @@ class NodePackager extends BasePackager {
// follow symlink dirs
follow: true
}).then((files) => {
let descriptors = [];
const descriptors = [];

if (!files) {
files = [];
Expand All @@ -79,7 +79,7 @@ class NodePackager extends BasePackager {
files = files.filter(f => f.indexOf('node_modules') !== 0);

files.forEach((entry) => {
let desc = {
const desc = {
name: path.join('src', entry).split('\\').join('/'), // for windows style paths
fqp: path.join(filePath, entry)
};
Expand Down
Loading

0 comments on commit f48cb3c

Please sign in to comment.