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
This commit is contained in:
Yasin, Siraj (SY495P) 2020-04-20 08:52:30 -05:00
parent 009794e408
commit 8ed791e6e6
2 changed files with 15 additions and 10 deletions

View File

@ -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
}

View File

@ -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