@@ -3,10 +3,12 @@ package restrictusers
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "io"
6
7
"strings"
7
8
"testing"
8
9
9
10
corev1 "k8s.io/api/core/v1"
11
+ "k8s.io/apimachinery/pkg/api/equality"
10
12
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
13
"k8s.io/apimachinery/pkg/runtime"
12
14
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -402,3 +404,74 @@ func TestAdmission(t *testing.T) {
402
404
}
403
405
}
404
406
}
407
+
408
+ func TestPluginForConfig (t * testing.T ) {
409
+ testcases := []struct {
410
+ name string
411
+ config string
412
+ expectedErr string
413
+ expectedPlugin admission.Interface
414
+ }{
415
+ {
416
+ name : "no config, no err, expect plugin" ,
417
+ expectedPlugin : func () admission.Interface {
418
+ plugin , _ := NewRestrictUsersAdmission ()
419
+ return plugin
420
+ }(),
421
+ },
422
+ {
423
+ name : "config sets openshiftOAuthDesiredState to NotDesired, no err, nil plugin" ,
424
+ config : `apiVersion: authorization.openshift.io/v1alpha1
425
+ kind: RestrictSubjectBindingsAdmissionConfig
426
+ openshiftOAuthDesiredState: NotDesired
427
+ ` ,
428
+ expectedPlugin : nil ,
429
+ },
430
+ {
431
+ name : "config sets openshiftOAuthDesiredState to Desired, no err, expect plugin" ,
432
+ config : `apiVersion: authorization.openshift.io/v1alpha1
433
+ kind: RestrictSubjectBindingsAdmissionConfig
434
+ openshiftOAuthDesiredState: Desired
435
+ ` ,
436
+ expectedPlugin : func () admission.Interface {
437
+ plugin , _ := NewRestrictUsersAdmission ()
438
+ return plugin
439
+ }(),
440
+ },
441
+ {
442
+ name : "config sets openshiftOAuthDesiredState to invalid value, err, nil plugin" ,
443
+ config : `apiVersion: authorization.openshift.io/v1alpha1
444
+ kind: RestrictSubjectBindingsAdmissionConfig
445
+ openshiftOAuthDesiredState: FooBar
446
+ ` ,
447
+ expectedPlugin : nil ,
448
+ expectedErr : "config is invalid, openshiftOAuthDesiredState must be one of Desired,NotDesired" ,
449
+ },
450
+ }
451
+
452
+ for _ , tc := range testcases {
453
+ t .Run (tc .name , func (t * testing.T ) {
454
+ var reader io.Reader
455
+ if len (tc .config ) > 0 {
456
+ reader = strings .NewReader (tc .config )
457
+ }
458
+
459
+ plugin , err := pluginForConfig (reader )
460
+ switch {
461
+ case len (tc .expectedErr ) == 0 && err == nil :
462
+ case len (tc .expectedErr ) == 0 && err != nil :
463
+ t .Errorf ("%s: unexpected error: %v" , tc .name , err )
464
+ case len (tc .expectedErr ) != 0 && err == nil :
465
+ t .Errorf ("%s: missing error: %v" , tc .name , tc .expectedErr )
466
+ case len (tc .expectedErr ) != 0 && err != nil &&
467
+ ! strings .Contains (err .Error (), tc .expectedErr ):
468
+ t .Errorf ("%s: missing error: expected %v, got %v" ,
469
+ tc .name , tc .expectedErr , err )
470
+ }
471
+
472
+ if ! equality .Semantic .DeepEqual (tc .expectedPlugin , plugin ) {
473
+ t .Errorf ("plugin does not match. expected %v, got %v" , tc .expectedPlugin , plugin )
474
+ }
475
+ })
476
+ }
477
+ }
0 commit comments