Skip to content

Commit

Permalink
More integration tests for datastore layer. (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Jul 2, 2024
1 parent 35b27c2 commit a01e40a
Show file tree
Hide file tree
Showing 4 changed files with 475 additions and 7 deletions.
35 changes: 29 additions & 6 deletions cmd/metal-api/internal/datastore/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package datastore

import (
"context"
"fmt"

"github.com/metal-stack/metal-lib/rest"
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
Expand All @@ -13,6 +14,21 @@ func (rs *RethinkStore) ServiceName() string {

// Check implements the health interface and tests if the database is healthy.
func (rs *RethinkStore) Check(ctx context.Context) (rest.HealthResult, error) {
var version string

returnStatus := func(err error) (rest.HealthResult, error) {
if err != nil {
return rest.HealthResult{
Status: rest.HealthStatusUnhealthy,
}, err
}

return rest.HealthResult{
Status: rest.HealthStatusHealthy,
Message: fmt.Sprintf("connected to rethinkdb version: %s", version),
}, nil
}

t := r.Branch(
rs.db().TableList().SetIntersection(r.Expr(tables)).Count().Eq(len(tables)),
r.Expr(true),
Expand All @@ -21,12 +37,19 @@ func (rs *RethinkStore) Check(ctx context.Context) (rest.HealthResult, error) {

err := t.Exec(rs.session, r.ExecOpts{Context: ctx})
if err != nil {
return rest.HealthResult{
Status: rest.HealthStatusUnhealthy,
}, err
return returnStatus(err)
}

return rest.HealthResult{
Status: rest.HealthStatusHealthy,
}, nil
cursor, err := r.DB("rethinkdb").Table("server_status").Field("process").Field("version").Run(rs.session)
if err != nil {
return returnStatus(err)
}

err = cursor.One(&version)
if err != nil {
return returnStatus(err)
}

return returnStatus(err)

}
19 changes: 19 additions & 0 deletions cmd/metal-api/internal/datastore/health_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build integration
// +build integration

package datastore

import (
"context"
"testing"

"github.com/metal-stack/metal-lib/rest"
"github.com/stretchr/testify/require"
)

func TestRethinkStore_Health(t *testing.T) {
result, err := sharedDS.Check(context.Background())
require.NoError(t, err)
require.Equal(t, rest.HealthStatusHealthy, result.Status)
require.Contains(t, result.Message, "connected to rethinkdb version: rethinkdb")
}
Loading

0 comments on commit a01e40a

Please sign in to comment.