Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deepsource: GO-W1009 using a deprecated function or etc #1846

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion internal/config/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ func (g *GRPCClient) Opts() ([]grpc.Option, error) {
grpc.WithInsecure(g.DialOption.Insecure),
grpc.WithBackoffMaxDelay(g.DialOption.BackoffMaxDelay),
grpc.WithBackoffMaxDelay(g.DialOption.BackoffMaxDelay),
grpc.WithDialTimeout(g.DialOption.Timeout),
grpc.WithClientInterceptors(g.DialOption.Interceptors...),
)

Expand Down
2 changes: 1 addition & 1 deletion internal/config/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ func TestGRPCClient_Opts(t *testing.T) {
},
},
want: want{
want: make([]grpc.Option, 26),
want: make([]grpc.Option, 25),
},
},
{
Expand Down
25 changes: 13 additions & 12 deletions internal/net/grpc/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"google.golang.org/grpc"
gbackoff "google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
)

Expand Down Expand Up @@ -287,23 +288,23 @@ func WithInsecure(flg bool) Option {
return func(g *gRPCClient) {
if flg {
g.dopts = append(g.dopts,
grpc.WithInsecure(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
}
}
}

func WithDialTimeout(dur string) Option {
return func(g *gRPCClient) {
d, err := timeutil.Parse(dur)
if err != nil {
return
}
g.dopts = append(g.dopts,
grpc.WithTimeout(d),
)
}
}
// func WithDialTimeout(dur string) Option {
// return func(g *gRPCClient) {
// d, err := timeutil.Parse(dur)
// if err != nil {
// return
// }
// g.dopts = append(g.dopts,
// grpc.WithTimeout(d),
// )
// }
// }

func WithKeepaliveParams(t, to string, permitWithoutStream bool) Option {
return func(g *gRPCClient) {
Expand Down
116 changes: 0 additions & 116 deletions internal/net/grpc/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2929,122 +2929,6 @@ func TestWithInsecure(t *testing.T) {
}
}

func TestWithDialTimeout(t *testing.T) {
t.Parallel()
// Change interface type to the type of object you are testing
type T = interface{}
type args struct {
dur string
}
type want struct {
obj *T
// Uncomment this line if the option returns an error, otherwise delete it
// err error
}
type test struct {
name string
args args
want want
// Use the first line if the option returns an error. otherwise use the second line
// checkFunc func(want, *T, error) error
// checkFunc func(want, *T) error
beforeFunc func(args)
afterFunc func(args)
}

// Uncomment this block if the option returns an error, otherwise delete it
/*
defaultCheckFunc := func(w want, obj *T, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", obj, w.obj)
}
return nil
}
*/

// Uncomment this block if the option do not returns an error, otherwise delete it
/*
defaultCheckFunc := func(w want, obj *T) error {
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", obj, w.obj)
}
return nil
}
*/

tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
args: args {
dur: "",
},
want: want {
obj: new(T),
},
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
args: args {
dur: "",
},
want: want {
obj: new(T),
},
}
}(),
*/
}

for _, tc := range tests {
test := tc
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}

// Uncomment this block if the option returns an error, otherwise delete it
/*
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := WithDialTimeout(test.args.dur)
obj := new(T)
if err := checkFunc(test.want, obj, got(obj)); err != nil {
tt.Errorf("error = %v", err)
}
*/

// Uncomment this block if the option do not return an error, otherwise delete it
/*
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}
got := WithDialTimeout(test.args.dur)
obj := new(T)
got(obj)
if err := checkFunc(test.want, obj); err != nil {
tt.Errorf("error = %v", err)
}
*/
})
}
}

func TestWithKeepaliveParams(t *testing.T) {
t.Parallel()
// Change interface type to the type of object you are testing
Expand Down
3 changes: 2 additions & 1 deletion internal/net/grpc/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/vdaas/vald/internal/strings"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
)

type (
Expand Down Expand Up @@ -528,7 +529,7 @@ func isGRPCPort(ctx context.Context, host string, port uint16) bool {
defer cancel()
conn, err := grpc.DialContext(ctx,
net.JoinHostPort(host, port),
grpc.WithInsecure(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions internal/net/grpc/pool/pool_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/vdaas/vald/internal/log/level"
"github.com/vdaas/vald/internal/net"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

const (
Expand Down Expand Up @@ -100,7 +101,7 @@ func Benchmark_ConnPool(b *testing.B) {
pool, err := New(ctx,
WithAddr(DefaultServerAddr),
WithSize(DefaultPoolSize),
WithDialOptions(grpc.WithInsecure()),
WithDialOptions(grpc.WithTransportCredentials(insecure.NewCredentials())),
)
if err != nil {
b.Error(err)
Expand All @@ -126,7 +127,7 @@ func Benchmark_ConnPool(b *testing.B) {
func Benchmark_StaticDial(b *testing.B) {
defer ListenAndServe(b, DefaultServerAddr)()

conn, err := grpc.DialContext(context.Background(), DefaultServerAddr, grpc.WithInsecure())
conn, err := grpc.DialContext(context.Background(), DefaultServerAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
b.Error(err)
}
Expand Down Expand Up @@ -155,7 +156,7 @@ func BenchmarkParallel_ConnPool(b *testing.B) {
pool, err := New(ctx,
WithAddr(DefaultServerAddr),
WithSize(DefaultPoolSize),
WithDialOptions(grpc.WithInsecure()),
WithDialOptions(grpc.WithTransportCredentials(insecure.NewCredentials())),
)
if err != nil {
b.Error(err)
Expand Down Expand Up @@ -183,7 +184,7 @@ func BenchmarkParallel_ConnPool(b *testing.B) {
func BenchmarkParallel_StaticDial(b *testing.B) {
defer ListenAndServe(b, DefaultServerAddr)()

conn, err := grpc.DialContext(context.Background(), DefaultServerAddr, grpc.WithInsecure())
conn, err := grpc.DialContext(context.Background(), DefaultServerAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
b.Error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func LoadLocalIP() string {
}
for _, address := range addrs {
if ipn, ok := address.(*net.IPNet); ok {
if ip, ok := netaddr.FromStdIPNet(ipn); ok && ip.Valid() && ip.IP().IsLoopback() &&
if ip, ok := netaddr.FromStdIPNet(ipn); ok && ip.IsValid() && ip.IP().IsLoopback() &&
(ip.IP().Is4() || ip.IP().Is6() || ip.IP().Is4in6()) {
return ip.IP().String()
}
Expand Down
39 changes: 15 additions & 24 deletions internal/tls/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,8 @@ func TestNew(t *testing.T) {
return errors.Errorf("Certificates[0] want: %v, but got: %v", want, got)
}

sl := len(c.ClientCAs.Subjects())
if sl == 0 {
return errors.New("subjects are empty")
}

if got, want := c.ClientCAs.Subjects()[sl-1], w.want.ClientCAs.Subjects()[sl-1]; !reflect.DeepEqual(got, want) {
return errors.Errorf("ClientCAs.Subjects want: %v, got: %v", want, got)
}

if got, want := c.ClientCAs.Subjects()[sl-1], w.want.ClientCAs.Subjects()[sl-1]; !reflect.DeepEqual(got, want) {
return errors.Errorf("ClientCAs.Subjects want: %v, got: %v", want, got)
if ok := c.ClientCAs.Equal(w.want.ClientCAs); !ok {
return errors.Errorf("ClientCAs.Equal want: %v, got: %v", want, got)
}

if got, want := c.ClientAuth, w.want.ClientAuth; want != got {
Expand Down Expand Up @@ -344,9 +335,15 @@ func TestNewX509CertPool(t *testing.T) {
},
want: want{
want: func() *x509.CertPool {
pool := x509.NewCertPool()
b, _ := file.ReadFile(testdata.GetTestdataPath("tls/dummyServer.crt"))
pool.AppendCertsFromPEM(b)
path := testdata.GetTestdataPath("tls/dummyServer.crt")
pool, err := x509.SystemCertPool()
if err != nil {
pool = x509.NewCertPool()
}
b, err := file.ReadFile(path)
if err == nil {
pool.AppendCertsFromPEM(b)
}
return pool
}(),
},
Expand All @@ -357,15 +354,9 @@ func TestNewX509CertPool(t *testing.T) {
if cp == nil {
return errors.New("got is nil")
}

if len(cp.Subjects()) == 0 {
return errors.New("cert files are empty")
}
l := len(cp.Subjects()) - 1
if got, want := cp.Subjects()[l], w.want.Subjects()[0]; !reflect.DeepEqual(got, want) {
return errors.Errorf("not equals. want: %v, got: %v", want, got)
if ok := cp.Equal(w.want); !ok {
return errors.Errorf("not equals. want: %#v, got: %#v", w.want, cp)
}

return nil
},
},
Expand Down Expand Up @@ -397,7 +388,7 @@ func TestNewX509CertPool(t *testing.T) {
return errors.New("err is nil")
}
if cp != nil {
return errors.Errorf("got is not nil: %v", cp)
return errors.Errorf("got is not nil: %v, want: %v", cp, w)
}
return nil
},
Expand All @@ -409,7 +400,7 @@ func TestNewX509CertPool(t *testing.T) {
return errors.New("err is nil")
}
if cp != nil {
return errors.Errorf("got is not nil: %v", cp)
return errors.Errorf("got is not nil: %v, want: %v", cp, w)
}
return nil
},
Expand Down