Repository: pulumi/pulumi-cdk
Pull request #207 (2024-11-08 15:18:58)

Add integration tests for logs construct library
re #183


Patch:
diff --git a/.gitignore b/.gitignore
index 06c3fc7..87af7f7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,6 +9,7 @@ integration/**/yarn.lock
 
 # Logs
 logs
+!integration/logs
 *.log
 npm-debug.log*
 yarn-debug.log*
diff --git a/integration/logs/Pulumi.yaml b/integration/logs/Pulumi.yaml
new file mode 100644
index 0000000..7dadb2e
--- /dev/null
+++ b/integration/logs/Pulumi.yaml
@@ -0,0 +1,3 @@
+name: pulumi-aws-logs
+runtime: nodejs
+description: logs integration test
diff --git a/integration/logs/index.ts b/integration/logs/index.ts
new file mode 100644
index 0000000..5a4871d
--- /dev/null
+++ b/integration/logs/index.ts
@@ -0,0 +1,57 @@
+import * as kinesis from 'aws-cdk-lib/aws-kinesis';
+import * as iam from 'aws-cdk-lib/aws-iam';
+import * as logs_destinations from 'aws-cdk-lib/aws-logs-destinations';
+import * as logs from 'aws-cdk-lib/aws-logs';
+import * as pulumicdk from '@pulumi/cdk';
+
+class LogsStack extends pulumicdk.Stack {
+    constructor(app: pulumicdk.App, id: string, options?: pulumicdk.StackOptions) {
+        super(app, id, options);
+        const logGroup = new logs.LogGroup(this, 'LogGroup', {
+            retention: logs.RetentionDays.ONE_DAY,
+        });
+        new logs.LogStream(this, 'LogStream', {
+            logGroup,
+        });
+        new logs.MetricFilter(this, 'MetricFilter', {
+            logGroup,
+            filterPattern: logs.FilterPattern.allTerms('ERROR'),
+            metricNamespace: 'MyApp',
+            metricName: 'ErrorCount',
+        });
+        const queryString = new logs.QueryString({
+            fields: ['@timestamp', '@message'],
+            sort: '@timestamp desc',
+            limit: 20,
+        });
+        new logs.QueryDefinition(this, 'QueryDefinition', {
+            queryString,
+            logGroups: [logGroup],
+            queryDefinitionName: 'cdk-test-query',
+        });
+
+        logGroup.addToResourcePolicy(
+            new iam.PolicyStatement({
+                effect: iam.Effect.ALLOW,
+                resources: ['*'],
+                actions: ['logs:PutLogEvents'],
+                principals: [new iam.ServicePrincipal('logs.amazonaws.com')],
+            }),
+        );
+
+        const logGroup2 = new logs.LogGroup(this, 'LogGroup2', {
+            retention: logs.RetentionDays.ONE_DAY,
+        });
+        const stream = new kinesis.Stream(this, 'stream', {
+            encryption: kinesis.StreamEncryption.UNENCRYPTED,
+        });
+        logGroup2.addSubscriptionFilter('cdk-filter', {
+            destination: new logs_destinations.KinesisDestination(stream),
+            filterPattern: logs.FilterPattern.allTerms('ERROR'),
+        });
+    }
+}
+
+new pulumicdk.App('app', (scope: pulumicdk.App) => {
+    new LogsStack(scope, 'teststack');
+});
diff --git a/integration/logs/package.json b/integration/logs/package.json
new file mode 100644
index 0000000..888282a
--- /dev/null
+++ b/integration/logs/package.json
@@ -0,0 +1,15 @@
+{
+    "name": "pulumi-aws-cdk",
+    "devDependencies": {
+        "@types/node": "^10.0.0"
+    },
+    "dependencies": {
+        "@pulumi/aws": "^6.0.0",
+        "@pulumi/aws-native": "^1.5.0",
+        "@pulumi/cdk": "^0.5.0",
+        "@pulumi/pulumi": "^3.0.0",
+        "aws-cdk-lib": "2.149.0",
+        "constructs": "10.3.0",
+        "esbuild": "^0.24.0"
+    }
+}
diff --git a/integration/logs/tsconfig.json b/integration/logs/tsconfig.json
new file mode 100644
index 0000000..eac442c
--- /dev/null
+++ b/integration/logs/tsconfig.json
@@ -0,0 +1,18 @@
+{
+    "compilerOptions": {
+        "strict": true,
+        "outDir": "bin",
+        "target": "es2019",
+        "module": "commonjs",
+        "moduleResolution": "node",
+        "sourceMap": true,
+        "experimentalDecorators": true,
+        "pretty": true,
+        "noFallthroughCasesInSwitch": true,
+        "noImplicitReturns": true,
+        "forceConsistentCasingInFileNames": true
+    },
+    "include": [
+        "./*.ts"
+    ]
+}
diff --git a/src/aws-resource-mappings.ts b/src/aws-resource-mappings.ts
index 339b2bb..29e699e 100644
--- a/src/aws-resource-mappings.ts
+++ b/src/aws-resource-mappings.ts
@@ -93,10 +93,14 @@ export function mapToAwsResource(
                 const id = i === 0 ? logicalId : `${logicalId}-policy-${i}`;
                 return {
                     logicalId: id,
-                    resource: new aws.sqs.QueuePolicy(id, {
-                        policy: rawProps.PolicyDocument,
-                        queueUrl: q,
-                    }),
+                    resource: new aws.sqs.QueuePolicy(
+                        id,
+                        {
+                            policy: rawProps.PolicyDocument,
+                            queueUrl: q,
+                        },
+                        options,
+                    ),
                 };
             });
         }
@@ -113,10 +117,14 @@ export function mapToAwsResource(
                 const id = i === 0 ? logicalId : `${logicalId}-policy-${i}`;
                 return {
                     logicalId: id,
-                    resource: new aws.sns.TopicPolicy(id, {
-                        policy: rawProps.PolicyDocument,
-                        arn,
-                    }),
+                    resource: new aws.sns.TopicPolicy(
+                        id,
+                        {
+                            policy: rawProps.PolicyDocument,
+                            arn,
+                        },
+                        options,
+                    ),
                 };
             });
         }


Test patch:
diff --git a/integration/examples_nodejs_test.go b/integration/examples_nodejs_test.go
index 2059c84..be14c83 100644
--- a/integration/examples_nodejs_test.go
+++ b/integration/examples_nodejs_test.go
@@ -59,7 +59,7 @@ func TestRoute53(t *testing.T) {
 				"aws:region":        "us-east-1",
 				"aws-native:region": "us-east-1",
 			},
-    })
+		})
 
 	integration.ProgramTest(t, &test)
 }
@@ -73,6 +73,15 @@ func TestKms(t *testing.T) {
 	integration.ProgramTest(t, &test)
 }
 
+func TestLogs(t *testing.T) {
+	test := getJSBaseOptions(t).
+		With(integration.ProgramTestOptions{
+			Dir: filepath.Join(getCwd(t), "logs"),
+		})
+
+	integration.ProgramTest(t, &test)
+}
+
 func getJSBaseOptions(t *testing.T) integration.ProgramTestOptions {
 	base := getBaseOptions(t)
 	baseJS := base.With(integration.ProgramTestOptions{
diff --git a/tests/aws-resource-mappings.test.ts b/tests/aws-resource-mappings.test.ts
index e5b8d02..08e2648 100644
--- a/tests/aws-resource-mappings.test.ts
+++ b/tests/aws-resource-mappings.test.ts
@@ -150,6 +150,7 @@ describe('AWS Resource Mappings', () => {
                     ],
                 },
             }),
+            {},
         );
         expect(aws.sns.TopicPolicy).toHaveBeenCalledWith(
             `${logicalId}-policy-1`,
@@ -166,6 +167,7 @@ describe('AWS Resource Mappings', () => {
                     ],
                 },
             }),
+            {},
         );
     });
 
@@ -207,6 +209,7 @@ describe('AWS Resource Mappings', () => {
                     ],
                 },
             }),
+            {},
         );
 
         expect(aws.sqs.QueuePolicy).toHaveBeenCalledWith(
@@ -224,6 +227,7 @@ describe('AWS Resource Mappings', () => {
                     ],
                 },
             }),
+            {},
         );
     });