Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial generic rethinkdb store #539

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions cmd/metal-api/internal/datastore/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ type IPSearchQuery struct {
}

// GenerateTerm generates the project search query term.
func (p *IPSearchQuery) generateTerm(rs *RethinkStore) *r.Term {
q := *rs.ipTable()

func (p *IPSearchQuery) Query(q r.Term) *r.Term {
if p.IPAddress != nil {
q = q.Filter(func(row r.Term) r.Term {
return row.Field("id").Eq(*p.IPAddress)
Expand Down Expand Up @@ -94,7 +92,7 @@ func (rs *RethinkStore) FindIPByID(id string) (*metal.IP, error) {

// SearchIPs returns the result of the ips search request query.
func (rs *RethinkStore) SearchIPs(q *IPSearchQuery, ips *metal.IPs) error {
return rs.searchEntities(q.generateTerm(rs), ips)
return rs.searchEntities(q.Query(*rs.ipTable()), ips)
}

// ListIPs returns all ips.
Expand Down
56 changes: 48 additions & 8 deletions cmd/metal-api/internal/datastore/ip_test.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
package datastore

import (
"context"
"log/slog"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/generic-datastore"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/testdata"
"github.com/metal-stack/metal-lib/pkg/pointer"
)

func TestRethinkStore_FindIPByID(t *testing.T) {
ds, mock := InitMockDB(t)
testdata.InitMockDBData(mock)

is := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).IP()
tests := []struct {
name string
rs *RethinkStore
is generic.Storage[*metal.IP]
id string
want *metal.IP
wantErr bool
}{
{
name: "TestRethinkStore_FindIP Test 1",
rs: ds,
is: is,
id: "1.2.3.4",
want: &testdata.IP1,
wantErr: false,
},
{
name: "TestRethinkStore_FindIP Test 2",
rs: ds,
is: is,
id: "2.3.4.5",
want: &testdata.IP2,
wantErr: false,
Expand All @@ -37,7 +41,42 @@ func TestRethinkStore_FindIPByID(t *testing.T) {
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
got, err := tt.rs.FindIPByID(tt.id)
got, err := tt.is.Get(context.Background(), tt.id)
if (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.FindIP() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("RethinkStore.FindIP() mismatch (-want +got):\n%s", diff)
}
})
}
}

// FIXME needs proper mock to work
func TestRethinkStore_QueryIP(t *testing.T) {
ds, mock := InitMockDB(t)
testdata.InitMockDBData(mock)
is := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).IP()
tests := []struct {
name string
is generic.Storage[*metal.IP]
query generic.EntityQuery
want *metal.IP
wantErr bool
}{
{
name: "TestRethinkStore_FindIP Test 1",
is: is,
query: &IPSearchQuery{IPAddress: pointer.Pointer("1.2.3.4")},
want: &testdata.IP1,
wantErr: false,
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
got, err := tt.is.Find(context.Background(), tt.query)
if (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.FindIP() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -52,24 +91,25 @@ func TestRethinkStore_FindIPByID(t *testing.T) {
func TestRethinkStore_ListIPs(t *testing.T) {
ds, mock := InitMockDB(t)
testdata.InitMockDBData(mock)
is := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).IP()

tests := []struct {
name string
rs *RethinkStore
is generic.Storage[*metal.IP]
want metal.IPs
wantErr bool
}{
{
name: "TestRethinkStore_ListIPs Test 1",
rs: ds,
is: is,
want: testdata.TestIPs,
wantErr: false,
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
got, err := tt.rs.ListIPs()
got, err := tt.is.List(context.Background())
if (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.ListIPs() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
45 changes: 27 additions & 18 deletions cmd/metal-api/internal/datastore/partition_test.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
package datastore

import (
"context"
"log/slog"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/generic-datastore"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/testdata"
)

func TestRethinkStore_FindPartition(t *testing.T) {
ds, mock := InitMockDB(t)

ps := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).Partition()
testdata.InitMockDBData(mock)

tests := []struct {
name string
rs *RethinkStore
ps generic.Storage[*metal.Partition]
id string
want *metal.Partition
wantErr bool
}{
{
name: "Test 1",
rs: ds,
ps: ps,
id: "1",
want: &testdata.Partition1,
wantErr: false,
},
{
name: "Test 2",
rs: ds,
ps: ps,
id: "2",
want: &testdata.Partition2,
wantErr: false,
Expand All @@ -37,7 +42,7 @@ func TestRethinkStore_FindPartition(t *testing.T) {
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
got, err := tt.rs.FindPartition(tt.id)
got, err := tt.ps.Get(context.Background(), tt.id)
if (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.FindPartition() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -52,24 +57,25 @@ func TestRethinkStore_FindPartition(t *testing.T) {
func TestRethinkStore_ListPartitions(t *testing.T) {
ds, mock := InitMockDB(t)
testdata.InitMockDBData(mock)
ps := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).Partition()

tests := []struct {
name string
rs *RethinkStore
ps generic.Storage[*metal.Partition]
want metal.Partitions
wantErr bool
}{
{
name: "Test 1",
rs: ds,
ps: ps,
want: testdata.TestPartitions,
wantErr: false,
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
got, err := tt.rs.ListPartitions()
got, err := tt.ps.List(context.Background())
if (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.ListPartitions() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -84,24 +90,25 @@ func TestRethinkStore_ListPartitions(t *testing.T) {
func TestRethinkStore_CreatePartition(t *testing.T) {
ds, mock := InitMockDB(t)
testdata.InitMockDBData(mock)
ps := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).Partition()

tests := []struct {
name string
rs *RethinkStore
ps generic.Storage[*metal.Partition]
p *metal.Partition
wantErr bool
}{
{
name: "Test 1",
rs: ds,
ps: ps,
p: &testdata.Partition1,
wantErr: false,
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
if err := tt.rs.CreatePartition(tt.p); (err != nil) != tt.wantErr {
if err := tt.ps.Create(context.Background(), tt.p); (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.CreatePartition() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand All @@ -111,30 +118,31 @@ func TestRethinkStore_CreatePartition(t *testing.T) {
func TestRethinkStore_DeletePartition(t *testing.T) {
ds, mock := InitMockDB(t)
testdata.InitMockDBData(mock)
ps := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).Partition()

tests := []struct {
name string
rs *RethinkStore
ps generic.Storage[*metal.Partition]
p *metal.Partition
wantErr bool
}{
{
name: "Test 1",
rs: ds,
ps: ps,
p: &testdata.Partition1,
wantErr: false,
},
{
name: "Test 2",
rs: ds,
ps: ps,
p: &testdata.Partition2,
wantErr: false,
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
err := tt.rs.DeletePartition(tt.p)
err := tt.ps.Delete(context.Background(), tt.p)
if (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.DeletePartition() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -146,24 +154,25 @@ func TestRethinkStore_DeletePartition(t *testing.T) {
func TestRethinkStore_UpdatePartition(t *testing.T) {
ds, mock := InitMockDB(t)
testdata.InitMockDBData(mock)
ps := generic.New(slog.Default(), ds.DBName(), ds.QueryExecutor()).Partition()

tests := []struct {
name string
rs *RethinkStore
ps generic.Storage[*metal.Partition]
oldPartition *metal.Partition
newPartition *metal.Partition
wantErr bool
}{
{
name: "Test 1",
rs: ds,
ps: ps,
oldPartition: &testdata.Partition1,
newPartition: &testdata.Partition2,
wantErr: false,
},
{
name: "Test 2",
rs: ds,
ps: ps,
oldPartition: &testdata.Partition2,
newPartition: &testdata.Partition1,
wantErr: false,
Expand All @@ -172,7 +181,7 @@ func TestRethinkStore_UpdatePartition(t *testing.T) {
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
if err := tt.rs.UpdatePartition(tt.oldPartition, tt.newPartition); (err != nil) != tt.wantErr {
if err := tt.ps.Update(context.Background(), tt.oldPartition, tt.newPartition); (err != nil) != tt.wantErr {
t.Errorf("RethinkStore.UpdatePartition() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
8 changes: 8 additions & 0 deletions cmd/metal-api/internal/datastore/rethinkdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,14 @@ tryAgain:
return s
}

func (rs *RethinkStore) DBName() string {
return rs.dbname
majst01 marked this conversation as resolved.
Show resolved Hide resolved
}

func (rs *RethinkStore) QueryExecutor() r.QueryExecutor {
return rs.session
}

func (rs *RethinkStore) findEntityByID(table *r.Term, entity interface{}, id string) error {
res, err := table.Get(id).Run(rs.session)
if err != nil {
Expand Down
Loading
Loading