From 6cca5ae66f0664b72a62e8fe40ba2b7c617921cb Mon Sep 17 00:00:00 2001 From: CMGS Date: Tue, 5 Jul 2022 10:30:39 +0800 Subject: [PATCH] refactor for updating --- 3rdmocks/ServerStream.go | 17 +++++++++++++- Makefile | 3 +-- cluster/mocks/Cluster.go | 17 +++++++++++++- engine/docker/mocks/APIClient.go | 17 +++++++++++++- engine/mocks/API.go | 17 +++++++++++++- lock/mocks/DistributedLock.go | 17 +++++++++++++- metrics/metrics.go | 8 +++---- resources/manager.go | 14 ------------ resources/mocks/Plugin.go | 17 +++++++++++++- rpc/mocks/CoreRPC_RunAndWaitServer.go | 17 +++++++++++++- source/mocks/Source.go | 17 +++++++++++++- store/etcdv3/meta/mocks/ETCDClientV3.go | 17 +++++++++++++- store/etcdv3/meta/mocks/KV.go | 17 +++++++++++++- store/etcdv3/meta/mocks/Txn.go | 17 +++++++++++++- store/mocks/Store.go | 17 +++++++++++++- utils/file.go | 5 +---- utils/file_test.go | 30 +++++++++++++++++++++++++ wal/mocks/WAL.go | 17 +++++++++++++- 18 files changed, 244 insertions(+), 37 deletions(-) diff --git a/3rdmocks/ServerStream.go b/3rdmocks/ServerStream.go index 3f2cebdfc..426cd34fa 100644 --- a/3rdmocks/ServerStream.go +++ b/3rdmocks/ServerStream.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.14.0. DO NOT EDIT. package mocks @@ -91,3 +91,18 @@ func (_m *ServerStream) SetHeader(_a0 metadata.MD) error { func (_m *ServerStream) SetTrailer(_a0 metadata.MD) { _m.Called(_a0) } + +type mockConstructorTestingTNewServerStream interface { + mock.TestingT + Cleanup(func()) +} + +// NewServerStream creates a new instance of ServerStream. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewServerStream(t mockConstructorTestingTNewServerStream) *ServerStream { + mock := &ServerStream{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/Makefile b/Makefile index 15f8b8089..4f797ef11 100644 --- a/Makefile +++ b/Makefile @@ -18,8 +18,7 @@ grpc: ./rpc/gen/core.proto deps: - env GO111MODULE=on go mod download - env GO111MODULE=on go mod vendor + go mod vendor binary: CGO_ENABLED=0 go build -ldflags "$(GO_LDFLAGS)" -gcflags=all=-G=3 -o eru-core diff --git a/cluster/mocks/Cluster.go b/cluster/mocks/Cluster.go index c00c6c884..9dab6abdf 100644 --- a/cluster/mocks/Cluster.go +++ b/cluster/mocks/Cluster.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.14.0. DO NOT EDIT. package mocks @@ -948,3 +948,18 @@ func (_m *Cluster) WorkloadStatusStream(ctx context.Context, appname string, ent return r0 } + +type mockConstructorTestingTNewCluster interface { + mock.TestingT + Cleanup(func()) +} + +// NewCluster creates a new instance of Cluster. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewCluster(t mockConstructorTestingTNewCluster) *Cluster { + mock := &Cluster{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/engine/docker/mocks/APIClient.go b/engine/docker/mocks/APIClient.go index 3783fd42e..d64976cfc 100644 --- a/engine/docker/mocks/APIClient.go +++ b/engine/docker/mocks/APIClient.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.14.0. DO NOT EDIT. package mocks @@ -2410,3 +2410,18 @@ func (_m *APIClient) VolumesPrune(ctx context.Context, pruneFilter filters.Args) return r0, r1 } + +type mockConstructorTestingTNewAPIClient interface { + mock.TestingT + Cleanup(func()) +} + +// NewAPIClient creates a new instance of APIClient. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewAPIClient(t mockConstructorTestingTNewAPIClient) *APIClient { + mock := &APIClient{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/engine/mocks/API.go b/engine/mocks/API.go index 266b5ce0b..cfeb8fba2 100644 --- a/engine/mocks/API.go +++ b/engine/mocks/API.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.14.0. DO NOT EDIT. package mocks @@ -759,3 +759,18 @@ func (_m *API) VirtualizationWait(ctx context.Context, ID string, state string) return r0, r1 } + +type mockConstructorTestingTNewAPI interface { + mock.TestingT + Cleanup(func()) +} + +// NewAPI creates a new instance of API. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewAPI(t mockConstructorTestingTNewAPI) *API { + mock := &API{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/lock/mocks/DistributedLock.go b/lock/mocks/DistributedLock.go index 4b941e5f1..f3cf45d11 100644 --- a/lock/mocks/DistributedLock.go +++ b/lock/mocks/DistributedLock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.14.0. DO NOT EDIT. package mocks @@ -72,3 +72,18 @@ func (_m *DistributedLock) Unlock(ctx context.Context) error { return r0 } + +type mockConstructorTestingTNewDistributedLock interface { + mock.TestingT + Cleanup(func()) +} + +// NewDistributedLock creates a new instance of DistributedLock. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewDistributedLock(t mockConstructorTestingTNewDistributedLock) *DistributedLock { + mock := &DistributedLock{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/metrics/metrics.go b/metrics/metrics.go index 39408041f..ce664ec47 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -93,22 +93,22 @@ func (m *Metrics) SendMetrics(metrics ...*resources.Metrics) { log.Errorf(nil, "[SendMetrics] Collector not found: %s", metric.Name) //nolint continue } - switch collector.(type) { // nolint: gocritic + switch collector.(type) { // nolint case *prometheus.GaugeVec: value, err := strconv.ParseFloat(metric.Value, 64) if err != nil { log.Errorf(nil, "[SendMetrics] Error occurred while parsing %v value %v: %v", metric.Name, metric.Value, err) //nolint } - collector.(*prometheus.GaugeVec).WithLabelValues(metric.Labels...).Set(value) + collector.(*prometheus.GaugeVec).WithLabelValues(metric.Labels...).Set(value) // nolint if err := m.gauge(metric.Key, value); err != nil { log.Errorf(nil, "[SendMetrics] Error occurred while sending %v data to statsd: %v", metric.Name, err) //nolint } case *prometheus.CounterVec: - value, err := strconv.ParseInt(metric.Value, 10, 32) + value, err := strconv.ParseInt(metric.Value, 10, 32) // nolint if err != nil { log.Errorf(nil, "[SendMetrics] Error occurred while parsing %v value %v: %v", metric.Name, metric.Value, err) //nolint } - collector.(*prometheus.CounterVec).WithLabelValues(metric.Labels...).Add(float64(value)) + collector.(*prometheus.CounterVec).WithLabelValues(metric.Labels...).Add(float64(value)) // nolint if err := m.count(metric.Key, int(value), 1.0); err != nil { log.Errorf(nil, "[SendMetrics] Error occurred while sending %v data to statsd: %v", metric.Name, err) //nolint } diff --git a/resources/manager.go b/resources/manager.go index 648735d39..3214b604d 100644 --- a/resources/manager.go +++ b/resources/manager.go @@ -25,7 +25,6 @@ func NewPluginManager(config types.Config) (*PluginManager, error) { config: config, plugins: []Plugin{}, } - return pm, nil } @@ -56,19 +55,6 @@ func (pm *PluginManager) GetPlugins() []Plugin { return pm.plugins } -func (pm *PluginManager) callPlugins(plugins []Plugin, f func(Plugin)) { - wg := &sync.WaitGroup{} - wg.Add(len(plugins)) - - for _, plugin := range plugins { - go func(p Plugin) { - defer wg.Done() - f(p) - }(plugin) - } - wg.Wait() -} - func callPlugins[T any](ctx context.Context, plugins []Plugin, f func(Plugin) (T, error)) (map[Plugin]T, error) { resMap := sync.Map{} combinedErr := types.NewCombinedErr() diff --git a/resources/mocks/Plugin.go b/resources/mocks/Plugin.go index ae3dbcb09..3ad1057fb 100644 --- a/resources/mocks/Plugin.go +++ b/resources/mocks/Plugin.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.14.0. DO NOT EDIT. package mocks @@ -353,3 +353,18 @@ func (_m *Plugin) SetNodeResourceUsage(ctx context.Context, nodeName string, nod return r0, r1 } + +type mockConstructorTestingTNewPlugin interface { + mock.TestingT + Cleanup(func()) +} + +// NewPlugin creates a new instance of Plugin. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewPlugin(t mockConstructorTestingTNewPlugin) *Plugin { + mock := &Plugin{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/rpc/mocks/CoreRPC_RunAndWaitServer.go b/rpc/mocks/CoreRPC_RunAndWaitServer.go index 9377f73e7..1ce40a1d3 100644 --- a/rpc/mocks/CoreRPC_RunAndWaitServer.go +++ b/rpc/mocks/CoreRPC_RunAndWaitServer.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.14.0. DO NOT EDIT. package mocks @@ -129,3 +129,18 @@ func (_m *CoreRPC_RunAndWaitServer) SetHeader(_a0 metadata.MD) error { func (_m *CoreRPC_RunAndWaitServer) SetTrailer(_a0 metadata.MD) { _m.Called(_a0) } + +type mockConstructorTestingTNewCoreRPC_RunAndWaitServer interface { + mock.TestingT + Cleanup(func()) +} + +// NewCoreRPC_RunAndWaitServer creates a new instance of CoreRPC_RunAndWaitServer. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewCoreRPC_RunAndWaitServer(t mockConstructorTestingTNewCoreRPC_RunAndWaitServer) *CoreRPC_RunAndWaitServer { + mock := &CoreRPC_RunAndWaitServer{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/source/mocks/Source.go b/source/mocks/Source.go index ac84f3a54..45a42d19e 100644 --- a/source/mocks/Source.go +++ b/source/mocks/Source.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.14.0. DO NOT EDIT. package mocks @@ -54,3 +54,18 @@ func (_m *Source) SourceCode(ctx context.Context, repository string, path string return r0 } + +type mockConstructorTestingTNewSource interface { + mock.TestingT + Cleanup(func()) +} + +// NewSource creates a new instance of Source. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewSource(t mockConstructorTestingTNewSource) *Source { + mock := &Source{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/store/etcdv3/meta/mocks/ETCDClientV3.go b/store/etcdv3/meta/mocks/ETCDClientV3.go index 28a4221c8..19d55d466 100644 --- a/store/etcdv3/meta/mocks/ETCDClientV3.go +++ b/store/etcdv3/meta/mocks/ETCDClientV3.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.14.0. DO NOT EDIT. package mocks @@ -367,3 +367,18 @@ func (_m *ETCDClientV3) Watch(ctx context.Context, key string, opts ...clientv3. return r0 } + +type mockConstructorTestingTNewETCDClientV3 interface { + mock.TestingT + Cleanup(func()) +} + +// NewETCDClientV3 creates a new instance of ETCDClientV3. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewETCDClientV3(t mockConstructorTestingTNewETCDClientV3) *ETCDClientV3 { + mock := &ETCDClientV3{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/store/etcdv3/meta/mocks/KV.go b/store/etcdv3/meta/mocks/KV.go index 6125d4552..38c122160 100644 --- a/store/etcdv3/meta/mocks/KV.go +++ b/store/etcdv3/meta/mocks/KV.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.14.0. DO NOT EDIT. package mocks @@ -479,3 +479,18 @@ func (_m *KV) Watch(ctx context.Context, key string, opts ...clientv3.OpOption) return r0 } + +type mockConstructorTestingTNewKV interface { + mock.TestingT + Cleanup(func()) +} + +// NewKV creates a new instance of KV. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewKV(t mockConstructorTestingTNewKV) *KV { + mock := &KV{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/store/etcdv3/meta/mocks/Txn.go b/store/etcdv3/meta/mocks/Txn.go index 15e96cfc3..8ddcebf49 100644 --- a/store/etcdv3/meta/mocks/Txn.go +++ b/store/etcdv3/meta/mocks/Txn.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.14.0. DO NOT EDIT. package mocks @@ -100,3 +100,18 @@ func (_m *Txn) Then(ops ...clientv3.Op) clientv3.Txn { return r0 } + +type mockConstructorTestingTNewTxn interface { + mock.TestingT + Cleanup(func()) +} + +// NewTxn creates a new instance of Txn. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewTxn(t mockConstructorTestingTNewTxn) *Txn { + mock := &Txn{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/store/mocks/Store.go b/store/mocks/Store.go index 6d6d1380f..ba93dd7c7 100644 --- a/store/mocks/Store.go +++ b/store/mocks/Store.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.14.0. DO NOT EDIT. package mocks @@ -628,3 +628,18 @@ func (_m *Store) WorkloadStatusStream(ctx context.Context, appname string, entry return r0 } + +type mockConstructorTestingTNewStore interface { + mock.TestingT + Cleanup(func()) +} + +// NewStore creates a new instance of Store. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewStore(t mockConstructorTestingTNewStore) *Store { + mock := &Store{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/utils/file.go b/utils/file.go index 780d7dc82..99c0c866d 100644 --- a/utils/file.go +++ b/utils/file.go @@ -23,10 +23,7 @@ func ListAllExecutableFiles(basedir string) ([]string, error) { return nil }) - if err != nil { - return nil, err - } - return files, nil + return files, err } func isExecutable(perm fs.FileMode) bool { diff --git a/utils/file_test.go b/utils/file_test.go index d4b585bf7..415e82f80 100644 --- a/utils/file_test.go +++ b/utils/file_test.go @@ -1 +1,31 @@ package utils + +import ( + "io/ioutil" + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestListAllExecutableFiles(t *testing.T) { + dir, err := ioutil.TempDir(os.TempDir(), "test*") + assert.NoError(t, err) + defer os.RemoveAll(dir) + + file, err := ioutil.TempFile(dir, "abc") + assert.NoError(t, err) + + subdir, err := ioutil.TempDir(dir, "def") + assert.NoError(t, err) + + assert.NotNil(t, file) + assert.NotNil(t, subdir) + + fInfo, err := os.Stat(file.Name()) + assert.NoError(t, err) + assert.NotNil(t, fInfo) + + //file + //isExecutable(file.Mode().Perm()) +} diff --git a/wal/mocks/WAL.go b/wal/mocks/WAL.go index f0f7a60d8..2f06cbca4 100644 --- a/wal/mocks/WAL.go +++ b/wal/mocks/WAL.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.14.0. DO NOT EDIT. package mocks @@ -60,3 +60,18 @@ func (_m *WAL) Recover(_a0 context.Context) { func (_m *WAL) Register(_a0 wal.EventHandler) { _m.Called(_a0) } + +type mockConstructorTestingTNewWAL interface { + mock.TestingT + Cleanup(func()) +} + +// NewWAL creates a new instance of WAL. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewWAL(t mockConstructorTestingTNewWAL) *WAL { + mock := &WAL{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +}