Skip to content

Commit

Permalink
make utils test cover great again
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Jul 5, 2022
1 parent 6cca5ae commit b5737a5
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 59 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ require (
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/grpc v1.40.0
google.golang.org/protobuf v1.27.1
github.com/mitchellh/mapstructure v1.5.0
)

require github.com/cornelk/hashmap v1.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS4
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/moby/sys/mount v0.2.0 h1:WhCW5B355jtxndN5ovugJlMFJawbUODuW8fSnEH6SSM=
github.com/moby/sys/mount v0.2.0/go.mod h1:aAivFE2LB3W4bACsUXChRHQ0qKWsetY4Y9V7sxOougM=
github.com/moby/sys/mountinfo v0.4.0/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A=
Expand Down
34 changes: 12 additions & 22 deletions resources/cpumem/cpumem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"strconv"

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

resp := &resources.GetDeployArgsResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"engine_args": engineArgs,
"resource_args": resourceArgs,
}, resp)
return resp, err
}

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

resp := &resources.GetReallocArgsResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"engine_args": engineArgs,
"delta": delta,
"resource_args": resourceArgs,
}, resp)
return resp, err
}

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

resp := &resources.GetRemapArgsResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"engine_args": engineArgs,
}, resp)
return resp, err
}

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

resp := &resources.GetNodesDeployCapacityResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"nodes": nodesDeployCapacity,
"total": total,
}, resp)
return resp, err
}

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

resp := &resources.GetMostIdleNodeResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"node": nodeName,
"priority": priority,
}, resp)
return resp, err
}

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

resp := &resources.SetNodeResourceUsageResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"before": before,
"after": after,
}, resp)
return resp, err
}

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

resp := &resources.SetNodeResourceCapacityResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"before": before,
"after": after,
}, resp)
return resp, err
}

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

resp := &resources.AddNodeResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"capacity": nodeResourceInfo.Capacity,
"usage": nodeResourceInfo.Usage,
}, resp)
return resp, err
}

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

resp := &resources.GetNodeResourceInfoResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"resource_info": nodeResourceInfo,
"diffs": diffs,
}, resp)
return resp, err
}

// GetMetricsDescription .
func (c *Plugin) GetMetricsDescription(ctx context.Context) (*resources.GetMetricsDescriptionResponse, error) {
resp := &resources.GetMetricsDescriptionResponse{}
err := resources.ToResp(c.c.GetMetricsDescription(), resp)
return resp, err
return resp, mapstructure.Decode(c.c.GetMetricsDescription(), resp)
}

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

metrics := c.c.ResolveNodeResourceInfoToMetrics(podName, nodeName, capacity, usage)
resp := &resources.ResolveNodeResourceInfoToMetricsResponse{}
err := resources.ToResp(metrics, resp)
return resp, err
return resp, mapstructure.Decode(metrics, resp)
}
12 changes: 0 additions & 12 deletions resources/helper.go

This file was deleted.

2 changes: 1 addition & 1 deletion resources/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (pm *PluginManager) GetPlugins() []Plugin {
}

func callPlugins[T any](ctx context.Context, plugins []Plugin, f func(Plugin) (T, error)) (map[Plugin]T, error) {
resMap := sync.Map{}
resMap := sync.Map{} // TODO hashmap
combinedErr := types.NewCombinedErr()
wg := &sync.WaitGroup{}
wg.Add(len(plugins))
Expand Down
34 changes: 12 additions & 22 deletions resources/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package volume
import (
"context"

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

resp := &resources.GetDeployArgsResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"engine_args": engineArgs,
"resource_args": resourceArgs,
}, resp)
return resp, err
}

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

resp := &resources.GetReallocArgsResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"engine_args": engineArgs,
"delta": delta,
"resource_args": resourceArgs,
}, resp)
return resp, err
}

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

resp := &resources.GetRemapArgsResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"engine_args": engineArgs,
}, resp)
return resp, err
}

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

resp := &resources.GetNodesDeployCapacityResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"nodes": nodesDeployCapacity,
"total": total,
}, resp)
return resp, err
}

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

resp := &resources.GetMostIdleNodeResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"node": nodeName,
"priority": priority,
}, resp)
return resp, err
}

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

resp := &resources.SetNodeResourceUsageResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"before": before,
"after": after,
}, resp)
return resp, err
}

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

resp := &resources.SetNodeResourceCapacityResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"before": before,
"after": after,
}, resp)
return resp, err
}

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

resp := &resources.AddNodeResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"capacity": nodeResourceInfo.Capacity,
"usage": nodeResourceInfo.Usage,
}, resp)
return resp, err
}

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

resp := &resources.GetNodeResourceInfoResponse{}
err = resources.ToResp(map[string]interface{}{
return resp, mapstructure.Decode(map[string]interface{}{
"resource_info": nodeResourceInfo,
"diffs": diffs,
}, resp)
return resp, err
}

// GetMetricsDescription .
func (v *Plugin) GetMetricsDescription(ctx context.Context) (*resources.GetMetricsDescriptionResponse, error) {
resp := &resources.GetMetricsDescriptionResponse{}
err := resources.ToResp(v.v.GetMetricsDescription(), resp)
return resp, err
return resp, mapstructure.Decode(v.v.GetMetricsDescription(), resp)
}

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

metrics := v.v.ResolveNodeResourceInfoToMetrics(podName, nodeName, capacity, usage)
resp := &resources.ResolveNodeResourceInfoToMetricsResponse{}
err := resources.ToResp(metrics, resp)
return resp, err
return resp, mapstructure.Decode(metrics, resp)
}
11 changes: 11 additions & 0 deletions utils/contest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestInheritTracingInfo(t *testing.T) {
assert.Nil(t, InheritTracingInfo(nil, nil))
}
11 changes: 9 additions & 2 deletions utils/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ func TestListAllExecutableFiles(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, fInfo)

//file
//isExecutable(file.Mode().Perm())
assert.False(t, isExecutable(fInfo.Mode().Perm()))

os.Chmod(file.Name(), 0777)
fInfo, _ = os.Stat(file.Name())
assert.True(t, isExecutable(fInfo.Mode().Perm()))

fs, err := ListAllExecutableFiles(dir)
assert.NoError(t, err)
assert.Len(t, fs, 1)
}
28 changes: 28 additions & 0 deletions utils/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package utils

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetHTTPClient(t *testing.T) {
assert.NotNil(t, GetHTTPClient())
}

func TestGetUnixSockClient(t *testing.T) {
assert.NotNil(t, GetUnixSockClient())
}

func TestGetHTTPSClient(t *testing.T) {
ctx := context.Background()
client, err := GetHTTPSClient(ctx, "", "abc", "", "", "")
assert.NoError(t, err)
assert.NotNil(t, client)

client, err = GetHTTPSClient(ctx, os.TempDir(), "abc", "1", "2", "3")
assert.Error(t, err)
assert.Nil(t, client)
}
Loading

0 comments on commit b5737a5

Please sign in to comment.