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

Add dataset upload test (without using frontend) #8184

Merged
merged 10 commits into from
Nov 13, 2024

Conversation

frcroth
Copy link
Member

@frcroth frcroth commented Nov 11, 2024

Steps to test:

  • CI

Issues:

Summary by CodeRabbit

  • New Features

    • Introduced a new test case for dataset upload functionality, enhancing the testing suite.
    • Improved the import structure for handling HTTP requests with FormData and File.
  • Bug Fixes

    • Enhanced URL validation logic in the fetchWrapper function to ensure correct protocol handling.
  • Refactor

    • Modified logic in the ensureTestDataset method to delete and recreate the data directory, improving test setup reliability.

@frcroth frcroth requested a review from fm3 November 11, 2024 13:45
@frcroth frcroth self-assigned this Nov 11, 2024
Copy link
Contributor

coderabbitai bot commented Nov 11, 2024

Walkthrough

The pull request introduces enhancements to the end-to-end testing framework for dataset uploads. It adds a new test case in datasets.e2e.ts to validate the dataset upload process, including reserving uploads, reading files, and performing multipart uploads. Modifications in e2e-setup.ts improve import structures and URL handling. Additionally, the End2EndSpec.scala file is updated to manage the test dataset directory more effectively by deleting it if it exists before recreating it.

Changes

Files Change Summary
frontend/javascripts/test/backend-snapshot-tests/datasets.e2e.ts Added a new test case for dataset upload functionality, including file reading and multipart upload.
frontend/javascripts/test/e2e-setup.ts Updated imports for FormData and File, refined URL handling in fetchWrapper, extended global scope.
test/e2e/End2EndSpec.scala Modified ensureTestDataset to delete existing dataDirectory before recreating it; updated import statement.

Assessment against linked issues

Objective Addressed Explanation
e2e tests for dataset uploads (#8099)

Possibly related PRs

  • Zarr streaming e2e test #8137: The changes in this PR involve end-to-end tests for the Zarr streaming feature, which may relate to the dataset upload functionality being tested in the main PR, as both involve handling datasets and ensuring their integrity during upload and retrieval processes.
  • Always Upload Screenshot Artifacts for Nightly Tests #8171: This PR enhances the debugging process for nightly tests, including improvements to BrowserStack integration, which could be relevant to the testing environment used for the dataset upload tests in the main PR, ensuring that the tests are properly tracked and logged.

Suggested labels

testing, CI

Suggested reviewers

  • fm3
  • daniel-wer

Poem

🐰 In the meadow where data flows,
A test for uploads now brightly glows.
With files and forms, we dance and play,
Ensuring our datasets find their way.
So hop along, let’s code and cheer,
For every upload brings us near! 🎉


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.

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.

@frcroth frcroth mentioned this pull request Nov 11, 2024
@frcroth frcroth linked an issue Nov 11, 2024 that may be closed by this pull request
Copy link
Member

@fm3 fm3 left a comment

Choose a reason for hiding this comment

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

LGTM, but I’m not very familiar with typescript or the existing e2e tests. @daniel-wer could you maybe also have a quick look?

@fm3 fm3 requested a review from daniel-wer November 11, 2024 13:50
Base automatically changed from test-zarr-streaming to master November 11, 2024 13:56
@frcroth frcroth force-pushed the test-dataset-upload-clean-history branch from 43fff69 to 84eed1e Compare November 11, 2024 13:59
Copy link
Member

@daniel-wer daniel-wer left a comment

Choose a reason for hiding this comment

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

Sorry for the delayed review. Great addition to our tests, thank you! 🙏

Why did you choose to implement this without any frontend api code (for example, the reserveDatasetUpload function from the admin_rest_api)? I can imagine the resumable-js lib to be hard to work with in the tests, but was that the reason?
If it was by choice, do you think it would be worthwhile and possible (with reasonable effort) to add a similar test in a followup issue, testing the frontend functionality as well?

@frcroth
Copy link
Member Author

frcroth commented Nov 13, 2024

@daniel-wer I have tried to test the frontend. However it is just not easy to do. The resumable JS library uses Browser APIs e.g. File and XMLHttpRequest that are not available in the node test environment. I tried to mock them e.g. using JSDom, but it failed at sending the Multipart Formdata correctly (compare jsdom/jsdom#2300). So maybe it would be possible to do it but that way was tried and failed.
I also looked into pupetter, but that did not seem easy either and would probably slow down the test suite significantly.

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

🧹 Outside diff range and nitpick comments (6)
test/e2e/End2EndSpec.scala (1)

54-58: Consider enhancing error handling and logging

While the directory cleanup logic is a good addition for ensuring test isolation, consider these improvements:

  1. Add error handling for directory deletion
  2. Use proper logging instead of println
  3. Consider adding file locks or synchronization for parallel test execution

Here's a suggested improvement:

    if (dataDirectory.exists()) {
-     println("Deleting existing data directory Organization_X")
-     PathUtils.deleteDirectoryRecursively(dataDirectory.toPath)
+     logger.info(s"Deleting existing data directory: ${dataDirectory.getPath}")
+     try {
+       PathUtils.deleteDirectoryRecursively(dataDirectory.toPath)
+     } catch {
+       case e: Exception =>
+         logger.error(s"Failed to delete directory: ${e.getMessage}")
+         throw new Exception(s"Failed to prepare test environment: ${e.getMessage}", e)
+     }
    }
frontend/javascripts/test/e2e-setup.ts (1)

70-70: Consider using URL parsing for more robust protocol checking

While checking for both protocols is good, using indexOf for URL validation can be fragile. Consider using the URL API for more robust protocol checking.

-  if (url.indexOf("http:") === -1 && url.indexOf("https:") === -1) {
+  try {
+    const urlObj = new URL(url.toString());
+    if (!['http:', 'https:'].includes(urlObj.protocol)) {
+      newUrl = `http://localhost:9000${url}`;
+    }
+  } catch {
+    newUrl = `http://localhost:9000${url}`;
+  }
frontend/javascripts/test/backend-snapshot-tests/datasets.e2e.ts (4)

123-123: Avoid hardcoding folderId; retrieve it dynamically

Hardcoding the folderId may lead to test failures if the ID changes or differs between environments. Consider retrieving the folderId dynamically or using a constant defined in the test setup.


151-151: Generate a dynamic boundary instead of hardcoding it

If manual construction of the multipart body is necessary, avoid hardcoding the boundary value. Generate it dynamically to prevent potential conflicts and comply with HTTP specifications.

Example:

- const boundary = "----WebKitFormBoundaryAqTsFa4N9FW7zF7I";
+ const boundary = "----WebKitFormBoundary" + Math.random().toString(36).substring(2);

181-206: Differentiate error messages for clearer debugging

The error messages in t.fail() are identical at lines 182, 197, and 205. This can make debugging more difficult. Consider making each message unique to reflect the specific failure point.

Apply this diff to update the error messages:

  if (uploadResult.status !== 200) {
-   t.fail("Dataset upload failed");
+   t.fail("Dataset upload failed at initial POST");
  }

  if (finishResult.status !== 200) {
-   t.fail("Dataset upload failed at finish");
+   t.fail("Dataset upload failed at finishUpload");
  }

  if (result.status !== 200) {
-   t.fail("Dataset upload failed");
+   t.fail("Dataset health check after upload failed");
  }

127-127: Avoid hardcoding organization names; use variables or config

Hardcoding "Organization_X" may lead to issues if the organization name changes. Consider using a variable or retrieving the organization name dynamically to make the test more maintainable.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a8c0969 and 03ece1c.

📒 Files selected for processing (3)
  • frontend/javascripts/test/backend-snapshot-tests/datasets.e2e.ts (2 hunks)
  • frontend/javascripts/test/e2e-setup.ts (3 hunks)
  • test/e2e/End2EndSpec.scala (2 hunks)
🔇 Additional comments (5)
test/e2e/End2EndSpec.scala (2)

3-3: LGTM: Import statement properly updated

The addition of PathUtils to the import statement is well-organized and properly grouped with related IO utilities.


54-58: Well-implemented test environment preparation

The addition of directory cleanup before test dataset setup is a good practice that:

  1. Ensures test isolation
  2. Prevents interference from previous test runs
  3. Makes the tests more reliable and repeatable

This change aligns well with the PR's objective of implementing robust dataset upload tests.

frontend/javascripts/test/e2e-setup.ts (2)

6-6: LGTM: Import additions support dataset upload testing

The additional imports of FormData and File from node-fetch are appropriate for implementing dataset upload tests without frontend dependencies.


87-88: LGTM: Global assignments follow established pattern

The addition of FormData and File to the global scope is consistent with the file's pattern of making fetch-related APIs globally available for testing.

frontend/javascripts/test/backend-snapshot-tests/datasets.e2e.ts (1)

133-134: Ensure the test dataset file exists before reading

Attempting to read a file that doesn't exist will cause the test to fail. Verify that the file test/dataset/test-dataset.zip exists in the specified path, or include a check to handle the absence of the file gracefully.

Run the following script to check if the test dataset file exists:

✅ Verification successful

Test dataset file exists at the specified path

The file test/dataset/test-dataset.zip exists in the repository, so there's no need for additional file existence checks in the test code.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the test dataset file exists at the expected path.

# Test: Check for the existence of 'test-dataset.zip'. Expect: File exists.
if [ -f "test/dataset/test-dataset.zip" ]; then
  echo "File exists."
else
  echo "File does not exist."
fi

Length of output: 77


const boundary = "----WebKitFormBoundaryAqTsFa4N9FW7zF7I";
let bodyString = `--${boundary}\r\n`;
// @ts-ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Resolve the TypeScript error instead of using // @ts-ignore

Using // @ts-ignore suppresses TypeScript errors, potentially hiding real issues. Instead, address the type mismatch by casting formData appropriately or adjusting the code to satisfy the compiler.

Apply this change to resolve the typing issue:

- // @ts-ignore
  for (const [key, value] of formData.entries()) {
+ for (const [key, value] of (formData as any).entries()) {

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +151 to +170
const boundary = "----WebKitFormBoundaryAqTsFa4N9FW7zF7I";
let bodyString = `--${boundary}\r\n`;
// @ts-ignore
for (const [key, value] of formData.entries()) {
bodyString += `Content-Disposition: form-data; name="${key}"\r\n\r\n${value}\r\n`;
bodyString += `--${boundary}\r\n`;
}
bodyString += `Content-Disposition: form-data; name="file"; filename="test-dataset.zip"\r\n`;
bodyString += "Content-Type: application/octet-stream\r\n\r\n";

// We have to send the file as bytes, otherwise JS does some encoding, resulting in erroneous bytes

const formBytes = new TextEncoder().encode(bodyString);
const fileBytes = new Uint8Array(testDataset);
const endBytes = new TextEncoder().encode(`\r\n--${boundary}--`);
const body = new Uint8Array(formBytes.length + fileBytes.length + endBytes.length);
body.set(formBytes, 0);
body.set(fileBytes, formBytes.length);
body.set(endBytes, formBytes.length + fileBytes.length);

Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Simplify multipart/form-data handling by using built-in FormData methods

Manually constructing the multipart/form-data body and hardcoding the boundary can lead to errors and maintenance challenges. Use the built-in FormData object directly with the fetch API to handle multipart/form-data encoding automatically.

Apply this diff to simplify the upload request:

- // Manual construction of the multipart/form-data body
- const boundary = "----WebKitFormBoundaryAqTsFa4N9FW7zF7I";
- let bodyString = `--${boundary}\r\n`;
- // ...additional code to construct `bodyString`...
- const formBytes = new TextEncoder().encode(bodyString);
- const fileBytes = new Uint8Array(testDataset);
- const endBytes = new TextEncoder().encode(`\r\n--${boundary}--`);
- const body = new Uint8Array(formBytes.length + fileBytes.length + endBytes.length);
- body.set(formBytes, 0);
- body.set(fileBytes, formBytes.length);
- body.set(endBytes, formBytes.length + fileBytes.length);
- let content_type = `multipart/form-data; boundary=${boundary}`;

+ formData.append("file", new Blob([testDataset], { type: "application/octet-stream" }), "test-dataset.zip");
+
+ const uploadResult = await fetch("/data/datasets", {
+   method: "POST",
+   body: formData,
+ });

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Member

@daniel-wer daniel-wer left a comment

Choose a reason for hiding this comment

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

Thanks for elaborating, that's understandable and good to have documented as part of this PR 👍

@frcroth frcroth merged commit 3a18852 into master Nov 13, 2024
3 checks passed
@frcroth frcroth deleted the test-dataset-upload-clean-history branch November 13, 2024 12:27
@coderabbitai coderabbitai bot mentioned this pull request Dec 18, 2024
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.

e2e tests for dataset uploads
3 participants