Skip to content

Fix CloudWatch Logs permissions and add comprehensive testing helpers#2

Merged
ncipollina merged 1 commit into
mainfrom
feature/fix-cloudwatch-logs-permissions-and-testing-helpers
Jul 3, 2025
Merged

Fix CloudWatch Logs permissions and add comprehensive testing helpers#2
ncipollina merged 1 commit into
mainfrom
feature/fix-cloudwatch-logs-permissions-and-testing-helpers

Conversation

@ncipollina

Copy link
Copy Markdown
Contributor

Summary

  • Fixed CloudWatch Logs policy to use full function name with suffix
  • Added comprehensive testing helpers for library consumers
  • Enhanced documentation and examples

Changes Made

🐛 Bug Fixes

  • Fixed CloudWatch Logs IAM policy: Resource ARNs now correctly include function suffix (function-name-suffix instead of just function-name)
  • Updated existing tests: Modified tests to match the corrected CloudWatch permissions pattern

✨ New Features

  • Testing Helpers Package: Added comprehensive testing infrastructure in src/LayeredCraft.Cdk.Constructs/Testing/:
    • CdkTestHelper: Utilities for creating test stacks and resolving asset paths
    • LambdaFunctionConstructAssertions: Extension methods for CDK template assertions
    • LambdaFunctionConstructPropsBuilder: Fluent builder for creating test props with AWS service integrations

📚 Documentation Updates

  • README.md: Added comprehensive testing section with examples and assertion method documentation
  • CLAUDE.md: Updated with testing infrastructure details and critical CDK template creation patterns
  • Examples: Added examples showing correct CDK template creation timing

🧪 Test Coverage

Added comprehensive test coverage for:

  • ShouldHaveEnvironmentVariables and ShouldHaveLambdaPermissions assertion methods
  • WithS3Access, WithCustomPolicy, WithCustomPermission builder methods
  • GetTestAssetPath and GetTestLambdaZipPath utility methods

Testing Helpers Usage

// Create test infrastructure
var stack = CdkTestHelper.CreateTestStackMinimal();

// Build test props with fluent API
var props = CdkTestHelper.CreatePropsBuilder()
    .WithFunctionName("my-api")
    .WithDynamoDbAccess("users-table")
    .WithApiGatewayPermission("arn:aws:execute-api:us-east-1:123456789012:abc/prod/GET/users")
    .Build();

// Create construct and template
_ = new LambdaFunctionConstruct(stack, "TestLambda", props);
var template = Template.FromStack(stack); // CRITICAL: Create template AFTER construct

// Use assertion helpers
template.ShouldHaveLambdaFunction("my-api-test");
template.ShouldHaveCloudWatchLogsPermissions("my-api-test");
template.ShouldHaveEnvironmentVariables(expectedVars);

Test Plan

  • All existing tests pass
  • New testing helper methods are fully tested
  • CloudWatch Logs permissions fixed and verified
  • Documentation examples are accurate
  • CDK template creation timing is correct

🤖 Generated with Claude Code

## Summary
- Fixed CloudWatch Logs policy to use full function name with suffix
- Added comprehensive testing helpers for library consumers
- Enhanced documentation and examples

## Changes
### Bug Fixes
- Fixed CloudWatch Logs IAM policy resource ARNs to include function suffix
- Updated existing tests to match corrected CloudWatch permissions

### New Features
- Added testing helpers in `src/LayeredCraft.Cdk.Constructs/Testing/`:
  - `CdkTestHelper`: Stack creation and asset path utilities
  - `LambdaFunctionConstructAssertions`: Extension methods for CDK template assertions
  - `LambdaFunctionConstructPropsBuilder`: Fluent builder for test props
- Added comprehensive test coverage for all testing helper methods

### Documentation
- Updated README.md with testing helpers documentation and examples
- Updated CLAUDE.md with testing infrastructure details and critical patterns
- Added examples showing correct CDK template creation timing

## Test Coverage
- Added tests for `ShouldHaveEnvironmentVariables` and `ShouldHaveLambdaPermissions` assertions
- Added tests for `WithS3Access`, `WithCustomPolicy`, `WithCustomPermission` builder methods
- Added tests for `GetTestAssetPath` and `GetTestLambdaZipPath` utility methods

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@ncipollina
ncipollina requested a review from Copilot July 3, 2025 19:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes the CloudWatch Logs IAM policy to include function suffixes and adds a rich set of testing helpers with comprehensive documentation and examples.

  • Corrected the CloudWatch Logs policy resource ARNs to use full function name plus suffix
  • Introduced testing infrastructure (CdkTestHelper, assertion extensions, props builder) and updated tests
  • Enhanced README and CLAUDE.md with usage examples and documentation of new helpers

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/LayeredCraft.Cdk.Constructs/Constructs/LambdaFunctionConstruct.cs Updated log policy ARNs to include FunctionSuffix
src/LayeredCraft.Cdk.Constructs/Testing/ Added CdkTestHelper, LambdaFunctionConstructPropsBuilder, and LambdaFunctionConstructAssertions
test/LayeredCraft.Cdk.Constructs.Tests/Testing/TestingHelpersTests.cs New tests for the testing helpers
test/LayeredCraft.Cdk.Constructs.Tests/TestKit/Extensions/AssetPathExtensions.cs Added asset‐path resolution extensions
README.md Documented new testing support
CLAUDE.md Updated instructions and helper docs
Comments suppressed due to low confidence (6)

test/LayeredCraft.Cdk.Constructs.Tests/TestKit/Extensions/AssetPathExtensions.cs:1

  • AssetPathExtensions uses Path.Combine and Path.GetDirectoryName but doesn’t import System.IO. Add using System.IO; to the top of this file so that Path is resolved correctly.
using System.Reflection;

src/LayeredCraft.Cdk.Constructs/Testing/CdkTestHelper.cs:1

  • CdkTestHelper calls Path.GetDirectoryName and Path.Combine but lacks using System.IO;. Add that import to avoid compilation errors.
using System.Reflection;

src/LayeredCraft.Cdk.Constructs/Testing/LambdaFunctionConstructPropsBuilder.cs:1

  • This builder uses List<T> and Dictionary<TKey, TValue> but doesn’t import System.Collections.Generic. Add using System.Collections.Generic; at the top.
using Amazon.CDK.AWS.IAM;

src/LayeredCraft.Cdk.Constructs/Testing/LambdaFunctionConstructAssertions.cs:1

  • This assertions class builds Dictionary<string, object> instances but lacks using System.Collections.Generic;. Add that import to ensure the generic types resolve.
using Amazon.CDK.Assertions;

test/LayeredCraft.Cdk.Constructs.Tests/Testing/TestingHelpersTests.cs:1

  • TestingHelpersTests calls Path.IsPathRooted but doesn’t import System.IO. Add using System.IO; so Path is recognized.
using Amazon.CDK.Assertions;

test/LayeredCraft.Cdk.Constructs.Tests/Testing/TestingHelpersTests.cs:2

  • This test file constructs Dictionary<string, string> but doesn’t import System.Collections.Generic. Add using System.Collections.Generic; to avoid errors.
using AwesomeAssertions;

@ncipollina
ncipollina merged commit 40aba7a into main Jul 3, 2025
1 check passed
@ncipollina
ncipollina deleted the feature/fix-cloudwatch-logs-permissions-and-testing-helpers branch July 3, 2025 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants