Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript (v3): Integration tests updates for stability. #6514

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion javascriptv3/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules/
*_test.log
*.pem
package-lock.json
package-lock.json
test_results/
test-results.xml
2 changes: 1 addition & 1 deletion javascriptv3/example_code/bedrock-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"test": "vitest run **/*.unit.test.js",
"integration-test": "vitest run **/*.integration.test.js"
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml"
},
"dependencies": {
"@aws-sdk/client-bedrock-agent": "^3.515.0"
Expand Down
2 changes: 1 addition & 1 deletion javascriptv3/example_code/bedrock-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "Apache-2.0",
"type": "module",
"scripts": {
"integration-test": "vitest run **/*.integration.test.js"
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml"
},
"devDependencies": {
"vitest": "^1.6.0"
Expand Down
2 changes: 1 addition & 1 deletion javascriptv3/example_code/bedrock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "Apache-2.0",
"type": "module",
"scripts": {
"integration-test": "vitest run **/*.integration.test.js"
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml"
},
"dependencies": {
"@aws-sdk/client-bedrock": "^3.485.0"
Expand Down
2 changes: 1 addition & 1 deletion javascriptv3/example_code/cloudwatch-events/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"type": "module",
"scripts": {
"integration-test": "vitest run **/*.integration.test.js"
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml"
},
"devDependencies": {
"vitest": "^1.6.0"
Expand Down
2 changes: 1 addition & 1 deletion javascriptv3/example_code/cloudwatch-logs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@aws-sdk/client-lambda": "^3.216.0"
},
"scripts": {
"integration-test": "vitest run **/*.integration.test.js",
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml",
"test": "vitest run **/*.unit.test.js"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion javascriptv3/example_code/cloudwatch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@aws-sdk/client-ec2": "^3.213.0"
},
"scripts": {
"integration-test": "vitest run **/*.integration.test.js"
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml"
},
"devDependencies": {
"uuid": "^9.0.0",
Expand Down
2 changes: 1 addition & 1 deletion javascriptv3/example_code/codebuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"type": "module",
"scripts": {
"integration-test": "vitest run **/*.integration.test.js"
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml"
},
"devDependencies": {
"@aws-sdk/client-iam": "^3.391.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "module",
"scripts": {
"test": "vitest run **/*.unit.test.js",
"integration-test": "vitest run **/*.integration.test.js"
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml"
},
"dependencies": {
"@aws-doc-sdk-examples/lib": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "Corey Pyle <[email protected]>",
"license": "Apache-2.0",
"scripts": {
"integration-test": "vitest run **/*.integration.test.js"
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml"
},
"dependencies": {
"@aws-sdk/client-auto-scaling": "^3.438.0",
Expand Down
2 changes: 1 addition & 1 deletion javascriptv3/example_code/dynamodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "Apache-2.0",
"type": "module",
"scripts": {
"integration-test": "vitest run **/*.integration.test.js"
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml"
},
"dependencies": {
"@aws-doc-sdk-examples/lib": "^1.0.0",
Expand Down
36 changes: 35 additions & 1 deletion javascriptv3/example_code/dynamodb/scenarios/partiql-batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,55 @@ import {
BillingMode,
CreateTableCommand,
DeleteTableCommand,
DescribeTableCommand,
DynamoDBClient,
waitUntilTableExists,
} from "@aws-sdk/client-dynamodb";
import {
DynamoDBDocumentClient,
BatchExecuteStatementCommand,
} from "@aws-sdk/lib-dynamodb";
import { ScenarioInput } from "@aws-doc-sdk-examples/lib/scenario";

const client = new DynamoDBClient({});
const docClient = DynamoDBDocumentClient.from(client);

const log = (msg) => console.log(`[SCENARIO] ${msg}`);
const tableName = "Cities";

export const main = async () => {
export const main = async (confirmAll = false) => {
/**
* Delete table if it exists.
*/
try {
await client.send(new DescribeTableCommand({ TableName: tableName }));
// If no error was thrown, the table exists.
const input = new ScenarioInput(
"deleteTable",
`A table named ${tableName} already exists. If you choose not to delete
this table, the scenario cannot continue. Delete it?`,
{ confirmAll },
);
const deleteTable = await input.handle({});
if (deleteTable) {
await client.send(new DeleteTableCommand({ tableName }));
} else {
console.warn(
"Scenario could not run. Either delete ${tableName} or provide a unique table name.",
);
return;
}
} catch (caught) {
if (
caught instanceof Error &&
caught.name === "ResourceNotFoundException"
) {
// Do nothing. This means the table is not there.
} else {
throw caught;
}
}

/**
* Create a table.
*/
Expand Down
36 changes: 35 additions & 1 deletion javascriptv3/example_code/dynamodb/scenarios/partiql-single.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,55 @@ import {
BillingMode,
CreateTableCommand,
DeleteTableCommand,
DescribeTableCommand,
DynamoDBClient,
waitUntilTableExists,
} from "@aws-sdk/client-dynamodb";
import {
DynamoDBDocumentClient,
ExecuteStatementCommand,
} from "@aws-sdk/lib-dynamodb";
import { ScenarioInput } from "@aws-doc-sdk-examples/lib/scenario";

const client = new DynamoDBClient({});
const docClient = DynamoDBDocumentClient.from(client);

const log = (msg) => console.log(`[SCENARIO] ${msg}`);
const tableName = "SingleOriginCoffees";

export const main = async () => {
export const main = async (confirmAll = false) => {
/**
* Delete table if it exists.
*/
try {
await client.send(new DescribeTableCommand({ TableName: tableName }));
// If no error was thrown, the table exists.
const input = new ScenarioInput(
"deleteTable",
`A table named ${tableName} already exists. If you choose not to delete
this table, the scenario cannot continue. Delete it?`,
{ confirmAll },
);
const deleteTable = await input.handle({});
if (deleteTable) {
await client.send(new DeleteTableCommand({ tableName }));
} else {
console.warn(
"Scenario could not run. Either delete ${tableName} or provide a unique table name.",
);
return;
}
} catch (caught) {
if (
caught instanceof Error &&
caught.name === "ResourceNotFoundException"
) {
// Do nothing. This means the table is not there.
} else {
throw caught;
}
}

/**
* Create a table.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import { main } from "../scenarios/partiql-batch.js";

describe("partiql batch", () => {
it("should run without error", async () => {
await main();
await main(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { main } from "../scenarios/partiql-single.js";

describe("basic scenario", () => {
it("should run without error", async () => {
await main();
await main(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"type": "module",
"scripts": {
"integration-test": "vitest run **/*.integration.test.js"
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml"
},
"author": "Corey Pyle <[email protected]>",
"license": "Apache-2.0",
Expand Down
2 changes: 1 addition & 1 deletion javascriptv3/example_code/eventbridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"author": "Corey Pyle <[email protected]>",
"type": "module",
"scripts": {
"integration-test": "vitest run **/*.integration.test.js"
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml"
},
"dependencies": {
"@aws-doc-sdk-examples/lib": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion javascriptv3/example_code/glue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "Apache-2.0",
"scripts": {
"test": "vitest run **/*.unit.test.js",
"integration-test": "vitest run **/*.integration.test.js"
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml"
},
"dependencies": {
"@aws-doc-sdk-examples/lib": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import { fileURLToPath } from "url";

// snippet-start:[iam.JavaScript.createservicelinkedrolev3]
import { CreateServiceLinkedRoleCommand, IAMClient } from "@aws-sdk/client-iam";
import {
CreateServiceLinkedRoleCommand,
GetRoleCommand,
IAMClient,
} from "@aws-sdk/client-iam";

const client = new IAMClient({});

Expand All @@ -20,10 +24,26 @@ export const createServiceLinkedRole = async (serviceName) => {
// For a list of AWS service endpoints, see https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html.
AWSServiceName: serviceName,
});

const response = await client.send(command);
console.log(response);
return response;
try {
const response = await client.send(command);
console.log(response);
return response;
} catch (caught) {
if (
caught instanceof Error &&
caught.name === "InvalidInputException" &&
caught.message.includes(
"Service role name AWSServiceRoleForElasticBeanstalk has been taken in this account",
)
) {
console.warn(caught.message);
return client.send(
new GetRoleCommand({ RoleName: "AWSServiceRoleForElasticBeanstalk" }),
);
} else {
throw caught;
}
}
};
// snippet-end:[iam.JavaScript.createservicelinkedrolev3]

Expand Down
2 changes: 1 addition & 1 deletion javascriptv3/example_code/iam/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "Apache-2.0",
"type": "module",
"scripts": {
"integration-test": "vitest run **/*.integration.test.js"
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml"
},
"dependencies": {
"@aws-doc-sdk-examples/lib": "^1.0.0",
Expand Down
48 changes: 44 additions & 4 deletions javascriptv3/example_code/iam/scenarios/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { fileURLToPath } from "url";
// snippet-start:[javascript.iam_scenarios.iam_basics]
import {
CreateUserCommand,
GetUserCommand,
CreateAccessKeyCommand,
CreatePolicyCommand,
CreateRoleCommand,
Expand All @@ -20,18 +21,57 @@ import {
import { ListBucketsCommand, S3Client } from "@aws-sdk/client-s3";
import { AssumeRoleCommand, STSClient } from "@aws-sdk/client-sts";
import { retry } from "@aws-doc-sdk-examples/lib/utils/util-timers.js";
import { ScenarioInput } from "@aws-doc-sdk-examples/lib/scenario/index.js";

// Set the parameters.
const iamClient = new IAMClient({});
const userName = "test_name";
const policyName = "test_policy";
const roleName = "test_role";

export const main = async () => {
/**
* Create a new IAM user. If the user already exists, give
* the option to delete and re-create it.
* @param {string} name
*/
export const createUser = async (name, confirmAll = false) => {
try {
const { User } = await iamClient.send(
new GetUserCommand({ UserName: name }),
);
const input = new ScenarioInput(
"deleteUser",
"Do you want to delete and remake this user?",
{ type: "confirm" },
);
const deleteUser = await input.handle({}, { confirmAll });
// If the user exists, and you want to delete it, delete the user
// and then create it again.
if (deleteUser) {
await iamClient.send(new DeleteUserCommand({ UserName: User.UserName }));
await iamClient.send(new CreateUserCommand({ UserName: name }));
} else {
console.warn(
`${name} already exists. The scenario may not work as expected.`,
);
return User;
}
} catch (caught) {
// If there is no user by that name, create one.
if (caught instanceof Error && caught.name === "NoSuchEntityException") {
const { User } = await iamClient.send(
new CreateUserCommand({ UserName: name }),
);
return User;
} else {
throw caught;
}
}
};

export const main = async (confirmAll = false) => {
// Create a user. The user has no permissions by default.
const { User } = await iamClient.send(
new CreateUserCommand({ UserName: userName }),
);
const User = await createUser(userName, confirmAll);

if (!User) {
throw new Error("User not created");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { main } from "../scenarios/basic.js";

describe("basic scenario", () => {
it("should run without error", async () => {
await main();
await main(true);
});
});
2 changes: 1 addition & 1 deletion javascriptv3/example_code/lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "module",
"scripts": {
"test": "vitest run **/*.unit.test.js",
"integration-test": "vitest run **/*.integration.test.js"
"integration-test": "vitest run **/*.integration.test.js --reporter=junit --outputFile=test_results/$npm_package_name.junit.xml"
},
"dependencies": {
"@aws-doc-sdk-examples/lib": "^1.0.0",
Expand Down
Loading
Loading