share_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 apis
  15. import (
  16. "testing"
  17. "yunion.io/x/pkg/util/rbacscope"
  18. )
  19. func TestSShareInfo_IsViolate(t *testing.T) {
  20. cases := []struct {
  21. name string
  22. s1 SShareInfo
  23. s2 SShareInfo
  24. violate1 bool
  25. violate2 bool
  26. }{
  27. {
  28. name: "case1",
  29. s1: SShareInfo{
  30. IsPublic: false,
  31. PublicScope: rbacscope.ScopeNone,
  32. },
  33. s2: SShareInfo{
  34. IsPublic: true,
  35. PublicScope: rbacscope.ScopeSystem,
  36. },
  37. violate1: false,
  38. violate2: true,
  39. },
  40. {
  41. name: "case2",
  42. s1: SShareInfo{
  43. IsPublic: true,
  44. PublicScope: rbacscope.ScopeProject,
  45. SharedProjects: []string{"p1"},
  46. },
  47. s2: SShareInfo{
  48. IsPublic: true,
  49. PublicScope: rbacscope.ScopeProject,
  50. SharedProjects: []string{"p2"},
  51. },
  52. violate1: true,
  53. violate2: true,
  54. },
  55. {
  56. name: "case3",
  57. s1: SShareInfo{
  58. IsPublic: true,
  59. PublicScope: rbacscope.ScopeDomain,
  60. SharedDomains: []string{"p1"},
  61. },
  62. s2: SShareInfo{
  63. IsPublic: true,
  64. PublicScope: rbacscope.ScopeProject,
  65. SharedProjects: []string{"p2"},
  66. },
  67. violate1: true,
  68. violate2: false,
  69. },
  70. {
  71. name: "case4",
  72. s1: SShareInfo{
  73. IsPublic: true,
  74. PublicScope: rbacscope.ScopeDomain,
  75. SharedDomains: []string{"p1", "p2"},
  76. },
  77. s2: SShareInfo{
  78. IsPublic: true,
  79. PublicScope: rbacscope.ScopeDomain,
  80. SharedDomains: []string{"p1", "p2"},
  81. },
  82. violate1: false,
  83. violate2: false,
  84. },
  85. }
  86. for _, c := range cases {
  87. if c.s1.IsViolate(c.s2) != c.violate1 {
  88. t.Errorf("[%s] s1.violate(s2) want %v", c.name, c.violate1)
  89. } else if c.s2.IsViolate(c.s1) != c.violate2 {
  90. t.Errorf("[%s] s2.violate(s1) want: %v", c.name, c.violate2)
  91. }
  92. }
  93. }
  94. func TestSShareInfo_Intersect(t *testing.T) {
  95. cases := []struct {
  96. name string
  97. s1 SShareInfo
  98. s2 SShareInfo
  99. want SShareInfo
  100. }{
  101. {
  102. name: "case1",
  103. s1: SShareInfo{
  104. IsPublic: false,
  105. PublicScope: rbacscope.ScopeNone,
  106. },
  107. s2: SShareInfo{
  108. IsPublic: true,
  109. PublicScope: rbacscope.ScopeSystem,
  110. },
  111. want: SShareInfo{
  112. IsPublic: false,
  113. PublicScope: rbacscope.ScopeNone,
  114. },
  115. },
  116. {
  117. name: "case2",
  118. s1: SShareInfo{
  119. IsPublic: true,
  120. PublicScope: rbacscope.ScopeProject,
  121. SharedProjects: []string{"p1"},
  122. },
  123. s2: SShareInfo{
  124. IsPublic: true,
  125. PublicScope: rbacscope.ScopeProject,
  126. SharedProjects: []string{"p2"},
  127. },
  128. want: SShareInfo{
  129. IsPublic: false,
  130. PublicScope: rbacscope.ScopeNone,
  131. },
  132. },
  133. {
  134. name: "case3",
  135. s1: SShareInfo{
  136. IsPublic: true,
  137. PublicScope: rbacscope.ScopeDomain,
  138. SharedDomains: []string{"p1"},
  139. },
  140. s2: SShareInfo{
  141. IsPublic: true,
  142. PublicScope: rbacscope.ScopeProject,
  143. SharedProjects: []string{"p2"},
  144. },
  145. want: SShareInfo{
  146. IsPublic: true,
  147. PublicScope: rbacscope.ScopeProject,
  148. SharedProjects: []string{"p2"},
  149. },
  150. },
  151. {
  152. name: "case4",
  153. s1: SShareInfo{
  154. IsPublic: true,
  155. PublicScope: rbacscope.ScopeDomain,
  156. SharedDomains: []string{"p1", "p2"},
  157. },
  158. s2: SShareInfo{
  159. IsPublic: true,
  160. PublicScope: rbacscope.ScopeDomain,
  161. SharedDomains: []string{"p1", "p2"},
  162. },
  163. want: SShareInfo{
  164. IsPublic: true,
  165. PublicScope: rbacscope.ScopeDomain,
  166. SharedDomains: []string{"p1", "p2"},
  167. },
  168. },
  169. {
  170. name: "case5",
  171. s1: SShareInfo{
  172. IsPublic: true,
  173. PublicScope: rbacscope.ScopeDomain,
  174. SharedDomains: []string{"p1", "p2", "p4"},
  175. },
  176. s2: SShareInfo{
  177. IsPublic: true,
  178. PublicScope: rbacscope.ScopeDomain,
  179. SharedDomains: []string{"p1", "p2", "p3"},
  180. },
  181. want: SShareInfo{
  182. IsPublic: true,
  183. PublicScope: rbacscope.ScopeDomain,
  184. SharedDomains: []string{"p1", "p2"},
  185. },
  186. },
  187. }
  188. for _, c := range cases {
  189. inter := c.s1.Intersect(c.s2)
  190. if !inter.Equals(c.want) {
  191. t.Errorf("[%s] intersect got %#v != want %#v", c.name, inter, c.want)
  192. }
  193. }
  194. }