ipaddressport_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package flags
  14. import (
  15. "testing"
  16. )
  17. func TestIPAddressPortSet(t *testing.T) {
  18. pass := []string{
  19. "1.2.3.4:8080",
  20. "10.1.1.1:80",
  21. }
  22. fail := []string{
  23. // bad IP specification
  24. ":4001",
  25. "127.0:8080",
  26. "123:456",
  27. // bad port specification
  28. "127.0.0.1:foo",
  29. "127.0.0.1:",
  30. // unix sockets not supported
  31. "unix://",
  32. "unix://tmp/etcd.sock",
  33. // bad strings
  34. "somewhere",
  35. "234#$",
  36. "file://foo/bar",
  37. "http://hello",
  38. }
  39. for i, tt := range pass {
  40. f := &IPAddressPort{}
  41. if err := f.Set(tt); err != nil {
  42. t.Errorf("#%d: unexpected error from IPAddressPort.Set(%q): %v", i, tt, err)
  43. }
  44. }
  45. for i, tt := range fail {
  46. f := &IPAddressPort{}
  47. if err := f.Set(tt); err == nil {
  48. t.Errorf("#%d: expected error from IPAddressPort.Set(%q)", i, tt)
  49. }
  50. }
  51. }
  52. func TestIPAddressPortString(t *testing.T) {
  53. f := &IPAddressPort{}
  54. if err := f.Set("127.0.0.1:4001"); err != nil {
  55. t.Fatalf("unexpected error: %v", err)
  56. }
  57. want := "127.0.0.1:4001"
  58. got := f.String()
  59. if want != got {
  60. t.Fatalf("IPAddressPort.String() value should be %q, got %q", want, got)
  61. }
  62. }