Skip to content

Commit 679c7d5

Browse files
easwarshfwang
authored andcommitted
Refactor tests to make it runnable within Google.
1 parent 1120bee commit 679c7d5

File tree

2 files changed

+75
-51
lines changed

2 files changed

+75
-51
lines changed

tests/common.go

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ import (
3838
"log"
3939
"net/http"
4040
"os"
41-
"os/exec"
42-
"path/filepath"
4341
"testing"
4442
"time"
4543

@@ -50,8 +48,6 @@ import (
5048

5149
"golang.org/x/crypto/ssh"
5250
"golang.org/x/net/context"
53-
"golang.org/x/oauth2"
54-
"golang.org/x/oauth2/google"
5551
compute "google.golang.org/api/compute/v1"
5652
)
5753

@@ -72,10 +68,7 @@ var (
7268
token = flag.String("token", "", "When set, the proxy uses this Bearer token for authorization.")
7369
)
7470

75-
const (
76-
defaultOS = "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20160329"
77-
buildShLocation = "cmd/cloud_sql_proxy/build.sh"
78-
)
71+
const defaultOS = "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20160329"
7972

8073
type logger interface {
8174
Log(args ...interface{})
@@ -159,31 +152,6 @@ func (p *process) Close() error {
159152
return p.sess.Close()
160153
}
161154

162-
func compileProxy() (string, error) {
163-
// Find the 'build.sh' script by looking for it in cwd, cwd/.., and cwd/../..
164-
var buildSh string
165-
166-
var parentPath []string
167-
for parents := 0; parents < 2; parents++ {
168-
cur := filepath.Join(append(parentPath, buildShLocation)...)
169-
if _, err := os.Stat(cur); err == nil {
170-
buildSh = cur
171-
break
172-
}
173-
parentPath = append(parentPath, "..")
174-
}
175-
if buildSh == "" {
176-
return "", fmt.Errorf("couldn't find %q; please cd into the local repository", buildShLocation)
177-
}
178-
179-
cmd := exec.Command(buildSh)
180-
if out, err := cmd.CombinedOutput(); err != nil {
181-
return "", fmt.Errorf("error during build.sh execution: %v;\n%s", err, out)
182-
}
183-
184-
return "cloud_sql_proxy", nil
185-
}
186-
187155
// startProxy executes the cloud_sql_proxy via ssh. The returned ReadCloser
188156
// must be serviced and closed when finished, otherwise the SSH connection may
189157
// block.
@@ -410,24 +378,6 @@ func sshKey() (pubKey string, auth ssh.AuthMethod, err error) {
410378
return string(ssh.MarshalAuthorizedKey(pub)), ssh.PublicKeys(signer), nil
411379
}
412380

413-
func clientFromCredentials(ctx context.Context) (*http.Client, error) {
414-
if f := *credentialFile; f != "" {
415-
all, err := ioutil.ReadFile(f)
416-
if err != nil {
417-
return nil, fmt.Errorf("invalid json file %q: %v", f, err)
418-
}
419-
cfg, err := google.JWTConfigFromJSON(all, proxy.SQLScope)
420-
if err != nil {
421-
return nil, fmt.Errorf("invalid json file %q: %v", f, err)
422-
}
423-
return cfg.Client(ctx), nil
424-
} else if tok := *token; tok != "" {
425-
src := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: tok})
426-
return oauth2.NewClient(ctx, src), nil
427-
}
428-
return google.DefaultClient(ctx, proxy.SQLScope)
429-
}
430-
431381
func TestMain(m *testing.M) {
432382
flag.Parse()
433383
switch "" {

tests/common_open_source.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2015 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package tests
16+
17+
import (
18+
"fmt"
19+
"io/ioutil"
20+
"net/http"
21+
"os"
22+
"os/exec"
23+
"path/filepath"
24+
25+
"github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/proxy"
26+
"golang.org/x/net/context"
27+
"golang.org/x/oauth2"
28+
"golang.org/x/oauth2/google"
29+
)
30+
31+
const buildShLocation = "cmd/cloud_sql_proxy/build.sh"
32+
33+
func clientFromCredentials(ctx context.Context) (*http.Client, error) {
34+
if f := *credentialFile; f != "" {
35+
all, err := ioutil.ReadFile(f)
36+
if err != nil {
37+
return nil, fmt.Errorf("invalid json file %q: %v", f, err)
38+
}
39+
cfg, err := google.JWTConfigFromJSON(all, proxy.SQLScope)
40+
if err != nil {
41+
return nil, fmt.Errorf("invalid json file %q: %v", f, err)
42+
}
43+
return cfg.Client(ctx), nil
44+
} else if tok := *token; tok != "" {
45+
src := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: tok})
46+
return oauth2.NewClient(ctx, src), nil
47+
}
48+
return google.DefaultClient(ctx, proxy.SQLScope)
49+
}
50+
51+
func compileProxy() (string, error) {
52+
// Find the 'build.sh' script by looking for it in cwd, cwd/.., and cwd/../..
53+
var buildSh string
54+
55+
var parentPath []string
56+
for parents := 0; parents < 2; parents++ {
57+
cur := filepath.Join(append(parentPath, buildShLocation)...)
58+
if _, err := os.Stat(cur); err == nil {
59+
buildSh = cur
60+
break
61+
}
62+
parentPath = append(parentPath, "..")
63+
}
64+
if buildSh == "" {
65+
return "", fmt.Errorf("couldn't find %q; please cd into the local repository", buildShLocation)
66+
}
67+
68+
cmd := exec.Command(buildSh)
69+
if out, err := cmd.CombinedOutput(); err != nil {
70+
return "", fmt.Errorf("error during build.sh execution: %v;\n%s", err, out)
71+
}
72+
73+
return "cloud_sql_proxy", nil
74+
}

0 commit comments

Comments
 (0)