-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgcplogs_test.go
128 lines (112 loc) · 5.27 KB
/
gcplogs_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
package gcplogs
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
func TestDefaultProjectID(t *testing.T) {
// create a sandbox directory and sanitize the environment
tempDir := t.TempDir()
// save the state of special environment variables and remove them so the test works
specialEnvVars := []string{ProjectEnvVar, "GOOGLE_APPLICATION_CREDENTIALS", "PATH"}
origValues := map[string]string{}
for _, key := range specialEnvVars {
origValues[key] = os.Getenv(key)
os.Unsetenv(key)
}
defer func() {
for key, value := range origValues {
os.Setenv(key, value)
}
}()
os.Setenv("PATH", tempDir)
projectID := DefaultProjectID()
if projectID != "" {
t.Fatal("Initial project ID must be empty; some environment must be wrong?")
}
// point this to application default credentials
keyPath := filepath.Join(tempDir, "key.json")
err := os.WriteFile(keyPath, []byte(invalidServiceAccountKey), 0600)
if err != nil {
t.Fatal(err)
}
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", keyPath)
projectID = DefaultProjectID()
if projectID != "bigquery-tools" {
t.Error("project ID must be bigquery-tools with key:", projectID)
}
// override with GOOGLE_CLOUD_PROJECT: Used with Cloud Shell, new App Engine
os.Setenv(ProjectEnvVar, "env-project")
projectID = DefaultProjectID()
if projectID != "env-project" {
t.Error("Project environment variable must take priority:", projectID)
}
os.Unsetenv(ProjectEnvVar)
os.Remove(keyPath)
if DefaultProjectID() != "" {
t.Error("need to reset environment to not find project ID")
}
// set up a fake gcloud
gcloudPath := filepath.Join(tempDir, "gcloud")
err = os.WriteFile(gcloudPath, []byte(fakeGcloud), 0700)
if err != nil {
t.Fatal(err)
}
// fmt.Println("WTF", tempdir)
// time.Sleep(time.Minute)
projectID = DefaultProjectID()
if projectID != "gcloud-project-id" {
t.Error("incorrect gcloud project:", projectID)
}
args, err := os.ReadFile(gcloudPath + ".args")
if err != nil {
t.Fatal(err)
}
const expectedArgs = "config get-value core/project\n"
if string(args) != expectedArgs {
t.Error("wrong gcloud args:", string(args))
}
}
const fakeGcloud = `#!/bin/sh
echo $@ > $0.args
echo "gcloud-project-id"`
// This is a revoked service account.
const invalidServiceAccountKey = `{
"type": "service_account",
"project_id": "bigquery-tools",
"private_key_id": "55ed102c272e1aa954893d6cefeec82dddba0bf5",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCDWEgEeauzMUq+\n8i8MzbMBc2txKsnrlHuTaw+usaWD0wrr8ApuOXyii1q8u3EcvA1KFHI4kX9fzLKw\nVDNc8LhX/yhwd9EyJLuihb7D3URJyNksbf8IsR1/KH0y6VmeaJVR9F/amN6uFy6s\nw8C3IpTgQjzrR7pFXAK0A3MaSSCUxRSioVq5PnXoE67zyR7XC57BaUas6WGVm9RT\ncsgrw4g53cDmUkFcmjgxgEFoFF7B08FBitjc3X8S50k2heCVIC5F+tCQcdPiyfax\nIk/LLUEq3Nw4u99BSD36Deb7h9NUAjVYnLUF+qH0nlClP5EepSUvCJti537C1JfT\nAzXru46HAgMBAAECggEACIJxuAiB/UwWQaTDM5soG9H0hhJ1npOyJezryS+tP4su\ny/ZzVozW7FkG+e9S9r+gRMpqVAvpKrXCZfYulbjq2JipcA/zN8J1faQYpevx/q3K\nlDxUJ6YB+TkQU3oW2lKASh0BENKSqsjJt1u/Yp4U8yqXc87j3JaHfk4y6OMP/1Nl\nrgiHuYmWyHgb/rGaviHUoZf/tVCNv2jngfCCpGW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protected]",
"client_id": "103262392421472942815",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/example%40bigquery-tools.iam.gserviceaccount.com"
}`
func TestTracerFromRequest(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"", ""},
{"invalid", ""},
{"105445aa7843bc8bf206b120001000/0;o=1", "projects/test_id/traces/105445aa7843bc8bf206b120001000"},
}
tracer := &Tracer{"test_id"}
zeroTracer := &Tracer{}
for i, test := range tests {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(TraceHeader, test.input)
output := tracer.FromRequest(req)
if output != test.expected {
t.Errorf("%d: FromRequest(%#v)=%#v; expected %#v", i, test.input, output, test.expected)
}
zeroOutput := zeroTracer.FromRequest(req)
if zeroOutput != "" {
t.Errorf("%d: FromRequest() must return the empty string if ProjectID is not set: %#v",
i, zeroOutput)
}
}
}