Skip to content

test(reporting/exporters/mongo): add mongo integration test with test… #6237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

loresuso
Copy link

@loresuso loresuso commented May 19, 2025

…container-go module

Proposed changes

Proposing to implement integration testing with testcontainers for all export methods

Checklist

  • Pull request is created against the dev branch
  • All checks passed (lint, unit/integration/regression tests etc.) with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Summary by CodeRabbit

  • New Features
    • Introduced integration tests for MongoDB exporter functionality, ensuring data export and verification with a real MongoDB container.
  • Chores
    • Updated and added multiple dependencies, including upgrades to Docker, OpenTelemetry, and MongoDB-related libraries, improving compatibility and support for containerized environments.

@auto-assign auto-assign bot requested a review from dogancanbakir May 19, 2025 11:36
Copy link
Contributor

coderabbitai bot commented May 19, 2025

Walkthrough

A new integration test for MongoDB exporter functionality was added, involving the orchestration of a MongoDB container, data export, and verification of persistence. The test is registered under a new "exporters" group in the integration test runner. Additionally, the go.mod file was updated to upgrade and add several dependencies related to containers, logging, telemetry, and testing.

Changes

File(s) Change Summary
go.mod Upgraded and added multiple dependencies, including Docker, containerd, OpenTelemetry, logging, and MongoDB/testing-related modules.
cmd/integration-test/exporters.go Added a new integration test for MongoDB exporter using a containerized MongoDB instance and verifying data persistence.
cmd/integration-test/integration-test.go Registered the new "exporters" test group in the protocolTests map for integration test execution.

Sequence Diagram(s)

sequenceDiagram
    participant TestRunner
    participant MongoDBContainer
    participant MongoExporter
    participant MongoDBClient

    TestRunner->>MongoDBContainer: Start MongoDB container
    MongoDBContainer-->>TestRunner: Provide connection string
    TestRunner->>MongoExporter: Export sample ResultEvent
    MongoExporter->>MongoDBContainer: Insert document
    TestRunner->>MongoDBClient: Connect and query for inserted document
    MongoDBClient-->>TestRunner: Return document
    TestRunner->>MongoDBContainer: Terminate container
    TestRunner->>MongoExporter: Close exporter
    TestRunner->>MongoDBClient: Close client
Loading

Poem

In a burrow deep, I watched the code grow,
With Mongo and Docker, the tests now flow.
Exporters are tested, their data secure,
With new dependencies, our builds are pure.
Hopping through changes, I wiggle my nose—
Integration complete, let the carrots compose! 🥕


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
pkg/reporting/exporters/mongo/mongo_test.go (2)

17-29: Consider adding context timeout and better error handling.

While the test correctly sets up a MongoDB container, it would benefit from a timeout context and improved error handling for container startup failures.

-	ctx := context.Background()
+	// Add timeout to prevent hanging tests
+	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
+	defer cancel()

 	mongodbContainer, err := mongodb.Run(ctx, "mongo:6")
 	defer func() {
 		if err := testcontainers.TerminateContainer(mongodbContainer); err != nil {
 			log.Printf("failed to terminate container: %s", err)
 		}
 	}()
 	if err != nil {
-		log.Printf("failed to start container: %s", err)
-		return
+		t.Fatalf("failed to start container: %s", err)
 	}

20-20: Consider parameterizing MongoDB version.

The MongoDB version is hardcoded to "mongo:6". Consider making this a constant or test parameter to make it easier to test with different versions.

+const (
+	dbName         = "test"
+	mongoDBVersion = "mongo:6"
+)

 func TestMongo(t *testing.T) {
 	ctx := context.Background()

-	mongodbContainer, err := mongodb.Run(ctx, "mongo:6")
+	mongodbContainer, err := mongodb.Run(ctx, mongoDBVersion)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 242b1e1 and 35a3832.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (2)
  • go.mod (7 hunks)
  • pkg/reporting/exporters/mongo/mongo_test.go (1 hunks)
🔇 Additional comments (15)
go.mod (10)

121-122: LGTM! Dependency version upgrade.

The upgrade of the dario.cat/mergo package from v1.0.0 to v1.0.1 is appropriate. This is a minor version update that likely contains bug fixes.


155-157: LGTM! New containerd-related dependencies.

These containerd-related dependencies are required to support the testcontainers functionality being introduced for MongoDB integration testing.


158-158: LGTM! Docker ecosystem dependencies.

These Docker-related dependencies are necessary for the testcontainers framework to manage Docker containers for the integration tests.

Also applies to: 162-162, 165-166, 167-167


169-170: LGTM! HTTP instrumentation dependencies.

The addition of HTTP instrumentation libraries (felixge/fgprof and felixge/httpsnoop) will support profiling and monitoring in the testing environment.


179-180: LGTM! Logging dependencies.

These logging libraries (go-logr/logr and go-logr/stdr) provide structured logging capabilities which are beneficial for test output and debugging.


212-212: LGTM! Additional container management dependencies.

These dependencies provide additional container management capabilities needed for the testcontainer functionality.

Also applies to: 217-221, 225-225


245-245: LGTM! System monitoring dependency.

Upgrading to the latest gopsutil version provides better system monitoring capabilities which may be helpful for resource tracking during tests.


250-251: LGTM! Core testcontainers dependencies.

These are the primary dependencies for the integration tests being implemented. The main testcontainers-go module along with its MongoDB-specific module are essential for the MongoDB integration testing implemented in this PR.


278-282: LGTM! OpenTelemetry dependencies.

The addition of OpenTelemetry dependencies will enable better observability in the testing environment, potentially helping with diagnosing test failures or performance issues.


365-365: LGTM! Windows-specific dependency upgrade.

Upgrading the Microsoft/go-winio package helps with Windows compatibility if tests need to run on Windows environments.

pkg/reporting/exporters/mongo/mongo_test.go (5)

1-12: LGTM! Proper package structure and imports.

The file is correctly set up with appropriate imports for the MongoDB integration test, including the necessary testcontainers libraries.


13-16: LGTM! Database name constant.

Good practice to define the database name as a constant for reuse throughout the test.


30-37: LGTM! Proper container connection setup.

The code correctly obtains the connection string from the container and appends the database name.


38-43: LGTM! Clear exporter configuration.

The MongoDB exporter is configured with appropriate test parameters, including a batch size of 1 which is good for testing purposes.


44-53: LGTM! Proper exporter initialization and cleanup.

The test correctly initializes the exporter and ensures it's properly closed after the test completes.

@loresuso loresuso force-pushed the testcontainer branch 4 times, most recently from 7b9adfe to dcfbd56 Compare May 19, 2025 13:13
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments. If you are seeing this consistently it is likely a permissions issue. Please check "Moderation" -> "Code review limits" under your organization settings.

Actionable comments posted: 3

♻️ Duplicate comments (1)
pkg/reporting/exporters/mongo/mongo_test.go (1)

69-85: 🛠️ Refactor suggestion

Improve verification of exported data.

The current verification only checks if a document can be found but doesn't verify that all the exported fields were correctly saved. The original res variable is also being overwritten, which isn't ideal for verification.

	// Verify that the result was written to the database
	clientOptions := mongooptions.Client().ApplyURI(connString)
	client, err := mongo.Connect(ctx, clientOptions)
	if err != nil {
		t.Fatalf("Error creating MongoDB client: %s", err)
	}
	defer func() {
		if err := client.Disconnect(ctx); err != nil {
			t.Fatalf("failed to disconnect from MongoDB: %s", err)
		}
	}()

	collection := client.Database(dbName).Collection(opts.CollectionName)
-	err = collection.FindOne(ctx, map[string]interface{}{"request": res.Request}).Decode(&res)
+	var retrievedResult output.ResultEvent
+	err = collection.FindOne(ctx, map[string]interface{}{"request": res.Request}).Decode(&retrievedResult)
	if err != nil {
		t.Fatalf("failed to find document in MongoDB: %s", err)
	}
+	
+	// Verify that the retrieved document matches the expected values
+	if retrievedResult.Request != res.Request || retrievedResult.Response != res.Response {
+		t.Fatalf("exported data doesn't match expected values: got %v, want Request=%s, Response=%s", 
+			retrievedResult, res.Request, res.Response)
+	}
🧹 Nitpick comments (1)
pkg/reporting/exporters/mongo/mongo_test.go (1)

59-62: Create a more comprehensive test case.

The current test case only populates the Request and Response fields. Consider creating a more comprehensive test case by populating additional fields from the ResultEvent struct to better test the exporter's capabilities.

	res := &output.ResultEvent{
		Request:  "test request",
		Response: "test response",
+		TemplateID: "test-template",
+		Type: "http",
+		Host: "example.com",
+		Matched: "test match",
+		ExtractedResults: []string{"test extraction"},
+		Timestamp: time.Now(),
+		MatcherStatus: true,
	}
🛑 Comments failed to post (3)
pkg/reporting/exporters/mongo/mongo_test.go (3)

39-39: 💡 Verification agent

🧩 Analysis chain

Check MongoDB connection string format.

When appending the database name to the connection string, you might need a separator character before the database name.


🏁 Script executed:

#!/bin/bash
# Check the typical format of MongoDB connection strings 
echo "Checking MongoDB Go driver documentation..."
curl -s https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo | grep -A 5 "mongodb://" || echo "No clear example found"

echo "Checking MongoDB connection string format..."
echo "Typical format is: mongodb://host:port/database"

Length of output: 4445


Ensure a “/” separator when appending the database name to the MongoDB URI

The standard MongoDB URI format is:

mongodb://host:port/database

In
• pkg/reporting/exporters/mongo/mongo_test.go:39
you’re doing:

connString = connString + dbName

which will yield e.g.
"mongodb://localhost:27017myDb"

Instead, include a slash before the database name:

- connString = connString + dbName
+ connString = connString + "/" + dbName

Or, for clarity:

connString = fmt.Sprintf("%s/%s", connString, dbName)
🤖 Prompt for AI Agents
In pkg/reporting/exporters/mongo/mongo_test.go at line 39, the code appends the
database name directly to the connection string without a separating slash,
resulting in an invalid MongoDB URI. Fix this by adding a "/" separator before
the database name when concatenating, for example by using string formatting to
combine the connection string and database name with a slash in between.

20-33: 🛠️ Refactor suggestion

Improve error handling for container startup failure.

Currently, if container creation fails, the code logs the error and returns instead of failing the test. Also, the deferred termination function would try to terminate a nil container, potentially causing a panic.

Apply this diff for better error handling:

	// Start a MongoDB container
	mongodbContainer, err := mongodb.Run(ctx, "mongo:8")
+	if err != nil {
+		t.Fatalf("failed to start container: %s", err)
+	}
	defer func() {
		if err := testcontainers.TerminateContainer(mongodbContainer); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
-	if err != nil {
-		log.Printf("failed to start container: %s", err)
-		return
-	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

func TestMongo(t *testing.T) {
    ctx := context.Background()

    // Start a MongoDB container
    mongodbContainer, err := mongodb.Run(ctx, "mongo:8")
    if err != nil {
        t.Fatalf("failed to start container: %s", err)
    }
    defer func() {
        if err := testcontainers.TerminateContainer(mongodbContainer); err != nil {
            log.Printf("failed to terminate container: %s", err)
        }
    }()
    // …rest of test…
}
🤖 Prompt for AI Agents
In pkg/reporting/exporters/mongo/mongo_test.go around lines 20 to 33, improve
error handling by checking if the container creation returned an error before
deferring the termination. If there is an error starting the container, call
t.Fatalf to fail the test immediately instead of just logging and returning.
Also, only defer the termination function if the container was successfully
created to avoid panics from terminating a nil container.

24-24: 💡 Verification agent

🧩 Analysis chain

Verify MongoDB version compatibility.

The test uses "mongo:8" which may not be a valid MongoDB version. The latest MongoDB version is 7.0 as of May 2023, and container tags usually follow MongoDB versioning conventions.


🌐 Web query:

What is the latest MongoDB version available as a Docker container?

💡 Result:

Based on the search results, the latest MongoDB version available as a Docker container depends on which image repository you're using.

From the official MongoDB Docker Hub repository, MongoDB 7.0.20 appears to be the latest version available as of May 2025. This is indicated by a recent update to the 7.0 branch that was made approximately three weeks ago[6].

For the Bitnami packaged version of MongoDB, version 7.0.14 is mentioned in an example from September 2024[8]. However, since this information is from 2024, there may be newer versions available now.

The official Docker Hub repository for MongoDB offers different versions, including the latest builds that can be pulled using the command:

docker pull mongo:latest

To determine the exact MongoDB version inside a container, you can run:

docker run --rm mongo:latest mongod --version

This command will display the specific MongoDB version installed in the container[8].

MongoDB Docker containers come in two main editions:

  • Community Edition: Ideal for non-commercial use or smaller setups
  • Enterprise Edition: Includes additional features like encryption, auditing, and LDAP integration for larger applications or businesses[7]

If you need the very latest version for your specific use case, it's recommended to check the Docker Hub tags page for the most current information, as new versions are regularly released.

Citations:


Use a valid MongoDB Docker image tag

The test in pkg/reporting/exporters/mongo/mongo_test.go (line 24) currently tries to pull mongo:8, which isn’t published. The latest official major release is 7.x (for example, 7.0.20). Please update the tag to a valid one—e.g.:

- mongodbContainer, err := mongodb.Run(ctx, "mongo:8")
+ mongodbContainer, err := mongodb.Run(ctx, "mongo:7")

or pin to a specific patch release:

- mongodbContainer, err := mongodb.Run(ctx, "mongo:8")
+ mongodbContainer, err := mongodb.Run(ctx, "mongo:7.0.20")

This ensures the container can be pulled successfully.

🤖 Prompt for AI Agents
In pkg/reporting/exporters/mongo/mongo_test.go at line 24, the Docker image tag
"mongo:8" is invalid because MongoDB version 8 does not exist yet. Update the
tag to a valid MongoDB version such as "mongo:7.0.20" or another current 7.x
release to ensure the container can be pulled and run successfully during tests.

@loresuso
Copy link
Author

cc @dogancanbakir what do you think? I believe we are missing integration tests for all our exporters. It would be nice to cover these tests

@dogancanbakir
Copy link
Member

@loresuso Thanks for the PR! 100%. What do you think about mocking MongoDB instead of setting up a container to run actual MongoDB? If you still believe we should proceed with this, we can write integration tests instead. We've a simple convention for it, in this dir https://github.com/projectdiscovery/nuclei/tree/dev/cmd/integration-test. Let me know what you think! We can collab on it!

@loresuso
Copy link
Author

Thanks for the comment @dogancanbakir ! Yes, my original idea would be to implement integration tests for all the exporters. The container felt the most useful way to test the integration, so that we know we are really writing to a mongo instance. I will move this file to the integration tests directory then! :)

@loresuso
Copy link
Author

Hey @dogancanbakir! so it looks like I need to put my logic inside an Execute function? even if I will not use the argument (no template needed)? Also, if you have instructions on how to run integration tests locally, that would be very much appreciated :)

@loresuso
Copy link
Author

Ok so now it should be in the expected format! Trying to find the best way to run it

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (5)
cmd/integration-test/mongo.go (5)

18-21: Consider using a more stable MongoDB version tag

The MongoDB image version is set to "mongo:8", which might become outdated or unsupported over time. Consider using a more stable tag like "latest" or a specific minor version that's known to be stable.

const (
	dbName  = "test"
-	dbImage = "mongo:8"
+	dbImage = "mongo:latest" // or a specific stable version like "mongo:6.0"
)

88-92: Enhance result verification

The current verification only checks if the document exists by request field. Consider verifying all fields of the exported result to ensure data integrity.

collection := client.Database(dbName).Collection(opts.CollectionName)
err = collection.FindOne(ctx, map[string]interface{}{"request": res.Request}).Decode(&res)
if err != nil {
	return fmt.Errorf("failed to find document in MongoDB: %s", err)
}

+ // Verify that the returned document matches our original test data
+ if res.Request != "test request" || res.Response != "test response" {
+ 	return fmt.Errorf("retrieved document does not match expected values: %+v", res)
+ }

48-52: Consider using a unique collection name for test isolation

Using a hardcoded "test" collection name may lead to conflicts with other tests or existing data. Consider using a unique/random name.

opts := mongo.Options{
	ConnectionString: connString,
-	CollectionName:   "test",
+	CollectionName:   fmt.Sprintf("test_%d", time.Now().UnixNano()), // Generate unique collection name
	BatchSize:        1, // Ensure we write the result immediately
}

93-95: Add cleanup to remove test collection

The test should clean up after itself by dropping the collection to avoid leaving test data in the database.

+ // Clean up: drop the test collection
+ if err := collection.Drop(ctx); err != nil {
+ 	log.Printf("warning: failed to drop test collection: %s", err)
+ }

return nil

65-73: Enhance test coverage with additional test cases

The current test only covers the basic export functionality. Consider adding test cases for edge cases, such as empty results or results with special characters.

You could extend the test to include multiple test cases with different types of data to ensure robust handling of various inputs:

testCases := []struct {
	name string
	result *output.ResultEvent
}{
	{
		name: "basic case",
		result: &output.ResultEvent{
			Request: "test request",
			Response: "test response",
		},
	},
	{
		name: "with special characters",
		result: &output.ResultEvent{
			Request: "test request with $pecial ch@racters",
			Response: "test response with \"quotes\" and other special characters",
		},
	},
	// Add more test cases as needed
}

for _, tc := range testCases {
	t.Run(tc.name, func(t *testing.T) {
		// Export the result
		err = exporter.Export(tc.result)
		if err != nil {
			return fmt.Errorf("failed to export result event to MongoDB: %s", err)
		}
		
		// Verify the export
		// ... (verification code)
	})
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between dcfbd56 and 9fa9913.

📒 Files selected for processing (1)
  • cmd/integration-test/mongo.go (1 hunks)

@dogancanbakir
Copy link
Member

@loresuso Yes! You're in the right direction. To run these tests, we need to add them to the integration-test.go, to the protocolTests (name can be confusing, but ignore it for now). Also, we can rename mongo.go to exporter.go or something similar to make it a place for other exporter tests.

To run the tests, go to the integration_tests at the root dir and run run.sh. Let me know if you have any other questions!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🔭 Outside diff range comments (1)
cmd/integration-test/mongo.go (1)

1-96: ⚠️ Potential issue

Remove redundant file due to code duplication

This file appears to duplicate the implementation that already exists in cmd/integration-test/exporters.go. The static analysis shows multiple redeclarations of dbName, dbImage, mongoExporter struct, and the Execute method.

Having duplicate implementations creates maintenance challenges and confusion about which file is actually used in the test framework.

Since exporters.go already contains the proper test case registration with exportersTestCases, this file should be removed completely. The integration test framework references the test cases through exportersTestCases in exporters.go, making this file redundant.

🧰 Tools
🪛 golangci-lint (1.64.8)

19-19: dbName redeclared in this block

(typecheck)


20-20: dbImage redeclared in this block

(typecheck)


23-23: mongoExporter redeclared in this block

(typecheck)


25-25: method mongoExporter.Execute already declared at cmd/integration-test/exporters.go:29:25

(typecheck)

♻️ Duplicate comments (3)
cmd/integration-test/mongo.go (3)

45-45: Fix incorrect use of fmt.Println with format string

fmt.Println doesn't process format directives like %s. You should use fmt.Printf instead.

- fmt.Println("MongoDB connection string: %s", connString)
+ fmt.Printf("MongoDB connection string: %s\n", connString)

58-63: Improve error handling in defer function

The current defer function returns an error, but return values from defer functions are ignored. Consider logging the error instead.

- defer func() error {
+ defer func() {
  	if err := exporter.Close(); err != nil {
-   	return fmt.Errorf("failed to close exporter: %s", err)
+   	log.Printf("failed to close exporter: %s", err)
  	}
- 	return nil
}()

81-86: Fix ineffective error return in defer function

Similar to the previous issue, this defer function returns an error which is ignored. The error should be logged instead.

- defer func() error {
+ defer func() {
  	if err := client.Disconnect(ctx); err != nil {
-   	return fmt.Errorf("failed to disconnect from MongoDB: %s", err)
+   	log.Printf("failed to disconnect from MongoDB: %s", err)
  	}
- 	return nil
}()
🧹 Nitpick comments (1)
cmd/integration-test/exporters.go (1)

68-76: Enhance test with more comprehensive ResultEvent data

The current test only populates Request and Response fields. Consider adding more fields to the ResultEvent to test a more realistic scenario, such as TemplateID, Host, and Timestamp.

res := &output.ResultEvent{
	Request:  "test request",
	Response: "test response",
+	TemplateID: "test-template-id",
+	Host:     "test-host",
+	Timestamp: time.Now(),
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 9fa9913 and 939a5cf.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (4)
  • cmd/integration-test/exporters.go (1 hunks)
  • cmd/integration-test/integration-test.go (1 hunks)
  • cmd/integration-test/mongo.go (1 hunks)
  • go.mod (7 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • go.mod
🧰 Additional context used
🧬 Code Graph Analysis (1)
cmd/integration-test/mongo.go (1)
pkg/output/output.go (1)
  • ResultEvent (146-221)
🪛 golangci-lint (1.64.8)
cmd/integration-test/exporters.go

19-19: other declaration of dbName

(typecheck)


20-20: other declaration of dbImage

(typecheck)


27-27: other declaration of mongoExporter

(typecheck)

cmd/integration-test/mongo.go

19-19: dbName redeclared in this block

(typecheck)


20-20: dbImage redeclared in this block

(typecheck)


23-23: mongoExporter redeclared in this block

(typecheck)


25-25: method mongoExporter.Execute already declared at cmd/integration-test/exporters.go:29:25

(typecheck)

🔇 Additional comments (2)
cmd/integration-test/integration-test.go (1)

59-59: LGTM: Successfully integrates the MongoDB exporter tests

The change correctly adds the "exporters" category to the protocolTests map, allowing the new MongoDB exporter integration tests to run within the existing framework.

cmd/integration-test/exporters.go (1)

34-42: LGTM: Proper MongoDB container setup with error handling

The container setup is well-implemented with appropriate error handling and cleanup in the defer function.

@loresuso loresuso force-pushed the testcontainer branch 3 times, most recently from 2ea0d88 to 1a0b72c Compare May 22, 2025 13:32
@loresuso
Copy link
Author

Ok, I think we are good! tested locally and it works again

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
cmd/integration-test/exporters.go (1)

44-48: 🛠️ Refactor suggestion

Validate the connection string format before appending the database name

The code currently appends the database name directly to the connection string without checking if the connection string already ends with a slash. This could lead to malformed URIs.

connString, err := mongodbContainer.ConnectionString(ctx)
if err != nil {
	return fmt.Errorf("failed to get connection string for MongoDB container: %s", err)
}
+// Ensure the connection string ends with a slash before appending database name
+if !strings.HasSuffix(connString, "/") {
+	connString += "/"
+}
connString = connString + dbName

Don't forget to add "strings" to the import statement.

🧹 Nitpick comments (3)
cmd/integration-test/exporters.go (3)

30-31: Consider using a shorter timeout for the test context

A 10-minute timeout seems excessive for this integration test. A shorter timeout (e.g., 1-2 minutes) would be more appropriate and would fail faster if there are issues.

-	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
+	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)

67-70: Consider using more realistic test data

The current test data is very minimal. Consider using more comprehensive test data that better represents real-world usage, including more fields from the ResultEvent struct.

res := &output.ResultEvent{
	Request:  "test request",
	Response: "test response",
+	// Add more fields to better represent real-world data
+	TemplateID: "test-template",
+	MatcherName: "test-matcher",
+	Type: "http",
+	Host: "example.com",
+	Timestamp: time.Now().UTC(),
}

51-55: Consider making test constants configurable

The collection name and batch size are hardcoded. Consider making these configurable constants at the top of the file alongside the other constants.

const (
	dbName  = "test"
	dbImage = "mongo:8"
+	collectionName = "test"
+	batchSize = 1 // Ensure we write the result immediately
)

// ...

opts := mongo.Options{
	ConnectionString: connString,
-	CollectionName:   "test",
-	BatchSize:        1, // Ensure we write the result immediately
+	CollectionName:   collectionName,
+	BatchSize:        batchSize,
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 2ea0d88 and 1a0b72c.

⛔ Files ignored due to path filters (8)
  • go.sum is excluded by !**/*.sum
  • integration_tests/protocols/code/pre-condition.yaml is excluded by !**/*.yaml
  • integration_tests/protocols/code/py-env-var.yaml is excluded by !**/*.yaml
  • integration_tests/protocols/code/py-file.yaml is excluded by !**/*.yaml
  • integration_tests/protocols/code/py-interactsh.yaml is excluded by !**/*.yaml
  • integration_tests/protocols/code/py-snippet.yaml is excluded by !**/*.yaml
  • integration_tests/workflow/code-template-1.yaml is excluded by !**/*.yaml
  • integration_tests/workflow/code-template-2.yaml is excluded by !**/*.yaml
📒 Files selected for processing (3)
  • cmd/integration-test/exporters.go (1 hunks)
  • cmd/integration-test/integration-test.go (1 hunks)
  • go.mod (7 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • cmd/integration-test/integration-test.go
  • go.mod
🔇 Additional comments (3)
cmd/integration-test/exporters.go (3)

96-98: Good implementation of result validation

The explicit validation of the exported result against the expected result is well implemented. This ensures that the data is correctly stored and retrieved from MongoDB.


23-25: Test case registration looks good

The test case is properly registered with a descriptive path "exporters/mongo" which clearly indicates its purpose.


3-16: Well-organized imports

The imports are well-organized and include all necessary packages for the MongoDB integration test.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
cmd/integration-test/exporters.go (1)

44-49: 🛠️ Refactor suggestion

Add validation for the connection string format

The connection string should be validated before appending the database name to ensure it's properly formatted.

connString, err := mongodbContainer.ConnectionString(ctx)
if err != nil {
	return fmt.Errorf("failed to get connection string for MongoDB container: %s", err)
}
+// Ensure the connection string ends with a slash before appending database name
+if !strings.HasSuffix(connString, "/") {
+	connString += "/"
+}
connString = connString + dbName

Don't forget to add "strings" to the import statement.

🧹 Nitpick comments (2)
cmd/integration-test/exporters.go (2)

51-55: Consider extracting test values as constants

For better maintainability, consider extracting the collection name and batch size as constants.

const (
	dbName  = "test"
	dbImage = "mongo:8"
+	collectionName = "test"
+	batchSize = 1
)

// ...

opts := mongo.Options{
	ConnectionString: connString,
-	CollectionName:   "test",
-	BatchSize:        1, // Ensure we write the result immediately
+	CollectionName:   collectionName,
+	BatchSize:        batchSize, // Ensure we write the result immediately
}

29-31: Consider a shorter timeout for integration tests

The 10-minute timeout is quite long for this type of test. While it's good to have a generous timeout to prevent flaky tests, consider if a shorter duration (e.g., 2-5 minutes) would be sufficient.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 1a0b72c and cbc5378.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (3)
  • cmd/integration-test/exporters.go (1 hunks)
  • cmd/integration-test/integration-test.go (1 hunks)
  • go.mod (7 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • cmd/integration-test/integration-test.go
  • go.mod
🧰 Additional context used
🧬 Code Graph Analysis (1)
cmd/integration-test/exporters.go (3)
cmd/integration-test/integration-test.go (1)
  • TestCaseInfo (19-23)
pkg/testutils/integration.go (1)
  • TestCase (247-250)
pkg/output/output.go (1)
  • ResultEvent (146-221)
🔇 Additional comments (5)
cmd/integration-test/exporters.go (5)

23-25: The test case path is now properly descriptive.

Good job setting a descriptive path for the MongoDB exporter test. This follows the best practices discussed in previous reviews.


33-42: LGTM! Good error handling for container startup.

The container setup and error handling look good. The deferred cleanup function for terminating the container is properly implemented.


67-75: LGTM! Test data creation and export.

The test case creates appropriate test data and exports it to MongoDB with proper error handling.


96-98: Good validation of the exported document.

The verification logic correctly checks that both request and response fields match the expected values.


89-94: LGTM! Good MongoDB query implementation.

The query to verify the exported document is well implemented, using the appropriate MongoDB driver functions and error handling.

@loresuso
Copy link
Author

Mmm interesting that it is failing on windows and macos. Wondering if there is Docker installed in the runner by default
Maybe we can run the test only on Linux? I'll look if I can find some easier ways to solve this, testcontainers should be compatible

@loresuso
Copy link
Author

Added the installation of docker in macos and windows

      - uses: docker/setup-docker-action@v4
        if: ${{ matrix.os != 'ubuntu-latest' }}

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
cmd/integration-test/exporters.go (1)

44-49: Add validation for the connection string

Currently, there's no validation that the connection string is properly formatted before appending the database name. Add a check to ensure the connection string ends with a slash before appending the database name.

connString, err := mongodbContainer.ConnectionString(ctx)
if err != nil {
	return fmt.Errorf("failed to get connection string for MongoDB container: %s", err)
}
+// Ensure the connection string ends with a slash before appending database name
+if !strings.HasSuffix(connString, "/") {
+	connString += "/"
+}
connString = connString + dbName

Don't forget to add "strings" to the import statement.

🧹 Nitpick comments (5)
cmd/integration-test/exporters.go (5)

34-39: Consider adding container startup configuration options

The current container setup doesn't specify startup timeout or resource constraints. Consider adding configuration options for more robust test behavior.

// Start a MongoDB container
-mongodbContainer, err := mongocontainer.Run(ctx, dbImage)
+mongodbContainer, err := mongocontainer.RunWithOptions(ctx, mongocontainer.RunOptions{
+	ContainerRequest: testcontainers.ContainerRequest{
+		Image:        dbImage,
+		ExposedPorts: []string{"27017/tcp"},
+		WaitingFor:   wait.ForListeningPort("27017/tcp").WithStartupTimeout(30 * time.Second),
+	},
+})
defer func() {
	if err := testcontainers.TerminateContainer(mongodbContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}

You'll need to add "github.com/testcontainers/testcontainers-go/wait" to your imports.


67-75: Add test for batch export functionality

The current test only verifies exporting a single document. Since the MongoDB exporter supports batch exports (as indicated by the BatchSize option), consider adding a test for batch exports with multiple documents.

res := &output.ResultEvent{
	Request:  "test request",
	Response: "test response",
}

err = exporter.Export(res)
if err != nil {
	return fmt.Errorf("failed to export result event to MongoDB: %s", err)
}

+// Test batch export with multiple documents
+res2 := &output.ResultEvent{
+	Request:  "test request 2",
+	Response: "test response 2",
+}
+
+err = exporter.Export(res2)
+if err != nil {
+	return fmt.Errorf("failed to export second result event to MongoDB: %s", err)
+}

61-65: Improve error handling in defer function

Currently, errors in the defer function for closing the exporter are only printed. Consider propagating these errors or at least using the same error formatting pattern as other parts of the code.

defer func() {
	if err := exporter.Close(); err != nil {
-		fmt.Printf("failed to close exporter: %s\n", err)
+		log.Printf("failed to close exporter: %s", err)
	}
}()

83-87: Apply consistent error handling pattern

Use the same error logging pattern as other defer functions for consistency.

defer func() {
	if err := client.Disconnect(ctx); err != nil {
-		fmt.Printf("failed to disconnect from MongoDB: %s\n", err)
+		log.Printf("failed to disconnect from MongoDB: %s", err)
	}
}()

29-101: Consider adding negative test cases

The current test only covers the happy path. Consider adding test cases for error scenarios, such as:

  1. Invalid connection string
  2. Connection timeout
  3. Authentication failure
  4. MongoDB server shutdown during operation

This would provide more comprehensive test coverage for the MongoDB exporter functionality.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between cbc5378 and 2b2dff0.

⛔ Files ignored due to path filters (2)
  • .github/workflows/tests.yaml is excluded by !**/*.yaml
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (3)
  • cmd/integration-test/exporters.go (1 hunks)
  • cmd/integration-test/integration-test.go (1 hunks)
  • go.mod (7 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • cmd/integration-test/integration-test.go
  • go.mod
🔇 Additional comments (2)
cmd/integration-test/exporters.go (2)

1-25: LGTM! The integration test for MongoDB exporter is well set up.

The test case structure follows the project's conventions with proper Path naming and TestCase implementation.


96-98: Good validation of exported results

The explicit validation of the exported document is a good practice, ensuring that the data was correctly stored and retrieved from MongoDB.

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