blob: 8adaacfc4ec2c2ac0f0c6ab8ab994c4ec3a6d9c4 [file] [log] [blame]
Cosmos Nicolaoubdc917c2014-10-24 12:41:47 -07001package flags_test
2
3import (
4 "flag"
Cosmos Nicolaou4e213d72014-10-26 22:21:52 -07005 "io/ioutil"
Cosmos Nicolaoud811b072014-10-28 17:46:27 -07006 "os"
7 "reflect"
Cosmos Nicolaoubdc917c2014-10-24 12:41:47 -07008 "testing"
9
10 "veyron.io/veyron/veyron/lib/flags"
Asim Shankar95910b62014-10-31 22:02:29 -070011 "veyron.io/veyron/veyron/lib/flags/consts"
Cosmos Nicolaoubdc917c2014-10-24 12:41:47 -070012)
13
14func TestFlags(t *testing.T) {
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -070015 fs := flag.NewFlagSet("test", flag.ContinueOnError)
16 if flags.CreateAndRegister(fs) != nil {
Cosmos Nicolaou78237372014-11-04 18:19:09 -080017 t.Fatalf("should have returned a nil value")
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -070018 }
19 fl := flags.CreateAndRegister(fs, flags.Runtime)
20 if fl == nil {
Cosmos Nicolaou78237372014-11-04 18:19:09 -080021 t.Errorf("should have failed")
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -070022 }
Cosmos Nicolaoud811b072014-10-28 17:46:27 -070023 creds := "creddir"
24 roots := []string{"ab:cd:ef"}
25 args := []string{"--veyron.credentials=" + creds, "--veyron.namespace.root=" + roots[0]}
26 fl.Parse(args)
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -070027 rtf := fl.RuntimeFlags()
28 if got, want := rtf.NamespaceRoots, roots; !reflect.DeepEqual(got, want) {
Cosmos Nicolaoud811b072014-10-28 17:46:27 -070029 t.Errorf("got %v, want %v", got, want)
Cosmos Nicolaoubdc917c2014-10-24 12:41:47 -070030 }
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -070031 if got, want := rtf.Credentials, creds; !reflect.DeepEqual(got, want) {
Cosmos Nicolaoud811b072014-10-28 17:46:27 -070032 t.Errorf("got %v, want %v", got, want)
Cosmos Nicolaoubdc917c2014-10-24 12:41:47 -070033 }
Cosmos Nicolaoud811b072014-10-28 17:46:27 -070034 if got, want := fl.HasGroup(flags.Listen), false; got != want {
35 t.Errorf("got %t, want %t", got, want)
36 }
37 // Make sure we have a deep copy.
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -070038 rtf.NamespaceRoots[0] = "oooh"
39 rtf = fl.RuntimeFlags()
40 if got, want := rtf.NamespaceRoots, roots; !reflect.DeepEqual(got, want) {
Cosmos Nicolaoud811b072014-10-28 17:46:27 -070041 t.Errorf("got %v, want %v", got, want)
Cosmos Nicolaou4e213d72014-10-26 22:21:52 -070042 }
43}
44
Cosmos Nicolaou78237372014-11-04 18:19:09 -080045func TestACLFlags(t *testing.T) {
46 fs := flag.NewFlagSet("test", flag.ContinueOnError)
47 fl := flags.CreateAndRegister(fs, flags.Runtime, flags.ACL)
Cosmos Nicolaouc7ddcf02014-11-05 16:26:56 -080048 args := []string{"--veyron.acl=runtime:foo.json", "--veyron.acl=bar:bar.json", "--veyron.acl=baz:bar:baz.json"}
Cosmos Nicolaou78237372014-11-04 18:19:09 -080049 fl.Parse(args)
50 aclf := fl.ACLFlags()
Cosmos Nicolaouc7ddcf02014-11-05 16:26:56 -080051 if got, want := aclf.ACLFile("runtime"), "foo.json"; got != want {
Cosmos Nicolaou78237372014-11-04 18:19:09 -080052 t.Errorf("got %t, want %t", got, want)
53 }
54 if got, want := aclf.ACLFile("bar"), "bar.json"; got != want {
55 t.Errorf("got %t, want %t", got, want)
56 }
57 if got, want := aclf.ACLFile("wombat"), ""; got != want {
58 t.Errorf("got %t, want %t", got, want)
59 }
60 if got, want := aclf.ACLFile("baz"), "bar:baz.json"; got != want {
61 t.Errorf("got %t, want %t", got, want)
62 }
63}
64
Cosmos Nicolaou4e213d72014-10-26 22:21:52 -070065func TestFlagError(t *testing.T) {
66 fs := flag.NewFlagSet("test", flag.ContinueOnError)
67 fs.SetOutput(ioutil.Discard)
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -070068 fl := flags.CreateAndRegister(fs, flags.Runtime)
Cosmos Nicolaou4e213d72014-10-26 22:21:52 -070069 addr := "192.168.10.1:0"
70 args := []string{"--xxxveyron.tcp.address=" + addr, "not an arg"}
71 err := fl.Parse(args)
72 if err == nil {
73 t.Fatalf("expected this to fail!")
74 }
75 if got, want := len(fl.Args()), 1; got != want {
76 t.Errorf("got %d, want %d [args: %v]", got, want, fl.Args())
77 }
Cosmos Nicolaou78237372014-11-04 18:19:09 -080078
79 fs = flag.NewFlagSet("test", flag.ContinueOnError)
Cosmos Nicolaou78237372014-11-04 18:19:09 -080080 fl = flags.CreateAndRegister(fs, flags.ACL)
81 args = []string{"--veyron.acl=noname"}
82 err = fl.Parse(args)
83 if err == nil {
84 t.Fatalf("expected this to fail!")
85 }
Cosmos Nicolaoud811b072014-10-28 17:46:27 -070086}
87
88func TestFlagsGroups(t *testing.T) {
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -070089 fl := flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Runtime, flags.Listen)
Cosmos Nicolaoud811b072014-10-28 17:46:27 -070090 if got, want := fl.HasGroup(flags.Listen), true; got != want {
91 t.Errorf("got %t, want %t", got, want)
92 }
93 addr := "192.168.10.1:0"
94 roots := []string{"ab:cd:ef"}
95 args := []string{"--veyron.tcp.address=" + addr, "--veyron.namespace.root=" + roots[0]}
96 fl.Parse(args)
97 lf := fl.ListenFlags()
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -070098 if got, want := fl.RuntimeFlags().NamespaceRoots, roots; !reflect.DeepEqual(got, want) {
Cosmos Nicolaoud811b072014-10-28 17:46:27 -070099 t.Errorf("got %v, want %v", got, want)
100 }
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -0800101 if got, want := lf.Addrs[0].Address, addr; got != want {
Cosmos Nicolaoud811b072014-10-28 17:46:27 -0700102 t.Errorf("got %q, want %q", got, want)
103 }
104}
105
Asim Shankar95910b62014-10-31 22:02:29 -0700106const (
107 rootEnvVar = consts.NamespaceRootPrefix
108 rootEnvVar0 = consts.NamespaceRootPrefix + "0"
109)
Cosmos Nicolaoud811b072014-10-28 17:46:27 -0700110
111func TestEnvVars(t *testing.T) {
Asim Shankar95910b62014-10-31 22:02:29 -0700112 oldcreds := os.Getenv(consts.VeyronCredentials)
113 defer os.Setenv(consts.VeyronCredentials, oldcreds)
Cosmos Nicolaoud811b072014-10-28 17:46:27 -0700114
115 oldroot := os.Getenv(rootEnvVar)
116 oldroot0 := os.Getenv(rootEnvVar0)
117 defer os.Setenv(rootEnvVar, oldroot)
118 defer os.Setenv(rootEnvVar0, oldroot0)
119
Asim Shankar95910b62014-10-31 22:02:29 -0700120 os.Setenv(consts.VeyronCredentials, "bar")
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -0700121 fl := flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Runtime)
Cosmos Nicolaoud811b072014-10-28 17:46:27 -0700122 if err := fl.Parse([]string{}); err != nil {
123 t.Fatalf("unexpected error: %s", err)
124 }
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -0700125 rtf := fl.RuntimeFlags()
126 if got, want := rtf.Credentials, "bar"; got != want {
Cosmos Nicolaoud811b072014-10-28 17:46:27 -0700127 t.Errorf("got %q, want %q", got, want)
128 }
129
130 if err := fl.Parse([]string{"--veyron.credentials=baz"}); err != nil {
131 t.Fatalf("unexpected error: %s", err)
132 }
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -0700133 rtf = fl.RuntimeFlags()
134 if got, want := rtf.Credentials, "baz"; got != want {
Cosmos Nicolaoud811b072014-10-28 17:46:27 -0700135 t.Errorf("got %q, want %q", got, want)
136 }
137
138 os.Setenv(rootEnvVar, "a:1")
139 os.Setenv(rootEnvVar0, "a:2")
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -0700140 fl = flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Runtime)
Cosmos Nicolaoud811b072014-10-28 17:46:27 -0700141 if err := fl.Parse([]string{}); err != nil {
142 t.Fatalf("unexpected error: %s", err)
143 }
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -0700144 rtf = fl.RuntimeFlags()
145 if got, want := rtf.NamespaceRoots, []string{"a:1", "a:2"}; !reflect.DeepEqual(got, want) {
Cosmos Nicolaoud811b072014-10-28 17:46:27 -0700146 t.Errorf("got %q, want %q", got, want)
147 }
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -0700148 if err := fl.Parse([]string{"--veyron.namespace.root=b:1", "--veyron.namespace.root=b:2", "--veyron.namespace.root=b:3", "--veyron.credentials=b:4"}); err != nil {
Cosmos Nicolaoud811b072014-10-28 17:46:27 -0700149 t.Fatalf("unexpected error: %s", err)
150 }
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -0700151 rtf = fl.RuntimeFlags()
152 if got, want := rtf.NamespaceRoots, []string{"b:1", "b:2", "b:3"}; !reflect.DeepEqual(got, want) {
Cosmos Nicolaoud811b072014-10-28 17:46:27 -0700153 t.Errorf("got %q, want %q", got, want)
154 }
Cosmos Nicolaoue5b41502014-10-29 22:55:09 -0700155 if got, want := rtf.Credentials, "b:4"; got != want {
156 t.Errorf("got %q, want %q", got, want)
157 }
Cosmos Nicolaoubdc917c2014-10-24 12:41:47 -0700158}
Cosmos Nicolaou98960042014-10-31 00:05:51 -0700159
160func TestDefaults(t *testing.T) {
161 oldroot := os.Getenv(rootEnvVar)
162 oldroot0 := os.Getenv(rootEnvVar0)
163 defer os.Setenv(rootEnvVar, oldroot)
164 defer os.Setenv(rootEnvVar0, oldroot0)
165
166 os.Setenv(rootEnvVar, "")
167 os.Setenv(rootEnvVar0, "")
168
Cosmos Nicolaou78237372014-11-04 18:19:09 -0800169 fl := flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Runtime, flags.ACL)
Cosmos Nicolaou98960042014-10-31 00:05:51 -0700170 if err := fl.Parse([]string{}); err != nil {
171 t.Fatalf("unexpected error: %s", err)
172 }
173 rtf := fl.RuntimeFlags()
Robin Thellend8fea01c2014-12-11 13:48:10 -0800174 if got, want := rtf.NamespaceRoots, []string{"/ns.dev.v.io:8101"}; !reflect.DeepEqual(got, want) {
Cosmos Nicolaou98960042014-10-31 00:05:51 -0700175 t.Errorf("got %q, want %q", got, want)
176 }
Cosmos Nicolaou78237372014-11-04 18:19:09 -0800177 aclf := fl.ACLFlags()
Cosmos Nicolaouc7ddcf02014-11-05 16:26:56 -0800178 if got, want := aclf.ACLFile(""), ""; got != want {
Cosmos Nicolaou78237372014-11-04 18:19:09 -0800179 t.Errorf("got %q, want %q", got, want)
180 }
Cosmos Nicolaou98960042014-10-31 00:05:51 -0700181}
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -0800182
183func TestListenFlags(t *testing.T) {
184 fl := flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Listen)
185 if err := fl.Parse([]string{}); err != nil {
186 t.Fatalf("unexpected error: %s", err)
187 }
188 lf := fl.ListenFlags()
189 if got, want := len(lf.Addrs), 1; got != want {
190 t.Errorf("got %d, want %d", got, want)
191 }
192 def := struct{ Protocol, Address string }{"tcp", ":0"}
193 if got, want := lf.Addrs[0], def; !reflect.DeepEqual(got, want) {
194 t.Errorf("got %v, want %v", got, want)
195 }
196
197 fl = flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Listen)
198 if err := fl.Parse([]string{
199 "--veyron.tcp.address=172.0.0.1:10", "--veyron.tcp.protocol=ws", "--veyron.tcp.address=127.0.0.10:34", "--veyron.tcp.protocol=tcp6", "--veyron.tcp.address=172.0.0.100:100"}); err != nil {
200 t.Fatalf("unexpected error: %s", err)
201 }
202 lf = fl.ListenFlags()
203 if got, want := len(lf.Addrs), 3; got != want {
204 t.Errorf("got %d, want %d", got, want)
205 }
206 for i, p := range []string{"tcp", "ws", "tcp6"} {
207 if got, want := lf.Addrs[i].Protocol, p; got != want {
208 t.Errorf("got %q, want %q", got, want)
209 }
210 }
211 for i, p := range []string{"172.0.0.1:10", "127.0.0.10:34", "172.0.0.100:100"} {
212 if got, want := lf.Addrs[i].Address, p; got != want {
213 t.Errorf("got %q, want %q", got, want)
214 }
215 }
216}
Cosmos Nicolaou96fa9172014-12-16 12:57:18 -0800217
218func TestDuplicateFlags(t *testing.T) {
219 fl := flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Listen)
220 if err := fl.Parse([]string{
221 "--veyron.tcp.address=172.0.0.1:10", "--veyron.tcp.address=172.0.0.1:10", "--veyron.tcp.address=172.0.0.1:34", "--veyron.tcp.protocol=ws", "--veyron.tcp.address=172.0.0.1:10", "--veyron.tcp.address=172.0.0.1:34", "--veyron.tcp.address=172.0.0.1:34"}); err != nil {
222 t.Fatalf("unexpected error: %s", err)
223 }
224 lf := fl.ListenFlags()
225 if got, want := len(lf.Addrs), 4; got != want {
226 t.Errorf("got %d, want %d", got, want)
227 }
228 expected := flags.ListenAddrs{
229 {"tcp", "172.0.0.1:10"},
230 {"tcp", "172.0.0.1:34"},
231 {"ws", "172.0.0.1:10"},
232 {"ws", "172.0.0.1:34"},
233 }
234 if got, want := lf.Addrs, expected; !reflect.DeepEqual(got, want) {
235 t.Fatalf("got %#v, want %#v", got, want)
236 }
237 if err := fl.Parse([]string{
238 "--veyron.tcp.address=172.0.0.1:10", "--veyron.tcp.address=172.0.0.1:10", "--veyron.tcp.address=172.0.0.1:34", "--veyron.tcp.protocol=ws", "--veyron.tcp.address=172.0.0.1:10", "--veyron.tcp.address=127.0.0.1:34", "--veyron.tcp.address=127.0.0.1:34"}); err != nil {
239 t.Fatalf("unexpected error: %s", err)
240 }
241 if got, want := len(lf.Addrs), 4; got != want {
242 t.Errorf("got %d, want %d", got, want)
243 }
244 if got, want := lf.Addrs, expected; !reflect.DeepEqual(got, want) {
245 t.Fatalf("got %#v, want %#v", got, want)
246 }
247
248 fl = flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Runtime)
249
250 if err := fl.Parse([]string{"--veyron.namespace.root=ab", "--veyron.namespace.root=xy", "--veyron.namespace.root=ab"}); err != nil {
251 t.Fatalf("unexpected error: %s", err)
252 }
253
254 rf := fl.RuntimeFlags()
255 if got, want := len(rf.NamespaceRoots), 2; got != want {
256 t.Errorf("got %d, want %d", got, want)
257 }
258 if got, want := rf.NamespaceRoots, []string{"ab", "xy"}; !reflect.DeepEqual(got, want) {
259 t.Fatalf("got %#v, want %#v", got, want)
260 }
261}