fed_clusterrole_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package k8s
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func Test_parsePolicyRule(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. rule string
  23. want *PolicyRule
  24. wantErr bool
  25. }{
  26. {
  27. name: "apps/v1:deployments,daemonsets:get,watch",
  28. rule: "apps/v1:deployments,daemonsets:get,watch",
  29. want: &PolicyRule{
  30. APIGroups: []string{"apps/v1"},
  31. Resources: []string{"deployments", "daemonsets"},
  32. Verbs: []string{"get", "watch"},
  33. },
  34. wantErr: false,
  35. },
  36. {
  37. name: "*:*:get,watch",
  38. rule: "*:*:get,watch",
  39. want: &PolicyRule{
  40. APIGroups: []string{"*"},
  41. Resources: []string{"*"},
  42. Verbs: []string{"get", "watch"},
  43. },
  44. wantErr: false,
  45. },
  46. {
  47. name: ":pods,configmaps:*",
  48. rule: ":pods,configmaps:*",
  49. want: &PolicyRule{
  50. APIGroups: []string{""},
  51. Resources: []string{"pods", "configmaps"},
  52. Verbs: []string{"*"},
  53. },
  54. wantErr: false,
  55. },
  56. }
  57. for _, tt := range tests {
  58. t.Run(tt.name, func(t *testing.T) {
  59. got, err := parsePolicyRule(tt.rule)
  60. if (err != nil) != tt.wantErr {
  61. t.Errorf("parsePolicyRule() error = %v, wantErr %v", err, tt.wantErr)
  62. return
  63. }
  64. if !reflect.DeepEqual(got, tt.want) {
  65. t.Errorf("parsePolicyRule() = %v, want %v", got, tt.want)
  66. }
  67. })
  68. }
  69. }