|
| 1 | +/* |
| 2 | + * Copyright 2020 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict' |
| 7 | +const assert = require('node:assert') |
| 8 | +const test = require('node:test') |
| 9 | +const { createEmptyResponseServer, FAKE_CREDENTIALS } = require('../../lib/aws-server-stubs') |
| 10 | +const common = require('./common') |
| 11 | +const helper = require('../../lib/agent_helper') |
| 12 | +const { match } = require('../../lib/custom-assertions') |
| 13 | + |
| 14 | +test('Redshift-data', async (t) => { |
| 15 | + t.beforeEach(async (ctx) => { |
| 16 | + ctx.nr = {} |
| 17 | + const server = createEmptyResponseServer() |
| 18 | + |
| 19 | + await new Promise((resolve) => { |
| 20 | + server.listen(0, resolve) |
| 21 | + }) |
| 22 | + |
| 23 | + ctx.nr.server = server |
| 24 | + ctx.nr.agent = helper.instrumentMockedAgent() |
| 25 | + |
| 26 | + const lib = require('@aws-sdk/client-redshift-data') |
| 27 | + |
| 28 | + ctx.nr.redshiftCommands = { |
| 29 | + ExecuteStatementCommand: lib.ExecuteStatementCommand, |
| 30 | + BatchExecuteStatementCommand: lib.BatchExecuteStatementCommand, |
| 31 | + DescribeStatementCommand: lib.DescribeStatementCommand, |
| 32 | + GetStatementResultCommand: lib.GetStatementResultCommand, |
| 33 | + ListDatabasesCommand: lib.ListDatabasesCommand |
| 34 | + } |
| 35 | + |
| 36 | + const endpoint = `http://localhost:${server.address().port}` |
| 37 | + ctx.nr.client = new lib.RedshiftDataClient({ |
| 38 | + credentials: FAKE_CREDENTIALS, |
| 39 | + endpoint, |
| 40 | + region: 'us-east-1' |
| 41 | + }) |
| 42 | + |
| 43 | + ctx.nr.tests = createTests() |
| 44 | + }) |
| 45 | + |
| 46 | + t.afterEach(common.afterEach) |
| 47 | + |
| 48 | + await t.test('client commands', (t, end) => { |
| 49 | + const { redshiftCommands, client, agent, tests } = t.nr |
| 50 | + helper.runInTransaction(agent, async function (tx) { |
| 51 | + for (const test of tests) { |
| 52 | + const CommandClass = redshiftCommands[test.command] |
| 53 | + const command = new CommandClass(test.params) |
| 54 | + await client.send(command) |
| 55 | + } |
| 56 | + |
| 57 | + tx.end() |
| 58 | + |
| 59 | + const args = [end, tests, tx] |
| 60 | + setImmediate(finish, ...args) |
| 61 | + }) |
| 62 | + }) |
| 63 | +}) |
| 64 | + |
| 65 | +function finish(end, tests, tx) { |
| 66 | + const root = tx.trace.root |
| 67 | + const segments = common.checkAWSAttributes({ trace: tx.trace, segment: root, pattern: common.DATASTORE_PATTERN }) |
| 68 | + assert.equal(segments.length, tests.length, `should have ${tests.length} aws datastore segments`) |
| 69 | + |
| 70 | + const externalSegments = common.checkAWSAttributes({ trace: tx.trace, segment: root, pattern: common.EXTERN_PATTERN }) |
| 71 | + assert.equal(externalSegments.length, 0, 'should not have any External segments') |
| 72 | + |
| 73 | + segments.forEach((segment, i) => { |
| 74 | + const command = tests[i].command |
| 75 | + |
| 76 | + if (tests[i].command === 'ExecuteStatementCommand' || tests[i].command === 'BatchExecuteStatementCommand') { |
| 77 | + assert.equal( |
| 78 | + segment.name, |
| 79 | + `Datastore/statement/Redshift/${tests[i].tableName}/${tests[i].queryType}`, |
| 80 | + 'should have table name and query type in segment name' |
| 81 | + ) |
| 82 | + } else { |
| 83 | + assert.equal( |
| 84 | + segment.name, |
| 85 | + `Datastore/operation/Redshift/${command}`, |
| 86 | + 'should have command in segment name' |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + const attrs = segment.attributes.get(common.SEGMENT_DESTINATION) |
| 91 | + attrs.port_path_or_id = parseInt(attrs.port_path_or_id, 10) |
| 92 | + match(attrs, { |
| 93 | + host: String, |
| 94 | + port_path_or_id: Number, |
| 95 | + product: 'Redshift', |
| 96 | + database_name: String, |
| 97 | + 'aws.operation': command, |
| 98 | + 'aws.requestId': String, |
| 99 | + 'aws.region': 'us-east-1', |
| 100 | + 'aws.service': 'Redshift Data', |
| 101 | + }) |
| 102 | + |
| 103 | + assert(attrs.host, 'should have host') |
| 104 | + }) |
| 105 | + |
| 106 | + end() |
| 107 | +} |
| 108 | + |
| 109 | +function createTests() { |
| 110 | + const insertData = insertDataIntoTable() |
| 111 | + const selectData = selectDataFromTable() |
| 112 | + const updateData = updateDataInTable() |
| 113 | + const deleteData = deleteDataFromTable() |
| 114 | + const insertBatchData = insertBatchDataIntoTable() |
| 115 | + const describeSqlStatement = describeStatement() |
| 116 | + const getSqlStatement = getStatement() |
| 117 | + const getDatabases = listDatabases() |
| 118 | + |
| 119 | + return [ |
| 120 | + { params: insertData, tableName, queryType: 'insert', command: 'ExecuteStatementCommand' }, |
| 121 | + { params: selectData, tableName, queryType: 'select', command: 'ExecuteStatementCommand' }, |
| 122 | + { params: updateData, tableName, queryType: 'update', command: 'ExecuteStatementCommand' }, |
| 123 | + { params: deleteData, tableName, queryType: 'delete', command: 'ExecuteStatementCommand' }, |
| 124 | + { params: insertBatchData, tableName, queryType: 'insert', command: 'BatchExecuteStatementCommand' }, |
| 125 | + { params: describeSqlStatement, command: 'DescribeStatementCommand' }, |
| 126 | + { params: getSqlStatement, command: 'GetStatementResultCommand' }, |
| 127 | + { params: getDatabases, command: 'ListDatabasesCommand' } |
| 128 | + ] |
| 129 | +} |
| 130 | + |
| 131 | +const commonParams = { |
| 132 | + Database: 'dev', |
| 133 | + DbUser: 'a_user', |
| 134 | + ClusterIdentifier: 'a_cluster' |
| 135 | +} |
| 136 | + |
| 137 | +const tableName = 'test_table' |
| 138 | + |
| 139 | +function insertDataIntoTable() { |
| 140 | + return { |
| 141 | + ...commonParams, |
| 142 | + Sql: `INSERT INTO ${tableName} (id, name) VALUES (1, 'test')` |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +function selectDataFromTable() { |
| 147 | + return { |
| 148 | + ...commonParams, |
| 149 | + Sql: `SELECT id, name FROM ${tableName}` |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +function updateDataInTable() { |
| 154 | + return { |
| 155 | + ...commonParams, |
| 156 | + Sql: `UPDATE ${tableName} SET name = 'updated' WHERE id = 1` |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +function deleteDataFromTable() { |
| 161 | + return { |
| 162 | + ...commonParams, |
| 163 | + Sql: `DELETE FROM ${tableName} WHERE id = 1` |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +function insertBatchDataIntoTable() { |
| 168 | + return { |
| 169 | + ...commonParams, |
| 170 | + Sqls: ['INSERT INTO test_table (id, name) VALUES (2, \'test2\')', 'INSERT INTO test_table (id, name) VALUES (3, \'test3\')'] |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +function describeStatement() { |
| 175 | + return { |
| 176 | + Id: 'a_statement_id' |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +function getStatement() { |
| 181 | + return { |
| 182 | + Id: 'a_statement_id', |
| 183 | + NextToken: 'a_token' |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +function listDatabases() { |
| 188 | + return { |
| 189 | + ...commonParams, |
| 190 | + } |
| 191 | +} |
0 commit comments