Skip to content

Commit

Permalink
Move Health biz logic & update tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Marcantonio <[email protected]>
  • Loading branch information
lennysgarage committed Jun 27, 2024
1 parent 29a4994 commit 26f719c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 30 deletions.
13 changes: 13 additions & 0 deletions internal/biz/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package biz

type IsBackendAvaliableUsecase struct {
repo ZanzibarRepository
}

func NewIsBackendAvailableUsecase(repo ZanzibarRepository) *IsBackendAvaliableUsecase {
return &IsBackendAvaliableUsecase{repo: repo}
}

func (rc *IsBackendAvaliableUsecase) IsBackendAvailable() error {
return rc.repo.IsBackendAvailable()

Check failure on line 12 in internal/biz/health.go

View workflow job for this annotation

GitHub Actions / lint

rc.repo.IsBackendAvailable undefined (type ZanzibarRepository has no field or method IsBackendAvailable) (typecheck)

Check failure on line 12 in internal/biz/health.go

View workflow job for this annotation

GitHub Actions / lint

rc.repo.IsBackendAvailable undefined (type ZanzibarRepository has no field or method IsBackendAvailable)) (typecheck)

Check failure on line 12 in internal/biz/health.go

View workflow job for this annotation

GitHub Actions / lint

rc.repo.IsBackendAvailable undefined (type ZanzibarRepository has no field or method IsBackendAvailable)) (typecheck)

Check failure on line 12 in internal/biz/health.go

View workflow job for this annotation

GitHub Actions / Build & run tests

rc.repo.IsBackendAvailable undefined (type ZanzibarRepository has no field or method IsBackendAvailable)
}
12 changes: 0 additions & 12 deletions internal/biz/relationships.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,6 @@ func (rc *CheckUsecase) Check(ctx context.Context, check *v0.CheckRequest) (*v0.
return rc.repo.Check(ctx, check)
}

type IsBackendAvaliableUsecase struct {
repo ZanzibarRepository
}

func NewIsBackendAvailableUsecase(repo ZanzibarRepository) *IsBackendAvaliableUsecase {
return &IsBackendAvaliableUsecase{repo: repo}
}

func (rc *IsBackendAvaliableUsecase) IsBackendAvailable() error {
return rc.repo.IsBackendAvaliable()
}

type CreateRelationshipsUsecase struct {
repo ZanzibarRepository
log *log.Helper
Expand Down
20 changes: 3 additions & 17 deletions internal/data/spicedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,6 @@ func NewSpiceDbRepository(c *conf.Data, logger log.Logger) (*SpiceDbRepository,
return nil, nil, fmt.Errorf("error creating spicedb client: %w", err)
}

// Create health client for readyz
conn, err := grpc.NewClient(
c.SpiceDb.Endpoint,
opts...,
)
if err != nil {
return nil, nil, fmt.Errorf("error creating grpc health client: %w", err)
}
healthClient := grpc_health_v1.NewHealthClient(conn)
_, err = client.ReadSchema(context.TODO(), &v1.ReadSchemaRequest{})
if err != nil {
return nil, nil, fmt.Errorf("error testing connection to SpiceDB: %w", err)
}

// Create health client for readyz
conn, err := grpc.NewClient(
c.SpiceDb.Endpoint,
Expand Down Expand Up @@ -357,7 +343,7 @@ func (s *SpiceDbRepository) Check(ctx context.Context, check *apiV0.CheckRequest
return &apiV0.CheckResponse{Allowed: apiV0.CheckResponse_ALLOWED_FALSE}, nil
}

func (s *SpiceDbRepository) IsBackendAvaliable() error {
func (s *SpiceDbRepository) IsBackendAvailable() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

Expand All @@ -368,11 +354,11 @@ func (s *SpiceDbRepository) IsBackendAvaliable() error {

switch resp.Status {
case grpc_health_v1.HealthCheckResponse_NOT_SERVING, grpc_health_v1.HealthCheckResponse_SERVICE_UNKNOWN:
return err
return fmt.Errorf("error connecting to backend: %v", resp.Status.Descriptor())
case grpc_health_v1.HealthCheckResponse_SERVING:
return nil
}
return err
return fmt.Errorf("error connecting to backend")
}

func createSpiceDbRelationshipFilter(filter *apiV0.RelationTupleFilter) *v1.RelationshipFilter {
Expand Down
18 changes: 17 additions & 1 deletion internal/data/spicedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

apiV0 "github.com/project-kessel/relations-api/api/relations/v0"
"github.com/project-kessel/relations-api/internal/biz"
"github.com/project-kessel/relations-api/internal/conf"

"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/tracing"
Expand Down Expand Up @@ -124,10 +125,25 @@ func TestIsBackendAvailable(t *testing.T) {
spiceDbrepo, err := container.CreateSpiceDbRepository()
assert.NoError(t, err)

err = spiceDbrepo.IsBackendAvaliable()
err = spiceDbrepo.IsBackendAvailable()
assert.NoError(t, err)
}

func TestIsBackendUnavailable(t *testing.T) {
t.Parallel()

spiceDBRepo, _, err := NewSpiceDbRepository(&conf.Data{
SpiceDb: &conf.Data_SpiceDb{
Endpoint: "-1",
Token: "foobar",
UseTLS: true,
}}, log.GetLogger())
assert.NoError(t, err)

err = spiceDBRepo.IsBackendAvailable()
assert.Error(t, err)
}

func TestCreateRelationshipFailsWithBadSubjectType(t *testing.T) {
t.Parallel()

Expand Down
26 changes: 26 additions & 0 deletions internal/service/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"fmt"
"testing"

pb "github.com/project-kessel/relations-api/api/health/v1"
Expand Down Expand Up @@ -39,6 +40,31 @@ func TestHealthService_GetReadyz_SpiceDBAvailable(t *testing.T) {
assert.Equal(t, resp, &pb.GetReadyzReply{Status: "OK", Code: 200})
}

func TestHealthService_GetReadyz_SpiceDBUnavailable(t *testing.T) {
t.Parallel()

ctx := context.TODO()

d := &DummyZanzibar{}
service := createDummyHealthService(d)
resp, err := service.GetReadyz(ctx, &pb.GetReadyzRequest{})

assert.NoError(t, err)
assert.Equal(t, resp, &pb.GetReadyzReply{Status: "Unavailable", Code: 503})
}

type DummyZanzibar struct {
biz.ZanzibarRepository
}

func (dz *DummyZanzibar) IsBackendAvailable() error {
return fmt.Errorf("Unavailable")
}

func createDummyHealthService(d *DummyZanzibar) *HealthService {
return NewHealthService(biz.NewIsBackendAvailableUsecase(d))
}

func createHealthService(spicedb *data.SpiceDbRepository) *HealthService {
return NewHealthService(biz.NewIsBackendAvailableUsecase(spicedb))
}

0 comments on commit 26f719c

Please sign in to comment.