Skip to content

Commit

Permalink
test(graphql): modify all e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sundersc committed Sep 20, 2023
1 parent 24dfc2d commit 7d883bc
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 75 deletions.
1 change: 1 addition & 0 deletions packages/amplify-e2e-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@aws-sdk/client-sts": "3.338.0",
"@aws-sdk/credential-providers": "3.338.0",
"amplify-headless-interface": "^1.17.3",
"axios": "^0.26.0",
"chalk": "^4.1.1",
"execa": "^5.1.1",
"fs-extra": "^8.1.0",
Expand Down
14 changes: 14 additions & 0 deletions packages/amplify-e2e-core/src/utils/rds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import {
} from '@aws-sdk/client-rds';
import { EC2Client, AuthorizeSecurityGroupIngressCommand, RevokeSecurityGroupIngressCommand } from '@aws-sdk/client-ec2';
import { knex } from 'knex';
import axios from 'axios';

const DEFAULT_DB_INSTANCE_TYPE = 'db.m5.large';
const DEFAULT_DB_STORAGE = 8;
const DEFAULT_SECURITY_GROUP = 'default';

const IPIFY_URL = 'https://api.ipify.org/';
const AWSCHECKIP_URL = 'https://checkip.amazonaws.com/';

/**
* Creates a new RDS instance using the given input configuration and returns the details of the created RDS instance.
* @param config Configuration of the database instance
Expand Down Expand Up @@ -268,3 +272,13 @@ export const getResource = (resources: Map<string, any>, resourcePrefix: string,
}
return undefined;
};

export const getIpRanges = async (): Promise<string[]> => {
return Promise.all(
[IPIFY_URL, AWSCHECKIP_URL].map(async (url) => {
const response = await axios(url);
const ipParts = response.data.trim().split('.');
return `${ipParts[0]}.${ipParts[1]}.0.0/16`;
}),
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,6 @@ describe('RDS Tests', () => {
},
});

// VPC will not have VPC endpoints for SSM defined and the security group's inbound rule for port 443 is not defined.
// Expect the listComponents query to fail with an error.
expect(appSyncClient).toBeDefined();

const result = await listComponents(appSyncClient);
Expand Down
10 changes: 5 additions & 5 deletions packages/amplify-e2e-tests/src/__tests__/rds-model-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ describe('RDS Model Directive', () => {
// 1. Create a RDS Instance
// 2. Add the external IP address of the current machine to security group inbound rule to allow public access
// 3. Connect to the database and execute DDL
// 4. Remove the inbound rules.

const db = await createRDSInstance({
identifier,
Expand Down Expand Up @@ -172,11 +173,7 @@ describe('RDS Model Directive', () => {
'CREATE TABLE Student (studentId INT NOT NULL, classId CHAR(1) NOT NULL, FirstName VARCHAR(20), LastName VARCHAR(50), PRIMARY KEY (studentId, classId))',
]);
dbAdapter.cleanup();
};

const cleanupDatabase = async (): Promise<void> => {
// 1. Remove the IP address from the security group
// 2. Delete the RDS instance
await Promise.all(
ipAddresses.map((ip) => removeRDSPortInboundRule({
region,
Expand All @@ -185,6 +182,9 @@ describe('RDS Model Directive', () => {
}),
),
);
};

const cleanupDatabase = async (): Promise<void> => {
await deleteDBInstance(identifier, region);
};

Expand All @@ -204,7 +204,7 @@ describe('RDS Model Directive', () => {
port,
username,
password,
useVpc: false,
useVpc: true,
apiExists: true,
});

Expand Down
40 changes: 23 additions & 17 deletions packages/amplify-e2e-tests/src/__tests__/rds-refers-to.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,30 @@ import {
addApiWithoutSchema,
addRDSPortInboundRule,
amplifyPush,
apiGenerateSchema,
createNewProjectDir,
createRDSInstance,
deleteDBInstance,
deleteProject,
deleteProjectDir,
getAppSyncApi,
getIpRanges,
getProjectMeta,
importRDSDatabase,
initJSProjectWithProfile,
removeRDSPortInboundRule,
} from 'amplify-category-api-e2e-core';
import { existsSync, writeFileSync, mkdirSync, readFileSync } from 'fs-extra';
import { existsSync, writeFileSync } from 'fs-extra';
import generator from 'generate-password';
import path from 'path';
import AWSAppSyncClient, { AUTH_TYPE } from 'aws-appsync';
import { GQLQueryHelper } from '../query-utils/gql-helper';
import { gql } from 'graphql-transformer-core';
import { ObjectTypeDefinitionNode, parse } from 'graphql';

// to deal with bug in cognito-identity-js
(global as any).fetch = require('node-fetch');

describe('RDS Relational Directives', () => {
const publicIpCidr = '0.0.0.0/0';
const ipAddresses = [];
const [db_user, db_password, db_identifier] = generator.generateMultiple(3);

// Generate settings for RDS instance
Expand Down Expand Up @@ -96,11 +95,16 @@ describe('RDS Relational Directives', () => {
});
port = db.port;
host = db.endpoint;
await addRDSPortInboundRule({
region,
port: db.port,
cidrIp: publicIpCidr,
});

ipAddresses.push(...(await getIpRanges()));
await Promise.all(
ipAddresses.map((ip) => addRDSPortInboundRule({
region,
port: db.port,
cidrIp: ip,
}),
),
);

const dbAdapter = new RDSTestDataProvider({
host: db.endpoint,
Expand All @@ -118,16 +122,18 @@ describe('RDS Relational Directives', () => {
'CREATE TABLE Task (id VARCHAR(40) PRIMARY KEY, description VARCHAR(255))',
]);
dbAdapter.cleanup();

await Promise.all(
ipAddresses.map((ip) => removeRDSPortInboundRule({
region,
port,
cidrIp: ip,
}),
),
);
};

const cleanupDatabase = async (): Promise<void> => {
// 1. Remove the IP address from the security group
// 2. Delete the RDS instance
await removeRDSPortInboundRule({
region,
port: port,
cidrIp: publicIpCidr,
});
await deleteDBInstance(identifier, region);
};

Expand All @@ -147,7 +153,7 @@ describe('RDS Relational Directives', () => {
port,
username,
password,
useVpc: false,
useVpc: true,
apiExists: true,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
deleteProject,
deleteProjectDir,
getAppSyncApi,
getIpRanges,
getProjectMeta,
importRDSDatabase,
initJSProjectWithProfile,
Expand All @@ -28,6 +29,7 @@ import { ObjectTypeDefinitionNode, parse } from 'graphql';

describe('RDS Relational Directives', () => {
const publicIpCidr = '0.0.0.0/0';
const ipAddresses = [];
const [db_user, db_password, db_identifier] = generator.generateMultiple(3);

// Generate settings for RDS instance
Expand Down Expand Up @@ -101,11 +103,16 @@ describe('RDS Relational Directives', () => {
});
port = db.port;
host = db.endpoint;
await addRDSPortInboundRule({
region,
port: db.port,
cidrIp: publicIpCidr,
});

ipAddresses.push(...(await getIpRanges()));
await Promise.all(
ipAddresses.map((ip) => addRDSPortInboundRule({
region,
port: db.port,
cidrIp: ip,
}),
),
);

const dbAdapter = new RDSTestDataProvider({
host: db.endpoint,
Expand All @@ -131,16 +138,18 @@ describe('RDS Relational Directives', () => {
"INSERT INTO ZipCode VALUES ('20160', 'Lincoln', 'VA', 'US')",
]);
dbAdapter.cleanup();

await Promise.all(
ipAddresses.map((ip) => removeRDSPortInboundRule({
region,
port,
cidrIp: ip,
}),
),
);
};

const cleanupDatabase = async (): Promise<void> => {
// 1. Remove the IP address from the security group
// 2. Delete the RDS instance
await removeRDSPortInboundRule({
region,
port: port,
cidrIp: publicIpCidr,
});
await deleteDBInstance(identifier, region);
};

Expand Down Expand Up @@ -173,7 +182,7 @@ describe('RDS Relational Directives', () => {
port,
username,
password,
useVpc: false,
useVpc: true,
apiExists: true,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
removeRDSPortInboundRule,
apiGenerateSchema,
apiGenerateSchemaWithError,
getIpRanges,
} from 'amplify-category-api-e2e-core';
import axios from 'axios';
import { existsSync, readFileSync, writeFileSync } from 'fs-extra';
Expand All @@ -21,6 +22,7 @@ import path from 'path';

describe('RDS Generate Schema tests', () => {
let publicIpCidr = '0.0.0.0/0';
const ipAddresses = [];
const [db_user, db_password, db_identifier] = generator.generateMultiple(3);

// Generate settings for RDS instance
Expand Down Expand Up @@ -55,7 +57,7 @@ describe('RDS Generate Schema tests', () => {
port,
username,
password,
useVpc: false,
useVpc: true,
apiExists: true,
});
});
Expand Down Expand Up @@ -92,11 +94,16 @@ describe('RDS Generate Schema tests', () => {
});
port = db.port;
host = db.endpoint;
await addRDSPortInboundRule({
region,
port: db.port,
cidrIp: publicIpCidr,
});

ipAddresses.push(...(await getIpRanges()));
await Promise.all(
ipAddresses.map((ip) => addRDSPortInboundRule({
region,
port: db.port,
cidrIp: ip,
}),
),
);

const dbAdapter = new RDSTestDataProvider({
host: db.endpoint,
Expand All @@ -111,16 +118,18 @@ describe('RDS Generate Schema tests', () => {
'CREATE TABLE tbl_todos (ID INT PRIMARY KEY, description VARCHAR(20))',
]);
dbAdapter.cleanup();

await Promise.all(
ipAddresses.map((ip) => removeRDSPortInboundRule({
region,
port,
cidrIp: ip,
}),
),
);
};

const cleanupDatabase = async () => {
// 1. Remove the IP address from the security group
// 2. Delete the RDS instance
await removeRDSPortInboundRule({
region,
port: port,
cidrIp: publicIpCidr,
});
await deleteDBInstance(identifier, region);
};

Expand Down
Loading

0 comments on commit 7d883bc

Please sign in to comment.