Skip to content

Commit

Permalink
fix: enable usestdlibvars linter
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
  • Loading branch information
mmorel-35 committed Nov 3, 2024
1 parent 98e53e5 commit a98e44c
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 43 deletions.
10 changes: 5 additions & 5 deletions client/internal/v2/auth_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ func (r *httpAuthRoleAPI) AddRole(ctx context.Context, rolename string) error {
Role: rolename,
}
return r.addRemoveRole(ctx, &authRoleAPIAction{
verb: "PUT",
verb: http.MethodPut,
name: rolename,
role: role,
})
}

func (r *httpAuthRoleAPI) RemoveRole(ctx context.Context, rolename string) error {
return r.addRemoveRole(ctx, &authRoleAPIAction{
verb: "DELETE",
verb: http.MethodDelete,
name: rolename,
})
}
Expand All @@ -166,7 +166,7 @@ func (r *httpAuthRoleAPI) addRemoveRole(ctx context.Context, req *authRoleAPIAct

func (r *httpAuthRoleAPI) GetRole(ctx context.Context, rolename string) (*Role, error) {
return r.modRole(ctx, &authRoleAPIAction{
verb: "GET",
verb: http.MethodGet,
name: rolename,
})
}
Expand Down Expand Up @@ -194,7 +194,7 @@ func (r *httpAuthRoleAPI) GrantRoleKV(ctx context.Context, rolename string, pref
},
}
return r.modRole(ctx, &authRoleAPIAction{
verb: "PUT",
verb: http.MethodPut,
name: rolename,
role: role,
})
Expand All @@ -209,7 +209,7 @@ func (r *httpAuthRoleAPI) RevokeRoleKV(ctx context.Context, rolename string, pre
},
}
return r.modRole(ctx, &authRoleAPIAction{
verb: "PUT",
verb: http.MethodPut,
name: rolename,
role: role,
})
Expand Down
16 changes: 8 additions & 8 deletions client/internal/v2/auth_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ type httpAuthAPI struct {
}

func (s *httpAuthAPI) Enable(ctx context.Context) error {
return s.enableDisable(ctx, &authAPIAction{"PUT"})
return s.enableDisable(ctx, &authAPIAction{http.MethodPut})
}

func (s *httpAuthAPI) Disable(ctx context.Context) error {
return s.enableDisable(ctx, &authAPIAction{"DELETE"})
return s.enableDisable(ctx, &authAPIAction{http.MethodDelete})
}

func (s *httpAuthAPI) enableDisable(ctx context.Context, req httpAction) error {
Expand Down Expand Up @@ -217,15 +217,15 @@ func (u *httpAuthUserAPI) AddUser(ctx context.Context, username string, password
Password: password,
}
return u.addRemoveUser(ctx, &authUserAPIAction{
verb: "PUT",
verb: http.MethodPut,
username: username,
user: user,
})
}

func (u *httpAuthUserAPI) RemoveUser(ctx context.Context, username string) error {
return u.addRemoveUser(ctx, &authUserAPIAction{
verb: "DELETE",
verb: http.MethodDelete,
username: username,
})
}
Expand All @@ -248,7 +248,7 @@ func (u *httpAuthUserAPI) addRemoveUser(ctx context.Context, req *authUserAPIAct

func (u *httpAuthUserAPI) GetUser(ctx context.Context, username string) (*User, error) {
return u.modUser(ctx, &authUserAPIAction{
verb: "GET",
verb: http.MethodGet,
username: username,
})
}
Expand All @@ -259,7 +259,7 @@ func (u *httpAuthUserAPI) GrantUser(ctx context.Context, username string, roles
Grant: roles,
}
return u.modUser(ctx, &authUserAPIAction{
verb: "PUT",
verb: http.MethodPut,
username: username,
user: user,
})
Expand All @@ -271,7 +271,7 @@ func (u *httpAuthUserAPI) RevokeUser(ctx context.Context, username string, roles
Revoke: roles,
}
return u.modUser(ctx, &authUserAPIAction{
verb: "PUT",
verb: http.MethodPut,
username: username,
user: user,
})
Expand All @@ -283,7 +283,7 @@ func (u *httpAuthUserAPI) ChangePassword(ctx context.Context, username string, p
Password: password,
}
return u.modUser(ctx, &authUserAPIAction{
verb: "PUT",
verb: http.MethodPut,
username: username,
user: user,
})
Expand Down
4 changes: 2 additions & 2 deletions client/internal/v2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ func TestRedirectedHTTPAction(t *testing.T) {
act := &redirectedHTTPAction{
action: &staticHTTPAction{
request: http.Request{
Method: "DELETE",
Method: http.MethodDelete,
URL: &url.URL{
Scheme: "https",
Host: "foo.example.com",
Expand All @@ -549,7 +549,7 @@ func TestRedirectedHTTPAction(t *testing.T) {
}

want := &http.Request{
Method: "DELETE",
Method: http.MethodDelete,
URL: &url.URL{
Scheme: "https",
Host: "bar.example.com",
Expand Down
10 changes: 5 additions & 5 deletions client/internal/v2/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestGetAction(t *testing.T) {
wantURL := baseWantURL
wantURL.RawQuery = tt.wantQuery

err := assertRequest(got, "GET", wantURL, wantHeader, nil)
err := assertRequest(got, http.MethodGet, wantURL, wantHeader, nil)
if err != nil {
t.Errorf("#%d: %v", i, err)
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func TestWaitAction(t *testing.T) {
wantURL := baseWantURL
wantURL.RawQuery = tt.wantQuery

err := assertRequest(got, "GET", wantURL, wantHeader, nil)
err := assertRequest(got, http.MethodGet, wantURL, wantHeader, nil)
if err != nil {
t.Errorf("#%d: unexpected error: %#v", i, err)
}
Expand Down Expand Up @@ -424,7 +424,7 @@ func TestSetAction(t *testing.T) {
}

got := tt.act.HTTPRequest(url.URL{Scheme: "http", Host: "example.com"})
if err := assertRequest(*got, "PUT", u, wantHeader, []byte(tt.wantBody)); err != nil {
if err := assertRequest(*got, http.MethodPut, u, wantHeader, []byte(tt.wantBody)); err != nil {
t.Errorf("#%d: %v", i, err)
}
}
Expand Down Expand Up @@ -525,7 +525,7 @@ func TestCreateInOrderAction(t *testing.T) {
}

got := tt.act.HTTPRequest(url.URL{Scheme: "http", Host: "example.com"})
if err := assertRequest(*got, "POST", u, wantHeader, []byte(tt.wantBody)); err != nil {
if err := assertRequest(*got, http.MethodPost, u, wantHeader, []byte(tt.wantBody)); err != nil {
t.Errorf("#%d: %v", i, err)
}
}
Expand Down Expand Up @@ -627,7 +627,7 @@ func TestDeleteAction(t *testing.T) {
}

got := tt.act.HTTPRequest(url.URL{Scheme: "http", Host: "example.com"})
if err := assertRequest(*got, "DELETE", u, wantHeader, nil); err != nil {
if err := assertRequest(*got, http.MethodDelete, u, wantHeader, nil); err != nil {
t.Errorf("#%d: %v", i, err)
}
}
Expand Down
10 changes: 5 additions & 5 deletions client/internal/v2/members_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestMembersAPIActionList(t *testing.T) {
}

got := *act.HTTPRequest(ep)
err := assertRequest(got, "GET", wantURL, http.Header{}, nil)
err := assertRequest(got, http.MethodGet, wantURL, http.Header{}, nil)
if err != nil {
t.Error(err.Error())
}
Expand All @@ -65,7 +65,7 @@ func TestMembersAPIActionAdd(t *testing.T) {
wantBody := []byte(`{"peerURLs":["https://127.0.0.1:8081","http://127.0.0.1:8080"]}`)

got := *act.HTTPRequest(ep)
err := assertRequest(got, "POST", wantURL, wantHeader, wantBody)
err := assertRequest(got, http.MethodPost, wantURL, wantHeader, wantBody)
if err != nil {
t.Error(err.Error())
}
Expand All @@ -92,7 +92,7 @@ func TestMembersAPIActionUpdate(t *testing.T) {
wantBody := []byte(`{"peerURLs":["https://127.0.0.1:8081","http://127.0.0.1:8080"]}`)

got := *act.HTTPRequest(ep)
err := assertRequest(got, "PUT", wantURL, wantHeader, wantBody)
err := assertRequest(got, http.MethodPut, wantURL, wantHeader, wantBody)
if err != nil {
t.Error(err.Error())
}
Expand All @@ -109,7 +109,7 @@ func TestMembersAPIActionRemove(t *testing.T) {
}

got := *act.HTTPRequest(ep)
err := assertRequest(got, "DELETE", wantURL, http.Header{}, nil)
err := assertRequest(got, http.MethodDelete, wantURL, http.Header{}, nil)
if err != nil {
t.Error(err.Error())
}
Expand All @@ -126,7 +126,7 @@ func TestMembersAPIActionLeader(t *testing.T) {
}

got := *act.HTTPRequest(ep)
err := assertRequest(got, "GET", wantURL, http.Header{}, nil)
err := assertRequest(got, http.MethodGet, wantURL, http.Header{}, nil)
if err != nil {
t.Error(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion client/pkg/transport/timeout_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestNewTimeoutTransport(t *testing.T) {
}

// ensure not reuse timeout connection
req, err := http.NewRequest("GET", srv.URL, nil)
req, err := http.NewRequest(http.MethodGet, srv.URL, nil)
require.NoError(t, err)
resp, err := tr.RoundTrip(req)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions server/embed/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ func (ac *accessController) ServeHTTP(rw http.ResponseWriter, req *http.Request)
addCORSHeader(rw, origin)
}

if req.Method == "OPTIONS" {
if req.Method == http.MethodOptions {
rw.WriteHeader(http.StatusOK)
return
}
Expand Down Expand Up @@ -486,7 +486,7 @@ func (ch *corsHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
addCORSHeader(rw, origin)
}

if req.Method == "OPTIONS" {
if req.Method == http.MethodOptions {
rw.WriteHeader(http.StatusOK)
return
}
Expand Down
4 changes: 2 additions & 2 deletions server/etcdserver/api/etcdhttp/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func TestServeMembersGet(t *testing.T) {
}

for i, tt := range tests {
req, err := http.NewRequest("GET", testutil.MustNewURL(t, tt.path).String(), nil)
req, err := http.NewRequest(http.MethodGet, testutil.MustNewURL(t, tt.path).String(), nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -261,7 +261,7 @@ func TestNewPeerHandlerOnMembersPromotePrefix(t *testing.T) {
},
}
for i, tt := range tests {
req, err := http.NewRequest("POST", srv.URL+tt.path, nil)
req, err := http.NewRequest(http.MethodPost, srv.URL+tt.path, nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions server/etcdserver/api/rafthttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func newPipelineHandler(t *Transport, r Raft, cid types.ID) http.Handler {
}

func (h *pipelineHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
if r.Method != http.MethodPost {
w.Header().Set("Allow", "POST")
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
Expand Down Expand Up @@ -200,7 +200,7 @@ const unknownSnapshotSender = "UNKNOWN_SNAPSHOT_SENDER"
func (h *snapshotHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
start := time.Now()

if r.Method != "POST" {
if r.Method != http.MethodPost {
w.Header().Set("Allow", "POST")
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
snapshotReceiveFailures.WithLabelValues(unknownSnapshotSender).Inc()
Expand Down Expand Up @@ -348,7 +348,7 @@ func newStreamHandler(t *Transport, pg peerGetter, r Raft, id, cid types.ID) htt
}

func (h *streamHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
if r.Method != http.MethodGet {
w.Header().Set("Allow", "GET")
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
Expand Down
3 changes: 2 additions & 1 deletion server/etcdserver/apply_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package etcdserver

import (
"encoding/json"
"net/http"
"path"

"github.com/coreos/go-semver/semver"
Expand All @@ -26,7 +27,7 @@ import (
)

func (s *EtcdServer) applyV2Request(r *RequestV2, shouldApplyV3 membership.ShouldApplyV3) {
if r.Method != "PUT" || (!storeMemberAttributeRegexp.MatchString(r.Path) && r.Path != membership.StoreClusterVersionKey()) {
if r.Method != http.MethodPut || (!storeMemberAttributeRegexp.MatchString(r.Path) && r.Path != membership.StoreClusterVersionKey()) {
s.lg.Panic("detected disallowed v2 WAL for stage --v2-deprecation=write-only", zap.String("method", r.Method))
}
if storeMemberAttributeRegexp.MatchString(r.Path) {
Expand Down
2 changes: 1 addition & 1 deletion server/lease/leasehttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type leaseHandler struct {
}

func (h *leaseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
if r.Method != http.MethodPost {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/http_health_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func TestHTTPLivezReadyzHandler(t *testing.T) {

func doHealthCheckAndVerify(t *testing.T, client *http.Client, url string, expectTimeoutError bool, expectStatusCode int, expectRespSubStrings []string) {
ctx, cancel := context.WithTimeout(context.Background(), healthCheckTimeout)
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
require.NoErrorf(t, err, "failed to creat request %+v", err)
resp, herr := client.Do(req)
cancel()
Expand Down
4 changes: 2 additions & 2 deletions tests/framework/e2e/etcd_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func (f *BinaryFailpoints) SetupHTTP(ctx context.Context, failpoint, payload str
Host: host,
Path: failpoint,
}
r, err := http.NewRequestWithContext(ctx, "PUT", failpointURL.String(), bytes.NewBuffer([]byte(payload)))
r, err := http.NewRequestWithContext(ctx, http.MethodPut, failpointURL.String(), bytes.NewBuffer([]byte(payload)))
if err != nil {
return err
}
Expand Down Expand Up @@ -395,7 +395,7 @@ func (f *BinaryFailpoints) DeactivateHTTP(ctx context.Context, failpoint string)
Host: host,
Path: failpoint,
}
r, err := http.NewRequestWithContext(ctx, "DELETE", failpointURL.String(), nil)
r, err := http.NewRequestWithContext(ctx, http.MethodDelete, failpointURL.String(), nil)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions tools/.golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ linters:
- unconvert # Remove unnecessary type conversions
- unparam
- unused
- usestdlibvars
- whitespace
linters-settings: # please keep this alphabetized
goimports:
Expand Down
6 changes: 3 additions & 3 deletions tools/etcd-dump-logs/etcd-dump-log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ func appendNormalRequestEnts(ents *[]raftpb.Entry) {

requests := []etcdserverpb.Request{
{ID: 0, Method: "", Path: "/path0", Val: "{\"hey\":\"ho\",\"hi\":[\"yo\"]}", Dir: true, PrevValue: "", PrevIndex: 0, PrevExist: &b, Expiration: 9, Wait: false, Since: 1, Recursive: false, Sorted: false, Quorum: false, Time: 1, Stream: false, Refresh: &b},
{ID: 1, Method: "QGET", Path: "/path1", Val: "{\"0\":\"1\",\"2\":[\"3\"]}", Dir: false, PrevValue: "", PrevIndex: 0, PrevExist: &b, Expiration: 9, Wait: false, Since: 1, Recursive: false, Sorted: false, Quorum: false, Time: 1, Stream: false, Refresh: &b},
{ID: 2, Method: "SYNC", Path: "/path2", Val: "{\"0\":\"1\",\"2\":[\"3\"]}", Dir: false, PrevValue: "", PrevIndex: 0, PrevExist: &b, Expiration: 2, Wait: false, Since: 1, Recursive: false, Sorted: false, Quorum: false, Time: 1, Stream: false, Refresh: &b},
{ID: 3, Method: "DELETE", Path: "/path3", Val: "{\"hey\":\"ho\",\"hi\":[\"yo\"]}", Dir: false, PrevValue: "", PrevIndex: 0, PrevExist: &a, Expiration: 2, Wait: false, Since: 1, Recursive: false, Sorted: false, Quorum: false, Time: 1, Stream: false, Refresh: &b},
{ID: 1, Method: MethodQGet, Path: "/path1", Val: "{\"0\":\"1\",\"2\":[\"3\"]}", Dir: false, PrevValue: "", PrevIndex: 0, PrevExist: &b, Expiration: 9, Wait: false, Since: 1, Recursive: false, Sorted: false, Quorum: false, Time: 1, Stream: false, Refresh: &b},
{ID: 2, Method: MethodSync, Path: "/path2", Val: "{\"0\":\"1\",\"2\":[\"3\"]}", Dir: false, PrevValue: "", PrevIndex: 0, PrevExist: &b, Expiration: 2, Wait: false, Since: 1, Recursive: false, Sorted: false, Quorum: false, Time: 1, Stream: false, Refresh: &b},
{ID: 3, Method: MethodDelete, Path: "/path3", Val: "{\"hey\":\"ho\",\"hi\":[\"yo\"]}", Dir: false, PrevValue: "", PrevIndex: 0, PrevExist: &a, Expiration: 2, Wait: false, Since: 1, Recursive: false, Sorted: false, Quorum: false, Time: 1, Stream: false, Refresh: &b},
{ID: 4, Method: "RANDOM", Path: "/path4/superlong" + strings.Repeat("/path", 30), Val: "{\"hey\":\"ho\",\"hi\":[\"yo\"]}", Dir: false, PrevValue: "", PrevIndex: 0, PrevExist: &b, Expiration: 2, Wait: false, Since: 1, Recursive: false, Sorted: false, Quorum: false, Time: 1, Stream: false, Refresh: &b},
}

Expand Down
7 changes: 5 additions & 2 deletions tools/etcd-dump-logs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ import (

const (
defaultEntryTypes string = "Normal,ConfigChange"
MethodSync string = "SYNC"
MethodQGet string = "QGET"
MethodDelete string = "DELETE"
)

func main() {
Expand Down Expand Up @@ -292,9 +295,9 @@ func printRequest(entry raftpb.Entry) {
switch r.Method {
case "":
fmt.Print("\tnoop")
case "SYNC":
case MethodSync:
fmt.Printf("\tmethod=SYNC time=%q", time.Unix(0, r.Time).UTC())
case "QGET", "DELETE":
case MethodQGet, MethodDelete:
fmt.Printf("\tmethod=%s path=%s", r.Method, excerpt(r.Path, 64, 64))
default:
fmt.Printf("\tmethod=%s path=%s val=%s", r.Method, excerpt(r.Path, 64, 64), excerpt(r.Val, 128, 0))
Expand Down

0 comments on commit a98e44c

Please sign in to comment.