2014-12-15 18:16:43 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2016-08-03 06:10:11 +00:00
|
|
|
|
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/command/agent"
|
2014-12-15 18:16:43 +00:00
|
|
|
)
|
|
|
|
|
2015-01-17 01:45:13 +00:00
|
|
|
const (
|
|
|
|
defaultRPC = "127.0.0.1:8400"
|
|
|
|
defaultHTTP = "127.0.0.1:8500"
|
|
|
|
)
|
|
|
|
|
|
|
|
type flagFunc func(f *flag.FlagSet) *string
|
2014-12-15 18:16:43 +00:00
|
|
|
|
2015-01-17 01:45:13 +00:00
|
|
|
func getParsedAddr(t *testing.T, addrType, cliVal, envVal string) string {
|
|
|
|
var cliFlag, envVar string
|
|
|
|
var fn flagFunc
|
2014-12-15 18:16:43 +00:00
|
|
|
args := []string{}
|
|
|
|
|
2015-01-17 01:45:13 +00:00
|
|
|
switch addrType {
|
|
|
|
case "rpc":
|
|
|
|
fn = RPCAddrFlag
|
2016-08-03 06:10:11 +00:00
|
|
|
envVar = agent.RPCAddrEnvName
|
2015-01-17 01:45:13 +00:00
|
|
|
cliFlag = "-rpc-addr"
|
|
|
|
case "http":
|
|
|
|
fn = HTTPAddrFlag
|
2016-08-03 06:10:11 +00:00
|
|
|
envVar = consulapi.HTTPAddrEnvName
|
2015-01-17 01:45:13 +00:00
|
|
|
cliFlag = "-http-addr"
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown address type %s", addrType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cliVal != "" {
|
|
|
|
args = append(args, cliFlag+"="+cliVal)
|
2014-12-15 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Clearenv()
|
2015-01-17 01:45:13 +00:00
|
|
|
if envVal != "" {
|
|
|
|
os.Setenv(envVar, envVal)
|
2014-12-15 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
2015-01-17 01:45:13 +00:00
|
|
|
cmdFlags := flag.NewFlagSet(addrType, flag.ContinueOnError)
|
|
|
|
result := fn(cmdFlags)
|
2014-12-15 18:16:43 +00:00
|
|
|
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
t.Fatal("Parse error", err)
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:45:13 +00:00
|
|
|
return *result
|
2014-12-15 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
2015-01-17 01:45:13 +00:00
|
|
|
func TestAddrFlag_default(t *testing.T) {
|
|
|
|
for a, def := range map[string]string{
|
|
|
|
"rpc": defaultRPC,
|
|
|
|
"http": defaultHTTP,
|
|
|
|
} {
|
|
|
|
res := getParsedAddr(t, a, "", "")
|
2014-12-15 18:16:43 +00:00
|
|
|
|
2015-01-17 01:45:13 +00:00
|
|
|
if res != def {
|
2015-01-17 06:57:11 +00:00
|
|
|
t.Fatalf("Expected addr: %s, got: %s", def, res)
|
2015-01-17 01:45:13 +00:00
|
|
|
}
|
2014-12-15 18:16:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:45:13 +00:00
|
|
|
func TestAddrFlag_onlyEnv(t *testing.T) {
|
|
|
|
envAddr := "4.4.4.4:1234"
|
|
|
|
for _, a := range []string{"rpc", "http"} {
|
|
|
|
res := getParsedAddr(t, a, "", envAddr)
|
2014-12-15 18:16:43 +00:00
|
|
|
|
2015-01-17 01:45:13 +00:00
|
|
|
if res != envAddr {
|
|
|
|
t.Fatalf("Expected %s addr: %s, got: %s", a, envAddr, res)
|
|
|
|
}
|
2014-12-15 18:16:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:45:13 +00:00
|
|
|
func TestAddrFlag_precedence(t *testing.T) {
|
|
|
|
cliAddr := "8.8.8.8:8400"
|
|
|
|
envAddr := "4.4.4.4:8400"
|
|
|
|
for _, a := range []string{"rpc", "http"} {
|
|
|
|
res := getParsedAddr(t, a, cliAddr, envAddr)
|
2014-12-15 18:16:43 +00:00
|
|
|
|
2015-01-17 01:45:13 +00:00
|
|
|
if res != cliAddr {
|
|
|
|
t.Fatalf("Expected %s addr: %s, got: %s", a, cliAddr, res)
|
|
|
|
}
|
2014-12-15 18:16:43 +00:00
|
|
|
}
|
|
|
|
}
|