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: VET-V0008 lock erroneously passed by value pkg/discoverer #1857

138 changes: 94 additions & 44 deletions pkg/discoverer/k8s/service/discover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
)

func TestNew(t *testing.T) {
t.Parallel()
type args struct {
selector *config.Selectors
opts []Option
Expand All @@ -48,8 +47,8 @@ func TestNew(t *testing.T) {
args args
want want
checkFunc func(want, Discoverer, error) error
beforeFunc func(args)
afterFunc func(args)
beforeFunc func(*testing.T, args)
afterFunc func(*testing.T, args)
}
defaultCheckFunc := func(w want, gotDsc Discoverer, err error) error {
if !errors.Is(err, w.err) {
Expand All @@ -66,10 +65,17 @@ func TestNew(t *testing.T) {
{
name: "test_case_1",
args: args {
selector: nil,
opts: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
},
*/

Expand All @@ -79,10 +85,17 @@ func TestNew(t *testing.T) {
return test {
name: "test_case_2",
args: args {
selector: nil,
opts: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
}
}(),
*/
Expand All @@ -94,10 +107,10 @@ func TestNew(t *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(test.args)
test.beforeFunc(tt, test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
defer test.afterFunc(tt, test.args)
}
checkFunc := test.checkFunc
if test.checkFunc == nil {
Expand All @@ -108,21 +121,21 @@ func TestNew(t *testing.T) {
if err := checkFunc(test.want, gotDsc, err); err != nil {
tt.Errorf("error = %v", err)
}

vankichi marked this conversation as resolved.
Show resolved Hide resolved
})
}
}

func Test_discoverer_Start(t *testing.T) {
t.Parallel()
type args struct {
ctx context.Context
}
type fields struct {
maxPods int
nodes nodeMap
nodeMetrics nodeMetricsMap
pods podsMap
podMetrics podMetricsMap
nodes func() nodeMap
nodeMetrics func() nodeMetricsMap
pods func() podsMap
podMetrics func() podMetricsMap
podsByNode atomic.Value
podsByNamespace atomic.Value
podsByName atomic.Value
Expand All @@ -144,8 +157,8 @@ func Test_discoverer_Start(t *testing.T) {
fields fields
want want
checkFunc func(want, <-chan error, error) error
beforeFunc func(args)
afterFunc func(args)
beforeFunc func(*testing.T, args)
afterFunc func(*testing.T, args)
}
defaultCheckFunc := func(w want, got <-chan error, err error) error {
if !errors.Is(err, w.err) {
Expand Down Expand Up @@ -183,6 +196,12 @@ func Test_discoverer_Start(t *testing.T) {
},
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
},
*/

Expand Down Expand Up @@ -213,6 +232,12 @@ func Test_discoverer_Start(t *testing.T) {
},
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
}
}(),
*/
Expand All @@ -224,21 +249,21 @@ func Test_discoverer_Start(t *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(test.args)
test.beforeFunc(tt, test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
defer test.afterFunc(tt, test.args)
}
checkFunc := test.checkFunc
if test.checkFunc == nil {
checkFunc = defaultCheckFunc
}
d := &discoverer{
maxPods: test.fields.maxPods,
nodes: test.fields.nodes,
nodeMetrics: test.fields.nodeMetrics,
pods: test.fields.pods,
podMetrics: test.fields.podMetrics,
nodes: test.fields.nodes(),
nodeMetrics: test.fields.nodeMetrics(),
pods: test.fields.pods(),
podMetrics: test.fields.podMetrics(),
podsByNode: test.fields.podsByNode,
podsByNamespace: test.fields.podsByNamespace,
podsByName: test.fields.podsByName,
Expand All @@ -255,21 +280,21 @@ func Test_discoverer_Start(t *testing.T) {
if err := checkFunc(test.want, got, err); err != nil {
tt.Errorf("error = %v", err)
}

vankichi marked this conversation as resolved.
Show resolved Hide resolved
})
}
}

func Test_discoverer_GetPods(t *testing.T) {
t.Parallel()
type args struct {
req *payload.Discoverer_Request
}
type fields struct {
maxPods int
vankichi marked this conversation as resolved.
Show resolved Hide resolved
nodes nodeMap
nodeMetrics nodeMetricsMap
pods podsMap
podMetrics podMetricsMap
nodes func() nodeMap
nodeMetrics func() nodeMetricsMap
pods func() podsMap
podMetrics func() podMetricsMap
podsByNode atomic.Value
podsByNamespace atomic.Value
podsByName atomic.Value
Expand All @@ -291,8 +316,8 @@ func Test_discoverer_GetPods(t *testing.T) {
fields fields
want want
checkFunc func(want, *payload.Info_Pods, error) error
beforeFunc func(args)
afterFunc func(args)
beforeFunc func(*testing.T, args)
afterFunc func(*testing.T, args)
}
defaultCheckFunc := func(w want, gotPods *payload.Info_Pods, err error) error {
if !errors.Is(err, w.err) {
Expand Down Expand Up @@ -330,6 +355,12 @@ func Test_discoverer_GetPods(t *testing.T) {
},
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
},
*/

Expand Down Expand Up @@ -360,6 +391,12 @@ func Test_discoverer_GetPods(t *testing.T) {
},
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
}
}(),
*/
Expand All @@ -371,21 +408,21 @@ func Test_discoverer_GetPods(t *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(test.args)
test.beforeFunc(tt, test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
defer test.afterFunc(tt, test.args)
}
checkFunc := test.checkFunc
if test.checkFunc == nil {
checkFunc = defaultCheckFunc
}
d := &discoverer{
maxPods: test.fields.maxPods,
vankichi marked this conversation as resolved.
Show resolved Hide resolved
nodes: test.fields.nodes,
nodeMetrics: test.fields.nodeMetrics,
pods: test.fields.pods,
podMetrics: test.fields.podMetrics,
nodes: test.fields.nodes(),
nodeMetrics: test.fields.nodeMetrics(),
pods: test.fields.pods(),
podMetrics: test.fields.podMetrics(),
podsByNode: test.fields.podsByNode,
podsByNamespace: test.fields.podsByNamespace,
podsByName: test.fields.podsByName,
Expand All @@ -402,21 +439,21 @@ func Test_discoverer_GetPods(t *testing.T) {
if err := checkFunc(test.want, gotPods, err); err != nil {
tt.Errorf("error = %v", err)
}

vankichi marked this conversation as resolved.
Show resolved Hide resolved
})
}
}

func Test_discoverer_GetNodes(t *testing.T) {
t.Parallel()
type args struct {
req *payload.Discoverer_Request
}
type fields struct {
maxPods int
nodes nodeMap
nodeMetrics nodeMetricsMap
pods podsMap
podMetrics podMetricsMap
nodes func() nodeMap
nodeMetrics func() nodeMetricsMap
pods func() podsMap
podMetrics func() podMetricsMap
podsByNode atomic.Value
podsByNamespace atomic.Value
podsByName atomic.Value
Expand All @@ -438,8 +475,8 @@ func Test_discoverer_GetNodes(t *testing.T) {
fields fields
want want
checkFunc func(want, *payload.Info_Nodes, error) error
beforeFunc func(args)
afterFunc func(args)
beforeFunc func(*testing.T, args)
afterFunc func(*testing.T, args)
}
defaultCheckFunc := func(w want, gotNodes *payload.Info_Nodes, err error) error {
if !errors.Is(err, w.err) {
Expand Down Expand Up @@ -477,6 +514,12 @@ func Test_discoverer_GetNodes(t *testing.T) {
},
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
},
*/

Expand Down Expand Up @@ -507,6 +550,12 @@ func Test_discoverer_GetNodes(t *testing.T) {
},
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
}
}(),
*/
Expand All @@ -518,21 +567,21 @@ func Test_discoverer_GetNodes(t *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(test.args)
test.beforeFunc(tt, test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
defer test.afterFunc(tt, test.args)
}
checkFunc := test.checkFunc
if test.checkFunc == nil {
checkFunc = defaultCheckFunc
}
d := &discoverer{
maxPods: test.fields.maxPods,
nodes: test.fields.nodes,
nodeMetrics: test.fields.nodeMetrics,
pods: test.fields.pods,
podMetrics: test.fields.podMetrics,
nodes: test.fields.nodes(),
nodeMetrics: test.fields.nodeMetrics(),
pods: test.fields.pods(),
podMetrics: test.fields.podMetrics(),
podsByNode: test.fields.podsByNode,
podsByNamespace: test.fields.podsByNamespace,
podsByName: test.fields.podsByName,
Expand All @@ -549,6 +598,7 @@ func Test_discoverer_GetNodes(t *testing.T) {
if err := checkFunc(test.want, gotNodes, err); err != nil {
tt.Errorf("error = %v", err)
}

vankichi marked this conversation as resolved.
Show resolved Hide resolved
})
}
}
Loading