From 5807a05cbb4f1e5146389151a45a4d426f231b16 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Fri, 17 Apr 2026 08:25:05 +0000 Subject: [PATCH] fix: address P0-2b review issues - build tag and actionType guard 1. Add //go:build !cgo to control_host_test.go so the stub-testing tests only run in non-CGO builds, matching the stub.go build constraint. Clean up dead nil check in SignatureCompiles test. 2. Replace two independent if-statements for actionType mapping in ControlHost with a map lookup + explicit guard, consistent with ControlQueue's ActionMap pattern. Prevents silent zero-value actionType if SupportedHostActions expands later. --- src/frontend/apiserver/backend/control_host_test.go | 7 +++---- src/frontend/apiserver/controllers/hosts.go | 13 ++++++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/frontend/apiserver/backend/control_host_test.go b/src/frontend/apiserver/backend/control_host_test.go index 5bc6e4db..814a8b4f 100644 --- a/src/frontend/apiserver/backend/control_host_test.go +++ b/src/frontend/apiserver/backend/control_host_test.go @@ -1,3 +1,5 @@ +//go:build !cgo + package backend import ( @@ -18,8 +20,5 @@ func TestControlHost_StubReturnsError(t *testing.T) { // signature matches: func ControlHost(hostname string, action int) error. // If the signature changes this test will not compile. func TestControlHost_SignatureCompiles(t *testing.T) { - var fn func(string, int) error = ControlHost - if fn == nil { - t.Fatal("ControlHost must not be nil") - } + var _ func(string, int) error = ControlHost } diff --git a/src/frontend/apiserver/controllers/hosts.go b/src/frontend/apiserver/controllers/hosts.go index 6bd98691..81729590 100644 --- a/src/frontend/apiserver/controllers/hosts.go +++ b/src/frontend/apiserver/controllers/hosts.go @@ -407,12 +407,15 @@ func (h *HostHandler) ControlHost(ctx *gin.Context) { ReturnError(ctx, common.ERROR_CODE, err.Error(), nil) return } - var actionType int - if baseAction == "close" { - actionType = models.HOST_CLOSE + hostActionMap := map[string]int{ + "open": models.HOST_OPEN, + "close": models.HOST_CLOSE, } - if baseAction == "open" { - actionType = models.HOST_OPEN + actionType, exists := hostActionMap[baseAction] + if !exists { + logger.Error("Unsupported action type", zap.String("baseAction", baseAction)) + ReturnError(ctx, common.INVALID_PARAMS_CODE, "Unsupported action type: "+baseAction, nil) + return } successHosts := []map[string]interface{}{}