Skip to content

Commit

Permalink
fix zrouter (#85)
Browse files Browse the repository at this point in the history
* fix zrouter

* tests
  • Loading branch information
lucaslopezf authored Mar 8, 2024
1 parent 73f82ab commit 0d92218
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
11 changes: 8 additions & 3 deletions pkg/zrouter/zrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package zrouter
import (
"context"
"github.com/go-chi/chi/v5"
"github.com/zondax/golem/internal/version"
"github.com/zondax/golem/pkg/logger"
"github.com/zondax/golem/pkg/metrics"
"github.com/zondax/golem/pkg/metrics/collectors"
Expand All @@ -26,6 +25,8 @@ type Config struct {
WriteTimeOut time.Duration
Logger *logger.Logger
EnableRequestID bool
AppVersion string
AppRevision string
}

func (c *Config) setDefaultValues() {
Expand Down Expand Up @@ -84,6 +85,10 @@ func New(metricsServer metrics.TaskMetrics, config *Config) ZRouter {
config = &Config{}
}

if config.AppVersion == "" || config.AppRevision == "" {
panic("appVersion and appRevision are mandatory.")
}

config.setDefaultValues()
zr := &zrouter{
router: chi.NewRouter(),
Expand Down Expand Up @@ -144,15 +149,15 @@ func (r *zrouter) Run(addr ...string) error {
panic(err)
}

if err := r.metricsServer.UpdateMetric(appVersionMetric, 1, version.GitVersion); err != nil {
if err := r.metricsServer.UpdateMetric(appVersionMetric, 1, r.config.AppVersion); err != nil {
panic(err)
}

if err := r.metricsServer.RegisterMetric(appRevisionMetric, "Current revision of the application", []string{appRevisionMetric}, &collectors.Gauge{}); err != nil {
panic(err)
}

if err := r.metricsServer.UpdateMetric(appRevisionMetric, 1, version.GitRevision); err != nil {
if err := r.metricsServer.UpdateMetric(appRevisionMetric, 1, r.config.AppRevision); err != nil {
panic(err)
}

Expand Down
25 changes: 23 additions & 2 deletions pkg/zrouter/zrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ type ZRouterSuite struct {
}

func (suite *ZRouterSuite) SetupTest() {
suite.router = New(nil, nil)
suite.router = New(nil, &Config{AppVersion: "app_version", AppRevision: "app_revision"})
logger.InitLogger(logger.Config{})
}

func (suite *ZRouterSuite) TestRegisterAndGetRoutes() {

suite.router.GET("/get", func(ctx Context) (domain.ServiceResponse, error) {
return domain.NewServiceResponse(http.StatusOK, []byte("GET OK")), nil
})
Expand Down Expand Up @@ -54,3 +53,25 @@ func (suite *ZRouterSuite) TestRouteHandling() {
func TestZRouterSuite(t *testing.T) {
suite.Run(t, new(ZRouterSuite))
}

func TestValidateAppVersionAndRevision(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Errorf("The code did not panic")
return
}
errorMessage, ok := r.(string)
if !ok {
t.Errorf("Expected panic with a string message but got %T", r)
return
}

expectedMessage := "appVersion and appRevision are mandatory."
if errorMessage != expectedMessage {
t.Errorf("Expected panic with message %q but got %q", expectedMessage, errorMessage)
}
}()

New(nil, nil)
}

0 comments on commit 0d92218

Please sign in to comment.