Skip to content

Commit

Permalink
Fix issues reported by staticcheck (#6415)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjngx authored Sep 13, 2024
1 parent 06d5e44 commit 0432a95
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion internal/certmanager/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func TestSync(t *testing.T) {
t.Errorf("Not all expected reactors were called: %v", err)
}
if err := b.AllActionsExecuted(); err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/certmanager/test_files/context_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ func (b *Builder) CheckAndFinish(args ...interface{}) {
b.T.Errorf("Not all expected reactors were called: %v", err)
}
if err := b.AllActionsExecuted(); err != nil {
b.T.Errorf(err.Error())
b.T.Error(err.Error())
}
if err := b.AllEventsCalled(); err != nil {
b.T.Errorf(err.Error())
b.T.Error(err.Error())
}

// resync listers before running checks
Expand Down
20 changes: 10 additions & 10 deletions internal/configs/virtualserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,7 @@ func TestGenerateVirtualServerConfigWithBackupForNGINXPlus(t *testing.T) {

got, warnings := vsc.GenerateVirtualServerConfig(&virtualServerEx, nil, nil)
if !cmp.Equal(want, got) {
t.Errorf(cmp.Diff(want, got))
t.Error(cmp.Diff(want, got))
}
if len(warnings) != 0 {
t.Errorf("GenerateVirtualServerConfig returned warnings: %v", vsc.warnings)
Expand Down Expand Up @@ -1786,7 +1786,7 @@ func TestGenerateVirtualServerConfig_DoesNotGenerateBackupOnMissingBackupNameFor

got, warnings := vsc.GenerateVirtualServerConfig(&virtualServerEx, nil, nil)
if !cmp.Equal(want, got) {
t.Errorf(cmp.Diff(want, got))
t.Error(cmp.Diff(want, got))
}
if len(warnings) != 0 {
t.Errorf("GenerateVirtualServerConfig returned warnings: %v", vsc.warnings)
Expand Down Expand Up @@ -2092,7 +2092,7 @@ func TestGenerateVirtualServerConfig_DoesNotGenerateBackupOnMissingBackupPortFor

got, warnings := vsc.GenerateVirtualServerConfig(&virtualServerEx, nil, nil)
if !cmp.Equal(want, got) {
t.Errorf(cmp.Diff(want, got))
t.Error(cmp.Diff(want, got))
}
if len(warnings) != 0 {
t.Errorf("GenerateVirtualServerConfig returned warnings: %v", vsc.warnings)
Expand Down Expand Up @@ -2396,7 +2396,7 @@ func TestGenerateVirtualServerConfig_DoesNotGenerateBackupOnMissingBackupPortAnd

got, warnings := vsc.GenerateVirtualServerConfig(&virtualServerEx, nil, nil)
if !cmp.Equal(want, got) {
t.Errorf(cmp.Diff(want, got))
t.Error(cmp.Diff(want, got))
}
if len(warnings) != 0 {
t.Errorf("GenerateVirtualServerConfig returned warnings: %v", vsc.warnings)
Expand Down Expand Up @@ -6347,19 +6347,19 @@ func TestGenerateVirtualServerConfigAPIKeyClientMaps(t *testing.T) {
})

if !cmp.Equal(tc.expectedSpecAPIKey, vsConf.Server.APIKey) {
t.Errorf(cmp.Diff(tc.expectedSpecAPIKey, vsConf.Server.APIKey))
t.Error(cmp.Diff(tc.expectedSpecAPIKey, vsConf.Server.APIKey))
}

if !cmp.Equal(tc.expectedRoute1APIKey, vsConf.Server.Locations[0].APIKey) {
t.Errorf(cmp.Diff(tc.expectedRoute1APIKey, vsConf.Server.Locations[0].APIKey))
t.Error(cmp.Diff(tc.expectedRoute1APIKey, vsConf.Server.Locations[0].APIKey))
}

if !cmp.Equal(tc.expectedRoute2APIKey, vsConf.Server.Locations[1].APIKey) {
t.Errorf(cmp.Diff(tc.expectedRoute2APIKey, vsConf.Server.Locations[1].APIKey))
t.Error(cmp.Diff(tc.expectedRoute2APIKey, vsConf.Server.Locations[1].APIKey))
}

if !cmp.Equal(tc.expectedMapList, vsConf.Maps) {
t.Errorf(cmp.Diff(tc.expectedMapList, vsConf.Maps))
t.Error(cmp.Diff(tc.expectedMapList, vsConf.Maps))
}

if len(warnings) != 0 {
Expand Down Expand Up @@ -7161,7 +7161,7 @@ func TestGeneratePolicies(t *testing.T) {
result.BundleValidator = nil

if !cmp.Equal(tc.expected, result) {
t.Errorf(cmp.Diff(tc.expected, result))
t.Error(cmp.Diff(tc.expected, result))
}
if len(vsc.warnings) > 0 {
t.Errorf("generatePolicies() returned unexpected warnings %v for the case of %s", vsc.warnings, tc.msg)
Expand Down Expand Up @@ -15803,7 +15803,7 @@ func TestRFC1123ToSnake(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !cmp.Equal(rfc1123ToSnake(tt.input), tt.expected) {
t.Errorf(cmp.Diff(rfc1123ToSnake(tt.input), tt.expected))
t.Error(cmp.Diff(rfc1123ToSnake(tt.input), tt.expected))
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions internal/externaldns/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ func TestGetValidTargets(t *testing.T) {
t.Fatal(err)
}
if !cmp.Equal(tc.wantTargets, targets) {
t.Errorf(cmp.Diff(tc.wantTargets, targets))
t.Error(cmp.Diff(tc.wantTargets, targets))
}
if recordType != tc.wantRecord {
t.Errorf(cmp.Diff(tc.wantRecord, recordType))
t.Error(cmp.Diff(tc.wantRecord, recordType))
}
})
}
Expand Down
15 changes: 8 additions & 7 deletions internal/k8s/appprotect/app_protect_configuration.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package appprotect

import (
"errors"
"fmt"
"sort"
"time"
Expand Down Expand Up @@ -208,14 +209,14 @@ func createAppProtectPolicyEx(policyObj *unstructured.Unstructured) (*PolicyEx,
err := validation.ValidateAppProtectPolicy(policyObj)
if err != nil {
errMsg := fmt.Sprintf("Error validating policy %s: %v", policyObj.GetName(), err)
return &PolicyEx{Obj: policyObj, IsValid: false, ErrorMsg: failedValidationErrorMsg}, fmt.Errorf(errMsg)
return &PolicyEx{Obj: policyObj, IsValid: false, ErrorMsg: failedValidationErrorMsg}, errors.New(errMsg)
}
sigReqs := []SignatureReq{}
// Check if policy has signature requirement (revision timestamp) and map them to tags
list, found, err := unstructured.NestedSlice(policyObj.Object, "spec", "policy", "signature-requirements")
if err != nil {
errMsg := fmt.Sprintf("Error retrieving Signature requirements from %s: %v", policyObj.GetName(), err)
return &PolicyEx{Obj: policyObj, IsValid: false, ErrorMsg: failedValidationErrorMsg}, fmt.Errorf(errMsg)
return &PolicyEx{Obj: policyObj, IsValid: false, ErrorMsg: failedValidationErrorMsg}, errors.New(errMsg)
}
if found {
for _, req := range list {
Expand All @@ -224,7 +225,7 @@ func createAppProtectPolicyEx(policyObj *unstructured.Unstructured) (*PolicyEx,
timeReq, err := buildRevTimes(requirement)
if err != nil {
errMsg := fmt.Sprintf("Error creating time requirements from %s: %v", policyObj.GetName(), err)
return &PolicyEx{Obj: policyObj, IsValid: false, ErrorMsg: invalidTimestampErrorMsg}, fmt.Errorf(errMsg)
return &PolicyEx{Obj: policyObj, IsValid: false, ErrorMsg: invalidTimestampErrorMsg}, errors.New(errMsg)
}
sigReqs = append(sigReqs, SignatureReq{Tag: reqTag.(string), RevTimes: &timeReq})
}
Expand All @@ -243,15 +244,15 @@ func buildRevTimes(requirement map[string]interface{}) (RevTimes, error) {
minRevTime, err := time.Parse(timeLayout, minRev.(string))
if err != nil {
errMsg := fmt.Sprintf("Error Parsing time from minRevisionDatetime %v", err)
return timeReq, fmt.Errorf(errMsg)
return timeReq, errors.New(errMsg)
}
timeReq.MinRevTime = &minRevTime
}
if maxRev, ok := requirement["maxRevisionDatetime"]; ok {
maxRevTime, err := time.Parse(timeLayout, maxRev.(string))
if err != nil {
errMsg := fmt.Sprintf("Error Parsing time from maxRevisionDatetime %v", err)
return timeReq, fmt.Errorf(errMsg)
return timeReq, errors.New(errMsg)
}
timeReq.MaxRevTime = &maxRevTime
}
Expand All @@ -278,7 +279,7 @@ func createAppProtectUserSigEx(userSigObj *unstructured.Unstructured) (*UserSigE
err := validation.ValidateAppProtectUserSig(userSigObj)
if err != nil {
errMsg := failedValidationErrorMsg
return &UserSigEx{Obj: userSigObj, IsValid: false, Tag: sTag, ErrorMsg: errMsg}, fmt.Errorf(errMsg)
return &UserSigEx{Obj: userSigObj, IsValid: false, Tag: sTag, ErrorMsg: errMsg}, errors.New(errMsg)
}
// Previous validation ensures there will be no errors
tag, found, _ := unstructured.NestedString(userSigObj.Object, "spec", "tag")
Expand All @@ -290,7 +291,7 @@ func createAppProtectUserSigEx(userSigObj *unstructured.Unstructured) (*UserSigE
revTime, err := time.Parse(timeLayout, revTimeString)
if err != nil {
errMsg := invalidTimestampErrorMsg
return &UserSigEx{Obj: userSigObj, IsValid: false, ErrorMsg: errMsg}, fmt.Errorf(errMsg)
return &UserSigEx{Obj: userSigObj, IsValid: false, ErrorMsg: errMsg}, errors.New(errMsg)
}
return &UserSigEx{
Obj: userSigObj,
Expand Down
2 changes: 1 addition & 1 deletion internal/k8s/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ var escapedStringsFmtRegexp = regexp.MustCompile("^" + escapedStringsFmt + "$")
func ValidateEscapedString(body string, examples ...string) error {
if !escapedStringsFmtRegexp.MatchString(body) {
msg := validation.RegexError(escapedStringsErrMsg, escapedStringsFmt, examples...)
return fmt.Errorf(msg)
return errors.New(msg)
}
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/configuration/validation/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validation

import (
"errors"
"fmt"
"regexp"
"strings"
Expand All @@ -21,7 +22,7 @@ var escapedStringsFmtRegexp = regexp.MustCompile("^" + escapedStringsFmt + "$")
func ValidateEscapedString(body string, examples ...string) error {
if !escapedStringsFmtRegexp.MatchString(body) {
msg := validation.RegexError(escapedStringsErrMsg, escapedStringsFmt, examples...)
return fmt.Errorf(msg)
return errors.New(msg)
}
return nil
}
Expand Down

0 comments on commit 0432a95

Please sign in to comment.