From 8ed791e6e6f26b6e552f16a8e8a7908441a80e89 Mon Sep 17 00:00:00 2001 From: "Yasin, Siraj (SY495P)" Date: Mon, 20 Apr 2020 08:52:30 -0500 Subject: [PATCH] Fix Lint warnings: Use of basic type string as key warning: * should not use basic type string as key in context.WithValue Fix: * Define a separate type for context Key(numRetries) and use the same to pass value for it * Added a constant ctxKeyNumRetries to be used by entire package Relates-To: #148 Change-Id: Ifd464346ae7a8ecd82a1f1073a026af1e182fbef --- pkg/remote/redfish/client.go | 15 ++++++++++----- pkg/remote/redfish/client_test.go | 10 +++++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/pkg/remote/redfish/client.go b/pkg/remote/redfish/client.go index 65a45f718..514e735e7 100644 --- a/pkg/remote/redfish/client.go +++ b/pkg/remote/redfish/client.go @@ -26,11 +26,16 @@ import ( "opendev.org/airship/airshipctl/pkg/log" ) +// contextKey is used by the redfish package as a unique key type in order to prevent collisions +// with context keys in other packages. +type contextKey string + const ( // ClientType is used by other packages as the identifier of the Redfish client. - ClientType string = "redfish" - systemActionRetries = 30 - systemRebootDelay = 30 * time.Second + ClientType string = "redfish" + systemActionRetries = 30 + systemRebootDelay = 30 * time.Second + ctxKeyNumRetries contextKey = "numRetries" ) // Client holds details about a Redfish out-of-band system required for out-of-band management. @@ -49,7 +54,7 @@ func (c *Client) NodeID() string { func (c *Client) EjectVirtualMedia(ctx context.Context) error { waitForEjectMedia := func(managerID string, mediaID string) error { // Check if number of retries is defined in context - totalRetries, ok := ctx.Value("numRetries").(int) + totalRetries, ok := ctx.Value(ctxKeyNumRetries).(int) if !ok { totalRetries = systemActionRetries } @@ -108,7 +113,7 @@ func (c *Client) EjectVirtualMedia(ctx context.Context) error { func (c *Client) RebootSystem(ctx context.Context) error { waitForPowerState := func(desiredState redfishClient.PowerState) error { // Check if number of retries is defined in context - totalRetries, ok := ctx.Value("numRetries").(int) + totalRetries, ok := ctx.Value(ctxKeyNumRetries).(int) if !ok { totalRetries = systemActionRetries } diff --git a/pkg/remote/redfish/client_test.go b/pkg/remote/redfish/client_test.go index 96aa96e51..6110417ee 100644 --- a/pkg/remote/redfish/client_test.go +++ b/pkg/remote/redfish/client_test.go @@ -79,7 +79,7 @@ func TestEjectVirtualMedia(t *testing.T) { client.nodeID = nodeID // Normal retries are 30. Limit them here for test time. - ctx := context.WithValue(context.Background(), "numRetries", 2) + ctx := context.WithValue(context.Background(), ctxKeyNumRetries, 2) // Mark CD and DVD test media as inserted inserted := true @@ -131,7 +131,7 @@ func TestEjectVirtualMediaRetriesExceeded(t *testing.T) { client.nodeID = nodeID - ctx := context.WithValue(context.Background(), "numRetries", 1) + ctx := context.WithValue(context.Background(), ctxKeyNumRetries, 1) // Mark test media as inserted inserted := true @@ -260,7 +260,7 @@ func TestRebootSystemTimeout(t *testing.T) { client.nodeID = nodeID - ctx := context.WithValue(context.Background(), "numRetries", 1) + ctx := context.WithValue(context.Background(), ctxKeyNumRetries, 1) resetReq := redfishClient.ResetRequestBody{} resetReq.ResetType = redfishClient.RESETTYPE_FORCE_OFF @@ -363,7 +363,7 @@ func TestSetVirtualMediaEjectExistingMedia(t *testing.T) { client.nodeID = nodeID // Normal retries are 30. Limit them here for test time. - ctx := context.WithValue(context.Background(), "numRetries", 1) + ctx := context.WithValue(context.Background(), ctxKeyNumRetries, 1) // Mark test media as inserted inserted := true @@ -408,7 +408,7 @@ func TestSetVirtualMediaEjectExistingMediaFailure(t *testing.T) { client.nodeID = nodeID // Normal retries are 30. Limit them here for test time. - ctx := context.WithValue(context.Background(), "numRetries", 1) + ctx := context.WithValue(context.Background(), ctxKeyNumRetries, 1) // Mark test media as inserted inserted := true