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 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 10 | "v.io/core/veyron/lib/flags" |
| 11 | "v.io/core/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 { |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 21 | t.Errorf("should have succeeded") |
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]} |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 26 | fl.Parse(args, nil) |
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) |
Robert Kroeger | 5096c4b | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 48 | args := []string{"--veyron.acl.file=runtime:foo.json", "--veyron.acl.file=bar:bar.json", "--veyron.acl.file=baz:bar:baz.json"} |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 49 | fl.Parse(args, nil) |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 50 | aclf := fl.ACLFlags() |
Robert Kroeger | 5096c4b | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 51 | |
Cosmos Nicolaou | c7ddcf0 | 2014-11-05 16:26:56 -0800 | [diff] [blame] | 52 | if got, want := aclf.ACLFile("runtime"), "foo.json"; got != want { |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 53 | t.Errorf("got %t, want %t", got, want) |
| 54 | } |
| 55 | if got, want := aclf.ACLFile("bar"), "bar.json"; got != want { |
| 56 | t.Errorf("got %t, want %t", got, want) |
| 57 | } |
| 58 | if got, want := aclf.ACLFile("wombat"), ""; got != want { |
| 59 | t.Errorf("got %t, want %t", got, want) |
| 60 | } |
| 61 | if got, want := aclf.ACLFile("baz"), "bar:baz.json"; got != want { |
| 62 | t.Errorf("got %t, want %t", got, want) |
| 63 | } |
| 64 | } |
| 65 | |
Robert Kroeger | 5096c4b | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 66 | func TestACLLiteralFlags(t *testing.T) { |
| 67 | fs := flag.NewFlagSet("test", flag.ContinueOnError) |
| 68 | fl := flags.CreateAndRegister(fs, flags.Runtime, flags.ACL) |
| 69 | args := []string{"--veyron.acl.literal=hedgehog"} |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 70 | fl.Parse(args, nil) |
Robert Kroeger | 5096c4b | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 71 | aclf := fl.ACLFlags() |
| 72 | |
| 73 | if got, want := aclf.ACLFile("runtime"), ""; got != want { |
| 74 | t.Errorf("got %t, want %t", got, want) |
| 75 | } |
| 76 | if got, want := aclf.ACLLiteral(), "hedgehog"; got != want { |
| 77 | t.Errorf("got %t, want %t, ok %t", got, want) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestACLLiteralBoth(t *testing.T) { |
| 82 | fs := flag.NewFlagSet("test", flag.ContinueOnError) |
| 83 | fl := flags.CreateAndRegister(fs, flags.Runtime, flags.ACL) |
| 84 | args := []string{"--veyron.acl.file=runtime:foo.json", "--veyron.acl.literal=hedgehog"} |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 85 | fl.Parse(args, nil) |
Robert Kroeger | 5096c4b | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 86 | aclf := fl.ACLFlags() |
| 87 | |
| 88 | if got, want := aclf.ACLFile("runtime"), "foo.json"; got != want { |
| 89 | t.Errorf("got %t, want %t", got, want) |
| 90 | } |
| 91 | if got, want := aclf.ACLLiteral(), "hedgehog"; got != want { |
| 92 | t.Errorf("got %t, want %t, ok %t", got, want) |
| 93 | } |
| 94 | } |
| 95 | |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 96 | func TestFlagError(t *testing.T) { |
| 97 | fs := flag.NewFlagSet("test", flag.ContinueOnError) |
| 98 | fs.SetOutput(ioutil.Discard) |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 99 | fl := flags.CreateAndRegister(fs, flags.Runtime) |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 100 | addr := "192.168.10.1:0" |
| 101 | args := []string{"--xxxveyron.tcp.address=" + addr, "not an arg"} |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 102 | err := fl.Parse(args, nil) |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 103 | if err == nil { |
| 104 | t.Fatalf("expected this to fail!") |
| 105 | } |
| 106 | if got, want := len(fl.Args()), 1; got != want { |
| 107 | t.Errorf("got %d, want %d [args: %v]", got, want, fl.Args()) |
| 108 | } |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 109 | |
| 110 | fs = flag.NewFlagSet("test", flag.ContinueOnError) |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 111 | fl = flags.CreateAndRegister(fs, flags.ACL) |
Robert Kroeger | 5096c4b | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 112 | args = []string{"--veyron.acl.file=noname"} |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 113 | err = fl.Parse(args, nil) |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 114 | if err == nil { |
| 115 | t.Fatalf("expected this to fail!") |
| 116 | } |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | func TestFlagsGroups(t *testing.T) { |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 120 | fl := flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Runtime, flags.Listen) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 121 | if got, want := fl.HasGroup(flags.Listen), true; got != want { |
| 122 | t.Errorf("got %t, want %t", got, want) |
| 123 | } |
| 124 | addr := "192.168.10.1:0" |
| 125 | roots := []string{"ab:cd:ef"} |
| 126 | args := []string{"--veyron.tcp.address=" + addr, "--veyron.namespace.root=" + roots[0]} |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 127 | fl.Parse(args, nil) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 128 | lf := fl.ListenFlags() |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 129 | if got, want := fl.RuntimeFlags().NamespaceRoots, roots; !reflect.DeepEqual(got, want) { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 130 | t.Errorf("got %v, want %v", got, want) |
| 131 | } |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 132 | if got, want := lf.Addrs[0].Address, addr; got != want { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 133 | t.Errorf("got %q, want %q", got, want) |
| 134 | } |
| 135 | } |
| 136 | |
Asim Shankar | 95910b6 | 2014-10-31 22:02:29 -0700 | [diff] [blame] | 137 | const ( |
| 138 | rootEnvVar = consts.NamespaceRootPrefix |
| 139 | rootEnvVar0 = consts.NamespaceRootPrefix + "0" |
| 140 | ) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 141 | |
| 142 | func TestEnvVars(t *testing.T) { |
Asim Shankar | 95910b6 | 2014-10-31 22:02:29 -0700 | [diff] [blame] | 143 | oldcreds := os.Getenv(consts.VeyronCredentials) |
| 144 | defer os.Setenv(consts.VeyronCredentials, oldcreds) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 145 | |
| 146 | oldroot := os.Getenv(rootEnvVar) |
| 147 | oldroot0 := os.Getenv(rootEnvVar0) |
| 148 | defer os.Setenv(rootEnvVar, oldroot) |
| 149 | defer os.Setenv(rootEnvVar0, oldroot0) |
| 150 | |
Asim Shankar | 95910b6 | 2014-10-31 22:02:29 -0700 | [diff] [blame] | 151 | os.Setenv(consts.VeyronCredentials, "bar") |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 152 | fl := flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Runtime) |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 153 | if err := fl.Parse([]string{}, nil); err != nil { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 154 | t.Fatalf("unexpected error: %s", err) |
| 155 | } |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 156 | rtf := fl.RuntimeFlags() |
| 157 | if got, want := rtf.Credentials, "bar"; got != want { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 158 | t.Errorf("got %q, want %q", got, want) |
| 159 | } |
| 160 | |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 161 | if err := fl.Parse([]string{"--veyron.credentials=baz"}, nil); err != nil { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 162 | t.Fatalf("unexpected error: %s", err) |
| 163 | } |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 164 | rtf = fl.RuntimeFlags() |
| 165 | if got, want := rtf.Credentials, "baz"; got != want { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 166 | t.Errorf("got %q, want %q", got, want) |
| 167 | } |
| 168 | |
| 169 | os.Setenv(rootEnvVar, "a:1") |
| 170 | os.Setenv(rootEnvVar0, "a:2") |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 171 | fl = flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Runtime) |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 172 | if err := fl.Parse([]string{}, nil); err != nil { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 173 | t.Fatalf("unexpected error: %s", err) |
| 174 | } |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 175 | rtf = fl.RuntimeFlags() |
| 176 | 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] | 177 | t.Errorf("got %q, want %q", got, want) |
| 178 | } |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 179 | if err := fl.Parse([]string{"--veyron.namespace.root=b:1", "--veyron.namespace.root=b:2", "--veyron.namespace.root=b:3", "--veyron.credentials=b:4"}, nil); err != nil { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 180 | t.Fatalf("unexpected error: %s", err) |
| 181 | } |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 182 | rtf = fl.RuntimeFlags() |
| 183 | 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] | 184 | t.Errorf("got %q, want %q", got, want) |
| 185 | } |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 186 | if got, want := rtf.Credentials, "b:4"; got != want { |
| 187 | t.Errorf("got %q, want %q", got, want) |
| 188 | } |
Cosmos Nicolaou | bdc917c | 2014-10-24 12:41:47 -0700 | [diff] [blame] | 189 | } |
Cosmos Nicolaou | 9896004 | 2014-10-31 00:05:51 -0700 | [diff] [blame] | 190 | |
| 191 | func TestDefaults(t *testing.T) { |
| 192 | oldroot := os.Getenv(rootEnvVar) |
| 193 | oldroot0 := os.Getenv(rootEnvVar0) |
| 194 | defer os.Setenv(rootEnvVar, oldroot) |
| 195 | defer os.Setenv(rootEnvVar0, oldroot0) |
| 196 | |
| 197 | os.Setenv(rootEnvVar, "") |
| 198 | os.Setenv(rootEnvVar0, "") |
| 199 | |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 200 | fl := flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Runtime, flags.ACL) |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 201 | if err := fl.Parse([]string{}, nil); err != nil { |
Cosmos Nicolaou | 9896004 | 2014-10-31 00:05:51 -0700 | [diff] [blame] | 202 | t.Fatalf("unexpected error: %s", err) |
| 203 | } |
| 204 | rtf := fl.RuntimeFlags() |
Robin Thellend | 8fea01c | 2014-12-11 13:48:10 -0800 | [diff] [blame] | 205 | 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] | 206 | t.Errorf("got %q, want %q", got, want) |
| 207 | } |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 208 | aclf := fl.ACLFlags() |
Cosmos Nicolaou | c7ddcf0 | 2014-11-05 16:26:56 -0800 | [diff] [blame] | 209 | if got, want := aclf.ACLFile(""), ""; got != want { |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 210 | t.Errorf("got %q, want %q", got, want) |
| 211 | } |
Cosmos Nicolaou | 9896004 | 2014-10-31 00:05:51 -0700 | [diff] [blame] | 212 | } |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 213 | |
| 214 | func TestListenFlags(t *testing.T) { |
| 215 | fl := flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Listen) |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 216 | if err := fl.Parse([]string{}, nil); err != nil { |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 217 | t.Fatalf("unexpected error: %s", err) |
| 218 | } |
| 219 | lf := fl.ListenFlags() |
| 220 | if got, want := len(lf.Addrs), 1; got != want { |
| 221 | t.Errorf("got %d, want %d", got, want) |
| 222 | } |
Nicolas LaCasse | 29d1d3a | 2015-01-22 10:48:18 -0800 | [diff] [blame] | 223 | |
| 224 | // Test the default protocol and address is "wsh" and ":0". |
| 225 | def := struct{ Protocol, Address string }{"wsh", ":0"} |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 226 | if got, want := lf.Addrs[0], def; !reflect.DeepEqual(got, want) { |
| 227 | t.Errorf("got %v, want %v", got, want) |
| 228 | } |
| 229 | |
| 230 | fl = flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Listen) |
| 231 | if err := fl.Parse([]string{ |
Nicolas LaCasse | 29d1d3a | 2015-01-22 10:48:18 -0800 | [diff] [blame] | 232 | "--veyron.tcp.address=172.0.0.1:10", // Will default to protocol "wsh". |
| 233 | "--veyron.tcp.protocol=tcp", "--veyron.tcp.address=127.0.0.10:34", |
| 234 | "--veyron.tcp.protocol=ws4", "--veyron.tcp.address=127.0.0.10:44", |
| 235 | "--veyron.tcp.protocol=tcp6", "--veyron.tcp.address=172.0.0.100:100"}, nil); err != nil { |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 236 | t.Fatalf("unexpected error: %s", err) |
| 237 | } |
| 238 | lf = fl.ListenFlags() |
Nicolas LaCasse | 29d1d3a | 2015-01-22 10:48:18 -0800 | [diff] [blame] | 239 | if got, want := len(lf.Addrs), 4; got != want { |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 240 | t.Errorf("got %d, want %d", got, want) |
| 241 | } |
Nicolas LaCasse | 29d1d3a | 2015-01-22 10:48:18 -0800 | [diff] [blame] | 242 | for i, p := range []string{"wsh", "tcp", "ws4", "tcp6"} { |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 243 | if got, want := lf.Addrs[i].Protocol, p; got != want { |
| 244 | t.Errorf("got %q, want %q", got, want) |
| 245 | } |
| 246 | } |
Nicolas LaCasse | 29d1d3a | 2015-01-22 10:48:18 -0800 | [diff] [blame] | 247 | for i, p := range []string{"172.0.0.1:10", "127.0.0.10:34", "127.0.0.10:44", "172.0.0.100:100"} { |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 248 | if got, want := lf.Addrs[i].Address, p; got != want { |
| 249 | t.Errorf("got %q, want %q", got, want) |
| 250 | } |
| 251 | } |
| 252 | } |
Cosmos Nicolaou | 96fa917 | 2014-12-16 12:57:18 -0800 | [diff] [blame] | 253 | |
| 254 | func TestDuplicateFlags(t *testing.T) { |
| 255 | fl := flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Listen) |
| 256 | if err := fl.Parse([]string{ |
Nicolas LaCasse | 29d1d3a | 2015-01-22 10:48:18 -0800 | [diff] [blame] | 257 | "--veyron.tcp.address=172.0.0.1:10", "--veyron.tcp.address=172.0.0.1:10", "--veyron.tcp.address=172.0.0.1:34", |
| 258 | "--veyron.tcp.protocol=tcp", "--veyron.tcp.address=172.0.0.1:10", "--veyron.tcp.address=172.0.0.1:10", "--veyron.tcp.address=172.0.0.1:34", |
| 259 | "--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"}, nil); err != nil { |
Cosmos Nicolaou | 96fa917 | 2014-12-16 12:57:18 -0800 | [diff] [blame] | 260 | t.Fatalf("unexpected error: %s", err) |
| 261 | } |
| 262 | lf := fl.ListenFlags() |
Nicolas LaCasse | 29d1d3a | 2015-01-22 10:48:18 -0800 | [diff] [blame] | 263 | if got, want := len(lf.Addrs), 6; got != want { |
Cosmos Nicolaou | 96fa917 | 2014-12-16 12:57:18 -0800 | [diff] [blame] | 264 | t.Errorf("got %d, want %d", got, want) |
| 265 | } |
| 266 | expected := flags.ListenAddrs{ |
Nicolas LaCasse | 29d1d3a | 2015-01-22 10:48:18 -0800 | [diff] [blame] | 267 | {"wsh", "172.0.0.1:10"}, |
| 268 | {"wsh", "172.0.0.1:34"}, |
Cosmos Nicolaou | 96fa917 | 2014-12-16 12:57:18 -0800 | [diff] [blame] | 269 | {"tcp", "172.0.0.1:10"}, |
| 270 | {"tcp", "172.0.0.1:34"}, |
| 271 | {"ws", "172.0.0.1:10"}, |
| 272 | {"ws", "172.0.0.1:34"}, |
| 273 | } |
| 274 | if got, want := lf.Addrs, expected; !reflect.DeepEqual(got, want) { |
| 275 | t.Fatalf("got %#v, want %#v", got, want) |
| 276 | } |
| 277 | if err := fl.Parse([]string{ |
Nicolas LaCasse | 29d1d3a | 2015-01-22 10:48:18 -0800 | [diff] [blame] | 278 | "--veyron.tcp.address=172.0.0.1:10", "--veyron.tcp.address=172.0.0.1:10", "--veyron.tcp.address=172.0.0.1:34", |
| 279 | "--veyron.tcp.protocol=tcp", "--veyron.tcp.address=172.0.0.1:10", "--veyron.tcp.address=127.0.0.1:34", "--veyron.tcp.address=127.0.0.1:34", |
| 280 | "--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"}, nil); err != nil { |
Cosmos Nicolaou | 96fa917 | 2014-12-16 12:57:18 -0800 | [diff] [blame] | 281 | t.Fatalf("unexpected error: %s", err) |
| 282 | } |
Nicolas LaCasse | 29d1d3a | 2015-01-22 10:48:18 -0800 | [diff] [blame] | 283 | if got, want := len(lf.Addrs), 6; got != want { |
Cosmos Nicolaou | 96fa917 | 2014-12-16 12:57:18 -0800 | [diff] [blame] | 284 | t.Errorf("got %d, want %d", got, want) |
| 285 | } |
| 286 | if got, want := lf.Addrs, expected; !reflect.DeepEqual(got, want) { |
| 287 | t.Fatalf("got %#v, want %#v", got, want) |
| 288 | } |
| 289 | |
| 290 | fl = flags.CreateAndRegister(flag.NewFlagSet("test", flag.ContinueOnError), flags.Runtime) |
| 291 | |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 292 | if err := fl.Parse([]string{"--veyron.namespace.root=ab", "--veyron.namespace.root=xy", "--veyron.namespace.root=ab"}, nil); err != nil { |
Cosmos Nicolaou | 96fa917 | 2014-12-16 12:57:18 -0800 | [diff] [blame] | 293 | t.Fatalf("unexpected error: %s", err) |
| 294 | } |
| 295 | |
| 296 | rf := fl.RuntimeFlags() |
| 297 | if got, want := len(rf.NamespaceRoots), 2; got != want { |
| 298 | t.Errorf("got %d, want %d", got, want) |
| 299 | } |
| 300 | if got, want := rf.NamespaceRoots, []string{"ab", "xy"}; !reflect.DeepEqual(got, want) { |
| 301 | t.Fatalf("got %#v, want %#v", got, want) |
| 302 | } |
| 303 | } |
Bogdan Caprita | bf356d7 | 2015-01-08 17:28:48 -0800 | [diff] [blame] | 304 | |
| 305 | func TestConfig(t *testing.T) { |
| 306 | fs := flag.NewFlagSet("test", flag.ContinueOnError) |
| 307 | var testFlag1, testFlag2 string |
| 308 | fs.StringVar(&testFlag1, "test_flag1", "default1", "") |
| 309 | fs.StringVar(&testFlag2, "test_flag2", "default2", "") |
| 310 | fl := flags.CreateAndRegister(fs, flags.Runtime) |
| 311 | args := []string{ |
| 312 | "--veyron.namespace.root=argRoot1", |
| 313 | "--veyron.namespace.root=argRoot2", |
| 314 | "--veyron.vtrace.cache_size=1234", |
| 315 | } |
| 316 | config := map[string]string{ |
| 317 | "veyron.namespace.root": "configRoot", |
| 318 | "veyron.credentials": "configCreds", |
| 319 | "veyron.vtrace.cache_size": "4321", |
| 320 | "test_flag1": "test value", |
| 321 | "flag.that.does.not.exist": "some value", |
| 322 | } |
| 323 | if err := fl.Parse(args, config); err != nil { |
| 324 | t.Errorf("Parse(%v, %v) failed: %v", args, config, err) |
| 325 | } |
| 326 | rtf := fl.RuntimeFlags() |
| 327 | if got, want := rtf.NamespaceRoots, []string{"argRoot1", "argRoot2", "configRoot"}; !reflect.DeepEqual(got, want) { |
| 328 | t.Errorf("Namespace roots: got %v, want %v", got, want) |
| 329 | } |
| 330 | if got, want := rtf.Credentials, "configCreds"; got != want { |
| 331 | t.Errorf("Credentials: got %v, want %v", got, want) |
| 332 | } |
| 333 | if got, want := testFlag1, "test value"; got != want { |
| 334 | t.Errorf("Test flag 1: got %v, want %v", got, want) |
| 335 | } |
| 336 | if got, want := testFlag2, "default2"; got != want { |
| 337 | t.Errorf("Test flag 2: got %v, want %v", got, want) |
| 338 | } |
| 339 | if got, want := rtf.Vtrace.CacheSize, 4321; got != want { |
| 340 | t.Errorf("Test flag 2: got %v, want %v", got, want) |
| 341 | } |
| 342 | } |