Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Commit

Permalink
Set up agent tests (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
kezhenxu94 committed Jun 7, 2020
1 parent 64bb646 commit 099adcf
Show file tree
Hide file tree
Showing 18 changed files with 532 additions and 15 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ jobs:
node-version: [8, 9, 10]
steps:
- uses: actions/checkout@v2
- name: checkout submodules
shell: bash
run: |
git submodule sync --recursive
git -c protocol.version=2 submodule update --init --force --recursive --depth=1
with:
submodules: true
- name: Cache node modules
uses: actions/cache@v1
with:
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test

on:
pull_request:
push:
branches:
- master
tags:
- 'v*'

jobs:
Test:
runs-on: ubuntu-18.04
timeout-minutes: 30
steps:
- uses: actions/checkout@v2
with:
submodules: true
- name: Use NodeJS 10
uses: actions/setup-node@v1
with:
node-version: 10
- name: Test
run: cd modules/nodejs-agent && npm i && npm run test
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ SkyAPM Node.js is the Node.js instrumentation agent, which is compatible with [A

## Support List
1. [Http](https://nodejs.org/api/http.html)
2. [Mysql](https://github.com/mysqljs/mysql)
3. [Egg](https://github.com/eggjs/egg)
1. [Mysql](https://github.com/mysqljs/mysql)
1. [Egg](https://github.com/eggjs/egg)

## Plugin Test
Read [plugin test](docs/PluginTest.md) if you want to contribute a new plugin to the community.

# Contact Us
* Submit an issue
Expand Down
32 changes: 32 additions & 0 deletions docs/PluginTest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Plugin Test

Plugin test is required and should pass before a new plugin is able to be merged into the master branch.

## Mock Collector

Mock Collector respects the same protocol as the SkyWalking backend, and thus receives the report data from the agent side,
besides, it also exposes some http endpoints for verification.

## Tested Service

A tested service is a service involving the plugin that is to be tested, and exposes some endpoints to trigger the instrumentation
and report data to the mock collector.

## Docker Compose

`docker-compose` is used to orchestrate the mock collector and the tested service(s), the `docker-compose.yml` should be
able to run with `docker-compose -f docker-compose.yml up` in the standalone mode, which can be used in debugging too.

## Expected Data

The `expected.data.yml` file contains the expected segment data after we have triggered the instrumentation and report to mock collector,
since the mock collector received the segment data then, we can post the expected data to the mock collector and verify whether
they match. This can be done through the `/dataValidate` of the mock collector, say `http://collector:12800/dataValidate`, for example.

## Example

If we want to test the plugin for the built-in library `http`, we will:

1. Build a tested service, which sets up an http server by `http` library, and exposes an http endpoint to be triggered in the test codes, say `/trigger`, take [this service](../modules/nodejs-agent/test/plugins/http/serverA.js) as example.
1. Compose a `docker-compose.yml` file, orchestrating the service built in step 1 and the mock collector, take [this `docker-compose.yml`](../modules/nodejs-agent/test/plugins/http/docker-compose.yml) as example.
1. Write test codes to trigger the endpoint int step 1, and send the expected data file to the mock collector to verify, take [this test](../modules/nodejs-agent/test/plugins/http/http.test.js) as example.
2 changes: 2 additions & 0 deletions modules/nodejs-agent/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/.git
**/node_modules
4 changes: 2 additions & 2 deletions modules/nodejs-agent/lib/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ AgentConfig.prototype.setDirectServices = function(directServices) {


AgentConfig.prototype.initConfig = function(agentOptions) {
this._serviceName = process.env.SW_SERVICE_NAME || (agentOptions && agentOptions.serviceName) || "You Application";
this._instanceName = process.env.SW_INSTANCE_NAME || (agentOptions && agentOptions.instanceName) || "You Instance";
this._serviceName = process.env.SW_SERVICE_NAME || (agentOptions && agentOptions.serviceName) || "Your Application";
this._instanceName = process.env.SW_INSTANCE_NAME || (agentOptions && agentOptions.instanceName) || "Your Instance";
this._directServices = process.env.SW_DIRECT_SERVERS || (agentOptions && agentOptions.directServers) || "localhost:11800";
this._authentication = process.env.SW_AUTHENTICATION || (agentOptions && agentOptions.authentication) || "";
};
Expand Down
2 changes: 1 addition & 1 deletion modules/nodejs-agent/lib/plugins/http/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ module.exports = function(httpModule, instrumentation, contextManager) {
function wrapRequest(original) {
return function(options, callback) {
let contextCarrier = new ContextCarrier();
let span = contextManager.createExitSpan(options.path, options.hostname + ":" + options.port, contextCarrier);
let span = contextManager.createExitSpan(options.path, (options.hostname || options.host) + ":" + options.port, contextCarrier);
contextCarrier.pushBy(function(key, value) {
if (!options.hasOwnProperty("headers") || !options.headers) {
options.headers = {};
Expand Down
6 changes: 3 additions & 3 deletions modules/nodejs-agent/lib/trace/context-carrier.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ ContextCarrier.prototype.serialize = function() {
traceContextArray.push(Base64.encode(this._traceId));
traceContextArray.push(Base64.encode(this._traceSegmentId));
traceContextArray.push(this._spanId);
traceContextArray.push(this._parentService);
traceContextArray.push(this._parentServiceInstance);
traceContextArray.push(Base64.encode(this._parentService));
traceContextArray.push(Base64.encode(this._parentServiceInstance));
traceContextArray.push(Base64.encode(this._parentEndpoint));
traceContextArray.push(Base64.encode(this._addressUsedAtClient));
return traceContextArray.join("-");
Expand All @@ -53,7 +53,7 @@ ContextCarrier.prototype.deserialize = function(traceContext) {
}

let traceContextSegment = traceContext.split("-");
if (traceContextSegment.length != 8) {
if (traceContextSegment.length !== 8) {
return this;
}

Expand Down
9 changes: 7 additions & 2 deletions modules/nodejs-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"generate-source": "run-script-os",
"generate-source:win32": "rd lib/network && md lib/network && cd proto && grpc_tools_node_protoc --js_out=import_style=commonjs,binary:../lib/network/ --grpc_out=../lib/network/ common/*.proto language-agent/Tracing.proto management/*.proto",
"generate-source:darwin:linux": "rm -rf lib/network && mkdir -p lib/network && cd proto && grpc_tools_node_protoc --js_out=import_style=commonjs,binary:../lib/network/ --grpc_out=../lib/network/ common/*.proto language-agent/Tracing.proto management/*.proto",
"check": "eslint lib/ && eslint index.js"
"check": "eslint lib/ && eslint index.js",
"test": "mocha test/**/*.test.js --recursive"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -46,14 +47,18 @@
"semver": "^5.5.1"
},
"devDependencies": {
"axios": "^0.19.2",
"cz-conventional-changelog": "^2.1.0",
"eslint": "^4.19.1",
"eslint-config-google": "^0.9.1",
"eslint-plugin-header": "^1.2.0",
"grpc-tools": "1.7.1",
"mocha": "^7.2.0",
"mysql": "^2.15.0",
"require-self": "^0.2.1",
"run-script-os": "^1.0.3"
"run-script-os": "^1.0.3",
"wait-until": "0.0.2",
"yaml": "^1.10.0"
},
"eslintIgnore": [
"lib/network/common/*.js",
Expand Down
44 changes: 44 additions & 0 deletions modules/nodejs-agent/test/common/Dockerfile.tool
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# 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.
#

FROM openjdk:8

WORKDIR /tests

ARG COMMIT_HASH=3c9d7099f05dc4a4b937c8a47506e56c130b6221

ADD https://github.com/apache/skywalking-agent-test-tool/archive/${COMMIT_HASH}.tar.gz .

RUN tar -xf ${COMMIT_HASH}.tar.gz --strip 1

RUN rm ${COMMIT_HASH}.tar.gz

RUN ./mvnw -B -DskipTests package

FROM openjdk:8

EXPOSE 19876 12800

WORKDIR /tests

COPY --from=0 /tests/dist/skywalking-mock-collector.tar.gz /tests

RUN tar -xf skywalking-mock-collector.tar.gz --strip 1

RUN chmod +x bin/collector-startup.sh

ENTRYPOINT bin/collector-startup.sh
36 changes: 36 additions & 0 deletions modules/nodejs-agent/test/common/base-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# 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:
base_collector:
build:
context: .
dockerfile: Dockerfile.tool
ports:
- 12800:12800
networks:
- beyond
healthcheck:
test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/12800"]
interval: 5s
timeout: 60s
retries: 120

networks:
beyond:
60 changes: 60 additions & 0 deletions modules/nodejs-agent/test/common/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to the SkyAPM 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.
*/

const wait = require("wait-until");
const {spawn, exec} = require("child_process");

const ifRebuild = process.env.BUILD !== "false";

module.exports = {
/**
* Spawn the containers defined in [composeFile].
*
* @param {string} composeFile
* @param {function(function(boolean)): void} ready readiness callback.
* @return {Promise<void>}
*/
setUp: (composeFile, ready) => new Promise((resolve, reject) => {
const options = ["--force-recreate", "-d"];
ifRebuild && options.push("--build");

const composeUp = spawn("docker-compose", ["-f", composeFile, "up", ...options]);

composeUp.stdout.on("data", (data) => process.stdout.write(data.toString()));
composeUp.stderr.on("data", (data) => process.stderr.write(data.toString()));

wait()
.times(200)
.interval(3000)
.condition(ready)
.done((result) => result ? resolve() : reject());
}),

/**
* Destroy the containers defined in [composeFile].
*
* @param {string} composeFile
* @return {Promise<void>}
*/
tearDown: (composeFile) => new Promise((resolve, reject) => {
exec(`docker-compose -f ${composeFile} down`, (err, stdout, stderr) => {
console.info(stdout);
console.error(stderr);
resolve();
});
}),
};
28 changes: 28 additions & 0 deletions modules/nodejs-agent/test/plugins/http/Dockerfile.agent
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# 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.
#

FROM node:10

WORKDIR /agent

ADD . /agent

RUN npm i

WORKDIR /app

RUN npm init -y && npm i /agent
Loading

0 comments on commit 099adcf

Please sign in to comment.