Skip to content

Commit

Permalink
Merge pull request #17582 from ivanvc/address-client-var-naming-lint-…
Browse files Browse the repository at this point in the history
…rule

client: address golangci var-naming issues
  • Loading branch information
ahrtr authored Mar 15, 2024
2 parents 8292553 + 578b784 commit d639abe
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 107 deletions.
12 changes: 8 additions & 4 deletions client/pkg/logutil/log_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@ package logutil
import "fmt"

const (
JsonLogFormat = "json"
JSONLogFormat = "json"
ConsoleLogFormat = "console"
//revive:disable:var-naming
// Deprecated: Please use JSONLogFormat.
JsonLogFormat = JSONLogFormat
//revive:enable:var-naming
)

var DefaultLogFormat = JsonLogFormat
var DefaultLogFormat = JSONLogFormat

// ConvertToZapFormat converts and validated log format string.
func ConvertToZapFormat(format string) (string, error) {
switch format {
case ConsoleLogFormat:
return ConsoleLogFormat, nil
case JsonLogFormat:
return JsonLogFormat, nil
case JSONLogFormat:
return JSONLogFormat, nil
case "":
return DefaultLogFormat, nil
default:
Expand Down
4 changes: 2 additions & 2 deletions client/pkg/logutil/log_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func TestLogFormat(t *testing.T) {
want string
errExpected bool
}{
{"json", JsonLogFormat, false},
{"json", JSONLogFormat, false},
{"console", ConsoleLogFormat, false},
{"", JsonLogFormat, false},
{"", JSONLogFormat, false},
{"konsole", "", true},
}

Expand Down
6 changes: 3 additions & 3 deletions client/pkg/tlsutil/cipher_suites_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func TestGetCipherSuite_not_existing(t *testing.T) {
}
}

func CipherSuiteExpectedToExist(tb testing.TB, cipher string, expectedId uint16) {
func CipherSuiteExpectedToExist(tb testing.TB, cipher string, expectedID uint16) {
vid, ok := GetCipherSuite(cipher)
if !ok {
tb.Errorf("Expected %v cipher to exist", cipher)
}
if vid != expectedId {
tb.Errorf("For %v expected=%v found=%v", cipher, expectedId, vid)
if vid != expectedID {
tb.Errorf("For %v expected=%v found=%v", cipher, expectedID, vid)
}
}

Expand Down
26 changes: 13 additions & 13 deletions client/pkg/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,54 @@ import (
"strings"
)

const ENV_VERIFY = "ETCD_VERIFY"
const envVerify = "ETCD_VERIFY"

type VerificationType string

const (
ENV_VERIFY_VALUE_ALL VerificationType = "all"
ENV_VERIFY_VALUE_ASSERT VerificationType = "assert"
envVerifyValueAll VerificationType = "all"
envVerifyValueAssert VerificationType = "assert"
)

func getEnvVerify() string {
return strings.ToLower(os.Getenv(ENV_VERIFY))
return strings.ToLower(os.Getenv(envVerify))
}

func IsVerificationEnabled(verification VerificationType) bool {
env := getEnvVerify()
return env == string(ENV_VERIFY_VALUE_ALL) || env == strings.ToLower(string(verification))
return env == string(envVerifyValueAll) || env == strings.ToLower(string(verification))
}

// EnableVerifications sets `ENV_VERIFY` and returns a function that
// EnableVerifications sets `envVerify` and returns a function that
// can be used to bring the original settings.
func EnableVerifications(verification VerificationType) func() {
previousEnv := getEnvVerify()
os.Setenv(ENV_VERIFY, string(verification))
os.Setenv(envVerify, string(verification))
return func() {
os.Setenv(ENV_VERIFY, previousEnv)
os.Setenv(envVerify, previousEnv)
}
}

// EnableAllVerifications enables verification and returns a function
// that can be used to bring the original settings.
func EnableAllVerifications() func() {
return EnableVerifications(ENV_VERIFY_VALUE_ALL)
return EnableVerifications(envVerifyValueAll)
}

// DisableVerifications unsets `ENV_VERIFY` and returns a function that
// DisableVerifications unsets `envVerify` and returns a function that
// can be used to bring the original settings.
func DisableVerifications() func() {
previousEnv := getEnvVerify()
os.Unsetenv(ENV_VERIFY)
os.Unsetenv(envVerify)
return func() {
os.Setenv(ENV_VERIFY, previousEnv)
os.Setenv(envVerify, previousEnv)
}
}

// Verify performs verification if the assertions are enabled.
// In the default setup running in tests and skipped in the production code.
func Verify(f func()) {
if IsVerificationEnabled(ENV_VERIFY_VALUE_ASSERT) {
if IsVerificationEnabled(envVerifyValueAssert) {
f()
}
}
Expand Down
6 changes: 3 additions & 3 deletions client/v3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,11 @@ func authority(endpoint string) string {
func (c *Client) credentialsForEndpoint(ep string) grpccredentials.TransportCredentials {
r := endpoint.RequiresCredentials(ep)
switch r {
case endpoint.CREDS_DROP:
case endpoint.CredsDrop:
return nil
case endpoint.CREDS_OPTIONAL:
case endpoint.CredsOptional:
return c.creds
case endpoint.CREDS_REQUIRE:
case endpoint.CredsRequire:
if c.creds != nil {
return c.creds
}
Expand Down
26 changes: 13 additions & 13 deletions client/v3/internal/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
type CredsRequirement int

const (
// CREDS_REQUIRE - Credentials/certificate required for thi type of connection.
CREDS_REQUIRE CredsRequirement = iota
// CREDS_DROP - Credentials/certificate not needed and should get ignored.
CREDS_DROP
// CREDS_OPTIONAL - Credentials/certificate might be used if supplied
CREDS_OPTIONAL
// CredsRequire - Credentials/certificate required for thi type of connection.
CredsRequire CredsRequirement = iota
// CredsDrop - Credentials/certificate not needed and should get ignored.
CredsDrop
// CredsOptional - Credentials/certificate might be used if supplied
CredsOptional
)

func extractHostFromHostPort(ep string) string {
Expand All @@ -54,20 +54,20 @@ func mustSplit2(s, sep string) (string, string) {
func schemeToCredsRequirement(schema string) CredsRequirement {
switch schema {
case "https", "unixs":
return CREDS_REQUIRE
return CredsRequire
case "http":
return CREDS_DROP
return CredsDrop
case "unix":
// Preserving previous behavior from:
// https://github.com/etcd-io/etcd/blob/dae29bb719dd69dc119146fc297a0628fcc1ccf8/client/v3/client.go#L212
// that likely was a bug due to missing 'fallthrough'.
// At the same time it seems legit to let the users decide whether they
// want credential control or not (and 'unixs' schema is not a standard thing).
return CREDS_OPTIONAL
return CredsOptional
case "":
return CREDS_OPTIONAL
return CredsOptional
default:
return CREDS_OPTIONAL
return CredsOptional
}
}

Expand Down Expand Up @@ -106,15 +106,15 @@ func translateEndpoint(ep string) (addr string, serverName string, requireCreds
if strings.Contains(ep, "://") {
url, err := url.Parse(ep)
if err != nil {
return ep, ep, CREDS_OPTIONAL
return ep, ep, CredsOptional
}
if url.Scheme == "http" || url.Scheme == "https" {
return url.Host, url.Host, schemeToCredsRequirement(url.Scheme)
}
return ep, url.Host, schemeToCredsRequirement(url.Scheme)
}
// Handles plain addresses like 10.0.0.44:437.
return ep, ep, CREDS_OPTIONAL
return ep, ep, CredsOptional
}

// RequiresCredentials returns whether given endpoint requires
Expand Down
52 changes: 26 additions & 26 deletions client/v3/internal/endpoint/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,38 @@ func Test_interpret(t *testing.T) {
wantServerName string
wantRequiresCreds CredsRequirement
}{
{"127.0.0.1", "127.0.0.1", "127.0.0.1", CREDS_OPTIONAL},
{"localhost", "localhost", "localhost", CREDS_OPTIONAL},
{"localhost:8080", "localhost:8080", "localhost:8080", CREDS_OPTIONAL},
{"127.0.0.1", "127.0.0.1", "127.0.0.1", CredsOptional},
{"localhost", "localhost", "localhost", CredsOptional},
{"localhost:8080", "localhost:8080", "localhost:8080", CredsOptional},

{"unix:127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_OPTIONAL},
{"unix:127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_OPTIONAL},
{"unix:127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CredsOptional},
{"unix:127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CredsOptional},

{"unix://127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_OPTIONAL},
{"unix://127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_OPTIONAL},
{"unix://127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CredsOptional},
{"unix://127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CredsOptional},

{"unixs:127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_REQUIRE},
{"unixs:127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_REQUIRE},
{"unixs://127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_REQUIRE},
{"unixs://127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_REQUIRE},
{"unixs:127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CredsRequire},
{"unixs:127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CredsRequire},
{"unixs://127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CredsRequire},
{"unixs://127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CredsRequire},

{"http://127.0.0.1", "127.0.0.1", "127.0.0.1", CREDS_DROP},
{"http://127.0.0.1:8080", "127.0.0.1:8080", "127.0.0.1:8080", CREDS_DROP},
{"https://127.0.0.1", "127.0.0.1", "127.0.0.1", CREDS_REQUIRE},
{"https://127.0.0.1:8080", "127.0.0.1:8080", "127.0.0.1:8080", CREDS_REQUIRE},
{"https://localhost:20000", "localhost:20000", "localhost:20000", CREDS_REQUIRE},
{"http://127.0.0.1", "127.0.0.1", "127.0.0.1", CredsDrop},
{"http://127.0.0.1:8080", "127.0.0.1:8080", "127.0.0.1:8080", CredsDrop},
{"https://127.0.0.1", "127.0.0.1", "127.0.0.1", CredsRequire},
{"https://127.0.0.1:8080", "127.0.0.1:8080", "127.0.0.1:8080", CredsRequire},
{"https://localhost:20000", "localhost:20000", "localhost:20000", CredsRequire},

{"unix:///tmp/abc", "unix:///tmp/abc", "abc", CREDS_OPTIONAL},
{"unixs:///tmp/abc", "unix:///tmp/abc", "abc", CREDS_REQUIRE},
{"unix:///tmp/abc:1234", "unix:///tmp/abc:1234", "abc:1234", CREDS_OPTIONAL},
{"unixs:///tmp/abc:1234", "unix:///tmp/abc:1234", "abc:1234", CREDS_REQUIRE},
{"etcd.io", "etcd.io", "etcd.io", CREDS_OPTIONAL},
{"http://etcd.io/abc", "etcd.io", "etcd.io", CREDS_DROP},
{"dns://something-other", "dns://something-other", "something-other", CREDS_OPTIONAL},
{"unix:///tmp/abc", "unix:///tmp/abc", "abc", CredsOptional},
{"unixs:///tmp/abc", "unix:///tmp/abc", "abc", CredsRequire},
{"unix:///tmp/abc:1234", "unix:///tmp/abc:1234", "abc:1234", CredsOptional},
{"unixs:///tmp/abc:1234", "unix:///tmp/abc:1234", "abc:1234", CredsRequire},
{"etcd.io", "etcd.io", "etcd.io", CredsOptional},
{"http://etcd.io/abc", "etcd.io", "etcd.io", CredsDrop},
{"dns://something-other", "dns://something-other", "something-other", CredsOptional},

{"http://[2001:db8:1f70::999:de8:7648:6e8]:100/", "[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", CREDS_DROP},
{"[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", CREDS_OPTIONAL},
{"unix:unexpected-file_name#123$456", "unix:unexpected-file_name#123$456", "unexpected-file_name#123$456", CREDS_OPTIONAL},
{"http://[2001:db8:1f70::999:de8:7648:6e8]:100/", "[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", CredsDrop},
{"[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", CredsOptional},
{"unix:unexpected-file_name#123$456", "unix:unexpected-file_name#123$456", "unexpected-file_name#123$456", CredsOptional},
}
for _, tt := range tests {
t.Run("Interpret_"+tt.endpoint, func(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions client/v3/mock/mockserver/mockserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type MockServer struct {
ln net.Listener
Network string
Address string
GrpcServer *grpc.Server
GRPCServer *grpc.Server
}

func (ms *MockServer) ResolverAddress() resolver.Address {
Expand Down Expand Up @@ -63,15 +63,15 @@ func StartMockServers(count int) (ms *MockServers, err error) {
func StartMockServersOnNetwork(count int, network string) (ms *MockServers, err error) {
switch network {
case "tcp":
return startMockServersTcp(count)
return startMockServersTCP(count)
case "unix":
return startMockServersUnix(count)
default:
return nil, fmt.Errorf("unsupported network type: %s", network)
}
}

func startMockServersTcp(count int) (ms *MockServers, err error) {
func startMockServersTCP(count int) (ms *MockServers, err error) {
addrs := make([]string, 0, count)
for i := 0; i < count; i++ {
addrs = append(addrs, "localhost:0")
Expand Down Expand Up @@ -133,12 +133,12 @@ func (ms *MockServers) StartAt(idx int) (err error) {
svr := grpc.NewServer()
pb.RegisterKVServer(svr, &mockKVServer{})
pb.RegisterLeaseServer(svr, &mockLeaseServer{})
ms.Servers[idx].GrpcServer = svr
ms.Servers[idx].GRPCServer = svr

ms.wg.Add(1)
go func(svr *grpc.Server, l net.Listener) {
svr.Serve(l)
}(ms.Servers[idx].GrpcServer, ms.Servers[idx].ln)
}(ms.Servers[idx].GRPCServer, ms.Servers[idx].ln)
return nil
}

Expand All @@ -151,8 +151,8 @@ func (ms *MockServers) StopAt(idx int) {
return
}

ms.Servers[idx].GrpcServer.Stop()
ms.Servers[idx].GrpcServer = nil
ms.Servers[idx].GRPCServer.Stop()
ms.Servers[idx].GRPCServer = nil
ms.Servers[idx].ln = nil
ms.wg.Done()
}
Expand Down
4 changes: 2 additions & 2 deletions client/v3/retry_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func waitRetryBackoff(ctx context.Context, attempt uint, callOpts *options) erro
select {
case <-ctx.Done():
timer.Stop()
return contextErrToGrpcErr(ctx.Err())
return contextErrToGRPCErr(ctx.Err())
case <-timer.C:
}
}
Expand Down Expand Up @@ -349,7 +349,7 @@ func isContextError(err error) bool {
return status.Code(err) == codes.DeadlineExceeded || status.Code(err) == codes.Canceled
}

func contextErrToGrpcErr(err error) error {
func contextErrToGRPCErr(err error) error {
switch err {
case context.DeadlineExceeded:
return status.Errorf(codes.DeadlineExceeded, err.Error())
Expand Down
Loading

0 comments on commit d639abe

Please sign in to comment.