Skip to content
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

chore(k6): Fix parsing error during k6 run in ci #1158

Merged

Conversation

oskogstad
Copy link
Collaborator

@oskogstad oskogstad commented Sep 18, 2024

  • Fixed issue where concurrency test output printed was in order wrong the
  • Fixed parsing issue for invalid URI test

Related issue: #1184

Summary by CodeRabbit

  • New Features

    • Enhanced validation for the process property in dialog queries.
    • Introduced a new function to handle batch HTTP POST requests for service operations.
    • Added a new test case for handling invalid process identifiers in dialog searches.
  • Bug Fixes

    • Updated test cases to improve validation of invalid process inputs, ensuring accurate error responses.
  • Documentation

    • Minor formatting adjustments for clarity in test scripts.

Copy link
Contributor

coderabbitai bot commented Sep 18, 2024

📝 Walkthrough

Walkthrough

The pull request includes modifications to the SearchDialogQueryValidator class to enhance the validation rules for the Process property, replacing the URI string check with a new method and adding a maximum length constraint. Additionally, a new function postBatchSO is introduced in the testing framework to handle batch HTTP POST requests. Various test files have been updated to reflect changes in process identifiers and to improve error handling, particularly in concurrency tests and dialog search tests.

Changes

File Change Summary
src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Queries/Search/SearchDialogQueryValidator.cs Updated validation for Process property to use IsValidUri() and added maximum length constraint. Added new using directive.
tests/k6/common/request.js Added postBatchSO(batch) function for processing batch HTTP POST requests. Minor formatting adjustments.
tests/k6/common/testimports.js Exported postBatchSO function from ./request.js.
tests/k6/suites/all-single-pass.js Added error handling in runAllTests() with a try-catch block to log errors.
tests/k6/tests/all-tests.js Added a newline at the end of the file in runAllTests function.
tests/k6/tests/enduser/dialogSearch.js Updated invalid process filter string in test suite for dialog searches.
tests/k6/tests/serviceowner/concurrency.js Updated concurrency test to use postBatchSO instead of postSOAsync.
tests/k6/tests/serviceowner/dialogCreateInvalidProcess.js Changed dialog.process value to test invalid URI handling.
tests/k6/tests/serviceowner/dialogSearch.js Added new test for invalid process identifier and updated existing test for consistency.

Possibly related PRs

  • chore: Updated validator messages #1133: This PR modifies the SearchDialogQueryValidator.cs file, specifically updating the validation rules for the Process property, which aligns directly with the changes made in the main PR regarding the validation of the Process property in the same file.

Suggested reviewers

  • elsand: Suggested reviewer for the changes made in this pull request.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

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.

@oskogstad oskogstad force-pushed the chore/attempt-at-fixing-parsing-error-during-k6-run-in-ci branch from 1aee441 to ab4096f Compare September 18, 2024 10:55
@oskogstad oskogstad changed the title [DRAFT] chore(k6): Fix parsing error during k6 run in ci chore(k6): Fix parsing error during k6 run in ci Oct 2, 2024
@oskogstad oskogstad marked this pull request as ready for review October 2, 2024 13:51
@oskogstad oskogstad requested a review from a team as a code owner October 2, 2024 13:51
Copy link

sonarqubecloud bot commented Oct 2, 2024

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: 4

🧹 Outside diff range comments (3)
tests/k6/tests/serviceowner/concurrency.js (1)

Refactor String Concatenations to Template Literals

Several instances of string concatenation were found in dialogSearch.js files:

  • tests/k6/tests/serviceowner/dialogSearch.js:

    • Lines involving 'dialogs/' + dialogId + '/activities?' + i
    • Multiple other concatenations in various getSO calls.
  • tests/k6/tests/enduser/dialogSearch.js:

    • Lines involving 'dialogs/' + defaultFilter + '&Search=' + titleToSearchFor
    • Multiple other concatenations in various getEU and console.log calls.

Refactoring these concatenations to use template literals will enhance readability and maintainability.

🔗 Analysis chain

Line range hint 1-35: Summary: Effective implementation with minor suggestions

The changes in this file successfully address the PR objectives by:

  1. Updating the import to use postBatchSO for batch operations.
  2. Implementing a batch approach for concurrent requests, which should resolve the output order issues.

The new implementation looks good, with only minor suggestions for improvement (using template literals and const declarations).

To ensure consistency across the codebase:

This will help identify if similar changes are needed in other test files for consistency.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if similar changes are needed in other test files

# Search for files that might need similar updates
echo "Files potentially needing similar updates:"
rg --type js 'postSOAsync' tests/k6/tests

# Check for other occurrences of string concatenation in test files
echo "\nOther occurrences of string concatenation that could be replaced with template literals:"
rg --type js '\+\s*\w+\s*\+' tests/k6/tests

Length of output: 4788

🧰 Tools
🪛 Biome

[error] 23-23: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


[error] 29-29: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


[error] 19-19: This let declares a variable that is only assigned once.

'batch' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)


[error] 22-22: This let declares a variable that is only assigned once.

'activity' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)

tests/k6/tests/serviceowner/dialogSearch.js (2)

Line range hint 179-185: LGTM! Consider adding a more specific error check.

The new test case for handling an invalid process parameter is well-structured and correctly tests for a 400 status code. Great job on improving the test coverage!

To make the test even more robust, consider checking for a specific error message or error code in the response. This would ensure that the correct error is being thrown for the invalid process. For example:

expect(r.json().errors[0]).to.have.property('code').that.equals('InvalidProcessIdentifier');

Replace 'InvalidProcessIdentifier' with the actual error code you expect from the API.

🧰 Tools
🪛 Biome

[error] 181-181: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


Line range hint 187-192: LGTM! Consider using a template literal for the URL.

The update to use the processToSeachFor variable in the test case is a good improvement for consistency. Well done!

To address the static analysis hint and improve code style, consider using a template literal for the URL string:

let r = getSO(`dialogs/?CreatedAfter=${createdAfter}&process=${processToSeachFor}`);

This change will make the code more readable and consistent with modern JavaScript practices.

🧰 Tools
🪛 Biome

[error] 181-181: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between f28e291 and 92a5164.

📒 Files selected for processing (9)
  • src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Queries/Search/SearchDialogQueryValidator.cs (2 hunks)
  • tests/k6/common/request.js (2 hunks)
  • tests/k6/common/testimports.js (1 hunks)
  • tests/k6/suites/all-single-pass.js (0 hunks)
  • tests/k6/tests/all-tests.js (1 hunks)
  • tests/k6/tests/enduser/dialogSearch.js (1 hunks)
  • tests/k6/tests/serviceowner/concurrency.js (2 hunks)
  • tests/k6/tests/serviceowner/dialogCreateInvalidProcess.js (1 hunks)
  • tests/k6/tests/serviceowner/dialogSearch.js (1 hunks)
💤 Files with no reviewable changes (1)
  • tests/k6/suites/all-single-pass.js
🧰 Additional context used
🪛 Biome
tests/k6/tests/serviceowner/concurrency.js

[error] 23-23: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


[error] 19-19: This let declares a variable that is only assigned once.

'batch' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)


[error] 22-22: This let declares a variable that is only assigned once.

'activity' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)

tests/k6/tests/serviceowner/dialogSearch.js

[error] 181-181: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)

🔇 Additional comments (9)
tests/k6/tests/all-tests.js (1)

7-7: LGTM: Newline added at end of file.

The addition of a newline at the end of the file is a good practice. It ensures compatibility with various text processing tools and can prevent issues in some environments.

However, I noticed that the PR objectives mention fixing parsing errors and output order issues, which are not directly visible in this file. These fixes might be implemented in other files not shown in this review, or they could be related to how this file is processed or executed in the CI environment.

To verify if there are any other relevant changes, let's check for modifications in other k6-related files:

✅ Verification successful

LGTM: PR successfully addresses parsing errors and output order issues across multiple k6 test files.

All required fixes related to parsing and output have been implemented consistently throughout the affected test suites and common modules. The addition of postBatchSO enhances batch processing, and the corrected process strings resolve the previously identified parsing errors.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other modified k6-related files

# Test: Look for modified k6 test files
git diff --name-only origin/main | grep -E 'tests/k6/.*\.js$'

# Test: Check for changes in CI configuration related to k6
git diff --name-only origin/main | grep -E '\.github/workflows/.*k6.*\.yml$'

Length of output: 447


Script:

#!/bin/bash
# Description: Retrieve diffs for all modified k6-related JavaScript files to verify fixes

# List of modified k6 test files from the previous output
modified_files=(
  "tests/k6/common/request.js"
  "tests/k6/common/testimports.js"
  "tests/k6/suites/all-single-pass.js"
  "tests/k6/tests/all-tests.js"
  "tests/k6/tests/enduser/dialogSearch.js"
  "tests/k6/tests/serviceowner/concurrency.js"
  "tests/k6/tests/serviceowner/dialogCreateInvalidProcess.js"
  "tests/k6/tests/serviceowner/dialogSearch.js"
)

# Iterate over each modified file and display its diff
for file in "${modified_files[@]}"; do
  echo "Diff for ${file}:"
  git diff origin/main -- "${file}"
  echo -e "\n"
done

Length of output: 8028

tests/k6/common/testimports.js (1)

11-11: LGTM! Verify the implementation of postBatchSO.

The addition of postBatchSO to the exports looks good and aligns with the existing pattern of exporting service operation functions. This change may be related to addressing the parsing issues mentioned in the PR objectives.

To ensure the correctness of this change, let's verify the implementation of postBatchSO in the './request.js' file:

✅ Verification successful

Verified: postBatchSO is correctly implemented and exported in tests/k6/common/request.js.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of postBatchSO in request.js

# Test: Search for the postBatchSO function definition
rg --type js -A 10 'function postBatchSO' tests/k6/common/request.js

# Test: Check if postBatchSO is properly exported
rg --type js 'export.*postBatchSO' tests/k6/common/request.js

Length of output: 644

tests/k6/tests/serviceowner/concurrency.js (2)

1-1: LGTM: Import statement updated to support batch operations

The import statement has been updated to include postBatchSO and remove postSOAsync. This change aligns with the PR objective of fixing parsing errors during k6 run in CI and supports the new approach for handling concurrent requests.


Line range hint 28-35: LGTM: Cleanup and assertions are correct

The cleanup process and assertions for the batch results are well-implemented and consistent with the new batch operation approach. This ensures that:

  1. The test doesn't leave residual data in the system.
  2. All concurrent operations are verified for success.

No changes are needed in this section.

🧰 Tools
🪛 Biome

[error] 23-23: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


[error] 29-29: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


[error] 19-19: This let declares a variable that is only assigned once.

'batch' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)


[error] 22-22: This let declares a variable that is only assigned once.

'activity' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)

src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Queries/Search/SearchDialogQueryValidator.cs (1)

5-5: LGTM: New using directive added.

The addition of using Digdir.Domain.Dialogporten.Domain.Common; is appropriate and likely related to the changes in the Process property validation.

tests/k6/common/request.js (2)

19-19: LGTM: Minor formatting improvement

The removal of the empty line improves code readability by eliminating unnecessary whitespace.


Line range hint 1-73: Summary: Changes align with PR objectives and improve code quality

The changes in this file contribute positively to the PR's objectives of fixing parsing errors during k6 runs in CI:

  1. The new postBatchSO function allows for efficient batch processing of requests, which could help address the issue of output from concurrency tests being printed in the incorrect order.
  2. By using consistent request parameter handling and error checking, this function may also contribute to better handling of invalid URI tests.

The suggested improvements to postBatchSO would further enhance its robustness and flexibility, potentially preventing future parsing issues.

Overall, these changes appear to be a step in the right direction for improving the reliability of k6 tests in the CI environment.

🧰 Tools
🪛 Biome

[error] 75-75: Reassigning a function parameter is confusing.

The parameter is declared here:

Use a local variable instead.

(lint/style/noParameterAssign)


[error] 76-76: Reassigning a function parameter is confusing.

The parameter is declared here:

Use a local variable instead.

(lint/style/noParameterAssign)

tests/k6/tests/serviceowner/dialogSearch.js (1)

Line range hint 1-203: Great improvements to the dialog search tests!

The changes in this file effectively address the PR objectives:

  1. The new test case for an invalid process (lines 179-185) directly tackles the parsing issue for invalid URI tests mentioned in the PR description.
  2. The update to the existing test case (lines 187-192) improves consistency in test data usage.

These changes contribute to enhancing the reliability and functionality of the k6 testing framework within the project's CI pipeline. Good job on improving both the test coverage and the overall quality of the test suite!

🧰 Tools
🪛 Biome

[error] 181-181: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)

tests/k6/tests/enduser/dialogSearch.js (1)

200-200: Approve: Improved invalid process filter test

This change addresses the parsing issue mentioned in the PR objectives while maintaining the effectiveness of the test. The new invalid process filter 'inval|d' is a good choice because:

  1. It doesn't contain spaces or special characters that might cause parsing problems in the CI environment.
  2. It still represents an invalid process identifier, ensuring the test's purpose is preserved.
  3. The test expectations remain unchanged, correctly asserting a 400 status code and checking for error messages.

This modification should resolve the parsing error during k6 runs in CI without compromising the test's integrity.

@@ -6,7 +6,7 @@ export default function (){

describe ('Attempt to create dialog with invalid URI', () => {
let dialog = dialogToInsert();
dialog.process = '?? ?';
dialog.process = 'inval|d';
Copy link
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

LGTM! Consider enhancing test coverage.

The change from '?? ?' to 'inval|d' for dialog.process is appropriate and aligns with the PR objective of addressing parsing issues for invalid URI tests. This should effectively trigger the expected 400 error.

To further improve the test case:

  1. Consider adding assertions to check for specific error messages or codes related to the invalid URI.
  2. It might be beneficial to include additional test cases with different types of invalid URIs to ensure comprehensive coverage.

Would you like assistance in implementing these suggestions?

Comment on lines +19 to +26
let batch = [];

for (let i=0; i<10; i++) {
let activity = { type: "Information", performedBy: { actorType: "serviceOwner" }, description: [ { value: i.toString(), languageCode: "nb"}]};
promises.push(postSOAsync('dialogs/' + dialogId + '/activities?' + i, activity))
batch.push(['dialogs/' + dialogId + '/activities?' + i, activity]);
}

const results = await Promise.all(promises);
const results = postBatchSO(batch);
Copy link
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

LGTM: Improved concurrency test implementation with minor suggestions

The changes effectively address the PR objective of fixing the output order from concurrency tests by using a batch operation instead of individual promises. This simplifies the concurrency handling and should resolve the parsing issues.

However, there are a few minor improvements we can make:

  1. On line 23, consider using a template literal instead of string concatenation:
batch.push([`dialogs/${dialogId}/activities?${i}`, activity]);
  1. On lines 19 and 22, you can use const instead of let as these variables are not reassigned:
const batch = [];
// ...
for (let i=0; i<10; i++) {
    const activity = { /* ... */ };
    // ...
}

These changes will improve code readability and follow best practices.

🧰 Tools
🪛 Biome

[error] 23-23: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


[error] 19-19: This let declares a variable that is only assigned once.

'batch' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)


[error] 22-22: This let declares a variable that is only assigned once.

'activity' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)

Comment on lines +57 to 59
.IsValidUri()
.MaximumLength(Constants.DefaultMaxUriLength)
.When(x => x.Process is not null);
Copy link
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

LGTM: Improved Process property validation.

The changes to the Process property validation are good improvements:

  1. Using IsValidUri() likely provides a more comprehensive check for well-formed URIs.
  2. Adding a maximum length constraint with Constants.DefaultMaxUriLength is a good practice to prevent issues with extremely long URIs.

These changes align well with the PR objective of addressing the parsing issue related to invalid URI tests.

Consider adding a comment explaining the purpose of Constants.DefaultMaxUriLength for better code documentation. For example:

.MaximumLength(Constants.DefaultMaxUriLength) // Limit URI length to prevent potential issues with extremely long URIs

Comment on lines +65 to +72
export function postBatchSO(batch) {
const processedBatch = batch.map(([url, body, params]) => {
params = extend(true, {}, params, { headers: { 'Content-Type': 'application/json' }});
return ['POST', baseUrlServiceOwner + url, JSON.stringify(body), getServiceOwnerRequestParams(params)];
});

return http.batch(processedBatch);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Good implementation, with suggestions for improvement

The postBatchSO function is a valuable addition for efficient batch processing of requests. It effectively reuses existing utility functions and maintains consistency with the codebase. However, consider the following improvements:

  1. Add input validation for the batch parameter to ensure it's an array and each item has the expected structure.
  2. Consider supporting different HTTP methods, not just POST, to increase flexibility.
  3. For consistency with other functions, add a tokenOptions parameter and pass it to getServiceOwnerRequestParams.

Here's a suggested implementation incorporating these improvements:

export function batchRequestsSO(batch, tokenOptions = null) {
    if (!Array.isArray(batch)) {
        throw new Error('batch must be an array');
    }

    const processedBatch = batch.map(([method, url, body, params]) => {
        if (!['GET', 'POST', 'PUT', 'PATCH', 'DELETE'].includes(method)) {
            throw new Error(`Unsupported HTTP method: ${method}`);
        }

        params = extend(true, {}, params, { headers: { 'Content-Type': 'application/json' }});
        return [
            method,
            baseUrlServiceOwner + url,
            body ? JSON.stringify(body) : null,
            getServiceOwnerRequestParams(params, tokenOptions)
        ];
    });

    return http.batch(processedBatch);
}

This implementation adds input validation, supports multiple HTTP methods, and includes the tokenOptions parameter.

@oskogstad oskogstad merged commit f2e68e8 into main Oct 2, 2024
23 of 24 checks passed
@oskogstad oskogstad deleted the chore/attempt-at-fixing-parsing-error-during-k6-run-in-ci branch October 2, 2024 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants