Skip to content

Conversation

@yingzhanredhat
Copy link
Contributor

@yingzhanredhat yingzhanredhat commented Dec 8, 2025

HYPERFLEET-295:
Fix the lint error that happens in presubmit job
https://qe-private-deck-ci.apps.ci.l2s4.p1.openshiftapps.com/view/gs/qe-private-deck/pr-logs/pull/openshift_release/72205/rehearse-72205-pull-ci-openshift-hyperfleet-hyperfleet-api-main-lint/1997915431523848192

Summary by CodeRabbit

  • Tests

    • Enhanced error handling validation in test suites to ensure marshalling operations succeed before executing assertions.
  • Chores

    • Refined internal error handling in middleware and integration tests for improved robustness.

✏️ Tip: You can customize this high-level summary in your review settings.

@openshift-ci openshift-ci bot requested review from crizzo71 and jsell-rh December 8, 2025 06:50
@openshift-ci openshift-ci bot added the approved label Dec 8, 2025
@coderabbitai
Copy link

coderabbitai bot commented Dec 8, 2025

Walkthrough

This PR adds and suppresses error handling across test and middleware files. The changes include: adding explicit error checks for json.Unmarshal operations in presenter tests, suppressing errors from JSON encoding and request body closing in middleware error handling, ignoring error returns from HTTP requests and environment variable operations in integration tests, and wrapping resource cleanup in deferred functions. No functional behavior changes are introduced; the modifications refine error handling practices and test robustness.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Repetitive pattern: Most changes follow a consistent theme of adding error checks or suppressing known errors across multiple files
  • Low logic density: Changes are straightforward error handling additions with no complex conditional logic
  • Potential attention areas:
    • pkg/middleware/schema_validation.go: Verify that deliberately suppressing encoder and Close errors is intentional and won't mask important failures
    • test/integration/clusters_test.go: Confirm that ignoring cluster creation return values and not storing results doesn't affect test coverage or assertions

Possibly related PRs

Suggested labels

lgtm

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Fix the lint failures' is vague and generic, describing what the PR does without specifying which lint failures or what underlying issues are being addressed. Consider a more specific title that indicates the nature of the lint failures, such as 'Add error handling for unmarshalling operations' or 'Suppress error handling in middleware and tests'.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 90.91% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
test/integration/clusters_test.go (1)

323-328: Potential nil pointer panic: check error before using resp2.

Suppressing the error from resty.R().Post() is dangerous. If the request fails (network error, timeout, etc.), resp2 could be nil, and accessing resp2.StatusCode() on line 329 will panic.

Check the error before using resp2:

-	resp2, _ := resty.R().
+	resp2, err := resty.R().
 		SetHeader("Content-Type", "application/json").
 		SetHeader("Authorization", fmt.Sprintf("Bearer %s", jwtToken)).
 		SetBody(invalidTypeJSON).
 		Post(h.RestURL("/clusters"))
+	
+	if err != nil {
+		t.Fatalf("HTTP request failed: %v", err)
+	}
 
 	if resp2.StatusCode() == http.StatusBadRequest {
🧹 Nitpick comments (1)
pkg/middleware/schema_validation.go (1)

26-26: Error suppression acceptable here, but consider logging.

Suppressing the JSON encoding error in an error handler is reasonable since there's limited recovery options. However, if encoding fails, the client receives an incomplete or malformed response.

Consider logging the encoding error for observability:

-	_ = json.NewEncoder(w).Encode(err.AsOpenapiError(operationID))
+	if encodeErr := json.NewEncoder(w).Encode(err.AsOpenapiError(operationID)); encodeErr != nil {
+		log.Errorf("Failed to encode validation error response: %v", encodeErr)
+	}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2c61fae and 4a80550.

📒 Files selected for processing (5)
  • pkg/api/presenters/cluster_test.go (3 hunks)
  • pkg/api/presenters/node_pool_test.go (2 hunks)
  • pkg/middleware/schema_validation.go (2 hunks)
  • test/integration/clusters_test.go (4 hunks)
  • test/integration/integration_test.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
**/*.go

📄 CodeRabbit inference engine (CLAUDE.md)

Use Go 1.24.9 with FIPS-compliant crypto enabled (CGO_ENABLED=1, GOEXPERIMENT=boringcrypto)

Files:

  • pkg/middleware/schema_validation.go
  • pkg/api/presenters/cluster_test.go
  • pkg/api/presenters/node_pool_test.go
  • test/integration/integration_test.go
  • test/integration/clusters_test.go
pkg/**/*.go

📄 CodeRabbit inference engine (CLAUDE.md)

Implement JWT authentication middleware that validates tokens and extracts account ID and username from claims

Files:

  • pkg/middleware/schema_validation.go
  • pkg/api/presenters/cluster_test.go
  • pkg/api/presenters/node_pool_test.go
pkg/api/**/*.go

📄 CodeRabbit inference engine (CLAUDE.md)

Use //go:embed directive to embed the OpenAPI specification at compile time from pkg/api/openapi/api/openapi.yaml

Files:

  • pkg/api/presenters/cluster_test.go
  • pkg/api/presenters/node_pool_test.go
test/integration/**/*.go

📄 CodeRabbit inference engine (CLAUDE.md)

test/integration/**/*.go: Create integration tests using Testcontainers for isolated PostgreSQL instances per test
All API endpoints must be integrated tested in test/integration/ covering CRUD operations, pagination, search, and error cases

Files:

  • test/integration/integration_test.go
  • test/integration/clusters_test.go
🧠 Learnings (7)
📓 Common learnings
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-03T15:47:39.989Z
Learning: Applies to test/integration/**/*.go : All API endpoints must be integrated tested in test/integration/ covering CRUD operations, pagination, search, and error cases
📚 Learning: 2025-12-03T15:47:39.989Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-03T15:47:39.989Z
Learning: Applies to pkg/handlers/**/*.go : Implement all HTTP handlers following the pattern: parse request → call service/DAO → handle errors → send response

Applied to files:

  • pkg/middleware/schema_validation.go
📚 Learning: 2025-12-03T15:47:39.989Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-03T15:47:39.989Z
Learning: Applies to pkg/api/**/*.go : Use //go:embed directive to embed the OpenAPI specification at compile time from pkg/api/openapi/api/openapi.yaml

Applied to files:

  • test/integration/integration_test.go
📚 Learning: 2025-12-03T15:47:39.989Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-03T15:47:39.989Z
Learning: Applies to cmd/hyperfleet/environments/**/*.go : Use OCM_ENV environment variable to select configuration environment (development, unit_testing, integration_testing, production)

Applied to files:

  • test/integration/integration_test.go
📚 Learning: 2025-12-03T15:47:39.989Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-03T15:47:39.989Z
Learning: Applies to test/integration/**/*.go : All API endpoints must be integrated tested in test/integration/ covering CRUD operations, pagination, search, and error cases

Applied to files:

  • test/integration/integration_test.go
  • test/integration/clusters_test.go
📚 Learning: 2025-12-03T15:47:39.989Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-03T15:47:39.989Z
Learning: Applies to pkg/db/migrations/**/*.go : Store cluster and nodepool specifications as JSONB in PostgreSQL schema, never as separate columns

Applied to files:

  • test/integration/clusters_test.go
📚 Learning: 2025-12-03T15:47:39.989Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-03T15:47:39.989Z
Learning: Applies to pkg/**/*.go : Implement JWT authentication middleware that validates tokens and extracts account ID and username from claims

Applied to files:

  • test/integration/clusters_test.go
🔇 Additional comments (9)
pkg/middleware/schema_validation.go (1)

47-47: Body.Close() error suppression is acceptable.

The request body has already been successfully read and will be replaced with a NopCloser. Suppressing the Close() error here is a standard pattern.

pkg/api/presenters/cluster_test.go (3)

89-90: Good improvement: explicit error handling added.

The test now properly checks that unmarshalling succeeds before asserting on the result values. This prevents misleading test failures if the JSON format is incorrect.


110-111: Good improvement: explicit error handling added.

The test now properly checks that unmarshalling succeeds before asserting on the result values.


141-142: Good improvement: explicit error handling added.

The test now properly checks that unmarshalling succeeds before asserting on the result values.

pkg/api/presenters/node_pool_test.go (2)

127-128: Good improvement: explicit error handling added.

The test now properly checks that unmarshalling succeeds before asserting on the result values. This aligns with the improvements made in cluster_test.go.


148-149: Good improvement: explicit error handling added.

The test now properly checks that unmarshalling succeeds before asserting on the result values.

test/integration/clusters_test.go (3)

333-333: Error suppression acceptable in test context.

Suppressing the unmarshal error here is acceptable since this is within a conditional block that already validated the status code. If unmarshalling fails, the test will fail on the subsequent assertions.


392-392: Standard defer cleanup pattern.

Wrapping Body.Close() in a deferred function with error suppression is a standard cleanup pattern for HTTP responses in tests.


549-549: Ignoring return values is acceptable here.

The test doesn't need the returned cluster objects, but properly checks for errors. This is appropriate for this test scenario.

glog.Warningf("Schema file not found at %s: %v, skipping OPENAPI_SCHEMA_PATH setup", schemaPath, err)
} else {
os.Setenv("OPENAPI_SCHEMA_PATH", schemaPath)
_ = os.Setenv("OPENAPI_SCHEMA_PATH", schemaPath)
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Suppressing os.Setenv error could hide test setup issues.

If os.Setenv fails (e.g., due to OS limits on environment variable size or permissions), the tests will proceed with incorrect configuration, potentially causing confusing failures.

Consider logging the error or using a fatal check:

-				_ = os.Setenv("OPENAPI_SCHEMA_PATH", schemaPath)
+				if err := os.Setenv("OPENAPI_SCHEMA_PATH", schemaPath); err != nil {
+					glog.Warningf("Failed to set OPENAPI_SCHEMA_PATH: %v", err)
+				} else {
+					glog.Infof("Set OPENAPI_SCHEMA_PATH=%s for integration tests", schemaPath)
+				}
-				glog.Infof("Set OPENAPI_SCHEMA_PATH=%s for integration tests", schemaPath)
📝 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.

Suggested change
_ = os.Setenv("OPENAPI_SCHEMA_PATH", schemaPath)
if err := os.Setenv("OPENAPI_SCHEMA_PATH", schemaPath); err != nil {
glog.Warningf("Failed to set OPENAPI_SCHEMA_PATH: %v", err)
} else {
glog.Infof("Set OPENAPI_SCHEMA_PATH=%s for integration tests", schemaPath)
}
🤖 Prompt for AI Agents
In test/integration/integration_test.go around line 41, the call to os.Setenv
ignores the returned error which can hide test setup failures; update the code
to check the error and fail the test on error (e.g., if inside a test function
call t.Fatalf with a clear message including the error, or if not available use
log.Fatalf/panic) so any environment variable setup failure stops the test
immediately and surfaces the underlying cause.

@yasun1
Copy link
Contributor

yasun1 commented Dec 8, 2025

/lgtm

@openshift-ci
Copy link

openshift-ci bot commented Dec 8, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: yasun1, yingzhanredhat

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:
  • OWNERS [yasun1,yingzhanredhat]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-merge-bot openshift-merge-bot bot merged commit b8897da into openshift-hyperfleet:main Dec 8, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants