opts_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package opts
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. )
  7. func TestValidateIPAddress(t *testing.T) {
  8. if ret, err := ValidateIPAddress(`1.2.3.4`); err != nil || ret == "" {
  9. t.Fatalf("ValidateIPAddress(`1.2.3.4`) got %s %s", ret, err)
  10. }
  11. if ret, err := ValidateIPAddress(`127.0.0.1`); err != nil || ret == "" {
  12. t.Fatalf("ValidateIPAddress(`127.0.0.1`) got %s %s", ret, err)
  13. }
  14. if ret, err := ValidateIPAddress(`::1`); err != nil || ret == "" {
  15. t.Fatalf("ValidateIPAddress(`::1`) got %s %s", ret, err)
  16. }
  17. if ret, err := ValidateIPAddress(`127`); err == nil || ret != "" {
  18. t.Fatalf("ValidateIPAddress(`127`) got %s %s", ret, err)
  19. }
  20. if ret, err := ValidateIPAddress(`random invalid string`); err == nil || ret != "" {
  21. t.Fatalf("ValidateIPAddress(`random invalid string`) got %s %s", ret, err)
  22. }
  23. }
  24. func TestMapOpts(t *testing.T) {
  25. tmpMap := make(map[string]string)
  26. o := NewMapOpts(tmpMap, logOptsValidator)
  27. o.Set("max-size=1")
  28. if o.String() != "map[max-size:1]" {
  29. t.Errorf("%s != [map[max-size:1]", o.String())
  30. }
  31. o.Set("max-file=2")
  32. if len(tmpMap) != 2 {
  33. t.Errorf("map length %d != 2", len(tmpMap))
  34. }
  35. if tmpMap["max-file"] != "2" {
  36. t.Errorf("max-file = %s != 2", tmpMap["max-file"])
  37. }
  38. if tmpMap["max-size"] != "1" {
  39. t.Errorf("max-size = %s != 1", tmpMap["max-size"])
  40. }
  41. if o.Set("dummy-val=3") == nil {
  42. t.Errorf("validator is not being called")
  43. }
  44. }
  45. func TestListOptsWithoutValidator(t *testing.T) {
  46. o := NewListOpts(nil)
  47. o.Set("foo")
  48. if o.String() != "[foo]" {
  49. t.Errorf("%s != [foo]", o.String())
  50. }
  51. o.Set("bar")
  52. if o.Len() != 2 {
  53. t.Errorf("%d != 2", o.Len())
  54. }
  55. o.Set("bar")
  56. if o.Len() != 3 {
  57. t.Errorf("%d != 3", o.Len())
  58. }
  59. if !o.Get("bar") {
  60. t.Error("o.Get(\"bar\") == false")
  61. }
  62. if o.Get("baz") {
  63. t.Error("o.Get(\"baz\") == true")
  64. }
  65. o.Delete("foo")
  66. if o.String() != "[bar bar]" {
  67. t.Errorf("%s != [bar bar]", o.String())
  68. }
  69. listOpts := o.GetAll()
  70. if len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" {
  71. t.Errorf("Expected [[bar bar]], got [%v]", listOpts)
  72. }
  73. mapListOpts := o.GetMap()
  74. if len(mapListOpts) != 1 {
  75. t.Errorf("Expected [map[bar:{}]], got [%v]", mapListOpts)
  76. }
  77. }
  78. func TestListOptsWithValidator(t *testing.T) {
  79. // Re-using logOptsvalidator (used by MapOpts)
  80. o := NewListOpts(logOptsValidator)
  81. o.Set("foo")
  82. if o.String() != "[]" {
  83. t.Errorf("%s != []", o.String())
  84. }
  85. o.Set("foo=bar")
  86. if o.String() != "[]" {
  87. t.Errorf("%s != []", o.String())
  88. }
  89. o.Set("max-file=2")
  90. if o.Len() != 1 {
  91. t.Errorf("%d != 1", o.Len())
  92. }
  93. if !o.Get("max-file=2") {
  94. t.Error("o.Get(\"max-file=2\") == false")
  95. }
  96. if o.Get("baz") {
  97. t.Error("o.Get(\"baz\") == true")
  98. }
  99. o.Delete("max-file=2")
  100. if o.String() != "[]" {
  101. t.Errorf("%s != []", o.String())
  102. }
  103. }
  104. func TestValidateDNSSearch(t *testing.T) {
  105. valid := []string{
  106. `.`,
  107. `a`,
  108. `a.`,
  109. `1.foo`,
  110. `17.foo`,
  111. `foo.bar`,
  112. `foo.bar.baz`,
  113. `foo.bar.`,
  114. `foo.bar.baz`,
  115. `foo1.bar2`,
  116. `foo1.bar2.baz`,
  117. `1foo.2bar.`,
  118. `1foo.2bar.baz`,
  119. `foo-1.bar-2`,
  120. `foo-1.bar-2.baz`,
  121. `foo-1.bar-2.`,
  122. `foo-1.bar-2.baz`,
  123. `1-foo.2-bar`,
  124. `1-foo.2-bar.baz`,
  125. `1-foo.2-bar.`,
  126. `1-foo.2-bar.baz`,
  127. }
  128. invalid := []string{
  129. ``,
  130. ` `,
  131. ` `,
  132. `17`,
  133. `17.`,
  134. `.17`,
  135. `17-.`,
  136. `17-.foo`,
  137. `.foo`,
  138. `foo-.bar`,
  139. `-foo.bar`,
  140. `foo.bar-`,
  141. `foo.bar-.baz`,
  142. `foo.-bar`,
  143. `foo.-bar.baz`,
  144. `foo.bar.baz.this.should.fail.on.long.name.beause.it.is.longer.thanisshouldbethis.should.fail.on.long.name.beause.it.is.longer.thanisshouldbethis.should.fail.on.long.name.beause.it.is.longer.thanisshouldbethis.should.fail.on.long.name.beause.it.is.longer.thanisshouldbe`,
  145. }
  146. for _, domain := range valid {
  147. if ret, err := ValidateDNSSearch(domain); err != nil || ret == "" {
  148. t.Fatalf("ValidateDNSSearch(`"+domain+"`) got %s %s", ret, err)
  149. }
  150. }
  151. for _, domain := range invalid {
  152. if ret, err := ValidateDNSSearch(domain); err == nil || ret != "" {
  153. t.Fatalf("ValidateDNSSearch(`"+domain+"`) got %s %s", ret, err)
  154. }
  155. }
  156. }
  157. func TestValidateLabel(t *testing.T) {
  158. if _, err := ValidateLabel("label"); err == nil || err.Error() != "bad attribute format: label" {
  159. t.Fatalf("Expected an error [bad attribute format: label], go %v", err)
  160. }
  161. if actual, err := ValidateLabel("key1=value1"); err != nil || actual != "key1=value1" {
  162. t.Fatalf("Expected [key1=value1], got [%v,%v]", actual, err)
  163. }
  164. // Validate it's working with more than one =
  165. if actual, err := ValidateLabel("key1=value1=value2"); err != nil {
  166. t.Fatalf("Expected [key1=value1=value2], got [%v,%v]", actual, err)
  167. }
  168. // Validate it's working with one more
  169. if actual, err := ValidateLabel("key1=value1=value2=value3"); err != nil {
  170. t.Fatalf("Expected [key1=value1=value2=value2], got [%v,%v]", actual, err)
  171. }
  172. }
  173. func logOptsValidator(val string) (string, error) {
  174. allowedKeys := map[string]string{"max-size": "1", "max-file": "2"}
  175. vals := strings.Split(val, "=")
  176. if allowedKeys[vals[0]] != "" {
  177. return val, nil
  178. }
  179. return "", fmt.Errorf("invalid key %s", vals[0])
  180. }
  181. func TestNamedListOpts(t *testing.T) {
  182. var v []string
  183. o := NewNamedListOptsRef("foo-name", &v, nil)
  184. o.Set("foo")
  185. if o.String() != "[foo]" {
  186. t.Errorf("%s != [foo]", o.String())
  187. }
  188. if o.Name() != "foo-name" {
  189. t.Errorf("%s != foo-name", o.Name())
  190. }
  191. if len(v) != 1 {
  192. t.Errorf("expected foo to be in the values, got %v", v)
  193. }
  194. }
  195. func TestNamedMapOpts(t *testing.T) {
  196. tmpMap := make(map[string]string)
  197. o := NewNamedMapOpts("max-name", tmpMap, nil)
  198. o.Set("max-size=1")
  199. if o.String() != "map[max-size:1]" {
  200. t.Errorf("%s != [map[max-size:1]", o.String())
  201. }
  202. if o.Name() != "max-name" {
  203. t.Errorf("%s != max-name", o.Name())
  204. }
  205. if _, exist := tmpMap["max-size"]; !exist {
  206. t.Errorf("expected map-size to be in the values, got %v", tmpMap)
  207. }
  208. }