Skip to content

Commit 87e6196

Browse files
committed
firestore: use a separate project for integration tests
Change-Id: Iea02d241a67f77b809b947edca4020ea4157f27b Reviewed-on: https://code-review.googlesource.com/17410 Reviewed-by: Michael Darakananda <pongad@google.com>
1 parent 6ee1e88 commit 87e6196

3 files changed

Lines changed: 30 additions & 14 deletions

File tree

CONTRIBUTING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ run the against the actual APIs.
4242
- **GCLOUD_TESTS_GOLANG_KEY**: The path to the JSON key file.
4343
- **GCLOUD_TESTS_API_KEY**: Your API key.
4444

45+
Firestore requires a different project and key:
46+
47+
- **GCLOUD_TESTS_GOLANG_FIRESTORE_PROJECT_ID**: Developers Console project's ID
48+
supporting Firestore
49+
- **GCLOUD_TESTS_GOLANG_FIRESTORE_KEY**: The path to the JSON key file.
50+
4551
Install the [gcloud command-line tool][gcloudcli] to your machine and use it
4652
to create some resources used in integration tests.
4753

firestore/integration_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ func TestMain(m *testing.M) {
4242
os.Exit(status)
4343
}
4444

45+
const (
46+
envProjID = "GCLOUD_TESTS_GOLANG_FIRESTORE_PROJECT_ID"
47+
envPrivateKey = "GCLOUD_TESTS_GOLANG_FIRESTORE_KEY"
48+
)
49+
4550
var (
4651
iClient *Client
4752
iColl *CollectionRef
@@ -54,12 +59,13 @@ func initIntegrationTest() {
5459
return
5560
}
5661
ctx := context.Background()
57-
testProjectID := testutil.ProjID()
62+
testProjectID := os.Getenv(envProjID)
5863
if testProjectID == "" {
5964
log.Println("Integration tests skipped. See CONTRIBUTING.md for details")
6065
return
6166
}
62-
ts := testutil.TokenSource(ctx, "https://www.googleapis.com/auth/cloud-platform",
67+
ts := testutil.TokenSourceEnv(ctx, envPrivateKey,
68+
"https://www.googleapis.com/auth/cloud-platform",
6369
"https://www.googleapis.com/auth/datastore")
6470
if ts == nil {
6571
log.Fatal("The project key must be set. See CONTRIBUTING.md for details")
@@ -551,9 +557,9 @@ func TestIntegration_ServerTimestamp(t *testing.T) {
551557
ctx := context.Background()
552558
doc := integrationColl(t).NewDoc()
553559
// Bound times of the RPC, with some slack for clock skew.
554-
start := time.Now().Add(-50 * time.Millisecond)
560+
start := time.Now()
555561
mustCreate("ServerTimestamp", t, doc, data)
556-
end := time.Now().Add(50 * time.Millisecond)
562+
end := time.Now()
557563
ds, err := doc.Get(ctx)
558564
if err != nil {
559565
t.Fatal(err)

internal/testutil/context.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,24 @@ const (
3333
// ProjID returns the project ID to use in integration tests, or the empty
3434
// string if none is configured.
3535
func ProjID() string {
36-
projID := os.Getenv(envProjID)
37-
if projID == "" {
38-
return ""
39-
}
40-
return projID
36+
return os.Getenv(envProjID)
4137
}
4238

4339
// TokenSource returns the OAuth2 token source to use in integration tests,
44-
// or nil if none is configured. If the environment variable is unset,
45-
// TokenSource will try to find 'Application Default Credentials'. Else,
46-
// TokenSource will return nil.
47-
// TokenSource will log.Fatal if the token source is specified but missing or invalid.
40+
// or nil if none is configured. It uses the standard environment variable
41+
// for tests in this repo.
4842
func TokenSource(ctx context.Context, scopes ...string) oauth2.TokenSource {
49-
key := os.Getenv(envPrivateKey)
43+
return TokenSourceEnv(ctx, envPrivateKey, scopes...)
44+
}
45+
46+
// TokenSourceEnv returns the OAuth2 token source to use in integration tests. or nil
47+
// if none is configured. It tries to get credentials from the filename in the
48+
// environment variable envVar. If the environment variable is unset, TokenSourceEnv
49+
// will try to find 'Application Default Credentials'. Else, TokenSourceEnv will
50+
// return nil. TokenSourceEnv will log.Fatal if the token source is specified but
51+
// missing or invalid.
52+
func TokenSourceEnv(ctx context.Context, envVar string, scopes ...string) oauth2.TokenSource {
53+
key := os.Getenv(envVar)
5054
if key == "" { // Try for application default credentials.
5155
ts, err := google.DefaultTokenSource(ctx, scopes...)
5256
if err != nil {

0 commit comments

Comments
 (0)