From b5737a53074cda505ec6b586ca713cc87dd787ba Mon Sep 17 00:00:00 2001 From: CMGS Date: Tue, 5 Jul 2022 11:41:56 +0800 Subject: [PATCH] make utils test cover great again --- go.mod | 1 + go.sum | 2 ++ resources/cpumem/cpumem.go | 34 ++++++++++++---------------------- resources/helper.go | 12 ------------ resources/manager.go | 2 +- resources/volume/volume.go | 34 ++++++++++++---------------------- utils/contest_test.go | 11 +++++++++++ utils/file_test.go | 11 +++++++++-- utils/http_test.go | 28 ++++++++++++++++++++++++++++ utils/transaction_test.go | 13 +++++++++++++ utils/utils_test.go | 10 ++++++++++ 11 files changed, 99 insertions(+), 59 deletions(-) delete mode 100644 resources/helper.go create mode 100644 utils/contest_test.go create mode 100644 utils/http_test.go diff --git a/go.mod b/go.mod index 4c295b2f7..951136ca1 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 8fc5c11f6..8eb1ecdd8 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/resources/cpumem/cpumem.go b/resources/cpumem/cpumem.go index acf82c98f..1ccaeb2e1 100644 --- a/resources/cpumem/cpumem.go +++ b/resources/cpumem/cpumem.go @@ -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" @@ -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 . @@ -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 . @@ -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 . @@ -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 . @@ -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 . @@ -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 . @@ -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 . @@ -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 . @@ -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 . @@ -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) } diff --git a/resources/helper.go b/resources/helper.go deleted file mode 100644 index f64b84bf6..000000000 --- a/resources/helper.go +++ /dev/null @@ -1,12 +0,0 @@ -package resources - -import "encoding/json" - -// ToResp converts a map to a response -func ToResp(m interface{}, resp interface{}) error { - body, err := json.Marshal(m) - if err != nil { - return err - } - return json.Unmarshal(body, resp) -} diff --git a/resources/manager.go b/resources/manager.go index 3214b604d..6186e549f 100644 --- a/resources/manager.go +++ b/resources/manager.go @@ -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)) diff --git a/resources/volume/volume.go b/resources/volume/volume.go index 90135f4b7..d07340062 100644 --- a/resources/volume/volume.go +++ b/resources/volume/volume.go @@ -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" @@ -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 . @@ -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 . @@ -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 . @@ -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 . @@ -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 . @@ -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 . @@ -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 . @@ -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 . @@ -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 . @@ -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) } diff --git a/utils/contest_test.go b/utils/contest_test.go new file mode 100644 index 000000000..9076334e1 --- /dev/null +++ b/utils/contest_test.go @@ -0,0 +1,11 @@ +package utils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestInheritTracingInfo(t *testing.T) { + assert.Nil(t, InheritTracingInfo(nil, nil)) +} diff --git a/utils/file_test.go b/utils/file_test.go index 415e82f80..a125e0761 100644 --- a/utils/file_test.go +++ b/utils/file_test.go @@ -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) } diff --git a/utils/http_test.go b/utils/http_test.go new file mode 100644 index 000000000..0a2abaf21 --- /dev/null +++ b/utils/http_test.go @@ -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) +} diff --git a/utils/transaction_test.go b/utils/transaction_test.go index 16851f716..342242759 100644 --- a/utils/transaction_test.go +++ b/utils/transaction_test.go @@ -3,6 +3,7 @@ package utils import ( "context" "errors" + "os" "testing" "time" @@ -46,3 +47,15 @@ func TestTxn(t *testing.T) { ) assert.NoError(t, err) } + +func TestPCR(t *testing.T) { + prepare := func(context.Context) error { + return os.ErrClosed + } + commit := func(context.Context) error { + return os.ErrClosed + } + + ctx := context.Background() + assert.Error(t, PCR(ctx, prepare, commit, commit, time.Second)) +} diff --git a/utils/utils_test.go b/utils/utils_test.go index 1fc7358fb..4105a1ada 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -9,6 +9,7 @@ import ( "reflect" "strconv" "testing" + "time" "github.com/projecteru2/core/cluster" "github.com/projecteru2/core/types" @@ -248,6 +249,15 @@ func TestUnique(t *testing.T) { assert.True(t, reflect.DeepEqual(s3, []string{"", "1"})) } +func TestWithTimeout(t *testing.T) { + r := true + f := func(context.Context) { + r = false + } + WithTimeout(context.Background(), time.Second, f) + assert.False(t, r) +} + func TestSHA256(t *testing.T) { str := "hhh" assert.Equal(t, "24d166cd6c8b826c779040b49d5b6708d649b236558e8744339dfee6afe11999", SHA256(str))