mirror of https://github.com/hashicorp/consul
Kyle Havlovitz
8 years ago
3 changed files with 11 additions and 146 deletions
@ -1,51 +0,0 @@
|
||||
package command |
||||
|
||||
import ( |
||||
"flag" |
||||
"os" |
||||
|
||||
consulapi "github.com/hashicorp/consul/api" |
||||
"github.com/hashicorp/consul/command/agent" |
||||
) |
||||
|
||||
// RPCAddrFlag returns a pointer to a string that will be populated
|
||||
// when the given flagset is parsed with the RPC address of the Consul.
|
||||
func RPCAddrFlag(f *flag.FlagSet) *string { |
||||
defaultRPCAddr := os.Getenv(agent.RPCAddrEnvName) |
||||
if defaultRPCAddr == "" { |
||||
defaultRPCAddr = "127.0.0.1:8400" |
||||
} |
||||
return f.String("rpc-addr", defaultRPCAddr, |
||||
"RPC address of the Consul agent") |
||||
} |
||||
|
||||
// RPCClient returns a new Consul RPC client with the given address.
|
||||
func RPCClient(addr string) (*agent.RPCClient, error) { |
||||
return agent.NewRPCClient(addr) |
||||
} |
||||
|
||||
// HTTPAddrFlag returns a pointer to a string that will be populated
|
||||
// when the given flagset is parsed with the HTTP address of the Consul.
|
||||
func HTTPAddrFlag(f *flag.FlagSet) *string { |
||||
defaultHTTPAddr := os.Getenv(consulapi.HTTPAddrEnvName) |
||||
if defaultHTTPAddr == "" { |
||||
defaultHTTPAddr = "127.0.0.1:8500" |
||||
} |
||||
return f.String("http-addr", defaultHTTPAddr, |
||||
"HTTP address of the Consul agent") |
||||
} |
||||
|
||||
// HTTPClient returns a new Consul HTTP client with the given address.
|
||||
func HTTPClient(addr string) (*consulapi.Client, error) { |
||||
return HTTPClientConfig(func(c *consulapi.Config) { |
||||
c.Address = addr |
||||
}) |
||||
} |
||||
|
||||
// HTTPClientConfig is used to return a new API client and modify its
|
||||
// configuration by passing in a config modifier function.
|
||||
func HTTPClientConfig(fn func(c *consulapi.Config)) (*consulapi.Client, error) { |
||||
conf := consulapi.DefaultConfig() |
||||
fn(conf) |
||||
return consulapi.NewClient(conf) |
||||
} |
@ -1,90 +0,0 @@
|
||||
package command |
||||
|
||||
import ( |
||||
"flag" |
||||
"os" |
||||
"testing" |
||||
|
||||
consulapi "github.com/hashicorp/consul/api" |
||||
"github.com/hashicorp/consul/command/agent" |
||||
) |
||||
|
||||
const ( |
||||
defaultRPC = "127.0.0.1:8400" |
||||
defaultHTTP = "127.0.0.1:8500" |
||||
) |
||||
|
||||
type flagFunc func(f *flag.FlagSet) *string |
||||
|
||||
func getParsedAddr(t *testing.T, addrType, cliVal, envVal string) string { |
||||
var cliFlag, envVar string |
||||
var fn flagFunc |
||||
args := []string{} |
||||
|
||||
switch addrType { |
||||
case "rpc": |
||||
fn = RPCAddrFlag |
||||
envVar = agent.RPCAddrEnvName |
||||
cliFlag = "-rpc-addr" |
||||
case "http": |
||||
fn = HTTPAddrFlag |
||||
envVar = consulapi.HTTPAddrEnvName |
||||
cliFlag = "-http-addr" |
||||
default: |
||||
t.Fatalf("unknown address type %s", addrType) |
||||
} |
||||
|
||||
if cliVal != "" { |
||||
args = append(args, cliFlag+"="+cliVal) |
||||
} |
||||
|
||||
os.Clearenv() |
||||
if envVal != "" { |
||||
os.Setenv(envVar, envVal) |
||||
} |
||||
|
||||
cmdFlags := flag.NewFlagSet(addrType, flag.ContinueOnError) |
||||
result := fn(cmdFlags) |
||||
|
||||
if err := cmdFlags.Parse(args); err != nil { |
||||
t.Fatal("Parse error", err) |
||||
} |
||||
|
||||
return *result |
||||
} |
||||
|
||||
func TestAddrFlag_default(t *testing.T) { |
||||
for a, def := range map[string]string{ |
||||
"rpc": defaultRPC, |
||||
"http": defaultHTTP, |
||||
} { |
||||
res := getParsedAddr(t, a, "", "") |
||||
|
||||
if res != def { |
||||
t.Fatalf("Expected addr: %s, got: %s", def, res) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestAddrFlag_onlyEnv(t *testing.T) { |
||||
envAddr := "4.4.4.4:1234" |
||||
for _, a := range []string{"rpc", "http"} { |
||||
res := getParsedAddr(t, a, "", envAddr) |
||||
|
||||
if res != envAddr { |
||||
t.Fatalf("Expected %s addr: %s, got: %s", a, envAddr, res) |
||||
} |
||||
} |
||||
} |
||||
|
||||
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) |
||||
|
||||
if res != cliAddr { |
||||
t.Fatalf("Expected %s addr: %s, got: %s", a, cliAddr, res) |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue