Skip to content

Commit 8316dcd

Browse files
authored
fix (#19)
1 parent 3dec4e8 commit 8316dcd

File tree

8 files changed

+240
-33
lines changed

8 files changed

+240
-33
lines changed

.circleci/config.yml

Lines changed: 116 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,127 @@
1-
version: 2
1+
version: 2.1
2+
parameters:
3+
node-version:
4+
type: string
5+
default: "18.13.0"
6+
orbs:
7+
node: circleci/[email protected]
8+
slack: circleci/[email protected]
9+
commands:
10+
notify_on_failure:
11+
steps:
12+
- slack/notify:
13+
event: fail
14+
custom: |
15+
{
16+
"blocks": [
17+
{
18+
"type": "section",
19+
"fields": [
20+
{
21+
"type": "mrkdwn",
22+
"text": ":red_circle: *$CIRCLE_PROJECT_REPONAME*:*$CIRCLE_TAG* build failed"
23+
}
24+
]
25+
},
26+
{
27+
"type": "actions",
28+
"elements": [
29+
{
30+
"type": "button",
31+
"text": {
32+
"type": "plain_text",
33+
"text": "View Job"
34+
},
35+
"url": "${CIRCLE_BUILD_URL}"
36+
}
37+
]
38+
}
39+
]
40+
}
41+
notify_on_pass:
42+
steps:
43+
- slack/notify:
44+
event: pass
45+
custom: |
46+
{
47+
"blocks": [
48+
{
49+
"type": "section",
50+
"fields": [
51+
{
52+
"type": "mrkdwn",
53+
"text": ":tada: *$CIRCLE_PROJECT_REPONAME*:*$CIRCLE_TAG* was successfully built and published"
54+
}
55+
]
56+
},
57+
{
58+
"type": "actions",
59+
"elements": [
60+
{
61+
"type": "button",
62+
"text": {
63+
"type": "plain_text",
64+
"text": "View Job"
65+
},
66+
"url": "${CIRCLE_BUILD_URL}"
67+
}
68+
]
69+
}
70+
]
71+
}
272
jobs:
373
test:
474
docker:
5-
- image: circleci/node:14-stretch
75+
- image: cimg/node:18.13.0
676
steps:
777
- checkout
8-
- restore_cache:
9-
key: dependency-cache-{{ checksum "package.json" }}
10-
- run:
11-
name: Installing Dependencies
12-
command: npm install
78+
- node/install:
79+
node-version: << pipeline.parameters.node-version >>
80+
- node/install-packages:
81+
cache-path: ./node_modules
82+
override-ci-command: npm install
1383
- run:
1484
name: Audit Dependencies
1585
command: npm run audit
16-
- save_cache:
17-
key: dependency-cache-{{ checksum "package.json" }}
18-
paths:
19-
- node_modules
2086
- run:
21-
name: Running Integration Tests
22-
command: npm run integration-test
23-
87+
name: Running Mocha Tests
88+
command: npm test
89+
build:
90+
docker:
91+
- image: cimg/node:18.13.0
92+
user: root
93+
steps:
94+
- checkout
95+
- node/install:
96+
node-version: << pipeline.parameters.node-version >>
97+
- setup_remote_docker:
98+
version: 19.03.13
99+
docker_layer_caching: true
100+
# build and push Docker image
101+
- run:
102+
name: Install component-build-helper lib
103+
command: npm install -g @elastic.io/component-build-helper
104+
- run:
105+
name: Build and publish docker image
106+
command: build_component_docker
107+
- notify_on_failure
108+
- notify_on_pass
24109
workflows:
25-
version: 2
26-
build_and_test:
110+
test:
111+
jobs:
112+
- test:
113+
name: "Running tests"
114+
filters:
115+
tags:
116+
ignore: /.*/
117+
publish_release:
27118
jobs:
28-
- test
119+
- build:
120+
name: "Build and publish docker image"
121+
context:
122+
- componentspusher
123+
filters:
124+
branches:
125+
ignore: /.*/
126+
tags:
127+
only: /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/

.grype-ignore.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ignore:
2+
- vulnerability: CVE-2022-3996
3+
package:
4+
name: libssl3
5+
version: 3.0.7-r0
6+
7+
- vulnerability: CVE-2022-3996
8+
package:
9+
name: libcrypto3
10+
version: 3.0.7-r0

component.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
2-
"title": "AMQP",
3-
"buildType": "docker",
4-
"description": "Pub/Sub Component for async communication with queues and topics",
5-
"version": "1.4.0",
2+
"title": "AMQP component",
3+
"description": "AMQP Component for async communication with queues and topics",
4+
"version": "1.4.0-dev.50",
65
"credentials": {
76
"fields": {
87
"amqpURI": {

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
{
2-
"name": "pub-sub",
3-
"description": "Pub/Sub Component for async communication with queues and topics",
2+
"name": "amqp-component",
43
"homepage": "http://www.elastic.io",
54
"author": {
65
"name": "elastic.io GmbH",
76
"email": "[email protected]",
87
"url": "https://elastic.io"
98
},
10-
"files": [
11-
"lib"
12-
],
139
"engines": {
14-
"node": "14"
10+
"node": "18.x"
1511
},
1612
"scripts": {
1713
"audit": "better-npm-audit audit --level high --production",
1814
"pretest": "eslint spec-integration lib verifyCredentials.js --fix",
19-
"test": "exit",
15+
"test": "mocha \"spec/**/*spec.js\"",
2016
"integration-test": "NODE_ENV=test mocha spec-integration/* --timeout 10000"
2117
},
22-
"main": "lib/index.js",
2318
"keywords": [
2419
"elasticio",
2520
"component",
@@ -48,6 +43,6 @@
4843
"mocha": "10.2.0",
4944
"sinon": "15.0.2"
5045
},
51-
"repository": "elasticio/pub-sub",
46+
"repository": "elasticio/amqp-component",
5247
"license": "Apache-2.0"
5348
}

spec/actions/publush.spec.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* eslint-disable no-unused-vars */
2+
const sinon = require('sinon');
3+
const { expect } = require('chai');
4+
const { getContext } = require('../common');
5+
const { AMQPClient } = require('../../lib/amqp');
6+
const { process } = require('../../lib/actions/publish');
7+
8+
describe('processAction', () => {
9+
beforeEach(() => {
10+
});
11+
afterEach(() => {
12+
sinon.restore();
13+
});
14+
15+
it('should send unencrypted message if doNotEncrypt is truthy', async () => {
16+
const message = {
17+
id: '123',
18+
body: {
19+
payload: {
20+
name: 'John',
21+
age: 30,
22+
},
23+
routingKey: 'test',
24+
},
25+
};
26+
const configuration = {
27+
doNotEncrypt: true,
28+
};
29+
const publishStub = sinon.stub(AMQPClient.prototype, 'publish').callsFake(async () => { });
30+
const result = await process.call(getContext(), message, configuration);
31+
32+
expect(result).to.deep.equal(message);
33+
expect(publishStub.getCall(0).args[0]).to.be.deep.equal(message.body.routingKey);
34+
expect(publishStub.getCall(0).args[1]).to.be.deep.equal(Buffer.from(JSON.stringify(message.body.payload)));
35+
expect(publishStub.getCall(0).args[2]).to.be.deep.equal({
36+
contentType: 'application/octet-stream',
37+
messageId: message.id,
38+
});
39+
});
40+
41+
it('should send encrypted message if doNotEncrypt is falsy', async () => {
42+
const message = {
43+
id: '123',
44+
body: {
45+
payload: {
46+
name: 'John',
47+
age: 30,
48+
},
49+
routingKey: 'test',
50+
},
51+
};
52+
const publishStub = sinon.stub(AMQPClient.prototype, 'publish').callsFake(async () => { });
53+
const result = await process.call(getContext(), message, {});
54+
55+
expect(result).to.deep.equal(message);
56+
expect(publishStub.getCall(0).args[0]).to.be.deep.equal(message.body.routingKey);
57+
expect(publishStub.getCall(0).args[1].toString('utf8')).to.be.deep.equal('Z�.�g�{.\u0018�Cƚ\n�95�Ì(��6�-rN*��h(]�=qEa���cu�<\u0013');
58+
expect(publishStub.getCall(0).args[2]).to.be.deep.equal({
59+
contentType: 'application/octet-stream',
60+
messageId: message.id,
61+
});
62+
});
63+
});

spec/common.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-disable import/first */
2+
process.env.LOG_OUTPUT_MODE = 'short';
3+
process.env.API_RETRY_DELAY = '0';
4+
process.env.ELASTICIO_MESSAGE_CRYPTO_PASSWORD = '1';
5+
process.env.ELASTICIO_MESSAGE_CRYPTO_IV = 'yaku5ooSh3iedaem';
6+
process.env.ELASTICIO_EXEC_ID = '1a';
7+
process.env.ELASTICIO_FLOW_ID = '2b';
8+
const sinon = require('sinon');
9+
const getLogger = require('@elastic.io/component-logger');
10+
11+
exports.getContext = () => ({
12+
logger: getLogger(),
13+
emit: sinon.spy(),
14+
});

spec/triggers/consume.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable no-unused-vars */
2+
const sinon = require('sinon');
3+
const { expect } = require('chai');
4+
const { getContext } = require('../common');
5+
const { AMQPClient } = require('../../lib/amqp');
6+
const consume = require('../../lib/triggers/consume');
7+
8+
describe('processAction', () => {
9+
beforeEach(() => {
10+
});
11+
afterEach(() => {
12+
sinon.restore();
13+
});
14+
15+
it('should start consume messages', async () => {
16+
const configuration = {
17+
doNotEncrypt: true,
18+
};
19+
sinon.stub(AMQPClient.prototype, 'init').callsFake(async () => { });
20+
const consumeStub = sinon.stub(AMQPClient.prototype, 'consume').callsFake(async () => { });
21+
await consume.process.call(getContext(), {}, configuration);
22+
expect(consumeStub.getCall(0).args[1]).to.be.deep.equal({
23+
noAck: true,
24+
consumerTag: `consumer_${process.env.ELASTICIO_EXEC_ID}_${process.env.ELASTICIO_FLOW_ID}`,
25+
});
26+
});
27+
});

0 commit comments

Comments
 (0)