Skip to content

Commit

Permalink
[event-hubs] Enable unit-test for node.js in CI (#17492)
Browse files Browse the repository at this point in the history
Replaces #14568

Uses the `@azure/mock-hub` package to start a local mocked version of Event Hubs to run tests against.

There are a lot of whitespace changes (indentation) so I recommend viewing with whitespace changes hidden.
  • Loading branch information
chradek authored Sep 17, 2021
1 parent 2961b5c commit bcb1827
Show file tree
Hide file tree
Showing 42 changed files with 9,844 additions and 9,233 deletions.
2 changes: 1 addition & 1 deletion sdk/eventhub/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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"
],

// 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 DISABLE_MULTI_VERSION_TESTING=true karma start --single-run",
"integration-test:node": "cross-env TEST_TARGET=live DISABLE_MULTI_VERSION_TESTING=true 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 DISABLE_MULTI_VERSION_TESTING=true 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.18.7",
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"
]);

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 { testWithServiceTypes } from "../public/utils/testWithServiceTypes";
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
})
)
);
testWithServiceTypes(() => {
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

0 comments on commit bcb1827

Please sign in to comment.