|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import { EnvVarKeys, getEnvVars } from "../../public/utils/testUtils"; |
| 5 | +import { EnvironmentCredential, TokenCredential } from "@azure/identity"; |
| 6 | +import { EventHubConsumerClient, EventHubProducerClient } from "../../../src"; |
| 7 | +import { assert, should as shouldFn } from "@azure/test-utils"; |
| 8 | + |
| 9 | +import Sinon from "sinon"; |
| 10 | +import { createMockServer } from "../../public/utils/mockService"; |
| 11 | +import { testWithServiceTypes } from "../../public/utils/testWithServiceTypes"; |
| 12 | +import { tracingClient } from "../../../src/diagnostics/tracing"; |
| 13 | + |
| 14 | +const should = shouldFn(); |
| 15 | + |
| 16 | +testWithServiceTypes((serviceVersion) => { |
| 17 | + const env = getEnvVars(); |
| 18 | + if (serviceVersion === "mock") { |
| 19 | + let service: ReturnType<typeof createMockServer>; |
| 20 | + before("Starting mock service", () => { |
| 21 | + service = createMockServer(); |
| 22 | + return service.start(); |
| 23 | + }); |
| 24 | + |
| 25 | + after("Stopping mock service", async () => { |
| 26 | + await service?.stop(); |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + describe("Create clients using Azure Identity (Internal)", function (): void { |
| 31 | + let endpoint: string; |
| 32 | + let credential: TokenCredential; |
| 33 | + let client: EventHubConsumerClient | EventHubProducerClient; |
| 34 | + |
| 35 | + afterEach(async () => { |
| 36 | + // The client must always be closed, or MockHub will hang on shutdown. |
| 37 | + await client?.close(); |
| 38 | + }); |
| 39 | + |
| 40 | + before("validate environment", function () { |
| 41 | + should.exist( |
| 42 | + env[EnvVarKeys.AZURE_CLIENT_ID], |
| 43 | + "define AZURE_CLIENT_ID in your environment before running integration tests." |
| 44 | + ); |
| 45 | + should.exist( |
| 46 | + env[EnvVarKeys.AZURE_TENANT_ID], |
| 47 | + "define AZURE_TENANT_ID in your environment before running integration tests." |
| 48 | + ); |
| 49 | + should.exist( |
| 50 | + env[EnvVarKeys.AZURE_CLIENT_SECRET], |
| 51 | + "define AZURE_CLIENT_SECRET in your environment before running integration tests." |
| 52 | + ); |
| 53 | + should.exist( |
| 54 | + env[EnvVarKeys.EVENTHUB_CONNECTION_STRING], |
| 55 | + "define EVENTHUB_CONNECTION_STRING in your environment before running integration tests." |
| 56 | + ); |
| 57 | + // This is of the form <your-namespace>.servicebus.windows.net |
| 58 | + endpoint = (env.EVENTHUB_CONNECTION_STRING.match("Endpoint=sb://(.*)/;") || "")[1]; |
| 59 | + if (serviceVersion === "mock") { |
| 60 | + // Create a mock credential that implements the TokenCredential interface. |
| 61 | + credential = { |
| 62 | + getToken(_args) { |
| 63 | + return Promise.resolve({ token: "token", expiresOnTimestamp: Date.now() + 360000 }); |
| 64 | + }, |
| 65 | + }; |
| 66 | + } else { |
| 67 | + credential = new EnvironmentCredential(); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + it("getEventHubProperties() creates a span with a peer.address attribute as the FQNS", async () => { |
| 72 | + client = new EventHubConsumerClient( |
| 73 | + EventHubConsumerClient.defaultConsumerGroupName, |
| 74 | + endpoint, |
| 75 | + env.EVENTHUB_NAME, |
| 76 | + credential |
| 77 | + ); |
| 78 | + should.equal(client.fullyQualifiedNamespace, endpoint); |
| 79 | + |
| 80 | + const withSpanStub = Sinon.spy(tracingClient, "withSpan"); |
| 81 | + |
| 82 | + // Ensure tracing is implemented correctly |
| 83 | + await assert.supportsTracing( |
| 84 | + (options) => client.getEventHubProperties(options), |
| 85 | + ["ManagementClient.getEventHubProperties"] |
| 86 | + ); |
| 87 | + |
| 88 | + // Additional validation that we created the correct initial span options |
| 89 | + const expectedSpanOptions = { |
| 90 | + spanAttributes: { |
| 91 | + "peer.address": client.fullyQualifiedNamespace, |
| 92 | + "message_bus.destination": client.eventHubName, |
| 93 | + }, |
| 94 | + }; |
| 95 | + |
| 96 | + assert.isTrue( |
| 97 | + withSpanStub.calledWith( |
| 98 | + Sinon.match.any, |
| 99 | + Sinon.match.any, |
| 100 | + Sinon.match.any, |
| 101 | + expectedSpanOptions |
| 102 | + ) |
| 103 | + ); |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
0 commit comments