Browse Source

Remove cli rpc functions

pull/2729/head
Kyle Havlovitz 8 years ago
parent
commit
a8eaad3ba4
No known key found for this signature in database
GPG Key ID: 8A5E6B173056AD6C
  1. 16
      command/exec_test.go
  2. 51
      command/rpc.go
  3. 90
      command/rpc_test.go

16
command/exec_test.go

@ -82,7 +82,7 @@ func TestExecCommandRun_CrossDC(t *testing.T) {
}
func waitForLeader(t *testing.T, httpAddr string) {
client, err := HTTPClient(httpAddr)
client, err := httpClient(httpAddr)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -94,6 +94,12 @@ func waitForLeader(t *testing.T, httpAddr string) {
})
}
func httpClient(addr string) (*consulapi.Client, error) {
conf := consulapi.DefaultConfig()
conf.Address = addr
return consulapi.NewClient(conf)
}
func TestExecCommand_Validate(t *testing.T) {
conf := &rExecConf{}
err := conf.validate()
@ -134,7 +140,7 @@ func TestExecCommand_Sessions(t *testing.T) {
defer a1.Shutdown()
waitForLeader(t, a1.httpAddr)
client, err := HTTPClient(a1.httpAddr)
client, err := httpClient(a1.httpAddr)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -175,7 +181,7 @@ func TestExecCommand_Sessions_Foreign(t *testing.T) {
defer a1.Shutdown()
waitForLeader(t, a1.httpAddr)
client, err := HTTPClient(a1.httpAddr)
client, err := httpClient(a1.httpAddr)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -226,7 +232,7 @@ func TestExecCommand_UploadDestroy(t *testing.T) {
defer a1.Shutdown()
waitForLeader(t, a1.httpAddr)
client, err := HTTPClient(a1.httpAddr)
client, err := httpClient(a1.httpAddr)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -283,7 +289,7 @@ func TestExecCommand_StreamResults(t *testing.T) {
defer a1.Shutdown()
waitForLeader(t, a1.httpAddr)
client, err := HTTPClient(a1.httpAddr)
client, err := httpClient(a1.httpAddr)
if err != nil {
t.Fatalf("err: %v", err)
}

51
command/rpc.go

@ -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)
}

90
command/rpc_test.go

@ -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…
Cancel
Save