Skip to content

Commit 2348994

Browse files
committed
Add table conversion smoke test
1 parent e7bfff1 commit 2348994

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

apiserver/organization/table.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ import (
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212
"k8s.io/apimachinery/pkg/runtime"
1313
"k8s.io/apimachinery/pkg/util/duration"
14+
"k8s.io/apiserver/pkg/registry/rest"
1415
)
1516

17+
var _ rest.TableConvertor = &organizationStorage{}
18+
19+
// ConvertToTable translates the given object to a table for kubectl printing
1620
func (s *organizationStorage) ConvertToTable(ctx context.Context, obj runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
1721
var table metav1.Table
1822

apiserver/organization/table_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package organization
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
10+
orgv1 "github.com/appuio/control-api/apis/organization/v1"
11+
12+
corev1 "k8s.io/api/core/v1"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
"k8s.io/apimachinery/pkg/runtime"
15+
)
16+
17+
func TestOrganizationStorage_ConvertToTable(t *testing.T) {
18+
tests := map[string]struct {
19+
obj runtime.Object
20+
tableOptions runtime.Object
21+
fail bool
22+
nrRows int
23+
}{
24+
"GivenEmptyOrg_ThenSingleRow": {
25+
obj: &orgv1.Organization{},
26+
nrRows: 1,
27+
},
28+
"GivenOrg_ThenSingleRow": {
29+
obj: &orgv1.Organization{
30+
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
31+
Spec: orgv1.OrganizationSpec{
32+
DisplayName: "bar",
33+
},
34+
},
35+
nrRows: 1,
36+
},
37+
"GivenOrgList_ThenMultipleRow": {
38+
obj: &orgv1.OrganizationList{
39+
Items: []orgv1.Organization{
40+
{},
41+
{},
42+
{},
43+
},
44+
},
45+
nrRows: 3,
46+
},
47+
"GivenNil_ThenFail": {
48+
obj: nil,
49+
fail: true,
50+
},
51+
"GivenNonOrg_ThenFail": {
52+
obj: &corev1.Pod{},
53+
fail: true,
54+
},
55+
"GivenNonOrgList_ThenFail": {
56+
obj: &corev1.PodList{},
57+
fail: true,
58+
},
59+
}
60+
orgStore := &organizationStorage{}
61+
for name, tc := range tests {
62+
t.Run(name, func(t *testing.T) {
63+
table, err := orgStore.ConvertToTable(context.TODO(), tc.obj, tc.tableOptions)
64+
if tc.fail {
65+
require.Error(t, err)
66+
return
67+
}
68+
require.NoError(t, err)
69+
assert.Len(t, table.Rows, tc.nrRows)
70+
})
71+
}
72+
}

0 commit comments

Comments
 (0)