Skip to content

Commit

Permalink
rabbitmq test, other tests tweaked
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-pytel committed Mar 2, 2021
1 parent 3c75ea9 commit e16767f
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 8,548 deletions.
8,543 changes: 0 additions & 8,543 deletions package-lock.json

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@types/node": "^14.0.11",
"@types/semver": "^7.2.0",
"@types/uuid": "^8.0.0",
"amqplib": "^0.7.0",
"axios": "^0.21.0",
"express": "^4.17.1",
"grpc-tools": "^1.10.0",
Expand Down
47 changes: 47 additions & 0 deletions tests/plugins/amqplib/consumer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*!
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
*/

import * as http from 'http';
import agent from '../../../src';

agent.start({
serviceName: 'client',
maxBufferSize: 1000,
})

const server = http.createServer(async (req, res) => {
const q = 'queue';

await new Promise((resolve, reject) => {
require('amqplib/callback_api').connect(`amqp://${process.env.RABBITMQ_HOST}`, (err: any, conn: any) => {
conn.createChannel((err: any, ch: any) => {
ch.assertQueue(q, {durable: false});
ch.consume(q, (msg: any) => {
ch.ack(msg);
ch.close(() => conn.close(() => {
res.end('done');
resolve(null);
}));
});
});
});
});
});

server.listen(5001, () => console.info('Listening on port 5001...'));
80 changes: 80 additions & 0 deletions tests/plugins/amqplib/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#

version: "2.1"

services:
collector:
extends:
file: ../common/base-compose.yml
service: collector
networks:
- traveling-light

rabbitmq:
container_name: rabbitmq
ports:
- 5672:5672
healthcheck:
test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/5672"]
interval: 5s
timeout: 60s
retries: 120
image: "rabbitmq:latest"
networks:
- traveling-light

producer:
extends:
file: ../common/base-compose.yml
service: agent
ports:
- 5000:5000
environment:
RABBITMQ_HOST: rabbitmq
healthcheck:
test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/5000"]
interval: 5s
timeout: 60s
retries: 120
entrypoint:
["bash", "-c", "npx ts-node /app/tests/plugins/amqplib/producer.ts"]
depends_on:
rabbitmq:
condition: service_healthy

consumer:
extends:
file: ../common/base-compose.yml
service: agent
ports:
- 5001:5001
environment:
RABBITMQ_HOST: rabbitmq
healthcheck:
test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/5001"]
interval: 5s
timeout: 60s
retries: 120
entrypoint:
["bash", "-c", "npx ts-node /app/tests/plugins/amqplib/consumer.ts"]
depends_on:
rabbitmq:
condition: service_healthy

networks:
traveling-light:
74 changes: 74 additions & 0 deletions tests/plugins/amqplib/expected.data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#

segmentItems:
- serviceName: server
segmentSize: 1
segments:
- segmentId: not null
spans:
- operationName: RabbitMQ//queue/Producer
operationId: 0
parentSpanId: 0
spanId: 1
spanLayer: MQ
tags:
- { key: mq.broker, value: amqp://192.168.16.3:5672 }
- { key: mq.queue, value: queue }
startTime: gt 0
endTime: gt 0
componentId: 52
spanType: Exit
peer: 192.168.16.3:5672
skipAnalysis: false
- operationName: /producer
operationId: 0
parentSpanId: -1
spanId: 0
spanLayer: Http
tags:
- { key: http.url, value: 'http://localhost:5000/producer' }
- { key: http.method, value: GET }
- { key: http.status.code, value: '200' }
- { key: http.status.msg, value: OK }
startTime: gt 0
endTime: gt 0
componentId: 49
spanType: Entry
peer: not null
skipAnalysis: false
- serviceName: client
segmentSize: 1
segments:
- segmentId: not null
spans:
- operationName: RabbitMQ//queue/Consumer
operationId: 0
parentSpanId: -1
spanId: 0
spanLayer: MQ
tags:
- { key: mq.broker, value: 'amqp://192.168.16.3:5672' }
- { key: mq.queue, value: queue }
- { key: http.status.code, value: '200' } # this is RabbitMQ Entry inheriting HTTP server Entry, should not happen, need to fix
- { key: http.status.msg, value: OK }
startTime: gt 0
endTime: gt 0
componentId: 53
spanType: Entry
peer: not null
skipAnalysis: false
45 changes: 45 additions & 0 deletions tests/plugins/amqplib/producer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*!
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
*/

import * as http from 'http';
import agent from '../../../src';

agent.start({
serviceName: 'server',
maxBufferSize: 1000,
});

const server = http.createServer(async (req, res) => {
const q = 'queue';

await new Promise((resolve, reject) => {
require('amqplib/callback_api').connect(`amqp://${process.env.RABBITMQ_HOST}`, (err: any, conn: any) => {
conn.createChannel((err: any, ch: any) => {
ch.assertQueue(q, {durable: false});
ch.sendToQueue(q, Buffer.from('something callback to do'));
ch.close(() => conn.close(() => {
res.end('done');
resolve(null);
}));
});
});
});
});

server.listen(5000, () => console.info('Listening on port 5000...'));
57 changes: 57 additions & 0 deletions tests/plugins/amqplib/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*!
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
*/

import * as path from 'path';
import { DockerComposeEnvironment, StartedDockerComposeEnvironment, Wait } from 'testcontainers';
import axios from 'axios';
import waitForExpect from 'wait-for-expect';
import { promises as fs } from 'fs';

const rootDir = path.resolve(__dirname);

describe('plugin tests', () => {
let compose: StartedDockerComposeEnvironment;

beforeAll(async () => {
compose = await new DockerComposeEnvironment(rootDir, 'docker-compose.yml')
.withWaitStrategy('rabbitmq', Wait.forHealthCheck())
.up();
});

afterAll(async () => {
await compose.down();
});

it(__filename, async () => {
await waitForExpect(async () => expect((await axios.get('http://localhost:5000/producer')).status).toBe(200));
await waitForExpect(async () => expect((await axios.get('http://localhost:5001/consumer')).status).toBe(200));

const expectedData = await fs.readFile(path.join(rootDir, 'expected.data.yaml'), 'utf8');

try {
await waitForExpect(async () =>
expect((await axios.post('http://localhost:12800/dataValidate', expectedData)).status).toBe(200),
);
} catch (e) {
const actualData = (await axios.get('http://localhost:12800/receiveData')).data;
console.info({ actualData });
throw e;
}
});
});
2 changes: 1 addition & 1 deletion tests/plugins/mongodb/expected.data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ segmentItems:
segments:
- segmentId: not null
spans:
- operationName: /admin
- operationName: MongoDB/findOne
operationId: 0
parentSpanId: 0
spanId: 1
Expand Down
2 changes: 0 additions & 2 deletions tests/plugins/pg/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import * as http from 'http';
import agent from '../../../src';

process.env.SW_AGENT_LOGGING_LEVEL = 'ERROR';

agent.start({
serviceName: 'client',
maxBufferSize: 1000,
Expand Down
2 changes: 0 additions & 2 deletions tests/plugins/pg/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import * as http from 'http';
import {Client} from 'pg';
import agent from '../../../src';

process.env.SW_AGENT_LOGGING_LEVEL = 'ERROR';

agent.start({
serviceName: 'server',
maxBufferSize: 1000,
Expand Down

0 comments on commit e16767f

Please sign in to comment.