Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add connection test for startup #1832

Merged
merged 4 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,10 @@ is the target account.`)
address returned by the SQL Admin API. In most cases, this flag should not be used.
Prefer default of public IP or use --private-ip instead.`)

pflags.BoolVar(&c.conf.RunConnectionTest, "run-connection-test", false, `Runs a connection test
against all specified instances. If an instance is unreachable, the Proxy exits with a failure
status code.`)

// Global and per instance flags
pflags.StringVarP(&c.conf.Addr, "address", "a", "127.0.0.1",
"(*) Address to bind Cloud SQL instance listeners.")
Expand Down Expand Up @@ -474,6 +478,10 @@ func parseConfig(cmd *Command, conf *proxy.Config, args []string) error {
}

if conf.FUSEDir != "" {
if conf.RunConnectionTest {
return newBadCommandError("cannot run connection tests in FUSE mode")
}

if err := proxy.SupportsFUSE(); err != nil {
return newBadCommandError(
fmt.Sprintf("--fuse is not supported: %v", err),
Expand Down
14 changes: 14 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ func TestNewCommandArguments(t *testing.T) {
AutoIP: true,
}),
},
{
desc: "using the run-connection-test flag",
args: []string{"--run-connection-test", "proj:region:inst"},
want: withDefaults(&proxy.Config{
RunConnectionTest: true,
}),
},
}

for _, tc := range tcs {
Expand Down Expand Up @@ -1057,6 +1064,13 @@ func TestNewCommandWithErrors(t *testing.T) {
"p:r:i?private-ip=true",
},
},
{
desc: "run-connection-test with fuse",
args: []string{
"--run-connection-test",
"--fuse", "myfusedir",
},
},
}

for _, tc := range tcs {
Expand Down
13 changes: 13 additions & 0 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ type Config struct {
// OtherUserAgents is a list of space separate user agents that will be
// appended to the default user agent.
OtherUserAgents string

// RunConnectionTest determines whether the Proxy should attempt a connection
// to all specified instances to verify the network path is valid.
RunConnectionTest bool
}

// dialOptions interprets appropriate dial options for a particular instance
Expand Down Expand Up @@ -571,6 +575,15 @@ func (c *Client) Serve(ctx context.Context, notify func()) error {
return c.serveFuse(ctx, notify)
}

if c.conf.RunConnectionTest {
c.logger.Infof("Connection test started")
if _, err := c.CheckConnections(ctx); err != nil {
c.logger.Errorf("Connection test failed")
return err
}
c.logger.Infof("Connection test passed")
}

exitCh := make(chan error)
for _, m := range c.mnts {
go func(mnt *socketMount) {
Expand Down
46 changes: 46 additions & 0 deletions internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,49 @@ func TestCheckConnections(t *testing.T) {
t.Fatalf("CheckConnections number of connections: want = %v, got = %v", want, got)
}
}

func TestRunConnectionCheck(t *testing.T) {
in := &proxy.Config{
Addr: "127.0.0.1",
Port: 50002,
Instances: []proxy.InstanceConnConfig{
{Name: "proj:region:pg"},
},
RunConnectionTest: true,
}
d := &fakeDialer{}
c, err := proxy.NewClient(context.Background(), d, testLogger, in)
if err != nil {
t.Fatalf("proxy.NewClient error: %v", err)
}
defer func(c *proxy.Client) {
err := c.Close()
if err != nil {
t.Log(err)
}
}(c)
go func() {
// Serve alone without any connections will still verify that the
// provided instances are reachable.
_ = c.Serve(context.Background(), func() {})
}()

verifyDialAttempts := func() error {
var tries int
for {
tries++
if tries == 10 {
return errors.New("failed to verify dial tries after 10 tries")
}
if got := d.dialAttempts(); got > 0 {
return nil
}
time.Sleep(100 * time.Millisecond)
}
}

if err := verifyDialAttempts(); err != nil {
t.Fatal(err)
}

}