Skip to content
Merged

fix #19

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 116 additions & 17 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,127 @@
version: 2
version: 2.1
parameters:
node-version:
type: string
default: "18.13.0"
orbs:
node: circleci/node@5.0.0
slack: circleci/slack@4.5.3
commands:
notify_on_failure:
steps:
- slack/notify:
event: fail
custom: |
{
"blocks": [
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": ":red_circle: *$CIRCLE_PROJECT_REPONAME*:*$CIRCLE_TAG* build failed"
}
]
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "View Job"
},
"url": "${CIRCLE_BUILD_URL}"
}
]
}
]
}
notify_on_pass:
steps:
- slack/notify:
event: pass
custom: |
{
"blocks": [
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": ":tada: *$CIRCLE_PROJECT_REPONAME*:*$CIRCLE_TAG* was successfully built and published"
}
]
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "View Job"
},
"url": "${CIRCLE_BUILD_URL}"
}
]
}
]
}
jobs:
test:
docker:
- image: circleci/node:14-stretch
- image: cimg/node:18.13.0
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run:
name: Installing Dependencies
command: npm install
- node/install:
node-version: << pipeline.parameters.node-version >>
- node/install-packages:
cache-path: ./node_modules
override-ci-command: npm install
- run:
name: Audit Dependencies
command: npm run audit
- save_cache:
key: dependency-cache-{{ checksum "package.json" }}
paths:
- node_modules
- run:
name: Running Integration Tests
command: npm run integration-test

name: Running Mocha Tests
command: npm test
build:
docker:
- image: cimg/node:18.13.0
user: root
steps:
- checkout
- node/install:
node-version: << pipeline.parameters.node-version >>
- setup_remote_docker:
version: 19.03.13
docker_layer_caching: true
# build and push Docker image
- run:
name: Install component-build-helper lib
command: npm install -g @elastic.io/component-build-helper
- run:
name: Build and publish docker image
command: build_component_docker
- notify_on_failure
- notify_on_pass
workflows:
version: 2
build_and_test:
test:
jobs:
- test:
name: "Running tests"
filters:
tags:
ignore: /.*/
publish_release:
jobs:
- test
- build:
name: "Build and publish docker image"
context:
- componentspusher
filters:
branches:
ignore: /.*/
tags:
only: /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/
10 changes: 10 additions & 0 deletions .grype-ignore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ignore:
- vulnerability: CVE-2022-3996
package:
name: libssl3
version: 3.0.7-r0

- vulnerability: CVE-2022-3996
package:
name: libcrypto3
version: 3.0.7-r0
7 changes: 3 additions & 4 deletions component.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"title": "AMQP",
"buildType": "docker",
"description": "Pub/Sub Component for async communication with queues and topics",
"version": "1.4.0",
"title": "AMQP component",
"description": "AMQP Component for async communication with queues and topics",
"version": "1.4.0-dev.50",
"credentials": {
"fields": {
"amqpURI": {
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
{
"name": "pub-sub",
"description": "Pub/Sub Component for async communication with queues and topics",
"name": "amqp-component",
"homepage": "http://www.elastic.io",
"author": {
"name": "elastic.io GmbH",
"email": "info@elastic.io",
"url": "https://elastic.io"
},
"files": [
"lib"
],
"engines": {
"node": "14"
"node": "18.x"
},
"scripts": {
"audit": "better-npm-audit audit --level high --production",
"pretest": "eslint spec-integration lib verifyCredentials.js --fix",
"test": "exit",
"test": "mocha \"spec/**/*spec.js\"",
"integration-test": "NODE_ENV=test mocha spec-integration/* --timeout 10000"
},
"main": "lib/index.js",
"keywords": [
"elasticio",
"component",
Expand Down Expand Up @@ -48,6 +43,6 @@
"mocha": "10.2.0",
"sinon": "15.0.2"
},
"repository": "elasticio/pub-sub",
"repository": "elasticio/amqp-component",
"license": "Apache-2.0"
}
63 changes: 63 additions & 0 deletions spec/actions/publush.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable no-unused-vars */
const sinon = require('sinon');
const { expect } = require('chai');
const { getContext } = require('../common');
const { AMQPClient } = require('../../lib/amqp');
const { process } = require('../../lib/actions/publish');

describe('processAction', () => {
beforeEach(() => {
});
afterEach(() => {
sinon.restore();
});

it('should send unencrypted message if doNotEncrypt is truthy', async () => {
const message = {
id: '123',
body: {
payload: {
name: 'John',
age: 30,
},
routingKey: 'test',
},
};
const configuration = {
doNotEncrypt: true,
};
const publishStub = sinon.stub(AMQPClient.prototype, 'publish').callsFake(async () => { });
const result = await process.call(getContext(), message, configuration);

expect(result).to.deep.equal(message);
expect(publishStub.getCall(0).args[0]).to.be.deep.equal(message.body.routingKey);
expect(publishStub.getCall(0).args[1]).to.be.deep.equal(Buffer.from(JSON.stringify(message.body.payload)));
expect(publishStub.getCall(0).args[2]).to.be.deep.equal({
contentType: 'application/octet-stream',
messageId: message.id,
});
});

it('should send encrypted message if doNotEncrypt is falsy', async () => {
const message = {
id: '123',
body: {
payload: {
name: 'John',
age: 30,
},
routingKey: 'test',
},
};
const publishStub = sinon.stub(AMQPClient.prototype, 'publish').callsFake(async () => { });
const result = await process.call(getContext(), message, {});

expect(result).to.deep.equal(message);
expect(publishStub.getCall(0).args[0]).to.be.deep.equal(message.body.routingKey);
expect(publishStub.getCall(0).args[1].toString('utf8')).to.be.deep.equal('Z�.�g�{.\u0018�Cƚ\n�95�Ì(��6�-rN*��h(]�=qEa���cu�<\u0013');
expect(publishStub.getCall(0).args[2]).to.be.deep.equal({
contentType: 'application/octet-stream',
messageId: message.id,
});
});
});
14 changes: 14 additions & 0 deletions spec/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable import/first */
process.env.LOG_OUTPUT_MODE = 'short';
process.env.API_RETRY_DELAY = '0';
process.env.ELASTICIO_MESSAGE_CRYPTO_PASSWORD = '1';
process.env.ELASTICIO_MESSAGE_CRYPTO_IV = 'yaku5ooSh3iedaem';
process.env.ELASTICIO_EXEC_ID = '1a';
process.env.ELASTICIO_FLOW_ID = '2b';
const sinon = require('sinon');
const getLogger = require('@elastic.io/component-logger');

exports.getContext = () => ({
logger: getLogger(),
emit: sinon.spy(),
});
27 changes: 27 additions & 0 deletions spec/triggers/consume.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable no-unused-vars */
const sinon = require('sinon');
const { expect } = require('chai');
const { getContext } = require('../common');
const { AMQPClient } = require('../../lib/amqp');
const consume = require('../../lib/triggers/consume');

describe('processAction', () => {
beforeEach(() => {
});
afterEach(() => {
sinon.restore();
});

it('should start consume messages', async () => {
const configuration = {
doNotEncrypt: true,
};
sinon.stub(AMQPClient.prototype, 'init').callsFake(async () => { });
const consumeStub = sinon.stub(AMQPClient.prototype, 'consume').callsFake(async () => { });
await consume.process.call(getContext(), {}, configuration);
expect(consumeStub.getCall(0).args[1]).to.be.deep.equal({
noAck: true,
consumerTag: `consumer_${process.env.ELASTICIO_EXEC_ID}_${process.env.ELASTICIO_FLOW_ID}`,
});
});
});