|
| 1 | +/* |
| 2 | + * Copyright 2025 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict' |
| 7 | + |
| 8 | +const test = require('node:test') |
| 9 | +const assert = require('node:assert') |
| 10 | +const helper = require('#testlib/agent_helper.js') |
| 11 | +const fetchAzureFunctionInfo = require('#agentlib/utilization/azurefunction-info.js') |
| 12 | + |
| 13 | +test.beforeEach((ctx) => { |
| 14 | + const agent = helper.loadMockedAgent({ |
| 15 | + config: { |
| 16 | + utilization: { |
| 17 | + detect_azurefunction: true |
| 18 | + } |
| 19 | + } |
| 20 | + }) |
| 21 | + ctx.nr = { agent } |
| 22 | +}) |
| 23 | + |
| 24 | +test.afterEach((ctx) => { |
| 25 | + helper.unloadAgent(ctx.nr.agent) |
| 26 | + ctx.nr.agent = null |
| 27 | + fetchAzureFunctionInfo.clearCache() |
| 28 | +}) |
| 29 | + |
| 30 | +test('should return null if detect_azurefunction is disabled', (ctx, end) => { |
| 31 | + const agent = ctx.nr.agent |
| 32 | + agent.config.utilization.detect_azurefunction = false |
| 33 | + |
| 34 | + fetchAzureFunctionInfo(agent, (err, result) => { |
| 35 | + assert.strictEqual(err, null) |
| 36 | + assert.strictEqual(result, null) |
| 37 | + end() |
| 38 | + }) |
| 39 | +}) |
| 40 | + |
| 41 | +test('should increment error metric if required environment variables are missing', (ctx, end) => { |
| 42 | + const agent = ctx.nr.agent |
| 43 | + const azureErrorMetric = agent.metrics.getOrCreateMetric('Supportability/utilization/azure/error') |
| 44 | + |
| 45 | + const initialCallCount = azureErrorMetric.callCount |
| 46 | + |
| 47 | + fetchAzureFunctionInfo(agent, (err, result) => { |
| 48 | + assert.strictEqual(err, null) |
| 49 | + assert.strictEqual(result, null) |
| 50 | + assert.strictEqual(azureErrorMetric.callCount, initialCallCount + 1) |
| 51 | + end() |
| 52 | + }) |
| 53 | +}) |
| 54 | + |
| 55 | +test('should derive metadata from required environment variables', (ctx, end) => { |
| 56 | + const agent = ctx.nr.agent |
| 57 | + |
| 58 | + process.env.REGION_NAME = 'Central US' |
| 59 | + process.env.WEBSITE_OWNER_NAME = '12345+resource-group' |
| 60 | + process.env.WEBSITE_SITE_NAME = 'my-function-app' |
| 61 | + |
| 62 | + fetchAzureFunctionInfo(agent, (err, result) => { |
| 63 | + assert.strictEqual(err, null) |
| 64 | + assert.deepStrictEqual(result, { |
| 65 | + 'faas.app_name': '/subscriptions/12345/resourceGroups/resource-group/providers/Microsoft.Web/sites/my-function-app', |
| 66 | + 'cloud.region': 'Central US' |
| 67 | + }) |
| 68 | + end() |
| 69 | + }) |
| 70 | +}) |
| 71 | + |
| 72 | +test('should derive metadata from required and optional environment variables', (ctx, end) => { |
| 73 | + const agent = ctx.nr.agent |
| 74 | + |
| 75 | + process.env.REGION_NAME = 'Central US' |
| 76 | + process.env.WEBSITE_RESOURCE_GROUP = 'resource-group-actual' |
| 77 | + process.env.WEBSITE_OWNER_NAME = '12345+resource-group' |
| 78 | + process.env.WEBSITE_SITE_NAME = 'my-function-app' |
| 79 | + |
| 80 | + fetchAzureFunctionInfo(agent, (err, result) => { |
| 81 | + assert.strictEqual(err, null) |
| 82 | + assert.deepStrictEqual(result, { |
| 83 | + 'faas.app_name': '/subscriptions/12345/resourceGroups/resource-group-actual/providers/Microsoft.Web/sites/my-function-app', |
| 84 | + 'cloud.region': 'Central US' |
| 85 | + }) |
| 86 | + end() |
| 87 | + }) |
| 88 | +}) |
0 commit comments