Skip to content

Commit 2e11056

Browse files
authored
test(logging): gen2 integ tests (#3683)
1 parent 5fd8464 commit 2e11056

File tree

3 files changed

+140
-40
lines changed

3 files changed

+140
-40
lines changed

AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/AWSCloudWatchLoggingPluginIntegrationTests/AWSCloudWatchLoggingPluginGen2IntegrationTests.xctestplan

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717
},
1818
"testTargets" : [
1919
{
20-
"skippedTests" : [
21-
"AWSCloudWatchLoggingPluginIntergrationTests\/testFlushLogWithMessages()",
22-
"AWSCloudWatchLoggingPluginIntergrationTests\/testFlushLogWithVerboseMessageAfterDisablingPlugin()",
23-
"AWSCloudWatchLoggingPluginIntergrationTests\/testFlushLogWithVerboseMessageAfterEnablingPlugin()"
24-
],
2520
"target" : {
2621
"containerPath" : "container:CloudWatchLoggingHostApp.xcodeproj",
2722
"identifier" : "21F762DF2BD6B55F0048845A",

AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/AWSCloudWatchLoggingPluginIntegrationTests/AWSCloudWatchLoggingPluginIntegrationTests.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
1515
let amplifyConfigurationFile = "testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration"
1616
let amplifyOutputsFile = "testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplify_outputs"
1717
#if os(tvOS)
18-
let amplifyConfigurationLoggingFile = "testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration_logging_tvOS"
18+
var amplifyConfigurationLoggingFile = "testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration_logging_tvOS"
1919
#elseif os(watchOS)
20-
let amplifyConfigurationLoggingFile = "testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration_logging_watchOS"
20+
var amplifyConfigurationLoggingFile = "testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration_logging_watchOS"
2121
#else
22-
let amplifyConfigurationLoggingFile = "testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration_logging"
22+
var amplifyConfigurationLoggingFile = "testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration_logging"
2323
#endif
2424
var loggingConfiguration: AWSCloudWatchLoggingPluginConfiguration?
2525

@@ -31,6 +31,11 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
3131
continueAfterFailure = false
3232
do {
3333
try Amplify.add(plugin: AWSCognitoAuthPlugin())
34+
35+
if useGen2Configuration {
36+
amplifyConfigurationLoggingFile += "_gen2"
37+
}
38+
3439
let loggingConfigurationFile = try TestConfigHelper.retrieveLoggingConfiguration(forResource: amplifyConfigurationLoggingFile)
3540
loggingConfiguration = try AWSCloudWatchLoggingPluginConfiguration.loadConfiguration(from: loggingConfigurationFile)
3641
let loggingPlugin = AWSCloudWatchLoggingPlugin(loggingPluginConfiguration: loggingConfiguration)

AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/AWSCloudWatchLoggingPluginIntegrationTests/README.md

Lines changed: 132 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,31 @@ The following steps demonstrate how to set up Logging. Auth category is also req
1010

1111
2. Copy `amplifyconfiguration.json` to a new file named `AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration.json` inside `~/.aws-amplify/amplify-ios/testconfiguration/`.
1212

13+
```
14+
cp amplifyconfiguration.json ~/.aws-amplify/amplify-ios/testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration.json
15+
```
16+
1317
3. Configure the `amplifyconfiguration-logging.json` file (https://docs.amplify.aws/swift/build-a-backend/more-features/logging/set-up-logging/#initialize-amplify-logging)
1418

15-
4. Copy `amplifyconfiguration-logging.json` to a new file named `AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration-logging.json` inside `~/.aws-amplify/amplify-ios/testconfiguration/`.
19+
```json
20+
{
21+
"awsCloudWatchLoggingPlugin": {
22+
"enable": true,
23+
"logGroupName": "<log-group-name>",
24+
"region": "<region>",
25+
"localStoreMaxSizeInMB": 1,
26+
"flushIntervalInSeconds": 60,
27+
"loggingConstraints": {
28+
"defaultLogLevel": "VERBOSE"
29+
}
30+
}
31+
}
32+
```
33+
4. Copy `amplifyconfiguration_logging.json` to a new file named `AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration_logging.json` inside `~/.aws-amplify/amplify-ios/testconfiguration/`.
34+
35+
```
36+
cp amplifyconfiguration_logging.json ~/.aws-amplify/amplify-ios/testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration_logging.json
37+
```
1638

1739
5. You can now run all of the integration tests.
1840

@@ -30,17 +52,17 @@ At the time this was written, it follows the steps from here https://docs.amplif
3052
{
3153
...
3254
"devDependencies": {
33-
"@aws-amplify/backend": "^0.13.0-beta.14",
34-
"@aws-amplify/backend-cli": "^0.12.0-beta.16",
35-
"aws-cdk": "^2.134.0",
36-
"aws-cdk-lib": "^2.134.0",
55+
"@aws-amplify/backend": "^0.15.0",
56+
"@aws-amplify/backend-cli": "^0.15.0",
57+
"aws-cdk": "^2.139.0",
58+
"aws-cdk-lib": "^2.139.0",
3759
"constructs": "^10.3.0",
3860
"esbuild": "^0.20.2",
39-
"tsx": "^4.7.1",
40-
"typescript": "^5.4.3"
61+
"tsx": "^4.7.3",
62+
"typescript": "^5.4.5"
4163
},
4264
"dependencies": {
43-
"aws-amplify": "^6.0.25"
65+
"aws-amplify": "^6.2.0"
4466
}
4567
}
4668

@@ -58,53 +80,131 @@ import { defineAuth, defineFunction } from '@aws-amplify/backend';
5880
export const auth = defineAuth({
5981
loginWith: {
6082
email: true
61-
},
62-
triggers: {
63-
// configure a trigger to point to a function definition
64-
preSignUp: defineFunction({
65-
entry: './pre-sign-up-handler.ts'
66-
})
6783
}
6884
});
6985

7086
```
7187

88+
3. Create `amplify/custom/RemoteLoggingConstraintsConstruct.resource.ts` and add the following
89+
7290
```ts
73-
import type { PreSignUpTriggerHandler } from 'aws-lambda';
91+
import * as cdk from "aws-cdk-lib"
92+
import { Construct } from "constructs"
93+
import * as logs from "aws-cdk-lib/aws-logs"
94+
import * as iam from "aws-cdk-lib/aws-iam"
95+
96+
export class RemoteLoggingConstraintsConstruct extends Construct {
97+
constructor(scope: Construct, id: string, authRoleName: string, unAuthRoleName: string) {
98+
super(scope, id)
99+
100+
const region = cdk.Stack.of(this).region
101+
const account = cdk.Stack.of(this).account
102+
const logGroupName = "<log-group-name>"
103+
104+
new logs.LogGroup(this, 'Log Group', {
105+
logGroupName: logGroupName,
106+
retention: logs.RetentionDays.INFINITE
107+
})
108+
109+
const authRole = iam.Role.fromRoleName(this, "Auth-Role", authRoleName)
110+
const unAuthRole = iam.Role.fromRoleName(this, "UnAuth-Role", unAuthRoleName)
111+
const logResource = `arn:aws:logs:${region}:${account}:log-group:${logGroupName}:log-stream:*`
112+
const logIAMPolicy = new iam.PolicyStatement({
113+
effect: iam.Effect.ALLOW,
114+
resources: [logResource],
115+
actions: ["logs:PutLogEvents", "logs:DescribeLogStreams", "logs:CreateLogStream", "logs:FilterLogEvents"]
116+
})
117+
118+
authRole.addToPrincipalPolicy(logIAMPolicy)
119+
unAuthRole.addToPrincipalPolicy(logIAMPolicy)
74120

75-
export const handler: PreSignUpTriggerHandler = async (event) => {
76-
// your code here
77-
event.response.autoConfirmUser = true
78-
return event;
79-
};
121+
new cdk.CfnOutput(this, 'CloudWatchLogGroupName', { value: logGroupName });
122+
new cdk.CfnOutput(this, 'CloudWatchRegion', { value: region });
123+
}
124+
}
80125
```
81126

82-
3. Commit and push the files to a git repository.
127+
Update `backend.ts`.
83128

84-
4. Navigate to the AWS Amplify console (https://us-east-1.console.aws.amazon.com/amplify/home?region=us-east-1#/)
129+
```ts
130+
import { defineBackend } from '@aws-amplify/backend';
131+
import { auth } from './auth/resource';
132+
import { RemoteLoggingConstraintsConstruct } from './custom/RemoteLoggingConstraintsConstruct/resource';
85133

86-
5. Click on "Try Amplify Gen 2" button.
134+
const backend = defineBackend({
135+
auth
136+
});
87137

88-
6. Choose "Option 2: Start with an existing app", and choose Github, and press Next.
138+
// Auth - sign in with username
139+
const { cfnUserPool } = backend.auth.resources.cfnResources
140+
cfnUserPool.usernameAttributes = []
89141

90-
7. Find the repository and branch, and click Next
142+
// ============ Logging Stack ===========
91143

92-
8. Click "Save and deploy" and wait for deployment to finish.
144+
const loggingConstruct = new RemoteLoggingConstraintsConstruct(
145+
backend.createStack('logging-stack'),
146+
'logging-stack',
147+
backend.auth.resources.authenticatedUserIamRole.roleName,
148+
backend.auth.resources.unauthenticatedUserIamRole.roleName
149+
);
93150

94-
9. Generate the `amplify_outputs.json` configuration file
151+
```
152+
153+
4. Deploy the backend with npx amplify sandbox
154+
155+
For example, this deploys to a sandbox env and generates the amplify_outputs.json file.
95156

96157
```
97-
npx amplify generate config --branch main --app-id [APP_ID] --profile [AWS_PROFILE] --config-version 1
158+
npx amplify sandbox --config-out-dir ./config --profile [PROFILE]
98159
```
99160

100-
10. Copy the `amplify_outputs.json` file over to the test directory as `AWSCloudWatchLoggingPluginIntegrationTests-amplify_outputs.json`. The tests will automatically pick this file up. Create the directories in this path first if it currently doesn't exist.
161+
5. Copy `amplify_outputs.json` to a new file named `AWSCloudWatchLoggingPluginIntegrationTests-amplify_outputs.json` inside `~/.aws-amplify/amplify-ios/testconfiguration/`.
101162

102163
```
103164
cp amplify_outputs.json ~/.aws-amplify/amplify-ios/testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplify_outputs.json
104165
```
105166

106-
11. Configure the `amplifyconfiguration-logging.json` file (https://docs.amplify.aws/swift/build-a-backend/more-features/logging/set-up-logging/#initialize-amplify-logging)
167+
6. Configure the `amplifyconfiguration_logging_gen2.json` file (https://docs.amplify.aws/swift/build-a-backend/add-aws-services/logging/set-up-logging/#initialize-amplify-logging)
168+
169+
```json
170+
{
171+
"awsCloudWatchLoggingPlugin": {
172+
"enable": true,
173+
"logGroupName": "<log-group-name>",
174+
"region": "<region>",
175+
"localStoreMaxSizeInMB": 1,
176+
"flushIntervalInSeconds": 60,
177+
"loggingConstraints": {
178+
"defaultLogLevel": "VERBOSE"
179+
}
180+
}
181+
}
182+
```
183+
184+
7. Copy `amplifyconfiguration_logging_gen2.json` to a new file named `AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration_logging_gen2.json` inside `~/.aws-amplify/amplify-ios/testconfiguration/`.
185+
186+
```
187+
cp amplifyconfiguration_logging_gen2.json ~/.aws-amplify/amplify-ios/testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration_logging_gen2.json
188+
```
107189

108-
12. Copy `amplifyconfiguration-logging.json` to a new file named `AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration-logging.json` inside `~/.aws-amplify/amplify-ios/testconfiguration/`.
190+
### Deploying from a branch (Optional)
109191

110-
13. You can now run all of the integration tests.
192+
If you want to be able utilize Git commits for deployments
193+
194+
1. Commit and push the files to a git repository.
195+
196+
2. Navigate to the AWS Amplify console (https://us-east-1.console.aws.amazon.com/amplify/home?region=us-east-1#/)
197+
198+
3. Click on "Try Amplify Gen 2" button.
199+
200+
4. Choose "Option 2: Start with an existing app", and choose Github, and press Next.
201+
202+
5. Find the repository and branch, and click Next
203+
204+
6. Click "Save and deploy" and wait for deployment to finish.
205+
206+
7. Generate the `amplify_outputs.json` configuration file
207+
208+
```
209+
npx amplify generate outputs --branch main --app-id [APP_ID] --profile [AWS_PROFILE]
210+
```

0 commit comments

Comments
 (0)