Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kidswiss committed Jun 23, 2023
1 parent d50da91 commit 5b91a8a
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 16 deletions.
8 changes: 8 additions & 0 deletions pkg/apiserver/vshn/redis/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ func (v vshnRedisBackupStorage) Destroy() {}
func (v *vshnRedisBackupStorage) NamespaceScoped() bool {
return true
}

func trimStringLenght(in string) string {
length := len(in)
if length > 8 {
length = 8
}
return in[:length]
}
9 changes: 2 additions & 7 deletions pkg/apiserver/vshn/redis/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,10 @@ func (v *vshnRedisBackupStorage) Get(ctx context.Context, name string, opts *met
}
return nil, err
}
name := *snap.Spec.ID
length := len(name)
if length > 8 {
length = 8
}
name = name[:length]

redisSnap = &appcatv1.VSHNRedisBackup{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Name: trimStringLenght(*snap.Spec.ID),
},
Status: appcatv1.VSHNRedisBackupStatus{
ID: *snap.Spec.ID,
Expand Down
3 changes: 3 additions & 0 deletions pkg/apiserver/vshn/redis/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func Test_vshnRedisBackupStorage_Get(t *testing.T) {
wantDate: metav1.Date(2023, time.April, 3, 13, 37, 0, 0, time.UTC),
wantNs: "ns1",
want: &appcatv1.VSHNRedisBackup{
ObjectMeta: metav1.ObjectMeta{
Name: "myid",
},
Status: appcatv1.VSHNRedisBackupStatus{
ID: "myid",
Instance: "myinstance",
Expand Down
9 changes: 1 addition & 8 deletions pkg/apiserver/vshn/redis/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,9 @@ func (v *vshnRedisBackupStorage) List(ctx context.Context, options *metainternal

for _, snap := range snapshots.Items {

name := *snap.Spec.ID
length := len(name)
if length > 8 {
length = 8
}
name = name[:8]

redisSnapshots.Items = append(redisSnapshots.Items, appcatv1.VSHNRedisBackup{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Name: trimStringLenght(*snap.Spec.ID),
},
Status: appcatv1.VSHNRedisBackupStatus{
ID: *snap.Spec.ID,
Expand Down
29 changes: 29 additions & 0 deletions pkg/apiserver/vshn/redis/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,19 @@ func Test_vshnRedisBackupStorage_List(t *testing.T) {
want: &appcatv1.VSHNRedisBackupList{
Items: []appcatv1.VSHNRedisBackup{
{
ObjectMeta: metav1.ObjectMeta{
Name: "snap1",
},
Status: appcatv1.VSHNRedisBackupStatus{
ID: "snap1",
Instance: "myinstance",
Date: metav1.Date(2023, time.April, 3, 13, 37, 0, 0, time.UTC),
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "snap2",
},
Status: appcatv1.VSHNRedisBackupStatus{
ID: "snap2",
Instance: "mysecondinstance",
Expand All @@ -93,6 +99,29 @@ func Test_vshnRedisBackupStorage_List(t *testing.T) {
},
},
},
{
name: "GivenNobackups_ThenExpectEmptyList",
instances: &vshnv1.VSHNRedisList{
Items: []vshnv1.VSHNRedis{
{
ObjectMeta: metav1.ObjectMeta{
Name: "myinstance",
Namespace: "ns1",
},
Status: vshnv1.VSHNRedisStatus{
InstanceNamespace: "ins1",
},
},
},
},
snapshots: &k8upv1.SnapshotList{
Items: []k8upv1.Snapshot{},
},
wantNs: "ns1",
want: &appcatv1.VSHNRedisBackupList{
Items: []appcatv1.VSHNRedisBackup{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/apiserver/vshn/redis/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ func (v *vshnRedisBackupStorage) ConvertToTable(_ context.Context, obj runtime.O
}

func backupToTableRow(backup *appcatv1.VSHNRedisBackup) metav1.TableRow {

return metav1.TableRow{
Cells: []interface{}{
backup.Status.ID[:8],
trimStringLenght(backup.Status.ID),
backup.Status.Instance,
backup.Status.Date},
Object: runtime.RawExtension{Object: backup},
Expand Down
65 changes: 65 additions & 0 deletions pkg/apiserver/vshn/redis/table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package redis

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
vshnv1 "github.com/vshn/appcat/apis/appcat/v1"
"k8s.io/apimachinery/pkg/runtime"
)

func Test_vshnRedisBackupStorage_ConvertToTable(t *testing.T) {
tests := []struct {
name string
obj runtime.Object
tableOptions runtime.Object
wantRows int
wantErr bool
}{
{
name: "GivenOneBackup_ThenExpectOneRow",
obj: &vshnv1.VSHNRedisBackup{},
wantRows: 1,
},
{
name: "GivenMiltipleBackups_ThenExpectMultipleRows",
obj: &vshnv1.VSHNRedisBackupList{
Items: []vshnv1.VSHNRedisBackup{
{},
{},
{},
},
},
wantRows: 3,
},
{
name: "GivenNoBackupObject_ThenExpectError",
obj: &vshnv1.AppCat{},
wantErr: true,
},
{
name: "GivenNil_ThenExpectError",
obj: nil,
wantErr: true,
},
{
name: "GivenNonBackupList_THenExpectError",
obj: &vshnv1.VSHNPostgresBackupList{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := &vshnRedisBackupStorage{}
got, err := v.ConvertToTable(context.TODO(), tt.obj, tt.tableOptions)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)

assert.Len(t, got.Rows, tt.wantRows)
})
}
}

0 comments on commit 5b91a8a

Please sign in to comment.