flags_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. *
  3. * Copyright 2019 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package flags
  19. import (
  20. "flag"
  21. "reflect"
  22. "testing"
  23. "time"
  24. "google.golang.org/grpc/internal/grpctest"
  25. )
  26. type s struct {
  27. grpctest.Tester
  28. }
  29. func Test(t *testing.T) {
  30. grpctest.RunSubTests(t, s{})
  31. }
  32. func (s) TestStringWithAllowedValues(t *testing.T) {
  33. const defaultVal = "default"
  34. tests := []struct {
  35. args string
  36. allowed []string
  37. wantVal string
  38. wantErr bool
  39. }{
  40. {"-workloads=all", []string{"unary", "streaming", "all"}, "all", false},
  41. {"-workloads=disallowed", []string{"unary", "streaming", "all"}, defaultVal, true},
  42. }
  43. for _, test := range tests {
  44. flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
  45. var w = StringWithAllowedValues("workloads", defaultVal, "usage", test.allowed)
  46. err := flag.CommandLine.Parse([]string{test.args})
  47. switch {
  48. case !test.wantErr && err != nil:
  49. t.Errorf("failed to parse command line args {%v}: %v", test.args, err)
  50. case test.wantErr && err == nil:
  51. t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args)
  52. default:
  53. if *w != test.wantVal {
  54. t.Errorf("flag value is %v, want %v", *w, test.wantVal)
  55. }
  56. }
  57. }
  58. }
  59. func (s) TestDurationSlice(t *testing.T) {
  60. defaultVal := []time.Duration{time.Second, time.Nanosecond}
  61. tests := []struct {
  62. args string
  63. wantVal []time.Duration
  64. wantErr bool
  65. }{
  66. {"-latencies=1s", []time.Duration{time.Second}, false},
  67. {"-latencies=1s,2s,3s", []time.Duration{time.Second, 2 * time.Second, 3 * time.Second}, false},
  68. {"-latencies=bad", defaultVal, true},
  69. }
  70. for _, test := range tests {
  71. flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
  72. var w = DurationSlice("latencies", defaultVal, "usage")
  73. err := flag.CommandLine.Parse([]string{test.args})
  74. switch {
  75. case !test.wantErr && err != nil:
  76. t.Errorf("failed to parse command line args {%v}: %v", test.args, err)
  77. case test.wantErr && err == nil:
  78. t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args)
  79. default:
  80. if !reflect.DeepEqual(*w, test.wantVal) {
  81. t.Errorf("flag value is %v, want %v", *w, test.wantVal)
  82. }
  83. }
  84. }
  85. }
  86. func (s) TestIntSlice(t *testing.T) {
  87. defaultVal := []int{1, 1024}
  88. tests := []struct {
  89. args string
  90. wantVal []int
  91. wantErr bool
  92. }{
  93. {"-kbps=1", []int{1}, false},
  94. {"-kbps=1,2,3", []int{1, 2, 3}, false},
  95. {"-kbps=20e4", defaultVal, true},
  96. }
  97. for _, test := range tests {
  98. flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
  99. var w = IntSlice("kbps", defaultVal, "usage")
  100. err := flag.CommandLine.Parse([]string{test.args})
  101. switch {
  102. case !test.wantErr && err != nil:
  103. t.Errorf("failed to parse command line args {%v}: %v", test.args, err)
  104. case test.wantErr && err == nil:
  105. t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args)
  106. default:
  107. if !reflect.DeepEqual(*w, test.wantVal) {
  108. t.Errorf("flag value is %v, want %v", *w, test.wantVal)
  109. }
  110. }
  111. }
  112. }
  113. func (s) TestStringSlice(t *testing.T) {
  114. defaultVal := []string{"bar", "baz"}
  115. tests := []struct {
  116. args string
  117. wantVal []string
  118. wantErr bool
  119. }{
  120. {"-name=foobar", []string{"foobar"}, false},
  121. {"-name=foo,bar", []string{"foo", "bar"}, false},
  122. {`-name="foo,bar",baz`, []string{"foo,bar", "baz"}, false},
  123. {`-name="foo,bar""",baz`, []string{`foo,bar"`, "baz"}, false},
  124. }
  125. for _, test := range tests {
  126. flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
  127. var w = StringSlice("name", defaultVal, "usage")
  128. err := flag.CommandLine.Parse([]string{test.args})
  129. switch {
  130. case !test.wantErr && err != nil:
  131. t.Errorf("failed to parse command line args {%v}: %v", test.args, err)
  132. case test.wantErr && err == nil:
  133. t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args)
  134. default:
  135. if !reflect.DeepEqual(*w, test.wantVal) {
  136. t.Errorf("flag value is %v, want %v", *w, test.wantVal)
  137. }
  138. }
  139. }
  140. }