Skip to content

Commit

Permalink
Add integration tests for logs construct library (#207)
Browse files Browse the repository at this point in the history
re #183
  • Loading branch information
corymhall authored Nov 8, 2024
1 parent 129e930 commit 0dfa36d
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ integration/**/yarn.lock

# Logs
logs
!integration/logs
*.log
npm-debug.log*
yarn-debug.log*
Expand Down
11 changes: 10 additions & 1 deletion integration/examples_nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestRoute53(t *testing.T) {
"aws:region": "us-east-1",
"aws-native:region": "us-east-1",
},
})
})

integration.ProgramTest(t, &test)
}
Expand All @@ -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{
Expand Down
3 changes: 3 additions & 0 deletions integration/logs/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: pulumi-aws-logs
runtime: nodejs
description: logs integration test
57 changes: 57 additions & 0 deletions integration/logs/index.ts
Original file line number Diff line number Diff line change
@@ -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');
});
15 changes: 15 additions & 0 deletions integration/logs/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
18 changes: 18 additions & 0 deletions integration/logs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
24 changes: 16 additions & 8 deletions src/aws-resource-mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
};
});
}
Expand All @@ -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,
),
};
});
}
Expand Down
4 changes: 4 additions & 0 deletions tests/aws-resource-mappings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ describe('AWS Resource Mappings', () => {
],
},
}),
{},
);
expect(aws.sns.TopicPolicy).toHaveBeenCalledWith(
`${logicalId}-policy-1`,
Expand All @@ -166,6 +167,7 @@ describe('AWS Resource Mappings', () => {
],
},
}),
{},
);
});

Expand Down Expand Up @@ -207,6 +209,7 @@ describe('AWS Resource Mappings', () => {
],
},
}),
{},
);

expect(aws.sqs.QueuePolicy).toHaveBeenCalledWith(
Expand All @@ -224,6 +227,7 @@ describe('AWS Resource Mappings', () => {
],
},
}),
{},
);
});

Expand Down

0 comments on commit 0dfa36d

Please sign in to comment.