Skip to content

test: using existing resources in e2e tests #1320

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

Merged
merged 2 commits into from
Jul 2, 2020
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
78 changes: 4 additions & 74 deletions clients/client-cognito-identity/e2e/CognitoIdentity.ispec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,93 +5,23 @@
*/
import { expect } from "chai";
import { CognitoIdentity } from "../index";
import { Credentials } from "@aws-sdk/types";
import { IAM } from "@aws-sdk/client-iam";
// There will be default values of defaultRegion, credentials, and isBrowser variable in browser tests.
// Define the values for Node.js tests
const region: string | undefined =
(globalThis as any).defaultRegion || undefined;
const credentials: Credentials | undefined =
(globalThis as any).credentials || undefined;
const IdentityPoolId =
(globalThis as any)?.window?.__env__?.AWS_SMOKE_TEST_IDENTITY_POOL_ID ||
process?.env?.AWS_SMOKE_TEST_IDENTITY_POOL_ID;

describe("@aws-sdk/client-cognito-identity", function () {
this.timeout(50000);
// Required in run
const unAuthClient = new CognitoIdentity({
region
});
const authClient = new CognitoIdentity({
credentials,
region
});
const iam = new IAM({ credentials, region });
const identityPoolName = `aws_sdk_unit_test_${Date.now()}`;
const identityPoolRoleName = `${identityPoolName}-role`;
let identityPoolRole: string;
let identityPoolId: string;

before(async () => {
// Create an identity pool
const createIdentityPoolResult = await authClient.createIdentityPool({
IdentityPoolName: identityPoolName,
AllowUnauthenticatedIdentities: true
});
expect(createIdentityPoolResult.$metadata.httpStatusCode).to.equal(200);
expect(typeof createIdentityPoolResult.IdentityPoolId).to.equal("string");
identityPoolId = createIdentityPoolResult.IdentityPoolId as string;

// Create a role to be attached to identity pool
const { Role } = await iam.createRole({
RoleName: identityPoolRoleName,
AssumeRolePolicyDocument: `{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity"
}
]
}`
});
identityPoolRole = Role?.Arn as string;

// wait for role creating
// TODO: add RoleExists waiter
await new Promise(resolve => setTimeout(resolve, 5000));

// Set role to identity pool
await authClient.setIdentityPoolRoles({
IdentityPoolId: identityPoolId,
Roles: {
unauthenticated: identityPoolRole!
}
});

// wait for role propagating
// TODO: add waiters
await new Promise(resolve => setTimeout(resolve, 10000));
});

after(async () => {
// Delete the identity pool
if (identityPoolId) {
await authClient.deleteIdentityPool({
IdentityPoolId: identityPoolId
});
}
// Delete identity pool role
if (identityPoolRole) {
await iam.deleteRole({ RoleName: identityPoolRoleName });
}
});

it("should successfully fetch Id and get credentials", async () => {
// Test getId()
const getIdResult = await unAuthClient.getId({
IdentityPoolId: identityPoolId
IdentityPoolId
});
expect(getIdResult.$metadata.httpStatusCode).to.equal(200);
expect(typeof getIdResult.IdentityId).to.equal("string");
Expand Down
46 changes: 46 additions & 0 deletions clients/client-cognito-identity/e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## Integration test resources

Certain resources need to be created to make sure the integration test
has backend resources to test against. Follow steps bellow to create them:

1. Create a Cognito Identity Pool

```console
aws cognito-identity create-identity-pool --identity-pool-name [NAME] --allow-unauthenticated-identities --output text --query '[IdentityPoolId]'
```

It outputs `IdentityPoolId`.

1. Create a IAM Role

1. If you create the Identity Pool with AWS console and allow creating roles on your behalf, you can skip this step

1. Command line:

```console
aws iam create-role --role-name=[ROLE_NAME] --assume-role-policy-document '{
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity"
}
]
}' --output text --query 'Role.Arn'
```

It outputs `RoleArn`

1. Set the unauthenticated role of the Identity Pool created in Step 1 to the role created in Step 2

```console
aws cognito-identity set-identity-pool-roles --identity-pool-id [IdentityPoolId] --roles unauthenticated=[RoleArn]
```

1. Run the test with ENV. For example:

```console
AWS_SMOKE_TEST_REGION=[Region] AWS_SMOKE_TEST_IDENTITY_POOL_ID=[IdentityPoolId] yarn test:e2e
```
9 changes: 7 additions & 2 deletions clients/client-cognito-identity/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function (config) {
frameworks: ["mocha", "chai"],
files: ["e2e/**/*.ispec.ts"],
preprocessors: {
"e2e/**/*.ispec.ts": ["webpack", "sourcemap", "credentials"]
"e2e/**/*.ispec.ts": ["webpack", "sourcemap", "credentials", "env"]
},
webpackMiddleware: {
stats: "minimal"
Expand Down Expand Up @@ -35,6 +35,10 @@ module.exports = function (config) {
},
devtool: "inline-source-map"
},
envPreprocessor: [
"AWS_SMOKE_TEST_REGION",
"AWS_SMOKE_TEST_IDENTITY_POOL_ID"
],
plugins: [
"@aws-sdk/karma-credential-loader",
"karma-chrome-launcher",
Expand All @@ -43,7 +47,8 @@ module.exports = function (config) {
"karma-chai",
"karma-webpack",
"karma-coverage",
"karma-sourcemap-loader"
"karma-sourcemap-loader",
"karma-env-preprocessor"
],
reporters: ["progress"],
port: 9876,
Expand Down
36 changes: 36 additions & 0 deletions clients/client-s3/e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## Integration test resources

Certain resources need to be created to make sure the integration test
has backend resources to test against. Follow steps bellow to create them:

1. Create S3 Bucket

1. Create Bucket with:

```console
aws s3 mb s3://[BucketName] --region [Region]
```

1. Put CORS config to the bucket

```console
aws s3api put-bucket-cors --bucket [BucketName] --cors-configuration '{
"CORSRules": [
{
"AllowedOrigins": ["*"],
"AllowedMethods": ["GET", "PUT", "POST", "DELETE", "HEAD"],
"MaxAgeSeconds": 3000,
"ExposeHeaders": ["ETag", "Date"],
"AllowedHeaders": ["*"]
}
]
}'
```

1. supply the bucket name to the test but setting the ENV `AWS_SMOKE_TEST_BUCKET`

1. Run the test with ENV. For example:

```console
AWS_SMOKE_TEST_REGION=[Region] AWS_SMOKE_TEST_BUCKET=[Bucket] yarn test:e2e
```
16 changes: 10 additions & 6 deletions clients/client-s3/e2e/S3.ispec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ const region: string | undefined =
const credentials: Credentials | undefined =
(globalThis as any).credentials || undefined;
const isBrowser: boolean | undefined = (globalThis as any).isBrowser || false;
const Bucket =
(globalThis as any)?.window?.__env__?.AWS_SMOKE_TEST_BUCKET ||
process?.env?.AWS_SMOKE_TEST_BUCKET;

// this bucket requires enabling CORS:
// AllowedOrigin(*), AllowedMethod(GET, PUT, POST, DELETE, HEAD), ExposeHeader(ETag), AllowedHeader(*)
const Bucket = "aws-sdk-unit-test";
let Key = `${Date.now()}`;

describe("@aws-sdk/client-s3", () => {
Expand Down Expand Up @@ -79,7 +79,7 @@ describe("@aws-sdk/client-s3", () => {
});
expect(result.$metadata.httpStatusCode).to.equal(200);
});
} else {
} else {
it("should succeed with Node.js readable stream body", async () => {
const length = 10 * 1000; // 10KB
const chunkSize = 10;
Expand Down Expand Up @@ -136,8 +136,12 @@ describe("@aws-sdk/client-s3", () => {
});

describe("ListObjects", () => {
before(() => {
before(async () => {
Key = `${Date.now()}`;
await client.putObject({ Bucket, Key, Body: "foo" });
});
after(async () => {
await client.deleteObject({ Bucket, Key });
});
it("should succeed with valid bucket", async () => {
const result = await client.listObjects({
Expand Down Expand Up @@ -247,7 +251,7 @@ describe("@aws-sdk/client-s3", () => {
});
expect(listUploadsResult.$metadata.httpStatusCode).to.equal(200);
expect(
listUploadsResult.Uploads?.map(upload => upload.UploadId)
(listUploadsResult.Uploads || []).map(upload => upload.UploadId)
).not.to.contain(toAbort);
});
});
Expand Down
6 changes: 4 additions & 2 deletions clients/client-s3/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function (config) {
frameworks: ["mocha", "chai"],
files: ["e2e/**/*.ispec.ts"],
preprocessors: {
"e2e/**/*.ispec.ts": ["webpack", "sourcemap", "credentials"]
"e2e/**/*.ispec.ts": ["webpack", "sourcemap", "credentials", "env"]
},
webpackMiddleware: {
stats: "minimal"
Expand Down Expand Up @@ -35,6 +35,7 @@ module.exports = function (config) {
},
devtool: "inline-source-map"
},
envPreprocessor: ["AWS_SMOKE_TEST_REGION", "AWS_SMOKE_TEST_BUCKET"],
plugins: [
"@aws-sdk/karma-credential-loader",
"karma-chrome-launcher",
Expand All @@ -43,7 +44,8 @@ module.exports = function (config) {
"karma-chai",
"karma-webpack",
"karma-coverage",
"karma-sourcemap-loader"
"karma-sourcemap-loader",
"karma-env-preprocessor"
],
reporters: ["progress"],
port: 9876,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage": "^2.0.2",
"karma-env-preprocessor": "^0.1.1",
"karma-firefox-launcher": "^1.3.0",
"karma-jasmine": "^3.3.1",
"karma-mocha": "^2.0.1",
Expand All @@ -69,9 +70,9 @@
"puppeteer": "^4.0.0",
"ts-loader": "^7.0.5",
"typescript": "~3.8.3",
"verdaccio": "^4.7.2",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.12",
"verdaccio": "^4.7.2",
"yarn": "1.22.4"
},
"workspaces": {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7409,6 +7409,11 @@ karma-coverage@^2.0.2:
istanbul-reports "^3.0.0"
minimatch "^3.0.4"

karma-env-preprocessor@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/karma-env-preprocessor/-/karma-env-preprocessor-0.1.1.tgz#bbe8c87d59c00edb76070bd3c31b4b39d5dc7e15"
integrity sha1-u+jIfVnADtt2BwvTwxtLOdXcfhU=

karma-firefox-launcher@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/karma-firefox-launcher/-/karma-firefox-launcher-1.3.0.tgz#ebcbb1d1ddfada6be900eb8fae25bcf2dcdc8171"
Expand Down