Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Jul 5, 2022
1 parent b5737a5 commit 725f587
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 112 deletions.
12 changes: 8 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ require (
github.com/go-redis/redis/v8 v8.8.2
github.com/google/uuid v1.2.0
github.com/gorilla/mux v1.7.4 // indirect
github.com/hashicorp/go-multierror v1.1.1
github.com/jinzhu/configor v1.2.0
github.com/mitchellh/mapstructure v1.5.0
github.com/moby/sys/mount v0.2.0 // indirect
github.com/moby/term v0.0.0-20201110203204-bea5bbe245bf // indirect
github.com/morikuni/aec v1.0.0 // indirect
Expand All @@ -43,18 +45,19 @@ require (
go.etcd.io/etcd/client/pkg/v3 v3.5.0
go.etcd.io/etcd/client/v3 v3.5.0
go.etcd.io/etcd/tests/v3 v3.5.0
go.opencensus.io v0.22.3 // indirect
go.uber.org/automaxprocs v1.3.0
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
golang.org/x/exp v0.0.0-20220328175248-053ad81199eb
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f
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
require (
github.com/cornelk/hashmap v1.0.1
go.uber.org/zap v1.17.0
)

require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
Expand Down Expand Up @@ -83,6 +86,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
Expand Down Expand Up @@ -111,6 +115,7 @@ require (
go.etcd.io/etcd/pkg/v3 v3.5.0 // indirect
go.etcd.io/etcd/raft/v3 v3.5.0 // indirect
go.etcd.io/etcd/server/v3 v3.5.0 // indirect
go.opencensus.io v0.22.3 // indirect
go.opentelemetry.io/contrib v0.20.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.20.0 // indirect
go.opentelemetry.io/otel v0.20.0 // indirect
Expand All @@ -123,7 +128,6 @@ require (
go.opentelemetry.io/proto/otlp v0.7.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/mod v0.6.0-dev.0.20211013180041-c96bc1413d57 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,14 @@ github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
Expand Down
6 changes: 4 additions & 2 deletions resources/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math"
"sync"

"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"

enginetypes "github.com/projecteru2/core/engine/types"
Expand Down Expand Up @@ -57,7 +58,8 @@ 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{} // TODO hashmap
combinedErr := types.NewCombinedErr()

var combinedErr error
wg := &sync.WaitGroup{}
wg.Add(len(plugins))

Expand All @@ -66,7 +68,7 @@ func callPlugins[T any](ctx context.Context, plugins []Plugin, f func(Plugin) (T
defer wg.Done()
if res, err := f(p); err != nil {
log.Errorf(ctx, "[callPlugins] failed to call plugin %v, err: %v", p.Name(), err)
combinedErr.Append(p.Name(), err)
combinedErr = multierror.Append(combinedErr, types.NewDetailedErr(err, p.Name()))
} else {
resMap.Store(p, res)
}
Expand Down
1 change: 1 addition & 0 deletions rpc/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ func newPBRawParamStringSlice(strs []string) *pb.RawParam {

// fillOldNodeMeta fills the old node meta based on the new node resource args.
// uses some hard code, should be removed in the future.
// TODO remove it!
func fillOldNodeMeta(node *pb.Node, resourceCapacity map[string]types.RawParams, resourceUsage map[string]types.RawParams) {
if capacity, ok := resourceCapacity["cpumem"]; ok {
usage := resourceUsage["cpumem"]
Expand Down
18 changes: 18 additions & 0 deletions types/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package types

import (
"testing"

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

func TestIdentifier(t *testing.T) {
config := Config{}
config.Etcd = EtcdConfig{
Machines: []string{
"1.1.1.1",
"2.2.2.2",
},
}
assert.NotEmpty(t, config.Identifier())
}
20 changes: 0 additions & 20 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,3 @@ func (d detailedErr) Unwrap() error {
func NewDetailedErr(err error, details interface{}) error {
return detailedErr{err: err, details: details}
}

// CombinedErr can combine multiple errors into one
type CombinedErr struct {
ErrMap map[string]error
}

// Error .
func (e *CombinedErr) Error() string {
return fmt.Sprintf("%+v", e.ErrMap)
}

// Append .
func (e *CombinedErr) Append(key string, err error) {
e.ErrMap[key] = err
}

// NewCombinedErr .
func NewCombinedErr() *CombinedErr {
return &CombinedErr{ErrMap: map[string]error{}}
}
8 changes: 0 additions & 8 deletions types/helper.go

This file was deleted.

18 changes: 0 additions & 18 deletions types/helper_test.go

This file was deleted.

16 changes: 8 additions & 8 deletions types/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ type NUMA map[string]string
// NUMAMemory fine NUMA memory NODE
type NUMAMemory map[string]int64

// NodeMetrics used for metrics collecting
type NodeMetrics struct {
Name string
Podname string
ResourceCapacity map[string]NodeResourceArgs
ResourceUsage map[string]NodeResourceArgs
}

// NodeMeta .
type NodeMeta struct {
Name string `json:"name"`
Expand Down Expand Up @@ -81,14 +89,6 @@ func (n *Node) IsDown() bool {
return n.Bypass || !n.Available
}

// NodeMetrics used for metrics collecting
type NodeMetrics struct {
Name string
Podname string
ResourceCapacity map[string]NodeResourceArgs
ResourceUsage map[string]NodeResourceArgs
}

// Metrics reports metrics value
func (n *Node) Metrics() *NodeMetrics {
return &NodeMetrics{
Expand Down
20 changes: 19 additions & 1 deletion types/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,34 @@ import (
"github.com/stretchr/testify/mock"
)

func TestNodeMeta(t *testing.T) {
nm := NodeMeta{Name: "1"}
nnm, err := nm.DeepCopy()
assert.NoError(t, err)
assert.Equal(t, nm.Name, nnm.Name)
}

func TestNodeInfo(t *testing.T) {
mockEngine := &enginemocks.API{}
r := &enginetypes.Info{ID: "test"}
mockEngine.On("Info", mock.Anything).Return(r, nil)
mockEngine.On("Info", mock.Anything).Return(r, ErrNoOps).Once()

node := &Node{}
ctx := context.Background()

node.Engine = mockEngine
err := node.Info(ctx)
assert.Error(t, err)
mockEngine.On("Info", mock.Anything).Return(r, nil)
err = node.Info(ctx)
assert.NoError(t, err)
assert.True(t, strings.Contains(node.NodeInfo, "test"))

node.Bypass = true
assert.True(t, node.IsDown())
}

func TestNodeMetrics(t *testing.T) {
nm := Node{}
assert.NotNil(t, nm.Metrics())
}
2 changes: 1 addition & 1 deletion types/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (r RawArgs) String() string {
return string(r)
}

// LitterDump fro litter.Dumper
// LitterDump from litter.Dumper
func (r RawArgs) LitterDump(w io.Writer) {
w.Write(r) // nolint:errcheck // here can't import core/log due to cycle dependence
}
22 changes: 22 additions & 0 deletions types/options_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package types

import (
"bufio"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"go.uber.org/zap/buffer"
)

func TestParseTriOption(t *testing.T) {
Expand All @@ -30,6 +32,8 @@ func TestDeployOptions(t *testing.T) {
o := &DeployOptions{Entrypoint: &Entrypoint{}}
assert.Equal(ErrEmptyAppName, errors.Unwrap(o.Validate()))

assert.NotNil(t, o.GetProcessing("t"))

o.Name = "testname"
assert.Equal(ErrEmptyPodName, errors.Unwrap(o.Validate()))

Expand Down Expand Up @@ -64,6 +68,16 @@ func TestCopyOptions(t *testing.T) {
assert.NoError(o.Validate())
}

func TestLinuxFile(t *testing.T) {
lf := LinuxFile{Filename: "s"}
nlf := lf.Clone()
assert.Equal(t, lf.Filename, nlf.Filename)
assert.NotEmpty(t, lf.String())
b := bufio.NewWriter(&buffer.Buffer{})
lf.LitterDump(b)
assert.NoError(t, b.Flush())
}

func TestSendOptions(t *testing.T) {
assert := assert.New(t)

Expand Down Expand Up @@ -142,3 +156,11 @@ func TestImageOptions(t *testing.T) {
o.Normalize()
assert.Equal(o.Step, 3)
}

func TestRawArges(t *testing.T) {
ra := RawArgs([]byte("abc"))
assert.Equal(t, ra.String(), "abc")
b := bufio.NewWriter(&buffer.Buffer{})
ra.LitterDump(b)
assert.NoError(t, b.Flush())
}
16 changes: 8 additions & 8 deletions types/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ func (r RawParams) IsSet(key string) bool {

// Float64 .
func (r RawParams) Float64(key string) float64 {
if !r.IsSet(key) {
return float64(0.0)
}
res, _ := strconv.ParseFloat(fmt.Sprintf("%v", r[key]), 64)
return res
}

// Int64 .
func (r RawParams) Int64(key string) int64 {
if !r.IsSet(key) {
return 0
return int64(0)
}
var str string
if f, ok := r[key].(float64); ok {
Expand Down Expand Up @@ -63,8 +66,6 @@ func (r RawParams) StringSlice(key string) []string {
for _, v := range s {
if str, ok := v.(string); ok {
res = append(res, str)
} else {
return nil
}
}
}
Expand All @@ -88,11 +89,10 @@ func (r RawParams) Bool(key string) bool {

// RawParams .
func (r RawParams) RawParams(key string) map[string]interface{} {
if !r.IsSet(key) {
return map[string]interface{}{}
}
if m, ok := r[key].(map[string]interface{}); ok {
return m
if r.IsSet(key) {
if m, ok := r[key].(map[string]interface{}); ok {
return m
}
}
return map[string]interface{}{}
}
Expand Down
19 changes: 19 additions & 0 deletions types/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ import (
func TestRawParams(t *testing.T) {
var r RawParams

r = RawParams{
"cde": 1,
"bef": []interface{}{1, 2, 3, "1"},
"efg": []string{},
}
assert.Equal(t, r.Float64("abc"), 0.0)
assert.Equal(t, r.Int64("abc"), int64(0))
assert.Equal(t, r.String("abc"), "")
assert.Equal(t, r.String("cde"), "")
assert.Len(t, r.StringSlice("bef"), 1)
assert.Nil(t, r.OneOfStringSlice("efg"))
assert.Len(t, r.RawParams("fgd"), 0)

r = RawParams{
"int64": 1,
"str-int": "1",
Expand Down Expand Up @@ -39,3 +52,9 @@ func TestRawParams(t *testing.T) {
assert.Equal(t, r.RawParams("raw-params")["int64"], 1)
assert.Equal(t, r.IsSet("?"), false)
}

func TestCovertRawToMap(t *testing.T) {
r := RawParams{}
res := ConvertRawParamsToMap[int64](r)
assert.NotNil(t, res)
}
6 changes: 6 additions & 0 deletions types/workload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func TestWorkloadInspect(t *testing.T) {

ctx := context.Background()
c := Workload{}
_, err := c.Inspect(ctx)
assert.Error(t, err)
c.Engine = mockEngine
r2, _ := c.Inspect(ctx)
assert.Equal(t, r.ID, r2.ID)
Expand All @@ -31,6 +33,10 @@ func TestWorkloadControl(t *testing.T) {

ctx := context.Background()
c := Workload{}
assert.Error(t, c.Start(ctx))
assert.Error(t, c.Stop(ctx, true))
assert.Error(t, c.Remove(ctx, true))

c.Engine = mockEngine
err := c.Start(ctx)
assert.NoError(t, err)
Expand Down
Loading

0 comments on commit 725f587

Please sign in to comment.