mirror of https://github.com/hashicorp/consul
Step 3: fix a bug in api.NewClient and fix the tests
The api client should never rever to HTTP if the user explicitly requested TLS. This change broke some tests because the tests always use an non-TLS http server, but some tests explicitly enable TLS.pull/7608/head
parent
1a8ffec6a7
commit
0888c6575b
|
@ -551,11 +551,11 @@ func NewClient(config *Config) (*Client, error) {
|
||||||
// bootstrap the config
|
// bootstrap the config
|
||||||
defConfig := DefaultConfig()
|
defConfig := DefaultConfig()
|
||||||
|
|
||||||
if len(config.Address) == 0 {
|
if config.Address == "" {
|
||||||
config.Address = defConfig.Address
|
config.Address = defConfig.Address
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(config.Scheme) == 0 {
|
if config.Scheme == "" {
|
||||||
config.Scheme = defConfig.Scheme
|
config.Scheme = defConfig.Scheme
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -599,7 +599,7 @@ func NewClient(config *Config) (*Client, error) {
|
||||||
if len(parts) == 2 {
|
if len(parts) == 2 {
|
||||||
switch parts[0] {
|
switch parts[0] {
|
||||||
case "http":
|
case "http":
|
||||||
config.Scheme = "http"
|
// Never revert to http if TLS was explicitly requested.
|
||||||
case "https":
|
case "https":
|
||||||
config.Scheme = "https"
|
config.Scheme = "https"
|
||||||
case "unix":
|
case "unix":
|
||||||
|
|
|
@ -199,16 +199,19 @@ func (c *cmd) Run(args []string) int {
|
||||||
if err := c.flags.Parse(args); err != nil {
|
if err := c.flags.Parse(args); err != nil {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
passThroughArgs := c.flags.Args()
|
|
||||||
|
|
||||||
// Setup Consul client
|
// Setup Consul client
|
||||||
client, err := c.http.APIClient()
|
var err error
|
||||||
|
c.client, err = c.http.APIClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
c.client = client
|
// TODO: refactor
|
||||||
|
return c.run(c.flags.Args())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cmd) run(args []string) int {
|
||||||
// Fixup for deprecated mesh-gateway flag
|
// Fixup for deprecated mesh-gateway flag
|
||||||
if c.meshGateway && c.gateway != "" {
|
if c.meshGateway && c.gateway != "" {
|
||||||
c.UI.Error("The mesh-gateway flag is deprecated and cannot be used alongside the gateway flag")
|
c.UI.Error("The mesh-gateway flag is deprecated and cannot be used alongside the gateway flag")
|
||||||
|
@ -311,7 +314,7 @@ func (c *cmd) Run(args []string) int {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := client.Agent().ServiceRegister(&svc); err != nil {
|
if err := c.client.Agent().ServiceRegister(&svc); err != nil {
|
||||||
c.UI.Error(fmt.Sprintf("Error registering service %q: %s", svc.Name, err))
|
c.UI.Error(fmt.Sprintf("Error registering service %q: %s", svc.Name, err))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
@ -363,7 +366,7 @@ func (c *cmd) Run(args []string) int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
err = execEnvoy(binary, nil, passThroughArgs, bootstrapJson)
|
err = execEnvoy(binary, nil, args, bootstrapJson)
|
||||||
if err == errUnsupportedOS {
|
if err == errUnsupportedOS {
|
||||||
c.UI.Error("Directly running Envoy is only supported on linux and macOS " +
|
c.UI.Error("Directly running Envoy is only supported on linux and macOS " +
|
||||||
"since envoy itself doesn't build on other platforms currently.")
|
"since envoy itself doesn't build on other platforms currently.")
|
||||||
|
|
|
@ -589,20 +589,24 @@ func TestGenerateConfig(t *testing.T) {
|
||||||
// test.
|
// test.
|
||||||
srv := httptest.NewServer(testMockAgent(tc.ProxyConfig, tc.GRPCPort))
|
srv := httptest.NewServer(testMockAgent(tc.ProxyConfig, tc.GRPCPort))
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
client, err := api.NewClient(&api.Config{Address: srv.URL})
|
||||||
|
require.NoError(err)
|
||||||
|
|
||||||
// Set the agent HTTP address in ENV to be our mock
|
|
||||||
tc.Env = append(tc.Env, "CONSUL_HTTP_ADDR="+srv.URL)
|
|
||||||
testDirPrefix := testDir + string(filepath.Separator)
|
testDirPrefix := testDir + string(filepath.Separator)
|
||||||
myEnv := copyAndReplaceAll(tc.Env, "@@TEMPDIR@@", testDirPrefix)
|
myEnv := copyAndReplaceAll(tc.Env, "@@TEMPDIR@@", testDirPrefix)
|
||||||
defer testSetAndResetEnv(t, myEnv)()
|
defer testSetAndResetEnv(t, myEnv)()
|
||||||
|
|
||||||
ui := cli.NewMockUi()
|
ui := cli.NewMockUi()
|
||||||
c := New(ui)
|
c := New(ui)
|
||||||
|
// explicitly set the client to one which can connect to the httptest.Server
|
||||||
|
c.client = client
|
||||||
|
|
||||||
// Run the command
|
// Run the command
|
||||||
myFlags := copyAndReplaceAll(tc.Flags, "@@TEMPDIR@@", testDirPrefix)
|
myFlags := copyAndReplaceAll(tc.Flags, "@@TEMPDIR@@", testDirPrefix)
|
||||||
args := append([]string{"-bootstrap"}, myFlags...)
|
args := append([]string{"-bootstrap"}, myFlags...)
|
||||||
code := c.Run(args)
|
|
||||||
|
require.NoError(c.flags.Parse(args))
|
||||||
|
code := c.run(c.flags.Args())
|
||||||
if tc.WantErr == "" {
|
if tc.WantErr == "" {
|
||||||
require.Equal(0, code, ui.ErrorWriter.String())
|
require.Equal(0, code, ui.ErrorWriter.String())
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue