Skip to content

Commit ad12aee

Browse files
authored
Merge pull request #54 from kaleido-io/test-cov
Bring FFTM back to 100% test coverage
2 parents f9b0abc + e1314aa commit ad12aee

File tree

3 files changed

+67
-11
lines changed

3 files changed

+67
-11
lines changed

pkg/fftm/manager.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2022 Kaleido, Inc.
1+
// Copyright © 2023 Kaleido, Inc.
22
//
33
// SPDX-License-Identifier: Apache-2.0
44
//
@@ -105,10 +105,6 @@ type manager struct {
105105
func InitConfig() {
106106
tmconfig.Reset()
107107
events.InitDefaults()
108-
109-
if config.GetBool(tmconfig.MetricsEnabled) {
110-
metrics.Registry()
111-
}
112108
}
113109

114110
func NewManager(ctx context.Context, connector ffcapi.API) (Manager, error) {

pkg/fftm/manager_test.go

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2022 Kaleido, Inc.
1+
// Copyright © 2023 Kaleido, Inc.
22
//
33
// SPDX-License-Identifier: Apache-2.0
44
//
@@ -44,18 +44,28 @@ const testManagerName = "unittest"
4444

4545
func strPtr(s string) *string { return &s }
4646

47-
func testManagerCommonInit(t *testing.T) string {
47+
func testManagerCommonInit(t *testing.T, withMetrics bool) string {
48+
4849
InitConfig()
4950
policyengines.RegisterEngine(&simple.PolicyEngineFactory{})
5051
tmconfig.PolicyEngineBaseConfig.SubSection("simple").SubSection(simple.GasOracleConfig).Set(simple.GasOracleMode, simple.GasOracleModeDisabled)
5152

53+
if withMetrics {
54+
tmconfig.MetricsConfig.Set("enabled", true)
55+
}
56+
5257
ln, err := net.Listen("tcp", "127.0.0.1:0")
5358
assert.NoError(t, err)
5459
managerPort := strings.Split(ln.Addr().String(), ":")[1]
5560
ln.Close()
5661
tmconfig.APIConfig.Set(httpserver.HTTPConfPort, managerPort)
5762
tmconfig.APIConfig.Set(httpserver.HTTPConfAddress, "127.0.0.1")
5863

64+
if withMetrics {
65+
tmconfig.MetricsConfig.Set(httpserver.HTTPConfPort, 6010)
66+
tmconfig.MetricsConfig.Set(httpserver.HTTPConfAddress, "127.0.0.1")
67+
}
68+
5969
config.Set(tmconfig.PolicyLoopInterval, "1ns")
6070
tmconfig.PolicyEngineBaseConfig.SubSection("simple").Set(simple.FixedGasPrice, "223344556677")
6171

@@ -64,7 +74,7 @@ func testManagerCommonInit(t *testing.T) string {
6474

6575
func newTestManager(t *testing.T) (string, *manager, func()) {
6676

67-
url := testManagerCommonInit(t)
77+
url := testManagerCommonInit(t, false)
6878

6979
dir, err := ioutil.TempDir("", "ldb_*")
7080
assert.NoError(t, err)
@@ -86,12 +96,37 @@ func newTestManager(t *testing.T) (string, *manager, func()) {
8696
m.Close()
8797
os.RemoveAll(dir)
8898
}
99+
}
100+
101+
func newTestManagerWithMetrics(t *testing.T) (string, *manager, func()) {
102+
103+
url := testManagerCommonInit(t, true)
104+
105+
dir, err := ioutil.TempDir("", "ldb_*")
106+
assert.NoError(t, err)
107+
config.Set(tmconfig.PersistenceLevelDBPath, dir)
108+
109+
mca := &ffcapimocks.API{}
110+
mca.On("NewBlockListener", mock.Anything, mock.Anything).Return(nil, ffcapi.ErrorReason(""), nil).Maybe()
111+
mm, err := NewManager(context.Background(), mca)
112+
assert.NoError(t, err)
113+
114+
m := mm.(*manager)
115+
mcm := &confirmationsmocks.Manager{}
116+
mcm.On("Start").Return().Maybe()
117+
m.confirmations = mcm
89118

119+
return url,
120+
m,
121+
func() {
122+
m.Close()
123+
os.RemoveAll(dir)
124+
}
90125
}
91126

92127
func newTestManagerMockPersistence(t *testing.T) (string, *manager, func()) {
93128

94-
url := testManagerCommonInit(t)
129+
url := testManagerCommonInit(t, false)
95130

96131
m := newManager(context.Background(), &ffcapimocks.API{})
97132
mp := &persistencemocks.Persistence{}
@@ -115,6 +150,7 @@ func TestNewManagerBadHttpConfig(t *testing.T) {
115150
tmconfig.PolicyEngineBaseConfig.SubSection("simple").Set(simple.FixedGasPrice, "223344556677")
116151

117152
_, err := NewManager(context.Background(), nil)
153+
assert.Error(t, err)
118154
assert.Regexp(t, "FF00151", err)
119155

120156
}
@@ -167,7 +203,29 @@ func TestNewManagerMetricsOffByDefault(t *testing.T) {
167203

168204
m := newManager(context.Background(), nil)
169205
assert.False(t, m.metricsEnabled)
206+
}
207+
208+
func TestNewManagerWithMetrics(t *testing.T) {
209+
210+
_, m, close := newTestManagerWithMetrics(t)
211+
defer close()
212+
_ = m.Start()
170213

214+
assert.True(t, m.metricsEnabled)
215+
}
216+
217+
func TestNewManagerWithMetricsBadConfig(t *testing.T) {
218+
219+
tmconfig.Reset()
220+
tmconfig.MetricsConfig.Set("enabled", true)
221+
tmconfig.MetricsConfig.Set(httpserver.HTTPConfAddress, "::::")
222+
223+
policyengines.RegisterEngine(&simple.PolicyEngineFactory{})
224+
tmconfig.PolicyEngineBaseConfig.SubSection("simple").Set(simple.FixedGasPrice, "223344556677")
225+
226+
_, err := NewManager(context.Background(), nil)
227+
assert.Error(t, err)
228+
assert.Regexp(t, "FF00151", err)
171229
}
172230

173231
func TestAddHistoryEntryMax(t *testing.T) {

pkg/fftm/policyloop_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2022 Kaleido, Inc.
1+
// Copyright © 2023 Kaleido, Inc.
22
//
33
// SPDX-License-Identifier: Apache-2.0
44
//
@@ -315,7 +315,7 @@ func TestPolicyLoopUpdateFail(t *testing.T) {
315315

316316
func TestPolicyEngineFailStaleThenUpdated(t *testing.T) {
317317

318-
_, m, cancel := newTestManager(t)
318+
_, m, cancel := newTestManagerWithMetrics(t)
319319
defer cancel()
320320
m.policyLoopInterval = 1 * time.Hour
321321

@@ -346,6 +346,8 @@ func TestPolicyEngineFailStaleThenUpdated(t *testing.T) {
346346

347347
<-done2
348348

349+
m.Close()
350+
349351
mpe.AssertExpectations(t)
350352

351353
}

0 commit comments

Comments
 (0)