|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const { randomUUID } = require('node:crypto'); |
| 5 | +const { |
| 6 | + DynamoDBClient, |
| 7 | + ListTablesCommand, |
| 8 | + CreateTableCommand, |
| 9 | + DescribeTableCommand, |
| 10 | + DeleteTableCommand, |
| 11 | + PutItemCommand, |
| 12 | + GetItemCommand, |
| 13 | + DeleteItemCommand, |
| 14 | +} = require('@aws-sdk/client-dynamodb'); |
| 15 | + |
| 16 | +// Parse arguments to find port |
| 17 | +const args = process.argv.slice(2); |
| 18 | +let port = 4566; |
| 19 | +for (let i = 0; i < args.length; i++) { |
| 20 | + if (args[i] === '--port' && args[i + 1]) { |
| 21 | + port = parseInt(args[i + 1], 10); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +main().catch((error) => { |
| 26 | + console.error('\nDynamoDB smoke test failed'); |
| 27 | + console.error(error instanceof Error ? error.stack || error.message : error); |
| 28 | + process.exitCode = 1; |
| 29 | +}); |
| 30 | + |
| 31 | +async function main() { |
| 32 | + const endpoint = process.env.MILDSTACK_DYNAMODB_ENDPOINT || process.env.AWS_DYNAMODB_ENDPOINT || `http://localhost:${port}`; |
| 33 | + |
| 34 | + console.log(`Running AWS SDK smoke mode against ${endpoint}`); |
| 35 | + const client = new DynamoDBClient({ |
| 36 | + region: process.env.AWS_REGION || 'us-east-1', |
| 37 | + endpoint, |
| 38 | + credentials: { |
| 39 | + accessKeyId: process.env.AWS_ACCESS_KEY_ID || 'test', |
| 40 | + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || 'test', |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + const table = uniqueTableName('native'); |
| 45 | + const commands = [ |
| 46 | + ['ListTables', new ListTablesCommand({})], |
| 47 | + ['CreateTable', new CreateTableCommand({ |
| 48 | + TableName: table, |
| 49 | + KeySchema: [ |
| 50 | + { AttributeName: 'id', KeyType: 'HASH' } |
| 51 | + ], |
| 52 | + AttributeDefinitions: [ |
| 53 | + { AttributeName: 'id', AttributeType: 'S' } |
| 54 | + ], |
| 55 | + BillingMode: 'PAY_PER_REQUEST' |
| 56 | + })], |
| 57 | + ['DescribeTable', new DescribeTableCommand({ TableName: table })], |
| 58 | + ['PutItem', new PutItemCommand({ |
| 59 | + TableName: table, |
| 60 | + Item: { |
| 61 | + id: { S: 'test-id' }, |
| 62 | + name: { S: 'native-mode smoke payload' }, |
| 63 | + } |
| 64 | + })], |
| 65 | + ['GetItem', new GetItemCommand({ |
| 66 | + TableName: table, |
| 67 | + Key: { id: { S: 'test-id' } } |
| 68 | + })], |
| 69 | + ['DeleteItem', new DeleteItemCommand({ |
| 70 | + TableName: table, |
| 71 | + Key: { id: { S: 'test-id' } } |
| 72 | + })], |
| 73 | + ['DeleteTable', new DeleteTableCommand({ TableName: table })], |
| 74 | + ]; |
| 75 | + |
| 76 | + for (const [name, command] of commands) { |
| 77 | + console.log(`\nExecuting ${name}...`); |
| 78 | + try { |
| 79 | + const response = await client.send(command); |
| 80 | + console.log(`✓ ${name} succeeded. Response:`); |
| 81 | + console.dir(response, { depth: 4, colors: true }); |
| 82 | + } catch (error) { |
| 83 | + console.error(`\nFailed during command: ${name}`); |
| 84 | + if (error.$response) { |
| 85 | + console.error('Response status:', error.$response.statusCode); |
| 86 | + console.error('Response headers:', error.$response.headers); |
| 87 | + if (error.$response.body) { |
| 88 | + const body = error.$response.body; |
| 89 | + if (typeof body.read === 'function') { |
| 90 | + const chunk = body.read(); |
| 91 | + if (chunk) { |
| 92 | + console.error('Response body:', chunk.toString()); |
| 93 | + } else { |
| 94 | + console.error('Response body:', body); |
| 95 | + } |
| 96 | + } else if (typeof body.toString === 'function') { |
| 97 | + console.error('Response body:', body.toString()); |
| 98 | + } else { |
| 99 | + console.error('Response body:', body); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + throw error; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + console.log('\n✓ Native AWS SDK smoke mode passed'); |
| 108 | +} |
| 109 | + |
| 110 | +function uniqueTableName(prefix) { |
| 111 | + return `mildstack_${prefix}_${Date.now().toString(36)}_${randomUUID().slice(0, 8)}`; |
| 112 | +} |
0 commit comments