Skip to content

Commit

Permalink
Introduce SonarCloud with code coverage (#131)
Browse files Browse the repository at this point in the history
* Introduce SonarCloud with code coverage

* Remove verbose output
  • Loading branch information
Stefan-Ethernal authored Jul 30, 2024
1 parent 95faf88 commit 3039179
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 73 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,33 @@
- name: Test
run: make test
working-directory: ./


- name: Archive code coverage results
uses: actions/upload-artifact@v3
with:
name: code-coverage-report
path: coverage.out

sonar-cloud:
needs: test
name: SonarCloud
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
submodules: recursive
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis

- name: Download code coverage results
uses: actions/download-artifact@v3
with:
name: code-coverage-report

- name: SonarCloud analysis
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
11 changes: 6 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
FROM golang:1.21 AS build

# INSTALL DEPENDENCIES
RUN go install github.com/gobuffalo/packr/v2/packr2@v2.8.3
COPY go.mod go.sum /src/
RUN cd /src && go mod download
WORKDIR /src
RUN go mod download

# BUILD BINARY
COPY relay /src/relay
COPY datastreamer /src/datastreamer
COPY log /src/log
COPY Makefile version.go config/environments/testnet/config.toml /src/
RUN cd /src && make build-dsrelay

RUN make build-dsrelay

# CONTAINER FOR RUNNING BINARY
FROM alpine:3.19.0
Expand All @@ -20,10 +21,10 @@ COPY --from=build /src/dist/dsrelay /app/dsrelay
COPY --from=build /src/config.toml /app/sample.config.toml

ARG USER=dsrelay
ENV HOME /home/$USER
ENV HOME=/home/$USER
RUN adduser -D $USER
USER $USER
WORKDIR $HOME

EXPOSE 7900
CMD ["/app/dsrelay"]
CMD ["/bin/sh", "-c", "/app/dsrelay"]
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ check-docker:
@which docker > /dev/null || (echo "Error: docker is not installed" && exit 1)

# Targets that require the checks
run-server: check-go
build-dsapp: check-go
build-dsrelay: check-go
build-docker: check-docker
Expand Down Expand Up @@ -45,7 +44,7 @@ build-docker-nc: ## Builds a docker image with datastream relay binary but witho

.PHONY: test
test:
go test -count=1 -short -race -p 1 -timeout 60s ./...
go test -coverprofile coverage.out -count=1 -short -race -p 1 -timeout 60s ./...

## Help display.
## Pulls comments from beside commands and prints a nicely formatted
Expand All @@ -57,3 +56,4 @@ help: ## Prints this help
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
| sort \
| awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

18 changes: 10 additions & 8 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ const (
streamerSystemID = 137
streamerVersion = 1

noneType = "none"
noneType = "none"
streamServerURL = "127.0.0.1:6900"
logLevelInfo = "log level (debug|info|warn|error)"
)

var (
Expand Down Expand Up @@ -83,7 +85,7 @@ func main() {
},
&cli.StringFlag{
Name: "log",
Usage: "log level (debug|info|warn|error)",
Usage: logLevelInfo,
Value: "info",
DefaultText: "info",
},
Expand Down Expand Up @@ -122,8 +124,8 @@ func main() {
&cli.StringFlag{
Name: "server",
Usage: "datastream server address to connect (IP:port)",
Value: "127.0.0.1:6900",
DefaultText: "127.0.0.1:6900",
Value: streamServerURL,
DefaultText: streamServerURL,
},
&cli.StringFlag{
Name: "from",
Expand Down Expand Up @@ -168,7 +170,7 @@ func main() {
},
&cli.StringFlag{
Name: "log",
Usage: "log level (debug|info|warn|error)",
Usage: logLevelInfo,
Value: "info",
DefaultText: "info",
},
Expand All @@ -183,8 +185,8 @@ func main() {
&cli.StringFlag{
Name: "server",
Usage: "datastream server address to connect (IP:port)",
Value: "127.0.0.1:6900",
DefaultText: "127.0.0.1:6900",
Value: streamServerURL,
DefaultText: streamServerURL,
},
&cli.Uint64Flag{
Name: "port",
Expand All @@ -200,7 +202,7 @@ func main() {
},
&cli.StringFlag{
Name: "log",
Usage: "log level (debug|info|warn|error)",
Usage: logLevelInfo,
Value: "info",
DefaultText: "info",
},
Expand Down
137 changes: 86 additions & 51 deletions datastreamer/streamserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,69 +765,25 @@ func (s *StreamServer) processCommand(command Command, client *client) error {

// Manage each different kind of command request from a client
var err error

switch command {
case CmdStart:
if cli.status != csStopped {
log.Error("Stream to client already started!")
err = ErrClientAlreadyStarted
_ = s.sendResultEntry(uint32(CmdErrAlreadyStarted), StrCommandErrors[CmdErrAlreadyStarted], client)
} else {
cli.status = csSyncing
err = s.processCmdStart(client)
if err == nil {
cli.status = csSynced
}
}
err = s.handleStartCommand(cli)

case CmdStartBookmark:
if cli.status != csStopped {
log.Error("Stream to client already started!")
err = ErrClientAlreadyStarted
_ = s.sendResultEntry(uint32(CmdErrAlreadyStarted), StrCommandErrors[CmdErrAlreadyStarted], client)
} else {
cli.status = csSyncing
err = s.processCmdStartBookmark(client)
if err == nil {
cli.status = csSynced
}
}
err = s.handleStartBookmarkCommand(cli)

case CmdStop:
if cli.status != csSynced {
log.Error("Stream to client already stopped!")
err = ErrClientAlreadyStopped
_ = s.sendResultEntry(uint32(CmdErrAlreadyStopped), StrCommandErrors[CmdErrAlreadyStopped], client)
} else {
cli.status = csStopped
err = s.processCmdStop(client)
}
err = s.handleStopCommand(cli)

case CmdHeader:
if cli.status != csStopped {
log.Error("Header command not allowed, stream started!")
err = ErrHeaderCommandNotAllowed
_ = s.sendResultEntry(uint32(CmdErrAlreadyStarted), StrCommandErrors[CmdErrAlreadyStarted], client)
} else {
err = s.processCmdHeader(client)
}
err = s.handleHeaderCommand(cli)

case CmdEntry:
if cli.status != csStopped {
log.Error("Entry command not allowed, stream started!")
err = ErrEntryCommandNotAllowed
_ = s.sendResultEntry(uint32(CmdErrAlreadyStarted), StrCommandErrors[CmdErrAlreadyStarted], client)
} else {
err = s.processCmdEntry(client)
}
err = s.handleEntryCommand(cli)

case CmdBookmark:
if cli.status != csStopped {
log.Error("Bookmark command not allowed, stream started!")
err = ErrBookmarkCommandNotAllowed
_ = s.sendResultEntry(uint32(CmdErrAlreadyStarted), StrCommandErrors[CmdErrAlreadyStarted], client)
} else {
err = s.processCmdBookmark(client)
}
err = s.handleBookmarkCommand(cli)

default:
log.Error("Invalid command!")
Expand All @@ -838,6 +794,85 @@ func (s *StreamServer) processCommand(command Command, client *client) error {
return err
}

// handleStartCommand processes the CmdStart command
func (s *StreamServer) handleStartCommand(cli *client) error {
if cli.status != csStopped {
log.Error("Stream to client already started!")
_ = s.sendResultEntry(uint32(CmdErrAlreadyStarted), StrCommandErrors[CmdErrAlreadyStarted], cli)
return ErrClientAlreadyStarted
}

cli.status = csSyncing
err := s.processCmdStart(cli)
if err == nil {
cli.status = csSynced
}

return err
}

// handleStartBookmarkCommand processes the CmdStartBookmark command
func (s *StreamServer) handleStartBookmarkCommand(cli *client) error {
if cli.status != csStopped {
log.Error("Stream to client already started!")
_ = s.sendResultEntry(uint32(CmdErrAlreadyStarted), StrCommandErrors[CmdErrAlreadyStarted], cli)
return ErrClientAlreadyStarted
}

cli.status = csSyncing
err := s.processCmdStartBookmark(cli)
if err == nil {
cli.status = csSynced
}

return err
}

// handleStopCommand processes the CmdStop command
func (s *StreamServer) handleStopCommand(cli *client) error {
if cli.status != csSynced {
log.Error("Stream to client already stopped!")
_ = s.sendResultEntry(uint32(CmdErrAlreadyStopped), StrCommandErrors[CmdErrAlreadyStopped], cli)
return ErrClientAlreadyStopped
}

cli.status = csStopped
return s.processCmdStop(cli)
}

// handleHeaderCommand processes the CmdHeader command
func (s *StreamServer) handleHeaderCommand(cli *client) error {
if cli.status != csStopped {
log.Error("Header command not allowed, stream started!")
_ = s.sendResultEntry(uint32(CmdErrAlreadyStarted), StrCommandErrors[CmdErrAlreadyStarted], cli)
return ErrHeaderCommandNotAllowed
}

return s.processCmdHeader(cli)
}

// handleEntryCommand processes the CmdEntry command
func (s *StreamServer) handleEntryCommand(cli *client) error {
if cli.status != csStopped {
log.Error("Entry command not allowed, stream started!")
_ = s.sendResultEntry(uint32(CmdErrAlreadyStarted), StrCommandErrors[CmdErrAlreadyStarted], cli)
return ErrEntryCommandNotAllowed
}

return s.processCmdEntry(cli)
}

// handleBookmarkCommand processes the CmdBookmark command
func (s *StreamServer) handleBookmarkCommand(cli *client) error {
if cli.status != csStopped {
log.Error("Bookmark command not allowed, stream started!")
_ = s.sendResultEntry(uint32(CmdErrAlreadyStarted), StrCommandErrors[CmdErrAlreadyStarted], cli)
return ErrBookmarkCommandNotAllowed
}

return s.processCmdBookmark(cli)
}

// processCmdStart processes the TCP Start command from the clients
func (s *StreamServer) processCmdStart(client *client) error {
// Read from entry number parameter
Expand Down
14 changes: 7 additions & 7 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
)

func TestLogNotInitialized(t *testing.T) {
Info("Test log.Info", " value is ", 10)
Info("Test log.Info value is ", 10)
Infof("Test log.Infof %d", 10)
Infow("Test log.Infow", "value", 10)
Debugf("Test log.Debugf %d", 10)
Error("Test log.Error", " value is ", 10)
Error("Test log.Error value is ", 10)
Errorf("Test log.Errorf %d", 10)
Errorw("Test log.Errorw", "value", 10)
Errorw("Test log.Errorw value", 10)
Warnf("Test log.Warnf %d", 10)
Warnw("Test log.Warnw", "value", 10)
Warnw("Test log.Warnw value", 10)
}

func TestLog(t *testing.T) {
Expand All @@ -25,13 +25,13 @@ func TestLog(t *testing.T) {

Init(cfg)

Info("Test log.Info", " value is ", 10)
Info("Test log.Info value is ", 10)
Infof("Test log.Infof %d", 10)
Infow("Test log.Infow", "value", 10)
Debugf("Test log.Debugf %d", 10)
Error("Test log.Error", " value is ", 10)
Error("Test log.Error value is ", 10)
Errorf("Test log.Errorf %d", 10)
Errorw("Test log.Errorw", "value", 10)
Warnf("Test log.Warnf %d", 10)
Warnw("Test log.Warnw", "value", 10)
Warnw("Test log.Warnw value", 10)
}
29 changes: 29 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# =====================================================
# Standard properties
# =====================================================

sonar.projectKey=0xPolygon_zkevm-data-streamer
sonar.projectName=zkevm-data-streamer
sonar.organization=0xpolygon

sonar.sources=.
sonar.exclusions=**/*_test.go

sonar.tests=.
sonar.test.inclusions=**/*_test.go
sonar.issue.enforceSemantic=true

# =====================================================
# Meta-data for the project
# =====================================================

sonar.links.homepage=https://github.com/0xPolygon/zkevm-data-streamer
sonar.links.ci=https://github.com/0xPolygon/zkevm-data-streamer/actions
sonar.links.scm=https://github.com/0xPolygon/zkevm-data-streamer
sonar.links.issue=https://github.com/0xPolygon/zkevm-data-streamer/issues

# =====================================================
# Properties specific to Go
# =====================================================

sonar.go.coverage.reportPaths=coverage.out

0 comments on commit 3039179

Please sign in to comment.