|
@@ -2,8 +2,13 @@ package pkg
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"flag"
|
|
"flag"
|
|
|
|
|
+ "net/url"
|
|
|
"os"
|
|
"os"
|
|
|
|
|
+ "reflect"
|
|
|
"testing"
|
|
"testing"
|
|
|
|
|
+
|
|
|
|
|
+ "github.com/coreos/etcd/pkg/flags"
|
|
|
|
|
+ "github.com/coreos/etcd/pkg/transport"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
func TestSetFlagsFromEnv(t *testing.T) {
|
|
func TestSetFlagsFromEnv(t *testing.T) {
|
|
@@ -49,3 +54,85 @@ func TestSetFlagsFromEnv(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func TestURLsFromFlags(t *testing.T) {
|
|
|
|
|
+ tests := []struct {
|
|
|
|
|
+ args []string
|
|
|
|
|
+ tlsInfo transport.TLSInfo
|
|
|
|
|
+ wantURLs []url.URL
|
|
|
|
|
+ wantFail bool
|
|
|
|
|
+ }{
|
|
|
|
|
+ // use -urls default when no flags defined
|
|
|
|
|
+ {
|
|
|
|
|
+ args: []string{},
|
|
|
|
|
+ tlsInfo: transport.TLSInfo{},
|
|
|
|
|
+ wantURLs: []url.URL{
|
|
|
|
|
+ url.URL{Scheme: "http", Host: "127.0.0.1:2379"},
|
|
|
|
|
+ },
|
|
|
|
|
+ wantFail: false,
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ // explicitly setting -urls should carry through
|
|
|
|
|
+ {
|
|
|
|
|
+ args: []string{"-urls=https://192.0.3.17:2930,http://127.0.0.1:1024"},
|
|
|
|
|
+ tlsInfo: transport.TLSInfo{},
|
|
|
|
|
+ wantURLs: []url.URL{
|
|
|
|
|
+ url.URL{Scheme: "https", Host: "192.0.3.17:2930"},
|
|
|
|
|
+ url.URL{Scheme: "http", Host: "127.0.0.1:1024"},
|
|
|
|
|
+ },
|
|
|
|
|
+ wantFail: false,
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ // explicitly setting -addr should carry through
|
|
|
|
|
+ {
|
|
|
|
|
+ args: []string{"-addr=192.0.2.3:1024"},
|
|
|
|
|
+ tlsInfo: transport.TLSInfo{},
|
|
|
|
|
+ wantURLs: []url.URL{
|
|
|
|
|
+ url.URL{Scheme: "http", Host: "192.0.2.3:1024"},
|
|
|
|
|
+ },
|
|
|
|
|
+ wantFail: false,
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ // scheme prepended to -addr should be https if TLSInfo non-empty
|
|
|
|
|
+ {
|
|
|
|
|
+ args: []string{"-addr=192.0.2.3:1024"},
|
|
|
|
|
+ tlsInfo: transport.TLSInfo{
|
|
|
|
|
+ CertFile: "/tmp/foo",
|
|
|
|
|
+ KeyFile: "/tmp/bar",
|
|
|
|
|
+ },
|
|
|
|
|
+ wantURLs: []url.URL{
|
|
|
|
|
+ url.URL{Scheme: "https", Host: "192.0.2.3:1024"},
|
|
|
|
|
+ },
|
|
|
|
|
+ wantFail: false,
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ // explicitly setting both -urls and -addr should fail
|
|
|
|
|
+ {
|
|
|
|
|
+ args: []string{"-urls=https://127.0.0.1:1024", "-addr=192.0.2.3:1024"},
|
|
|
|
|
+ tlsInfo: transport.TLSInfo{},
|
|
|
|
|
+ wantURLs: nil,
|
|
|
|
|
+ wantFail: true,
|
|
|
|
|
+ },
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for i, tt := range tests {
|
|
|
|
|
+ fs := flag.NewFlagSet("test", flag.PanicOnError)
|
|
|
|
|
+ fs.Var(flags.NewURLs("http://127.0.0.1:2379"), "urls", "")
|
|
|
|
|
+ fs.Var(&flags.IPAddressPort{}, "addr", "")
|
|
|
|
|
+
|
|
|
|
|
+ if err := fs.Parse(tt.args); err != nil {
|
|
|
|
|
+ t.Errorf("#%d: failed to parse flags: %v", i, err)
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ gotURLs, err := URLsFromFlags(fs, "urls", "addr", tt.tlsInfo)
|
|
|
|
|
+ if tt.wantFail != (err != nil) {
|
|
|
|
|
+ t.Errorf("#%d: wantFail=%t, got err=%v", i, tt.wantFail, err)
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if !reflect.DeepEqual(tt.wantURLs, gotURLs) {
|
|
|
|
|
+ t.Errorf("#%d: incorrect URLs\nwant=%#v\ngot=%#v", i, tt.wantURLs, gotURLs)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|