-
Notifications
You must be signed in to change notification settings - Fork 345
/
common_test.go
157 lines (144 loc) · 3.73 KB
/
common_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package tests contains end to end tests meant to verify the Cloud SQL Auth proxy
// works as expected when executed as a binary.
//
// Required flags:
//
// -mysql_conn_name, -db_user, -db_pass
package tests
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/GoogleCloudPlatform/cloud-sql-proxy/v2/cmd"
"github.com/GoogleCloudPlatform/cloud-sql-proxy/v2/internal/log"
)
// proxyExec represents an execution of the Cloud SQL proxy.
type proxyExec struct {
Out io.ReadCloser
cmd *cmd.Command
cancel context.CancelFunc
closers []io.Closer
done chan bool // closed once the cmd is completed
err error
}
// StartProxy returns a proxyExec representing a running instance of the proxy.
func StartProxy(ctx context.Context, args ...string) (*proxyExec, error) {
ctx, cancel := context.WithCancel(ctx)
// Open a pipe for tracking the output from the cmd
pr, pw, err := os.Pipe()
if err != nil {
cancel()
return nil, fmt.Errorf("unable to open stdout pipe: %w", err)
}
cmd := cmd.NewCommand(cmd.WithLogger(log.NewStdLogger(pw, pw)))
cmd.SetArgs(args)
cmd.SetOut(pw)
cmd.SetErr(pw)
p := &proxyExec{
Out: pr,
cmd: cmd,
cancel: cancel,
closers: []io.Closer{pr, pw},
done: make(chan bool),
}
// Start the command in the background
go func() {
defer close(p.done)
defer cancel()
p.err = cmd.ExecuteContext(ctx)
}()
return p, nil
}
// Stop sends the TERM signal to the proxy and returns.
func (p *proxyExec) Stop() {
p.cancel()
}
// Waits until the execution is completed and returns any error.
func (p *proxyExec) Wait(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-p.done:
return p.err
}
}
// Done returns true if the proxy has exited.
func (p *proxyExec) Done() bool {
select {
case <-p.done:
return true
default:
}
return false
}
// Close releases any resources associated with the instance.
func (p *proxyExec) Close() {
p.cancel()
for _, c := range p.closers {
c.Close()
}
}
// WaitForServe waits until the proxy ready to serve traffic. Returns any output from
// the proxy while starting or any errors experienced before the proxy was ready to
// server.
func (p *proxyExec) WaitForServe(ctx context.Context) (output string, err error) {
// Watch for the "Ready for new connections" to indicate the proxy is listening
buf, in, errCh := new(bytes.Buffer), bufio.NewReader(p.Out), make(chan error, 1)
go func() {
defer close(errCh)
for {
// if ctx is finished, stop processing
select {
case <-ctx.Done():
return
default:
}
s, err := in.ReadString('\n')
if err != nil {
errCh <- err
return
}
if _, err = buf.WriteString(s); err != nil {
errCh <- err
return
}
// Check for an unrecognized flag
if strings.Contains(s, "Error") {
errCh <- errors.New(s)
return
}
if strings.Contains(s, "ready for new connections") {
errCh <- nil
return
}
}
}()
// Wait for either the background thread of the context to complete
select {
case <-ctx.Done():
return buf.String(), ctx.Err()
case err := <-errCh:
if err != nil {
return buf.String(), err
}
}
return buf.String(), nil
}