Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[event-hubs] Enable unit-test for node.js in CI #17492

Merged
20 commits merged into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c768669
[mock-hub] make updates and fixes to pass JavaScript CI tests
chradek Sep 4, 2021
bb18612
[event-hubs] add wrapper and mock service for testing
chradek Sep 4, 2021
f9ecef6
[event-hubs] update npm scripts and dependencies to support running m…
chradek Sep 7, 2021
6eaee29
[event-hubs] update tests to use test target wrapper
chradek Sep 7, 2021
ba970cf
[event-hubs] enable unit-test in ci
chradek Sep 7, 2021
9b9951f
[event-hubs] fix eslint errors
chradek Sep 7, 2021
0691e8f
[event-hubs] tests now inject environment variables when running agai…
chradek Sep 7, 2021
1e172da
[mock-hub] update samples
chradek Sep 7, 2021
cdcd0e6
[event-hubs] improve token credential mock
chradek Sep 7, 2021
702e273
[event-hubs] fix lint errors
chradek Sep 7, 2021
d5c5739
[event-hubs] rename "wrapper" test helper to "testWithServiceTypes"
chradek Sep 8, 2021
564f72c
fix imports
chradek Sep 8, 2021
26fa11c
[event-hubs] improve testWithServiceTypes helper to get filepath base…
chradek Sep 15, 2021
565dd30
[mock-hub] add context around message.body.multiple check
chradek Sep 15, 2021
c0a57c8
Merge remote-tracking branch 'upstream/main' into eh-mock-hub-merger
chradek Sep 15, 2021
e0c63fd
debugging - add console logs
chradek Sep 16, 2021
4a6a66f
debugging - more logs
chradek Sep 16, 2021
2a1b174
[event-hubs] disable testing multiple service types when TEST_MODE is…
chradek Sep 16, 2021
8763cb3
[event-hubs] add additional serviceVersion check in test wrapper for …
chradek Sep 17, 2021
d93496a
[event-hubs] remove console.log from test
chradek Sep 17, 2021
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
2 changes: 1 addition & 1 deletion sdk/eventhub/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extends:
template: ../../eng/pipelines/templates/stages/archetype-sdk-client.yml
parameters:
ServiceDirectory: eventhub
RunUnitTests: false
RunUnitTests: true
Artifacts:
- name: azure-event-hubs
safeName: azureeventhubs
Expand Down
3 changes: 2 additions & 1 deletion sdk/eventhub/event-hubs/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ module.exports = function(config) {
envPreprocessor: [
"EVENTHUB_CONNECTION_STRING",
"EVENTHUB_NAME",
"IOTHUB_EH_COMPATIBLE_CONNECTION_STRING"
"IOTHUB_EH_COMPATIBLE_CONNECTION_STRING",
"TEST_TARGET"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it TEST_MODE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The multiversion test helper makes some assumptions with TEST_MODE that causes issue with what we're trying to do. I'm using the multiversion test helper to facilitate testing mock and live versions of the service, not multiple API versions of the service.

This works fine if I don't set TEST_MODE at all, but things break if I set TEST_MODE to live because it assumes I want different behavior than just running my tests against the live service. So, that's why I made TEST_TARGET instead.

],

// test results reporter to use
Expand Down
13 changes: 8 additions & 5 deletions sdk/eventhub/event-hubs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
}
},
"browser": {
"./dist-esm/src/util/runtimeInfo.js": "./dist-esm/src/util/runtimeInfo.browser.js"
"./dist-esm/src/util/runtimeInfo.js": "./dist-esm/src/util/runtimeInfo.browser.js",
"./dist-esm/test/public/utils/mockService.js": "./dist-esm/test/public/utils/mockService.browser.js"
},
"files": [
"dist/",
Expand All @@ -49,16 +50,17 @@
"build:samples": "echo Obsolete.",
"build:test:browser": "tsc -p . && cross-env ONLY_BROWSER=true rollup -c rollup.test.config.js 2>&1",
"build:test:node": "tsc -p . && cross-env ONLY_NODE=true rollup -c rollup.test.config.js 2>&1",
"build:test": "tsc -p . && rollup -c rollup.test.config.js 2>&1",
"build:test": "tsc -p . && rollup -c rollup.test.config.js 2>&1 && npm run generate-certs",
"build:types": "downlevel-dts types/latest types/3.1",
"build": "npm run clean && tsc -p . && rollup -c 2>&1 && api-extractor run --local && npm run build:types",
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
"clean": "rimraf dist dist-* types *.tgz *.log",
"execute:samples": "dev-tool samples run samples-dev",
"extract-api": "tsc -p . && api-extractor run --local",
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
"integration-test:browser": "karma start --single-run",
"integration-test:node": "nyc mocha -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace \"dist-esm/test/internal/*.spec.js\" \"dist-esm/test/public/*.spec.js\" \"dist-esm/test/public/**/*.spec.js\" \"dist-esm/test/internal/**/*.spec.js\"",
"generate-certs": "node ./scripts/generateCerts.js",
"integration-test:browser": "cross-env TEST_TARGET=live karma start --single-run",
"integration-test:node": "cross-env TEST_TARGET=live nyc mocha -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace \"dist-esm/test/internal/*.spec.js\" \"dist-esm/test/public/*.spec.js\" \"dist-esm/test/public/**/*.spec.js\" \"dist-esm/test/internal/**/*.spec.js\"",
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
"lint:fix": "eslint package.json api-extractor.json src test --ext .ts --fix --fix-type [problem,suggestion]",
"lint": "eslint package.json api-extractor.json src test --ext .ts",
Expand All @@ -68,7 +70,7 @@
"test:node": "npm run build:test && npm run unit-test:node && npm run integration-test:node",
"test": "npm run build:test && npm run unit-test && npm run integration-test",
"unit-test:browser": "echo skipped",
"unit-test:node": "echo skipped",
"unit-test:node": "cross-env NODE_EXTRA_CA_CERTS=\"./certs/my-private-root-ca.crt.pem\" TEST_TARGET=mock nyc mocha -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace \"dist-esm/test/internal/*.spec.js\" \"dist-esm/test/public/*.spec.js\" \"dist-esm/test/public/**/*.spec.js\" \"dist-esm/test/internal/**/*.spec.js\"",
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
"docs": "typedoc --excludePrivate --excludeNotExported --excludeExternals --stripInternal --mode file --out ./dist/docs ./src"
},
Expand Down Expand Up @@ -126,6 +128,7 @@
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@azure/identity": "2.0.0-beta.6",
"@azure/mock-hub": "^1.0.0",
"@azure/test-utils": "^1.0.0",
"@azure/test-utils-perfstress": "^1.0.0",
"@microsoft/api-extractor": "7.7.11",
Expand Down
82 changes: 82 additions & 0 deletions sdk/eventhub/event-hubs/scripts/generateCerts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

const { execFileSync } = require("child_process");
const { mkdirSync } = require("fs");
const { resolve: resolvePath } = require("path");

const cwd = process.cwd();
const certsDirectory = resolvePath(cwd, "certs");

// Create `certs` directory.
console.log(`Creating ${certsDirectory}`);
try {
mkdirSync(certsDirectory);
} catch (err) {
if (err.code !== "EEXIST") {
throw err;
}
}

// Create own Root Certificate Authority
execFileSync("openssl", [
"genrsa",
"-out",
`${resolvePath(certsDirectory, "my-private-root-ca.key.pem")}`,
"2048"
]);

// Self-sign Root Certificate Authority
execFileSync("openssl", [
"req",
"-x509",
"-new",
"-nodes",
"-key",
`${resolvePath(certsDirectory, "my-private-root-ca.key.pem")}`,
"-days",
"5",
"-out",
`${resolvePath(certsDirectory, "my-private-root-ca.crt.pem")}`,
"-subj",
"/C=US/ST=Washington/L=Seattle/O=Fake Signing Authority/CN=fake.foo"
]);

// Create a certificate for localhost
execFileSync("openssl", [
"genrsa",
"-out",
`${resolvePath(certsDirectory, "my-server.key.pem")}`,
"2048"
]);

// Create a request which the Root Certificate Authority will sign
execFileSync("openssl", [
"req",
"-new",
"-key",
`${resolvePath(certsDirectory, "my-server.key.pem")}`,
"-out",
`${resolvePath(certsDirectory, "my-server.csr.pem")}`,
"-subj",
"/C=US/ST=Washington/L=Seattle/O=Fake Hubs/CN=localhost"
]);

// Sign the request with the Root Certificate Authority
execFileSync("openssl", [
"x509",
"-req",
"-in",
`${resolvePath(certsDirectory, "my-server.csr.pem")}`,
"-CA",
`${resolvePath(certsDirectory, "my-private-root-ca.crt.pem")}`,
"-CAkey",
`${resolvePath(certsDirectory, "my-private-root-ca.key.pem")}`,
"-CAcreateserial",
"-out",
`${resolvePath(certsDirectory, "my-server.crt.pem")}`,
"-days",
"5"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to work on our live test times.

]);

console.log(`Certs created.`);
99 changes: 51 additions & 48 deletions sdk/eventhub/event-hubs/test/internal/amqp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,61 @@
import chai from "chai";
import { Constants } from "@azure/core-amqp";
import { fromRheaMessage, isAmqpAnnotatedMessage } from "../../src/eventData";
import { wrapper } from "../public/utils/wrapper";
const assert = chai.assert;

describe("AMQP message encoding", () => {
it("isAmqpAnnotatedMessage", () => {
assert.isFalse(isAmqpAnnotatedMessage({}));
assert.isFalse(isAmqpAnnotatedMessage({ body: "hello world" }));
assert.isFalse(
isAmqpAnnotatedMessage(
fromRheaMessage({
message_annotations: {
[Constants.enqueuedTime]: Date.now()
},
body: undefined
})
)
);
wrapper("internal/amqp.spec.ts", () => {
describe("AMQP message encoding", () => {
it("isAmqpAnnotatedMessage", () => {
assert.isFalse(isAmqpAnnotatedMessage({}));
assert.isFalse(isAmqpAnnotatedMessage({ body: "hello world" }));
assert.isFalse(
isAmqpAnnotatedMessage(
fromRheaMessage({
message_annotations: {
[Constants.enqueuedTime]: Date.now()
},
body: undefined
})
)
);

assert.isTrue(
isAmqpAnnotatedMessage(
fromRheaMessage({
message_annotations: {
[Constants.enqueuedTime]: Date.now()
},
body: undefined
}).getRawAmqpMessage()
)
);
assert.isTrue(
isAmqpAnnotatedMessage(
fromRheaMessage({
message_annotations: {
[Constants.enqueuedTime]: Date.now()
},
body: undefined
}).getRawAmqpMessage()
)
);

assert.isTrue(
isAmqpAnnotatedMessage({
body: "hello world",
bodyType: "sequence"
})
);
assert.isTrue(
isAmqpAnnotatedMessage({
body: "hello world",
bodyType: "value"
})
);
assert.isTrue(
isAmqpAnnotatedMessage({
body: "hello world",
bodyType: "data"
})
);
assert.isTrue(
isAmqpAnnotatedMessage({
body: "hello world",
bodyType: "sequence"
})
);
assert.isTrue(
isAmqpAnnotatedMessage({
body: "hello world",
bodyType: "value"
})
);
assert.isTrue(
isAmqpAnnotatedMessage({
body: "hello world",
bodyType: "data"
})
);

assert.isTrue(
isAmqpAnnotatedMessage({
body: "hello world",
bodyType: undefined // the property _must_ exist, but undefined is fine. We'll default to 'data'
})
);
assert.isTrue(
isAmqpAnnotatedMessage({
body: "hello world",
bodyType: undefined // the property _must_ exist, but undefined is fine. We'll default to 'data'
})
);
});
});
});
Loading