Cosmos Nicolaou | bdc917c | 2014-10-24 12:41:47 -0700 | [diff] [blame] | 1 | package flags_test |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 5 | "io/ioutil" |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 6 | "os" |
| 7 | "reflect" |
Cosmos Nicolaou | bdc917c | 2014-10-24 12:41:47 -0700 | [diff] [blame] | 8 | "testing" |
| 9 | |
| 10 | "veyron.io/veyron/veyron/lib/flags" |
Asim Shankar | 95910b6 | 2014-10-31 22:02:29 -0700 | [diff] [blame] | 11 | "veyron.io/veyron/veyron/lib/flags/consts" |
Cosmos Nicolaou | bdc917c | 2014-10-24 12:41:47 -0700 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | func TestFlags(t *testing.T) { |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 15 | fs := flag.NewFlagSet("test", flag.ContinueOnError) |
| 16 | if flags.CreateAndRegister(fs) != nil { |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 17 | t.Fatalf("should have returned a nil value") |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 18 | } |
| 19 | fl := flags.CreateAndRegister(fs, flags.Runtime) |
| 20 | if fl == nil { |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 21 | t.Errorf("should have failed") |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 22 | } |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 23 | creds := "creddir" |
| 24 | roots := []string{"ab:cd:ef"} |
| 25 | args := []string{"--veyron.credentials=" + creds, "--veyron.namespace.root=" + roots[0]} |
| 26 | fl.Parse(args) |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 27 | rtf := fl.RuntimeFlags() |
| 28 | if got, want := rtf.NamespaceRoots, roots; !reflect.DeepEqual(got, want) { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 29 | t.Errorf("got %v, want %v", got, want) |
Cosmos Nicolaou | bdc917c | 2014-10-24 12:41:47 -0700 | [diff] [blame] | 30 | } |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 31 | if got, want := rtf.Credentials, creds; !reflect.DeepEqual(got, want) { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 32 | t.Errorf("got %v, want %v", got, want) |
Cosmos Nicolaou | bdc917c | 2014-10-24 12:41:47 -0700 | [diff] [blame] | 33 | } |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 34 | 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 Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 38 | rtf.NamespaceRoots[0] = "oooh" |
| 39 | rtf = fl.RuntimeFlags() |
| 40 | if got, want := rtf.NamespaceRoots, roots; !reflect.DeepEqual(got, want) { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 41 | t.Errorf("got %v, want %v", got, want) |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 45 | func TestACLFlags(t *testing.T) { |
| 46 | fs := flag.NewFlagSet("test", flag.ContinueOnError) |
| 47 | fl := flags.CreateAndRegister(fs, flags.Runtime, flags.ACL) |
Cosmos Nicolaou | c7ddcf0 | 2014-11-05 16:26:56 -0800 | [diff] [blame] | 48 | args := []string{"--veyron.acl=runtime:foo.json", "--veyron.acl=bar:bar.json", "--veyron.acl=baz:bar:baz.json"} |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 49 | fl.Parse(args) |
| 50 | aclf := fl.ACLFlags() |
Cosmos Nicolaou | c7ddcf0 | 2014-11-05 16:26:56 -0800 | [diff] [blame] | 51 | if got, want := aclf.ACLFile("runtime"), "foo.json"; got != want { |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 52 | 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 Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 65 | func TestFlagError(t *testing.T) { |
| 66 | fs := flag.NewFlagSet("test", flag.ContinueOnError) |
| 67 | fs.SetOutput(ioutil.Discard) |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 68 | fl := flags.CreateAndRegister(fs, flags.Runtime) |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 69 | 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 Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 78 | |
| 79 | fs = flag.NewFlagSet("test", flag.ContinueOnError) |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 80 | 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 Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | func TestFlagsGroups(t *testing.T) { |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 89 | fl := flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Runtime, flags.Listen) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 90 | 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 Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 98 | if got, want := fl.RuntimeFlags().NamespaceRoots, roots; !reflect.DeepEqual(got, want) { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 99 | t.Errorf("got %v, want %v", got, want) |
| 100 | } |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 101 | if got, want := lf.Addrs[0].Address, addr; got != want { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 102 | t.Errorf("got %q, want %q", got, want) |
| 103 | } |
| 104 | } |
| 105 | |
Asim Shankar | 95910b6 | 2014-10-31 22:02:29 -0700 | [diff] [blame] | 106 | const ( |
| 107 | rootEnvVar = consts.NamespaceRootPrefix |
| 108 | rootEnvVar0 = consts.NamespaceRootPrefix + "0" |
| 109 | ) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 110 | |
| 111 | func TestEnvVars(t *testing.T) { |
Asim Shankar | 95910b6 | 2014-10-31 22:02:29 -0700 | [diff] [blame] | 112 | oldcreds := os.Getenv(consts.VeyronCredentials) |
| 113 | defer os.Setenv(consts.VeyronCredentials, oldcreds) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 114 | |
| 115 | oldroot := os.Getenv(rootEnvVar) |
| 116 | oldroot0 := os.Getenv(rootEnvVar0) |
| 117 | defer os.Setenv(rootEnvVar, oldroot) |
| 118 | defer os.Setenv(rootEnvVar0, oldroot0) |
| 119 | |
Asim Shankar | 95910b6 | 2014-10-31 22:02:29 -0700 | [diff] [blame] | 120 | os.Setenv(consts.VeyronCredentials, "bar") |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 121 | fl := flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Runtime) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 122 | if err := fl.Parse([]string{}); err != nil { |
| 123 | t.Fatalf("unexpected error: %s", err) |
| 124 | } |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 125 | rtf := fl.RuntimeFlags() |
| 126 | if got, want := rtf.Credentials, "bar"; got != want { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 127 | 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 Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 133 | rtf = fl.RuntimeFlags() |
| 134 | if got, want := rtf.Credentials, "baz"; got != want { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 135 | t.Errorf("got %q, want %q", got, want) |
| 136 | } |
| 137 | |
| 138 | os.Setenv(rootEnvVar, "a:1") |
| 139 | os.Setenv(rootEnvVar0, "a:2") |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 140 | fl = flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Runtime) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 141 | if err := fl.Parse([]string{}); err != nil { |
| 142 | t.Fatalf("unexpected error: %s", err) |
| 143 | } |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 144 | rtf = fl.RuntimeFlags() |
| 145 | if got, want := rtf.NamespaceRoots, []string{"a:1", "a:2"}; !reflect.DeepEqual(got, want) { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 146 | t.Errorf("got %q, want %q", got, want) |
| 147 | } |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 148 | 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 Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 149 | t.Fatalf("unexpected error: %s", err) |
| 150 | } |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 151 | rtf = fl.RuntimeFlags() |
| 152 | if got, want := rtf.NamespaceRoots, []string{"b:1", "b:2", "b:3"}; !reflect.DeepEqual(got, want) { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 153 | t.Errorf("got %q, want %q", got, want) |
| 154 | } |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 155 | if got, want := rtf.Credentials, "b:4"; got != want { |
| 156 | t.Errorf("got %q, want %q", got, want) |
| 157 | } |
Cosmos Nicolaou | bdc917c | 2014-10-24 12:41:47 -0700 | [diff] [blame] | 158 | } |
Cosmos Nicolaou | 9896004 | 2014-10-31 00:05:51 -0700 | [diff] [blame] | 159 | |
| 160 | func 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 Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 169 | fl := flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Runtime, flags.ACL) |
Cosmos Nicolaou | 9896004 | 2014-10-31 00:05:51 -0700 | [diff] [blame] | 170 | if err := fl.Parse([]string{}); err != nil { |
| 171 | t.Fatalf("unexpected error: %s", err) |
| 172 | } |
| 173 | rtf := fl.RuntimeFlags() |
Robin Thellend | 8fea01c | 2014-12-11 13:48:10 -0800 | [diff] [blame] | 174 | if got, want := rtf.NamespaceRoots, []string{"/ns.dev.v.io:8101"}; !reflect.DeepEqual(got, want) { |
Cosmos Nicolaou | 9896004 | 2014-10-31 00:05:51 -0700 | [diff] [blame] | 175 | t.Errorf("got %q, want %q", got, want) |
| 176 | } |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 177 | aclf := fl.ACLFlags() |
Cosmos Nicolaou | c7ddcf0 | 2014-11-05 16:26:56 -0800 | [diff] [blame] | 178 | if got, want := aclf.ACLFile(""), ""; got != want { |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 179 | t.Errorf("got %q, want %q", got, want) |
| 180 | } |
Cosmos Nicolaou | 9896004 | 2014-10-31 00:05:51 -0700 | [diff] [blame] | 181 | } |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 182 | |
| 183 | func 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 Nicolaou | 96fa917 | 2014-12-16 12:57:18 -0800 | [diff] [blame] | 217 | |
| 218 | func 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 | } |