Skip to content

Commit b5737a5

Browse files
committed
make utils test cover great again
1 parent 6cca5ae commit b5737a5

11 files changed

+99
-59
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ require (
5151
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
5252
google.golang.org/grpc v1.40.0
5353
google.golang.org/protobuf v1.27.1
54+
github.com/mitchellh/mapstructure v1.5.0
5455
)
5556

5657
require github.com/cornelk/hashmap v1.0.1

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,8 @@ github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS4
387387
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
388388
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
389389
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
390+
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
391+
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
390392
github.com/moby/sys/mount v0.2.0 h1:WhCW5B355jtxndN5ovugJlMFJawbUODuW8fSnEH6SSM=
391393
github.com/moby/sys/mount v0.2.0/go.mod h1:aAivFE2LB3W4bACsUXChRHQ0qKWsetY4Y9V7sxOougM=
392394
github.com/moby/sys/mountinfo v0.4.0/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A=

resources/cpumem/cpumem.go

+12-22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"strconv"
66

7+
"github.com/mitchellh/mapstructure"
78
enginetypes "github.com/projecteru2/core/engine/types"
89
"github.com/projecteru2/core/resources"
910
"github.com/projecteru2/core/resources/cpumem/models"
@@ -37,11 +38,10 @@ func (c *Plugin) GetDeployArgs(ctx context.Context, nodeName string, deployCount
3738
}
3839

3940
resp := &resources.GetDeployArgsResponse{}
40-
err = resources.ToResp(map[string]interface{}{
41+
return resp, mapstructure.Decode(map[string]interface{}{
4142
"engine_args": engineArgs,
4243
"resource_args": resourceArgs,
4344
}, resp)
44-
return resp, err
4545
}
4646

4747
// GetReallocArgs .
@@ -61,12 +61,11 @@ func (c *Plugin) GetReallocArgs(ctx context.Context, nodeName string, originReso
6161
}
6262

6363
resp := &resources.GetReallocArgsResponse{}
64-
err = resources.ToResp(map[string]interface{}{
64+
return resp, mapstructure.Decode(map[string]interface{}{
6565
"engine_args": engineArgs,
6666
"delta": delta,
6767
"resource_args": resourceArgs,
6868
}, resp)
69-
return resp, err
7069
}
7170

7271
// GetRemapArgs .
@@ -82,10 +81,9 @@ func (c *Plugin) GetRemapArgs(ctx context.Context, nodeName string, workloadMap
8281
}
8382

8483
resp := &resources.GetRemapArgsResponse{}
85-
err = resources.ToResp(map[string]interface{}{
84+
return resp, mapstructure.Decode(map[string]interface{}{
8685
"engine_args": engineArgs,
8786
}, resp)
88-
return resp, err
8987
}
9088

9189
// GetNodesDeployCapacity .
@@ -101,11 +99,10 @@ func (c *Plugin) GetNodesDeployCapacity(ctx context.Context, nodeNames []string,
10199
}
102100

103101
resp := &resources.GetNodesDeployCapacityResponse{}
104-
err = resources.ToResp(map[string]interface{}{
102+
return resp, mapstructure.Decode(map[string]interface{}{
105103
"nodes": nodesDeployCapacity,
106104
"total": total,
107105
}, resp)
108-
return resp, err
109106
}
110107

111108
// GetMostIdleNode .
@@ -116,11 +113,10 @@ func (c *Plugin) GetMostIdleNode(ctx context.Context, nodeNames []string) (*reso
116113
}
117114

118115
resp := &resources.GetMostIdleNodeResponse{}
119-
err = resources.ToResp(map[string]interface{}{
116+
return resp, mapstructure.Decode(map[string]interface{}{
120117
"node": nodeName,
121118
"priority": priority,
122119
}, resp)
123-
return resp, err
124120
}
125121

126122
// GetNodeResourceInfo .
@@ -169,11 +165,10 @@ func (c *Plugin) SetNodeResourceUsage(ctx context.Context, nodeName string, reso
169165
}
170166

171167
resp := &resources.SetNodeResourceUsageResponse{}
172-
err = resources.ToResp(map[string]interface{}{
168+
return resp, mapstructure.Decode(map[string]interface{}{
173169
"before": before,
174170
"after": after,
175171
}, resp)
176-
return resp, err
177172
}
178173

179174
// SetNodeResourceCapacity .
@@ -200,11 +195,10 @@ func (c *Plugin) SetNodeResourceCapacity(ctx context.Context, nodeName string, r
200195
}
201196

202197
resp := &resources.SetNodeResourceCapacityResponse{}
203-
err = resources.ToResp(map[string]interface{}{
198+
return resp, mapstructure.Decode(map[string]interface{}{
204199
"before": before,
205200
"after": after,
206201
}, resp)
207-
return resp, err
208202
}
209203

210204
// SetNodeResourceInfo .
@@ -252,11 +246,10 @@ func (c *Plugin) AddNode(ctx context.Context, nodeName string, resourceOpts core
252246
}
253247

254248
resp := &resources.AddNodeResponse{}
255-
err = resources.ToResp(map[string]interface{}{
249+
return resp, mapstructure.Decode(map[string]interface{}{
256250
"capacity": nodeResourceInfo.Capacity,
257251
"usage": nodeResourceInfo.Usage,
258252
}, resp)
259-
return resp, err
260253
}
261254

262255
// RemoveNode .
@@ -306,18 +299,16 @@ func (c *Plugin) getNodeResourceInfo(ctx context.Context, nodeName string, workl
306299
}
307300

308301
resp := &resources.GetNodeResourceInfoResponse{}
309-
err = resources.ToResp(map[string]interface{}{
302+
return resp, mapstructure.Decode(map[string]interface{}{
310303
"resource_info": nodeResourceInfo,
311304
"diffs": diffs,
312305
}, resp)
313-
return resp, err
314306
}
315307

316308
// GetMetricsDescription .
317309
func (c *Plugin) GetMetricsDescription(ctx context.Context) (*resources.GetMetricsDescriptionResponse, error) {
318310
resp := &resources.GetMetricsDescriptionResponse{}
319-
err := resources.ToResp(c.c.GetMetricsDescription(), resp)
320-
return resp, err
311+
return resp, mapstructure.Decode(c.c.GetMetricsDescription(), resp)
321312
}
322313

323314
// ResolveNodeResourceInfoToMetrics .
@@ -332,6 +323,5 @@ func (c *Plugin) ResolveNodeResourceInfoToMetrics(ctx context.Context, podName s
332323

333324
metrics := c.c.ResolveNodeResourceInfoToMetrics(podName, nodeName, capacity, usage)
334325
resp := &resources.ResolveNodeResourceInfoToMetricsResponse{}
335-
err := resources.ToResp(metrics, resp)
336-
return resp, err
326+
return resp, mapstructure.Decode(metrics, resp)
337327
}

resources/helper.go

-12
This file was deleted.

resources/manager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (pm *PluginManager) GetPlugins() []Plugin {
5656
}
5757

5858
func callPlugins[T any](ctx context.Context, plugins []Plugin, f func(Plugin) (T, error)) (map[Plugin]T, error) {
59-
resMap := sync.Map{}
59+
resMap := sync.Map{} // TODO hashmap
6060
combinedErr := types.NewCombinedErr()
6161
wg := &sync.WaitGroup{}
6262
wg.Add(len(plugins))

resources/volume/volume.go

+12-22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package volume
33
import (
44
"context"
55

6+
"github.com/mitchellh/mapstructure"
67
enginetypes "github.com/projecteru2/core/engine/types"
78
"github.com/projecteru2/core/resources"
89
"github.com/projecteru2/core/resources/volume/models"
@@ -36,11 +37,10 @@ func (v *Plugin) GetDeployArgs(ctx context.Context, nodeName string, deployCount
3637
}
3738

3839
resp := &resources.GetDeployArgsResponse{}
39-
err = resources.ToResp(map[string]interface{}{
40+
return resp, mapstructure.Decode(map[string]interface{}{
4041
"engine_args": engineArgs,
4142
"resource_args": resourceArgs,
4243
}, resp)
43-
return resp, err
4444
}
4545

4646
// GetReallocArgs .
@@ -60,12 +60,11 @@ func (v *Plugin) GetReallocArgs(ctx context.Context, nodeName string, originReso
6060
}
6161

6262
resp := &resources.GetReallocArgsResponse{}
63-
err = resources.ToResp(map[string]interface{}{
63+
return resp, mapstructure.Decode(map[string]interface{}{
6464
"engine_args": engineArgs,
6565
"delta": delta,
6666
"resource_args": resourceArgs,
6767
}, resp)
68-
return resp, err
6968
}
7069

7170
// GetRemapArgs .
@@ -81,10 +80,9 @@ func (v *Plugin) GetRemapArgs(ctx context.Context, nodeName string, workloadMap
8180
}
8281

8382
resp := &resources.GetRemapArgsResponse{}
84-
err = resources.ToResp(map[string]interface{}{
83+
return resp, mapstructure.Decode(map[string]interface{}{
8584
"engine_args": engineArgs,
8685
}, resp)
87-
return resp, err
8886
}
8987

9088
// GetNodesDeployCapacity .
@@ -100,11 +98,10 @@ func (v *Plugin) GetNodesDeployCapacity(ctx context.Context, nodeNames []string,
10098
}
10199

102100
resp := &resources.GetNodesDeployCapacityResponse{}
103-
err = resources.ToResp(map[string]interface{}{
101+
return resp, mapstructure.Decode(map[string]interface{}{
104102
"nodes": nodesDeployCapacity,
105103
"total": total,
106104
}, resp)
107-
return resp, err
108105
}
109106

110107
// GetMostIdleNode .
@@ -115,11 +112,10 @@ func (v *Plugin) GetMostIdleNode(ctx context.Context, nodeNames []string) (*reso
115112
}
116113

117114
resp := &resources.GetMostIdleNodeResponse{}
118-
err = resources.ToResp(map[string]interface{}{
115+
return resp, mapstructure.Decode(map[string]interface{}{
119116
"node": nodeName,
120117
"priority": priority,
121118
}, resp)
122-
return resp, err
123119
}
124120

125121
// GetNodeResourceInfo .
@@ -168,11 +164,10 @@ func (v *Plugin) SetNodeResourceUsage(ctx context.Context, nodeName string, reso
168164
}
169165

170166
resp := &resources.SetNodeResourceUsageResponse{}
171-
err = resources.ToResp(map[string]interface{}{
167+
return resp, mapstructure.Decode(map[string]interface{}{
172168
"before": before,
173169
"after": after,
174170
}, resp)
175-
return resp, err
176171
}
177172

178173
// SetNodeResourceCapacity .
@@ -205,11 +200,10 @@ func (v *Plugin) SetNodeResourceCapacity(ctx context.Context, nodeName string, r
205200
}
206201

207202
resp := &resources.SetNodeResourceCapacityResponse{}
208-
err = resources.ToResp(map[string]interface{}{
203+
return resp, mapstructure.Decode(map[string]interface{}{
209204
"before": before,
210205
"after": after,
211206
}, resp)
212-
return resp, err
213207
}
214208

215209
// SetNodeResourceInfo .
@@ -250,11 +244,10 @@ func (v *Plugin) AddNode(ctx context.Context, nodeName string, resourceOpts core
250244
}
251245

252246
resp := &resources.AddNodeResponse{}
253-
err = resources.ToResp(map[string]interface{}{
247+
return resp, mapstructure.Decode(map[string]interface{}{
254248
"capacity": nodeResourceInfo.Capacity,
255249
"usage": nodeResourceInfo.Usage,
256250
}, resp)
257-
return resp, err
258251
}
259252

260253
// RemoveNode .
@@ -304,18 +297,16 @@ func (v *Plugin) getNodeResourceInfo(ctx context.Context, nodeName string, workl
304297
}
305298

306299
resp := &resources.GetNodeResourceInfoResponse{}
307-
err = resources.ToResp(map[string]interface{}{
300+
return resp, mapstructure.Decode(map[string]interface{}{
308301
"resource_info": nodeResourceInfo,
309302
"diffs": diffs,
310303
}, resp)
311-
return resp, err
312304
}
313305

314306
// GetMetricsDescription .
315307
func (v *Plugin) GetMetricsDescription(ctx context.Context) (*resources.GetMetricsDescriptionResponse, error) {
316308
resp := &resources.GetMetricsDescriptionResponse{}
317-
err := resources.ToResp(v.v.GetMetricsDescription(), resp)
318-
return resp, err
309+
return resp, mapstructure.Decode(v.v.GetMetricsDescription(), resp)
319310
}
320311

321312
// ResolveNodeResourceInfoToMetrics .
@@ -330,6 +321,5 @@ func (v *Plugin) ResolveNodeResourceInfoToMetrics(ctx context.Context, podName s
330321

331322
metrics := v.v.ResolveNodeResourceInfoToMetrics(podName, nodeName, capacity, usage)
332323
resp := &resources.ResolveNodeResourceInfoToMetricsResponse{}
333-
err := resources.ToResp(metrics, resp)
334-
return resp, err
324+
return resp, mapstructure.Decode(metrics, resp)
335325
}

utils/contest_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestInheritTracingInfo(t *testing.T) {
10+
assert.Nil(t, InheritTracingInfo(nil, nil))
11+
}

utils/file_test.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ func TestListAllExecutableFiles(t *testing.T) {
2626
assert.NoError(t, err)
2727
assert.NotNil(t, fInfo)
2828

29-
//file
30-
//isExecutable(file.Mode().Perm())
29+
assert.False(t, isExecutable(fInfo.Mode().Perm()))
30+
31+
os.Chmod(file.Name(), 0777)
32+
fInfo, _ = os.Stat(file.Name())
33+
assert.True(t, isExecutable(fInfo.Mode().Perm()))
34+
35+
fs, err := ListAllExecutableFiles(dir)
36+
assert.NoError(t, err)
37+
assert.Len(t, fs, 1)
3138
}

utils/http_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestGetHTTPClient(t *testing.T) {
12+
assert.NotNil(t, GetHTTPClient())
13+
}
14+
15+
func TestGetUnixSockClient(t *testing.T) {
16+
assert.NotNil(t, GetUnixSockClient())
17+
}
18+
19+
func TestGetHTTPSClient(t *testing.T) {
20+
ctx := context.Background()
21+
client, err := GetHTTPSClient(ctx, "", "abc", "", "", "")
22+
assert.NoError(t, err)
23+
assert.NotNil(t, client)
24+
25+
client, err = GetHTTPSClient(ctx, os.TempDir(), "abc", "1", "2", "3")
26+
assert.Error(t, err)
27+
assert.Nil(t, client)
28+
}

0 commit comments

Comments
 (0)