Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pkg/api/presenters/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func TestConvertCluster_WithLabels(t *testing.T) {
Expect(err).To(BeNil())

var resultLabels map[string]string
json.Unmarshal(result.Labels, &resultLabels)
err = json.Unmarshal(result.Labels, &resultLabels)
Expect(err).To(BeNil())
Expect(resultLabels["env"]).To(Equal("production"))
Expect(resultLabels["team"]).To(Equal("platform"))
}
Expand All @@ -106,7 +107,8 @@ func TestConvertCluster_WithoutLabels(t *testing.T) {
Expect(err).To(BeNil())

var resultLabels map[string]string
json.Unmarshal(result.Labels, &resultLabels)
err = json.Unmarshal(result.Labels, &resultLabels)
Expect(err).To(BeNil())
Expect(len(resultLabels)).To(Equal(0)) // Empty map
}

Expand Down Expand Up @@ -136,7 +138,8 @@ func TestConvertCluster_SpecMarshaling(t *testing.T) {
Expect(err).To(BeNil())

var resultSpec map[string]interface{}
json.Unmarshal(result.Spec, &resultSpec)
err = json.Unmarshal(result.Spec, &resultSpec)
Expect(err).To(BeNil())
Expect(resultSpec["provider"]).To(Equal("gcp"))
Expect(resultSpec["region"]).To(Equal("us-east1"))

Expand Down
6 changes: 4 additions & 2 deletions pkg/api/presenters/node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ func TestConvertNodePool_WithLabels(t *testing.T) {
Expect(err).To(BeNil())

var resultLabels map[string]string
json.Unmarshal(result.Labels, &resultLabels)
err = json.Unmarshal(result.Labels, &resultLabels)
Expect(err).To(BeNil())
Expect(resultLabels["environment"]).To(Equal("production"))
Expect(resultLabels["team"]).To(Equal("platform"))
Expect(resultLabels["region"]).To(Equal("us-east"))
Expand All @@ -144,7 +145,8 @@ func TestConvertNodePool_WithoutLabels(t *testing.T) {
Expect(err).To(BeNil())

var resultLabels map[string]string
json.Unmarshal(result.Labels, &resultLabels)
err = json.Unmarshal(result.Labels, &resultLabels)
Expect(err).To(BeNil())
Expect(len(resultLabels)).To(Equal(0)) // Empty map
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/middleware/schema_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func handleValidationError(w http.ResponseWriter, r *http.Request, err *errors.S
// Write JSON error response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(err.HttpCode)
json.NewEncoder(w).Encode(err.AsOpenapiError(operationID))
_ = json.NewEncoder(w).Encode(err.AsOpenapiError(operationID))
}

// SchemaValidationMiddleware validates cluster and nodepool spec fields against OpenAPI schemas
Expand All @@ -44,7 +44,7 @@ func SchemaValidationMiddleware(validator *validators.SchemaValidator) func(http
handleValidationError(w, r, serviceErr)
return
}
r.Body.Close()
_ = r.Body.Close()

// Restore the request body for the next handler
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
Expand Down
10 changes: 4 additions & 6 deletions test/integration/clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func TestClusterSchemaValidation(t *testing.T) {

jwtToken := ctx.Value(openapi.ContextAccessToken)

resp2, err := resty.R().
resp2, _ := resty.R().
SetHeader("Content-Type", "application/json").
SetHeader("Authorization", fmt.Sprintf("Bearer %s", jwtToken)).
SetBody(invalidTypeJSON).
Expand All @@ -330,7 +330,7 @@ func TestClusterSchemaValidation(t *testing.T) {
t.Logf("Schema validation correctly rejected invalid spec type")
// Verify error response contains details
var errorResponse openapi.Error
json.Unmarshal(resp2.Body(), &errorResponse)
_ = json.Unmarshal(resp2.Body(), &errorResponse)
Expect(errorResponse.Code).ToNot(BeNil())
Expect(errorResponse.Reason).ToNot(BeNil())
} else {
Expand Down Expand Up @@ -389,7 +389,7 @@ func TestClusterSchemaValidationWithProviderSchema(t *testing.T) {
_, resp, err := client.DefaultAPI.PostCluster(ctx).ClusterCreateRequest(invalidInput).Execute()
Expect(err).To(HaveOccurred(), "Should reject spec with missing required field")
Expect(resp.StatusCode).To(Equal(http.StatusBadRequest))
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()

// Parse error response to verify field-level details
bodyBytes, err := io.ReadAll(resp.Body)
Expand Down Expand Up @@ -539,17 +539,15 @@ func TestClusterList_OrderByName(t *testing.T) {
fmt.Sprintf("%s-bravo", testPrefix),
}

var createdClusters []openapi.Cluster
for _, name := range names {
clusterInput := openapi.ClusterCreateRequest{
Kind: "Cluster",
Name: name,
Spec: map[string]interface{}{"test": "value"},
}

cluster, _, err := client.DefaultAPI.PostCluster(ctx).ClusterCreateRequest(clusterInput).Execute()
_, _, err := client.DefaultAPI.PostCluster(ctx).ClusterCreateRequest(clusterInput).Execute()
Expect(err).NotTo(HaveOccurred(), "Failed to create cluster %s", name)
createdClusters = append(createdClusters, *cluster)
}

// List with orderBy=name asc
Expand Down
2 changes: 1 addition & 1 deletion test/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestMain(m *testing.M) {
if _, err := os.Stat(schemaPath); err != nil {
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.

glog.Infof("Set OPENAPI_SCHEMA_PATH=%s for integration tests", schemaPath)
}
}
Expand Down