You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

197 lines
4.5 KiB

  1. /*
  2. Copyright 2015 Google LLC
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "strings"
  16. "testing"
  17. "time"
  18. "cloud.google.com/go/bigtable"
  19. "github.com/google/go-cmp/cmp"
  20. )
  21. func TestParseGCPolicy(t *testing.T) {
  22. for _, test := range []struct {
  23. in string
  24. want bigtable.GCPolicy
  25. }{
  26. {
  27. "never",
  28. bigtable.NoGcPolicy(),
  29. },
  30. {
  31. "maxage=3h",
  32. bigtable.MaxAgePolicy(3 * time.Hour),
  33. },
  34. {
  35. "maxversions=2",
  36. bigtable.MaxVersionsPolicy(2),
  37. },
  38. {
  39. "maxversions=2 and maxage=1h",
  40. bigtable.IntersectionPolicy(bigtable.MaxVersionsPolicy(2), bigtable.MaxAgePolicy(time.Hour)),
  41. },
  42. {
  43. "(((maxversions=2 and (maxage=1h))))",
  44. bigtable.IntersectionPolicy(bigtable.MaxVersionsPolicy(2), bigtable.MaxAgePolicy(time.Hour)),
  45. },
  46. {
  47. "maxversions=7 or maxage=8h",
  48. bigtable.UnionPolicy(bigtable.MaxVersionsPolicy(7), bigtable.MaxAgePolicy(8*time.Hour)),
  49. },
  50. {
  51. "maxversions = 7||maxage = 8h",
  52. bigtable.UnionPolicy(bigtable.MaxVersionsPolicy(7), bigtable.MaxAgePolicy(8*time.Hour)),
  53. },
  54. {
  55. "maxversions=7||maxage=8h",
  56. bigtable.UnionPolicy(bigtable.MaxVersionsPolicy(7), bigtable.MaxAgePolicy(8*time.Hour)),
  57. },
  58. {
  59. "maxage=30d || (maxage=3d && maxversions=100)",
  60. bigtable.UnionPolicy(
  61. bigtable.MaxAgePolicy(30*24*time.Hour),
  62. bigtable.IntersectionPolicy(
  63. bigtable.MaxAgePolicy(3*24*time.Hour),
  64. bigtable.MaxVersionsPolicy(100))),
  65. },
  66. {
  67. "maxage=30d || (maxage=3d && maxversions=100) || maxversions=7",
  68. bigtable.UnionPolicy(
  69. bigtable.UnionPolicy(
  70. bigtable.MaxAgePolicy(30*24*time.Hour),
  71. bigtable.IntersectionPolicy(
  72. bigtable.MaxAgePolicy(3*24*time.Hour),
  73. bigtable.MaxVersionsPolicy(100))),
  74. bigtable.MaxVersionsPolicy(7)),
  75. },
  76. {
  77. // && and || have same precedence, left associativity
  78. "maxage=1h && maxage=2h || maxage=3h",
  79. bigtable.UnionPolicy(
  80. bigtable.IntersectionPolicy(
  81. bigtable.MaxAgePolicy(1*time.Hour),
  82. bigtable.MaxAgePolicy(2*time.Hour)),
  83. bigtable.MaxAgePolicy(3*time.Hour)),
  84. },
  85. } {
  86. got, err := parseGCPolicy(test.in)
  87. if err != nil {
  88. t.Errorf("%s: %v", test.in, err)
  89. continue
  90. }
  91. if !cmp.Equal(got, test.want, cmp.AllowUnexported(bigtable.IntersectionPolicy(), bigtable.UnionPolicy())) {
  92. t.Errorf("%s: got %+v, want %+v", test.in, got, test.want)
  93. }
  94. }
  95. }
  96. func TestParseGCPolicyErrors(t *testing.T) {
  97. for _, in := range []string{
  98. "",
  99. "a",
  100. "b = 1h",
  101. "c = 1",
  102. "maxage=1", // need duration
  103. "maxversions=1h", // need int
  104. "maxage",
  105. "maxversions",
  106. "never=never",
  107. "maxversions=1 && never",
  108. "(((maxage=1h))",
  109. "((maxage=1h)))",
  110. "maxage=30d || ((maxage=3d && maxversions=100)",
  111. "maxversions = 3 and",
  112. } {
  113. _, err := parseGCPolicy(in)
  114. if err == nil {
  115. t.Errorf("%s: got nil, want error", in)
  116. }
  117. }
  118. }
  119. func TestTokenizeGCPolicy(t *testing.T) {
  120. for _, test := range []struct {
  121. in string
  122. want []string
  123. }{
  124. {
  125. "maxage=5d",
  126. []string{"maxage", "=", "5d"},
  127. },
  128. {
  129. "maxage = 5d",
  130. []string{"maxage", "=", "5d"},
  131. },
  132. {
  133. "maxage=5d or maxversions=5",
  134. []string{"maxage", "=", "5d", "or", "maxversions", "=", "5"},
  135. },
  136. {
  137. "maxage=5d || (maxversions=5)",
  138. []string{"maxage", "=", "5d", "||", "(", "maxversions", "=", "5", ")"},
  139. },
  140. {
  141. "maxage=5d||( maxversions=5 )",
  142. []string{"maxage", "=", "5d", "||", "(", "maxversions", "=", "5", ")"},
  143. },
  144. } {
  145. got, err := tokenizeGCPolicy(test.in)
  146. if err != nil {
  147. t.Errorf("%s: %v", test.in, err)
  148. continue
  149. }
  150. if diff := cmp.Diff(got, test.want); diff != "" {
  151. t.Errorf("%s: %s", test.in, diff)
  152. }
  153. }
  154. }
  155. func TestTokenizeGCPolicyErrors(t *testing.T) {
  156. for _, in := range []string{
  157. "a &",
  158. "a & b",
  159. "a &x b",
  160. "a |",
  161. "a | b",
  162. "a |& b",
  163. "a % b",
  164. } {
  165. _, err := tokenizeGCPolicy(in)
  166. if err == nil {
  167. t.Errorf("%s: got nil, want error", in)
  168. }
  169. }
  170. }
  171. func tokenizeGCPolicy(s string) ([]string, error) {
  172. var tokens []string
  173. r := strings.NewReader(s)
  174. for {
  175. tok, err := getToken(r)
  176. if err != nil {
  177. return nil, err
  178. }
  179. if tok == "" {
  180. break
  181. }
  182. tokens = append(tokens, tok)
  183. }
  184. return tokens, nil
  185. }