Skip to content

Commit 973aac3

Browse files
tac0turtletac0turtlecoderabbitai[bot]
authored
chore: increase test coverage (#2349)
<!-- Please read and fill out this form before submitting your PR. Please make sure you have reviewed our contributors guide before submitting your first PR. NOTE: PR titles should follow semantic commits: https://www.conventionalcommits.org/en/v1.0.0/ --> ## Overview This pr adds some error case testing in order to meet the minimum of 80% code coverage ref #2256 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Tests** - Added extensive new tests to improve error handling verification in the store and node components. - Introduced tests for instrumentation server startup and validation. - Enhanced test configuration to explicitly initialize instrumentation settings. - **Chores** - Updated configuration to exclude additional directories from code coverage reporting. - **Refactor** - Simplified process exit logic by removing internal helper functions. - **Bug Fixes** - Clarified error messages related to state serialization to accurately reflect protobuf usage. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: tac0turtle <you@example.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 1604652 commit 973aac3

6 files changed

Lines changed: 466 additions & 21 deletions

File tree

codecov.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ ignore:
1111
- "types/pb"
1212
- "scripts/"
1313
- "pkg/rpc/example/"
14+
- "apps"
1415
- "da/internal/mocks"
16+
- "da/cmd"
17+
- "execution/evm" # EVM is covered in e2e/integration tests

node/full_node_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package node
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
"testing"
9+
"time"
10+
11+
"cosmossdk.io/log"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
15+
"github.com/rollkit/rollkit/pkg/service"
16+
)
17+
18+
func TestStartInstrumentationServer(t *testing.T) {
19+
t.Parallel()
20+
require := require.New(t)
21+
assert := assert.New(t)
22+
23+
var config = getTestConfig(t, 1)
24+
config.Instrumentation.Prometheus = true
25+
config.Instrumentation.PrometheusListenAddr = "127.0.0.1:26660"
26+
config.Instrumentation.Pprof = true
27+
config.Instrumentation.PprofListenAddr = "127.0.0.1:26661"
28+
29+
node := &FullNode{
30+
nodeConfig: config,
31+
BaseService: *service.NewBaseService(log.NewTestLogger(t), "TestNode", nil),
32+
}
33+
34+
prometheusSrv, pprofSrv := node.startInstrumentationServer()
35+
36+
require.NotNil(prometheusSrv, "Prometheus server should be initialized")
37+
require.NotNil(pprofSrv, "Pprof server should be initialized")
38+
39+
time.Sleep(100 * time.Millisecond)
40+
41+
resp, err := http.Get(fmt.Sprintf("http://%s/metrics", config.Instrumentation.PrometheusListenAddr))
42+
require.NoError(err, "Failed to get Prometheus metrics")
43+
defer func() {
44+
err := resp.Body.Close()
45+
if err != nil {
46+
t.Logf("Error closing response body: %v", err)
47+
}
48+
}()
49+
assert.Equal(http.StatusOK, resp.StatusCode, "Prometheus metrics endpoint should return 200 OK")
50+
body, err := io.ReadAll(resp.Body)
51+
require.NoError(err)
52+
assert.Contains(string(body), "# HELP", "Prometheus metrics body should contain HELP lines") // Check for typical metrics content
53+
54+
resp, err = http.Get(fmt.Sprintf("http://%s/debug/pprof/", config.Instrumentation.PprofListenAddr))
55+
require.NoError(err, "Failed to get Pprof index")
56+
defer func() {
57+
err := resp.Body.Close()
58+
if err != nil {
59+
t.Logf("Error closing response body: %v", err)
60+
}
61+
}()
62+
assert.Equal(http.StatusOK, resp.StatusCode, "Pprof index endpoint should return 200 OK")
63+
body, err = io.ReadAll(resp.Body)
64+
require.NoError(err)
65+
assert.Contains(string(body), "Types of profiles available", "Pprof index body should contain expected text")
66+
67+
shutdownCtx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
68+
defer cancel()
69+
70+
if prometheusSrv != nil {
71+
err = prometheusSrv.Shutdown(shutdownCtx)
72+
assert.NoError(err, "Prometheus server shutdown should not return error")
73+
}
74+
if pprofSrv != nil {
75+
err = pprofSrv.Shutdown(shutdownCtx)
76+
assert.NoError(err, "Pprof server shutdown should not return error")
77+
}
78+
}

node/helpers_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ func getTestConfig(t *testing.T, n int) rollkitconfig.Config {
8888
RPC: rollkitconfig.RPCConfig{
8989
Address: fmt.Sprintf("127.0.0.1:%d", 8000+n),
9090
},
91-
ChainID: "test-chain",
91+
ChainID: "test-chain",
92+
Instrumentation: &rollkitconfig.InstrumentationConfig{},
9293
}
9394
}
9495

pkg/os/os.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,6 @@ func TrapSignal(logger logger, cb func()) {
2929
}()
3030
}
3131

32-
// Kill the running process by sending itself SIGTERM.
33-
func Kill() error {
34-
p, err := os.FindProcess(os.Getpid())
35-
if err != nil {
36-
return err
37-
}
38-
return p.Signal(syscall.SIGTERM)
39-
}
40-
41-
// Exit exits the program with a message and a status code of 1.
42-
func Exit(s string) {
43-
fmt.Println(s)
44-
os.Exit(1)
45-
}
46-
4732
// EnsureDir ensures the given directory exists, creating it if necessary.
4833
// Errors if the path already exists as a non-directory.
4934
func EnsureDir(dir string, mode os.FileMode) error {
@@ -69,7 +54,8 @@ func ReadFile(filePath string) ([]byte, error) {
6954
func MustReadFile(filePath string) []byte {
7055
fileBytes, err := os.ReadFile(filePath) //nolint:gosec
7156
if err != nil {
72-
Exit(fmt.Sprintf("MustReadFile failed: %v", err))
57+
fmt.Printf("MustReadFile failed: %v", err)
58+
os.Exit(1)
7359
return nil
7460
}
7561
return fileBytes
@@ -84,7 +70,8 @@ func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
8470
func MustWriteFile(filePath string, contents []byte, mode os.FileMode) {
8571
err := WriteFile(filePath, contents, mode)
8672
if err != nil {
87-
Exit(fmt.Sprintf("MustWriteFile failed: %v", err))
73+
fmt.Printf("MustWriteFile failed: %v", err)
74+
os.Exit(1)
8875
}
8976
}
9077

pkg/store/store.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ func (s *DefaultStore) GetSignature(ctx context.Context, height uint64) (*types.
184184
func (s *DefaultStore) UpdateState(ctx context.Context, state types.State) error {
185185
pbState, err := state.ToProto()
186186
if err != nil {
187-
return fmt.Errorf("failed to marshal state to JSON: %w", err)
187+
return fmt.Errorf("failed to convert type state to protobuf type: %w", err)
188188
}
189189
data, err := proto.Marshal(pbState)
190190
if err != nil {
191-
return err
191+
return fmt.Errorf("failed to marshal state to protobuf: %w", err)
192192
}
193193
return s.db.Put(ctx, ds.NewKey(getStateKey()), data)
194194
}
@@ -202,7 +202,7 @@ func (s *DefaultStore) GetState(ctx context.Context) (types.State, error) {
202202
var pbState pb.State
203203
err = proto.Unmarshal(blob, &pbState)
204204
if err != nil {
205-
return types.State{}, fmt.Errorf("failed to unmarshal state from JSON: %w", err)
205+
return types.State{}, fmt.Errorf("failed to unmarshal state from protobuf: %w", err)
206206
}
207207

208208
var state types.State

0 commit comments

Comments
 (0)