Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 3d2b6b2

Browse files
committed
Merge pull request #20 from carmark/test
first round test cases fix
2 parents 8b81dd4 + a47f8ed commit 3d2b6b2

File tree

26 files changed

+269
-128
lines changed

26 files changed

+269
-128
lines changed

examples/examples_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func validateObject(obj runtime.Object) (errors []error) {
8686
errors = validation.ValidateEndpoints(t)
8787
case *api.Namespace:
8888
errors = validation.ValidateNamespace(t)
89+
case *api.Tenant:
90+
errors = validation.ValidateTenant(t)
8991
case *api.Secret:
9092
if t.Namespace == "" {
9193
t.Namespace = api.NamespaceDefault
@@ -219,6 +221,8 @@ func TestExampleObjectSchemas(t *testing.T) {
219221
},
220222
"../docs/user-guide": {
221223
"multi-pod": nil,
224+
"ns": &api.Namespace{},
225+
"te": &api.Tenant{},
222226
"pod": &api.Pod{},
223227
"replication": &api.ReplicationController{},
224228
"job": &experimental.Job{},

pkg/api/meta/meta_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestGenericTypeMeta(t *testing.T) {
2828
type TypeMeta struct {
2929
Kind string `json:"kind,omitempty"`
3030
Namespace string `json:"namespace,omitempty"`
31+
Tenant string `json:"tenant,omitempty"`
3132
Name string `json:"name,omitempty"`
3233
GenerateName string `json:"generateName,omitempty"`
3334
UID string `json:"uid,omitempty"`
@@ -44,6 +45,7 @@ func TestGenericTypeMeta(t *testing.T) {
4445
j := Object{
4546
TypeMeta{
4647
Namespace: "bar",
48+
Tenant: "hah",
4749
Name: "foo",
4850
GenerateName: "prefix",
4951
UID: "uid",
@@ -62,6 +64,9 @@ func TestGenericTypeMeta(t *testing.T) {
6264
if e, a := "bar", accessor.Namespace(); e != a {
6365
t.Errorf("expected %v, got %v", e, a)
6466
}
67+
if e, a := "hah", accessor.Tenant(); e != a {
68+
t.Errorf("expected %v, got %v", e, a)
69+
}
6570
if e, a := "foo", accessor.Name(); e != a {
6671
t.Errorf("expected %v, got %v", e, a)
6772
}
@@ -96,6 +101,7 @@ func TestGenericTypeMeta(t *testing.T) {
96101
}
97102

98103
accessor.SetNamespace("baz")
104+
accessor.SetTenant("uau")
99105
accessor.SetName("bar")
100106
accessor.SetGenerateName("generate")
101107
accessor.SetUID("other")
@@ -108,6 +114,9 @@ func TestGenericTypeMeta(t *testing.T) {
108114
if e, a := "baz", j.Namespace; e != a {
109115
t.Errorf("expected %v, got %v", e, a)
110116
}
117+
if e, a := "uau", j.Tenant; e != a {
118+
t.Errorf("expected %v, got %v", e, a)
119+
}
111120
if e, a := "bar", j.Name; e != a {
112121
t.Errorf("expected %v, got %v", e, a)
113122
}
@@ -143,6 +152,7 @@ func TestGenericTypeMeta(t *testing.T) {
143152
type InternalTypeMeta struct {
144153
Kind string `json:"kind,omitempty"`
145154
Namespace string `json:"namespace,omitempty"`
155+
Tenant string `json:"tenant,omitempty"`
146156
Name string `json:"name,omitempty"`
147157
GenerateName string `json:"generateName,omitempty"`
148158
UID string `json:"uid,omitempty"`
@@ -163,6 +173,7 @@ func TestGenericTypeMetaAccessor(t *testing.T) {
163173
j := &InternalObject{
164174
InternalTypeMeta{
165175
Namespace: "bar",
176+
Tenant: "hah",
166177
Name: "foo",
167178
GenerateName: "prefix",
168179
UID: "uid",
@@ -182,6 +193,13 @@ func TestGenericTypeMetaAccessor(t *testing.T) {
182193
if e, a := "bar", namespace; e != a {
183194
t.Errorf("expected %v, got %v", e, a)
184195
}
196+
tenant, err := accessor.Tenant(j)
197+
if err != nil {
198+
t.Errorf("unexpected error: %v", err)
199+
}
200+
if e, a := "hah", tenant; e != a {
201+
t.Errorf("expected %v, got %v", e, a)
202+
}
185203
name, err := accessor.Name(j)
186204
if err != nil {
187205
t.Errorf("unexpected error: %v", err)
@@ -249,6 +267,9 @@ func TestGenericTypeMetaAccessor(t *testing.T) {
249267
if err := accessor.SetNamespace(j, "baz"); err != nil {
250268
t.Errorf("unexpected error: %v", err)
251269
}
270+
if err := accessor.SetTenant(j, "uau"); err != nil {
271+
t.Errorf("unexpected error: %v", err)
272+
}
252273
if err := accessor.SetName(j, "bar"); err != nil {
253274
t.Errorf("unexpected error: %v", err)
254275
}
@@ -282,6 +303,9 @@ func TestGenericTypeMetaAccessor(t *testing.T) {
282303
if e, a := "baz", j.TypeMeta.Namespace; e != a {
283304
t.Errorf("expected %v, got %v", e, a)
284305
}
306+
if e, a := "uau", j.TypeMeta.Tenant; e != a {
307+
t.Errorf("expected %v, got %v", e, a)
308+
}
285309
if e, a := "bar", j.TypeMeta.Name; e != a {
286310
t.Errorf("expected %v, got %v", e, a)
287311
}
@@ -318,6 +342,7 @@ func TestGenericObjectMeta(t *testing.T) {
318342
}
319343
type ObjectMeta struct {
320344
Namespace string `json:"namespace,omitempty"`
345+
Tenant string `json:"tenant,omitempty"`
321346
Name string `json:"name,omitempty"`
322347
GenerateName string `json:"generateName,omitempty"`
323348
UID string `json:"uid,omitempty"`
@@ -338,6 +363,7 @@ func TestGenericObjectMeta(t *testing.T) {
338363
},
339364
ObjectMeta{
340365
Namespace: "bar",
366+
Tenant: "hah",
341367
Name: "foo",
342368
GenerateName: "prefix",
343369
UID: "uid",
@@ -354,6 +380,9 @@ func TestGenericObjectMeta(t *testing.T) {
354380
if e, a := "bar", accessor.Namespace(); e != a {
355381
t.Errorf("expected %v, got %v", e, a)
356382
}
383+
if e, a := "hah", accessor.Tenant(); e != a {
384+
t.Errorf("expected %v, got %v", e, a)
385+
}
357386
if e, a := "foo", accessor.Name(); e != a {
358387
t.Errorf("expected %v, got %v", e, a)
359388
}
@@ -383,6 +412,7 @@ func TestGenericObjectMeta(t *testing.T) {
383412
}
384413

385414
accessor.SetNamespace("baz")
415+
accessor.SetTenant("uau")
386416
accessor.SetName("bar")
387417
accessor.SetGenerateName("generate")
388418
accessor.SetUID("other")
@@ -397,6 +427,9 @@ func TestGenericObjectMeta(t *testing.T) {
397427
if e, a := "baz", j.Namespace; e != a {
398428
t.Errorf("expected %v, got %v", e, a)
399429
}
430+
if e, a := "uau", j.Tenant; e != a {
431+
t.Errorf("expected %v, got %v", e, a)
432+
}
400433
if e, a := "bar", j.Name; e != a {
401434
t.Errorf("expected %v, got %v", e, a)
402435
}

pkg/api/validation/validation.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,15 +1798,34 @@ func ValidateResourceQuotaStatusUpdate(newResourceQuota, oldResourceQuota *api.R
17981798

17991799
// fake functions for 'tenant' validatation
18001800
func ValidateTenant(tenant *api.Tenant) errs.ValidationErrorList {
1801-
return nil
1801+
allErrs := errs.ValidationErrorList{}
1802+
allErrs = append(allErrs, ValidateObjectMeta(&tenant.ObjectMeta, false, ValidateTenantName).Prefix("metadata")...)
1803+
1804+
return allErrs
18021805
}
18031806

18041807
func ValidateTenantUpdate(newTenant *api.Tenant, oldTenant *api.Tenant) errs.ValidationErrorList {
1805-
return nil
1808+
allErrs := errs.ValidationErrorList{}
1809+
allErrs = append(allErrs, ValidateObjectMetaUpdate(&newTenant.ObjectMeta, &oldTenant.ObjectMeta).Prefix("metadata")...)
1810+
newTenant.Spec = oldTenant.Spec
1811+
newTenant.Status = oldTenant.Status
1812+
return allErrs
18061813
}
18071814

18081815
func ValidateTenantStatusUpdate(newTenant, oldTenant *api.Tenant) errs.ValidationErrorList {
1809-
return nil
1816+
allErrs := errs.ValidationErrorList{}
1817+
allErrs = append(allErrs, ValidateObjectMetaUpdate(&newTenant.ObjectMeta, &oldTenant.ObjectMeta).Prefix("metadata")...)
1818+
newTenant.Spec = oldTenant.Spec
1819+
if newTenant.DeletionTimestamp.IsZero() {
1820+
if newTenant.Status.Phase != api.TenantActive {
1821+
allErrs = append(allErrs, errs.NewFieldInvalid("Status.Phase", newTenant.Status.Phase, "A tenant may only be in active status if it does not have a deletion timestamp."))
1822+
}
1823+
} else {
1824+
if newTenant.Status.Phase != api.TenantTerminating {
1825+
allErrs = append(allErrs, errs.NewFieldInvalid("Status.Phase", newTenant.Status.Phase, "A tenant may only be in terminating status if it has a deletion timestamp."))
1826+
}
1827+
}
1828+
return allErrs
18101829
}
18111830

18121831
// ValidateNamespace tests if required fields are set.

pkg/api/validation/validation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ func TestValidateVolumes(t *testing.T) {
460460
{Name: "glusterfs", VolumeSource: api.VolumeSource{Glusterfs: &api.GlusterfsVolumeSource{EndpointsName: "host1", Path: "path", ReadOnly: false}}},
461461
{Name: "flocker", VolumeSource: api.VolumeSource{Flocker: &api.FlockerVolumeSource{DatasetName: "datasetName"}}},
462462
{Name: "rbd", VolumeSource: api.VolumeSource{RBD: &api.RBDVolumeSource{CephMonitors: []string{"foo"}, RBDImage: "bar", FSType: "ext4"}}},
463-
{Name: "cinder", VolumeSource: api.VolumeSource{Cinder: &api.CinderVolumeSource{"29ea5088-4f60-4757-962e-dba678767887", "ext4", false}}},
463+
{Name: "cinder", VolumeSource: api.VolumeSource{Cinder: &api.CinderVolumeSource{"29ea5088-4f60-4757-962e-dba678767887", "ext4", false, false}}},
464464
{Name: "cephfs", VolumeSource: api.VolumeSource{CephFS: &api.CephFSVolumeSource{Monitors: []string{"foo"}}}},
465465
{Name: "downwardapi", VolumeSource: api.VolumeSource{DownwardAPI: &api.DownwardAPIVolumeSource{Items: []api.DownwardAPIVolumeFile{
466466
{Path: "labels", FieldRef: api.ObjectFieldSelector{

pkg/apiserver/apiserver_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,13 +2250,19 @@ type setTestSelfLinker struct {
22502250
expectedSet string
22512251
name string
22522252
namespace string
2253+
tenant string
22532254
called bool
22542255
err error
22552256
}
22562257

22572258
func (s *setTestSelfLinker) Namespace(runtime.Object) (string, error) { return s.namespace, s.err }
2258-
func (s *setTestSelfLinker) Name(runtime.Object) (string, error) { return s.name, s.err }
2259-
func (s *setTestSelfLinker) SelfLink(runtime.Object) (string, error) { return "", s.err }
2259+
func (s *setTestSelfLinker) Tenant(runtime.Object) (string, error) { return s.tenant, s.err }
2260+
func (s *setTestSelfLinker) SetTenant(obj runtime.Object, tenant string) error {
2261+
s.tenant = tenant
2262+
return s.err
2263+
}
2264+
func (s *setTestSelfLinker) Name(runtime.Object) (string, error) { return s.name, s.err }
2265+
func (s *setTestSelfLinker) SelfLink(runtime.Object) (string, error) { return "", s.err }
22602266
func (s *setTestSelfLinker) SetSelfLink(obj runtime.Object, selfLink string) error {
22612267
if e, a := s.expectedSet, selfLink; e != a {
22622268
s.t.Errorf("expected '%v', got '%v'", e, a)

pkg/apiserver/authz_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
// and always return nil.
2525
func TestNewAlwaysAllowAuthorizer(t *testing.T) {
2626
aaa := NewAlwaysAllowAuthorizer()
27-
if result := aaa.Authorize(nil); result != nil {
27+
if _, result := aaa.Authorize(nil); result != nil {
2828
t.Errorf("AlwaysAllowAuthorizer.Authorize did not return nil. (%s)", result)
2929
}
3030
}
@@ -33,39 +33,51 @@ func TestNewAlwaysAllowAuthorizer(t *testing.T) {
3333
// and always return an error as everything is forbidden.
3434
func TestNewAlwaysDenyAuthorizer(t *testing.T) {
3535
ada := NewAlwaysDenyAuthorizer()
36-
if result := ada.Authorize(nil); result == nil {
36+
if _, result := ada.Authorize(nil); result == nil {
3737
t.Errorf("AlwaysDenyAuthorizer.Authorize returned nil instead of error.")
3838
}
3939
}
4040

4141
// NewAuthorizerFromAuthorizationConfig has multiple return possibilities. This test
4242
// validates that errors are returned only when proper.
4343
func TestNewAuthorizerFromAuthorizationConfig(t *testing.T) {
44+
testConfig := AuthorizerConfig{
45+
AuthorizationModes: []string{},
46+
AuthorizationPolicyFile: "",
47+
KeystonAuthURL: "",
48+
}
4449
// Unknown modes should return errors
45-
if _, err := NewAuthorizerFromAuthorizationConfig([]string{"DoesNotExist"}, ""); err == nil {
50+
testConfig.AuthorizationModes = []string{"DoesNotExist"}
51+
if _, err := NewAuthorizerFromAuthorizationConfig(testConfig); err == nil {
4652
t.Errorf("NewAuthorizerFromAuthorizationConfig using a fake mode should have returned an error")
4753
}
4854

4955
// ModeAlwaysAllow and ModeAlwaysDeny should return without authorizationPolicyFile
5056
// but error if one is given
51-
if _, err := NewAuthorizerFromAuthorizationConfig([]string{ModeAlwaysAllow, ModeAlwaysDeny}, ""); err != nil {
57+
testConfig.AuthorizationModes = []string{ModeAlwaysAllow, ModeAlwaysDeny}
58+
if _, err := NewAuthorizerFromAuthorizationConfig(testConfig); err != nil {
5259
t.Errorf("NewAuthorizerFromAuthorizationConfig returned an error: %s", err)
5360
}
5461

5562
// ModeABAC requires a policy file
56-
if _, err := NewAuthorizerFromAuthorizationConfig([]string{ModeAlwaysAllow, ModeAlwaysDeny, ModeABAC}, ""); err == nil {
63+
testConfig.AuthorizationModes = []string{ModeAlwaysAllow, ModeAlwaysDeny, ModeABAC}
64+
if _, err := NewAuthorizerFromAuthorizationConfig(testConfig); err == nil {
5765
t.Errorf("NewAuthorizerFromAuthorizationConfig using a fake mode should have returned an error")
5866
}
5967
// ModeABAC should not error if a valid policy path is provided
60-
if _, err := NewAuthorizerFromAuthorizationConfig([]string{ModeAlwaysAllow, ModeAlwaysDeny, ModeABAC}, "../auth/authorizer/abac/example_policy_file.jsonl"); err != nil {
68+
testConfig.AuthorizationModes = []string{ModeAlwaysAllow, ModeAlwaysDeny, ModeABAC}
69+
testConfig.AuthorizationPolicyFile = "../auth/authorizer/abac/example_policy_file.jsonl"
70+
if _, err := NewAuthorizerFromAuthorizationConfig(testConfig); err != nil {
6171
t.Errorf("NewAuthorizerFromAuthorizationConfig errored while using a valid policy file: %s", err)
6272
}
6373
// Authorization Policy file cannot be used without ModeABAC
64-
if _, err := NewAuthorizerFromAuthorizationConfig([]string{ModeAlwaysAllow, ModeAlwaysDeny}, "../auth/authorizer/abac/example_policy_file.jsonl"); err == nil {
74+
testConfig.AuthorizationModes = []string{ModeAlwaysAllow, ModeAlwaysDeny}
75+
if _, err := NewAuthorizerFromAuthorizationConfig(testConfig); err == nil {
6576
t.Errorf("NewAuthorizerFromAuthorizationConfig should have errored when Authorization Policy File is used without ModeABAC")
6677
}
6778
// Atleast one authorizationMode is necessary
68-
if _, err := NewAuthorizerFromAuthorizationConfig([]string{}, "../auth/authorizer/abac/example_policy_file.jsonl"); err == nil {
79+
testConfig.AuthorizationModes = []string{}
80+
if _, err := NewAuthorizerFromAuthorizationConfig(testConfig); err == nil {
6981
t.Errorf("NewAuthorizerFromAuthorizationConfig should have errored when no authorization modes are passed")
7082
}
7183
}

pkg/apiserver/resthandler_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func (p *testPatcher) Get(ctx api.Context, name string) (runtime.Object, error)
9696
type testNamer struct {
9797
namespace string
9898
name string
99+
tenant string
99100
}
100101

101102
func (p *testNamer) Namespace(req *restful.Request) (namespace string, err error) {
@@ -114,6 +115,15 @@ func (p *testNamer) ObjectName(obj runtime.Object) (namespace, name string, err
114115
return p.namespace, p.name, nil
115116
}
116117

118+
func (p *testNamer) ObjectTenant(obj runtime.Object) (tenant string, err error) {
119+
return p.tenant, nil
120+
}
121+
122+
func (p *testNamer) SetTenant(obj runtime.Object, tenant string) (err error) {
123+
p.tenant = tenant
124+
return nil
125+
}
126+
117127
// SetSelfLink sets the provided URL onto the object. The method should return nil if the object
118128
// does not support selfLinks.
119129
func (p *testNamer) SetSelfLink(obj runtime.Object, url string) error {
@@ -151,6 +161,7 @@ func (tc *patchTestCase) Run(t *testing.T) {
151161

152162
namespace := tc.startingPod.Namespace
153163
name := tc.startingPod.Name
164+
tenant := tc.startingPod.Tenant
154165

155166
codec := latest.GroupOrDie("").Codec
156167

@@ -161,7 +172,7 @@ func (tc *patchTestCase) Run(t *testing.T) {
161172
ctx := api.NewDefaultContext()
162173
ctx = api.WithNamespace(ctx, namespace)
163174

164-
namer := &testNamer{namespace, name}
175+
namer := &testNamer{namespace, name, tenant}
165176

166177
versionedObj, err := api.Scheme.ConvertToVersion(&api.Pod{}, "v1")
167178
if err != nil {

pkg/auth/authorizer/abac/abac_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestNotAuthorized(t *testing.T) {
122122
Namespace: tc.NS,
123123
}
124124
t.Logf("tc: %v -> attr %v", tc, attr)
125-
err := a.Authorize(attr)
125+
_, err := a.Authorize(attr)
126126
actualAllow := bool(err == nil)
127127
if tc.ExpectAllow != actualAllow {
128128
t.Errorf("%d: Expected allowed=%v but actually allowed=%v\n\t%v",

pkg/auth/authorizer/union/union_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ type mockAuthzHandler struct {
2828
err error
2929
}
3030

31-
func (mock *mockAuthzHandler) Authorize(a authorizer.Attributes) error {
31+
func (mock *mockAuthzHandler) Authorize(a authorizer.Attributes) (string, error) {
3232
if mock.err != nil {
33-
return mock.err
33+
return "", mock.err
3434
}
3535
if !mock.isAuthorized {
36-
return errors.New("Request unauthorized")
36+
return "", errors.New("Request unauthorized")
3737
} else {
38-
return nil
38+
return "", nil
3939
}
4040
}
4141

@@ -44,7 +44,7 @@ func TestAuthorizationSecondPasses(t *testing.T) {
4444
handler2 := &mockAuthzHandler{isAuthorized: true}
4545
authzHandler := New(handler1, handler2)
4646

47-
err := authzHandler.Authorize(nil)
47+
_, err := authzHandler.Authorize(nil)
4848
if err != nil {
4949
t.Errorf("Unexpected error: %v", err)
5050
}
@@ -55,7 +55,7 @@ func TestAuthorizationFirstPasses(t *testing.T) {
5555
handler2 := &mockAuthzHandler{isAuthorized: false}
5656
authzHandler := New(handler1, handler2)
5757

58-
err := authzHandler.Authorize(nil)
58+
_, err := authzHandler.Authorize(nil)
5959
if err != nil {
6060
t.Errorf("Unexpected error: %v", err)
6161
}
@@ -66,7 +66,7 @@ func TestAuthorizationNonePasses(t *testing.T) {
6666
handler2 := &mockAuthzHandler{isAuthorized: false}
6767
authzHandler := New(handler1, handler2)
6868

69-
err := authzHandler.Authorize(nil)
69+
_, err := authzHandler.Authorize(nil)
7070
if err == nil {
7171
t.Errorf("Expected error: %v", err)
7272
}

0 commit comments

Comments
 (0)