| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package upload |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"log" |
| 12 |
"net/http" |
| 13 |
"net/http/httptest" |
| 14 |
"os" |
| 15 |
"path/filepath" |
| 16 |
"runtime" |
| 17 |
"strings" |
| 18 |
"sync" |
| 19 |
"testing" |
| 20 |
"time" |
| 21 |
|
| 22 |
"golang.org/x/telemetry/counter" |
| 23 |
"golang.org/x/telemetry/internal/configtest" |
| 24 |
"golang.org/x/telemetry/internal/regtest" |
| 25 |
"golang.org/x/telemetry/internal/telemetry" |
| 26 |
"golang.org/x/telemetry/internal/testenv" |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
func CreateTestUploadServer(t *testing.T) (*httptest.Server, func() [][]byte) { |
| 32 |
s := &uploadQueue{} |
| 33 |
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 34 |
buf, err := io.ReadAll(r.Body) |
| 35 |
if err != nil { |
| 36 |
t.Errorf("invalid request received: %v", err) |
| 37 |
http.Error(w, "read failed", http.StatusBadRequest) |
| 38 |
return |
| 39 |
} |
| 40 |
s.Append(buf) |
| 41 |
})) |
| 42 |
t.Cleanup(srv.Close) |
| 43 |
return srv, s.Get |
| 44 |
} |
| 45 |
|
| 46 |
type uploadQueue struct { |
| 47 |
mu sync.Mutex |
| 48 |
data [][]byte |
| 49 |
} |
| 50 |
|
| 51 |
func (s *uploadQueue) Append(data []byte) { |
| 52 |
s.mu.Lock() |
| 53 |
defer s.mu.Unlock() |
| 54 |
s.data = append(s.data, data) |
| 55 |
} |
| 56 |
|
| 57 |
func (s *uploadQueue) Get() [][]byte { |
| 58 |
s.mu.Lock() |
| 59 |
defer s.mu.Unlock() |
| 60 |
return s.data |
| 61 |
} |
| 62 |
|
| 63 |
func CreateTestUploadConfig(t *testing.T, counterNames, stackCounterNames []string) *telemetry.UploadConfig { |
| 64 |
goVersion, progPath, progVersion := regtest.ProgramInfo(t) |
| 65 |
GOOS, GOARCH := runtime.GOOS, runtime.GOARCH |
| 66 |
programConfig := &telemetry.ProgramConfig{ |
| 67 |
Name: progPath, |
| 68 |
Versions: []string{progVersion}, |
| 69 |
} |
| 70 |
for _, c := range counterNames { |
| 71 |
programConfig.Counters = append(programConfig.Counters, telemetry.CounterConfig{Name: c, Rate: 1}) |
| 72 |
} |
| 73 |
for _, c := range stackCounterNames { |
| 74 |
programConfig.Stacks = append(programConfig.Stacks, telemetry.CounterConfig{Name: c, Rate: 1, Depth: 16}) |
| 75 |
} |
| 76 |
return &telemetry.UploadConfig{ |
| 77 |
GOOS: []string{GOOS}, |
| 78 |
GOARCH: []string{GOARCH}, |
| 79 |
SampleRate: 1.0, |
| 80 |
GoVersion: []string{goVersion}, |
| 81 |
Programs: []*telemetry.ProgramConfig{programConfig}, |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func TestDates(t *testing.T) { |
| 86 |
testenv.SkipIfUnsupportedPlatform(t) |
| 87 |
|
| 88 |
prog := regtest.NewProgram(t, "prog", func() int { |
| 89 |
counter.Inc("testing") |
| 90 |
counter.NewStack("aStack", 4).Inc() |
| 91 |
return 0 |
| 92 |
}) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
telemetryDir := t.TempDir() |
| 98 |
if out, err := regtest.RunProg(t, telemetryDir, prog); err != nil { |
| 99 |
t.Fatalf("failed to run program: %s", out) |
| 100 |
} |
| 101 |
cs := readCountFileInfo(t, filepath.Join(telemetryDir, "local")) |
| 102 |
uc := CreateTestUploadConfig(t, nil, []string{"aStack"}) |
| 103 |
env := configtest.LocalProxyEnv(t, uc, "v1.2.3") |
| 104 |
|
| 105 |
const today = "2020-01-24" |
| 106 |
const yesterday = "2020-01-23" |
| 107 |
telemetryEnableTime, _ := time.Parse(dateFormat, "2019-12-01") |
| 108 |
tests := []Test{ |
| 109 |
{ |
| 110 |
name: "beforefirstupload", |
| 111 |
today: "2019-12-04", |
| 112 |
date: "2019-12-03", |
| 113 |
begins: "2019-12-01", |
| 114 |
ends: "2019-12-03", |
| 115 |
readys: []string{"2019-12-01", "2019-12-02"}, |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
wantLocal: 1, |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
wantReady: 1, |
| 125 |
|
| 126 |
wantUploadeds: 1, |
| 127 |
}, |
| 128 |
{ |
| 129 |
name: "oktoupload", |
| 130 |
today: "2019-12-10", |
| 131 |
date: "2019-12-09", |
| 132 |
begins: "2019-12-02", |
| 133 |
ends: "2019-12-09", |
| 134 |
readys: []string{"2019-12-07"}, |
| 135 |
wantLocal: 1, |
| 136 |
wantUploadeds: 2, |
| 137 |
}, |
| 138 |
{ |
| 139 |
name: "oldcountfile", |
| 140 |
today: today, |
| 141 |
date: "2020-01-01", |
| 142 |
begins: "2020-01-01", |
| 143 |
ends: olderThan(t, today, distantPast, "oldcountfile"), |
| 144 |
|
| 145 |
wantLocal: 1, |
| 146 |
}, |
| 147 |
{ |
| 148 |
name: "todayscountfile", |
| 149 |
today: today, |
| 150 |
date: "2020-01-02", |
| 151 |
begins: "2020-01-08", |
| 152 |
ends: today, |
| 153 |
wantCounts: 1, |
| 154 |
}, |
| 155 |
{ |
| 156 |
name: "yesterdaycountfile", |
| 157 |
today: today, |
| 158 |
date: "2020-01-03", |
| 159 |
begins: "2020-01-16", |
| 160 |
ends: yesterday, |
| 161 |
wantLocal: 1, |
| 162 |
wantUploadeds: 1, |
| 163 |
}, |
| 164 |
{ |
| 165 |
name: "alreadydonelocal", |
| 166 |
today: today, |
| 167 |
date: "2020-01-04", |
| 168 |
begins: "2020-01-16", |
| 169 |
ends: yesterday, |
| 170 |
locals: []string{yesterday}, |
| 171 |
wantCounts: 0, |
| 172 |
wantLocal: 1, |
| 173 |
}, |
| 174 |
{ |
| 175 |
name: "alreadydoneuploaded", |
| 176 |
today: today, |
| 177 |
date: "2020-01-05", |
| 178 |
begins: "2020-01-16", |
| 179 |
ends: "2020-01-23", |
| 180 |
uploads: []string{"2020-01-23"}, |
| 181 |
wantCounts: 0, |
| 182 |
wantLocal: 0, |
| 183 |
wantUploadeds: 1, |
| 184 |
}, |
| 185 |
{ |
| 186 |
name: "futurereadyfile", |
| 187 |
today: "2020-01-24", |
| 188 |
date: "2020-01-06", |
| 189 |
begins: "2020-01-16", |
| 190 |
ends: "2020-01-24", |
| 191 |
readys: []string{"2020-01-25"}, |
| 192 |
wantCounts: 1, |
| 193 |
wantReady: 1, |
| 194 |
}, |
| 195 |
} |
| 196 |
|
| 197 |
for _, tx := range tests { |
| 198 |
t.Run(tx.name, func(t *testing.T) { |
| 199 |
telemetryDir := t.TempDir() |
| 200 |
|
| 201 |
srv, uploaded := CreateTestUploadServer(t) |
| 202 |
|
| 203 |
dbg := filepath.Join(telemetryDir, "debug") |
| 204 |
if err := os.MkdirAll(dbg, 0777); err != nil { |
| 205 |
t.Fatal(err) |
| 206 |
} |
| 207 |
uploader, err := newUploader(RunConfig{ |
| 208 |
TelemetryDir: telemetryDir, |
| 209 |
UploadURL: srv.URL, |
| 210 |
Env: env, |
| 211 |
}) |
| 212 |
if err != nil { |
| 213 |
t.Fatal(err) |
| 214 |
} |
| 215 |
defer uploader.Close() |
| 216 |
if err := uploader.dir.SetModeAsOf("on", telemetryEnableTime); err != nil { |
| 217 |
t.Fatal(err) |
| 218 |
} |
| 219 |
uploader.startTime = mustParseDate(tx.today) |
| 220 |
|
| 221 |
wantUploadCount := doTest(t, uploader, &tx, cs) |
| 222 |
if got := len(uploaded()); wantUploadCount != got { |
| 223 |
t.Errorf("server got %d upload requests, want %d", got, wantUploadCount) |
| 224 |
} |
| 225 |
}) |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
func mustParseDate(d string) time.Time { |
| 230 |
x, err := time.Parse("2006-01-02", d) |
| 231 |
if err != nil { |
| 232 |
log.Fatalf("couldn't parse time %s", d) |
| 233 |
} |
| 234 |
return x |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
func olderThan(t *testing.T, today string, old time.Duration, nm string) string { |
| 239 |
x, err := time.Parse("2006-01-02", today) |
| 240 |
if err != nil { |
| 241 |
t.Errorf("%q not a day in test %s (%v)", today, nm, err) |
| 242 |
return today |
| 243 |
} |
| 244 |
ans := x.Add(-old - 24*time.Hour) |
| 245 |
msg := ans.Format("2006-01-02") |
| 246 |
return msg |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
type Test struct { |
| 253 |
name string |
| 254 |
today string |
| 255 |
|
| 256 |
date string |
| 257 |
begins, ends string |
| 258 |
|
| 259 |
|
| 260 |
locals []string |
| 261 |
|
| 262 |
|
| 263 |
readys []string |
| 264 |
|
| 265 |
|
| 266 |
uploads []string |
| 267 |
|
| 268 |
|
| 269 |
wantCounts int |
| 270 |
wantReady int |
| 271 |
wantLocal int |
| 272 |
wantUploadeds int |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
type countFileInfo struct { |
| 278 |
beginOffset, endOffset int |
| 279 |
buf []byte |
| 280 |
namePrefix string |
| 281 |
originalName string |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
func readCountFileInfo(t *testing.T, localDir string) *countFileInfo { |
| 288 |
fis, err := os.ReadDir(localDir) |
| 289 |
if err != nil { |
| 290 |
t.Fatal(err) |
| 291 |
} |
| 292 |
var countFileName string |
| 293 |
var countFileBuf []byte |
| 294 |
for _, f := range fis { |
| 295 |
if strings.HasSuffix(f.Name(), ".count") { |
| 296 |
countFileName = filepath.Join(localDir, f.Name()) |
| 297 |
buf, err := os.ReadFile(countFileName) |
| 298 |
if err != nil { |
| 299 |
t.Fatal(err) |
| 300 |
} |
| 301 |
countFileBuf = buf |
| 302 |
break |
| 303 |
} |
| 304 |
} |
| 305 |
if len(countFileBuf) == 0 { |
| 306 |
t.Fatalf("no contents read for %s", countFileName) |
| 307 |
} |
| 308 |
|
| 309 |
var cfilename string = countFileName |
| 310 |
cfilename = filepath.Base(cfilename) |
| 311 |
flds := strings.Split(cfilename, "-") |
| 312 |
if len(flds) != 7 { |
| 313 |
t.Fatalf("got %d fields, expected 7 (%q)", len(flds), cfilename) |
| 314 |
} |
| 315 |
pr := strings.Join(flds[:4], "-") + "-" |
| 316 |
|
| 317 |
ans := countFileInfo{ |
| 318 |
buf: countFileBuf, |
| 319 |
namePrefix: pr, |
| 320 |
originalName: countFileName, |
| 321 |
} |
| 322 |
idx := bytes.Index(countFileBuf, []byte("TimeEnd: ")) |
| 323 |
if idx < 0 { |
| 324 |
t.Fatalf("couldn't find TimeEnd in count file %q", countFileBuf[:100]) |
| 325 |
} |
| 326 |
ans.endOffset = idx + len("TimeEnd: ") |
| 327 |
idx = bytes.Index(countFileBuf, []byte("TimeBegin: ")) |
| 328 |
if idx < 0 { |
| 329 |
t.Fatalf("couldn't find TimeBegin in countfile %q", countFileBuf[:100]) |
| 330 |
} |
| 331 |
ans.beginOffset = idx + len("TimeBegin: ") |
| 332 |
return &ans |
| 333 |
} |
| 334 |
|
| 335 |
func doTest(t *testing.T, u *uploader, test *Test, known *countFileInfo) int { |
| 336 |
|
| 337 |
contents := bytes.Join([][]byte{ |
| 338 |
known.buf[:known.beginOffset], |
| 339 |
[]byte(test.begins), |
| 340 |
known.buf[known.beginOffset+len("YYYY-MM-DD") : known.endOffset], |
| 341 |
[]byte(test.ends), |
| 342 |
known.buf[known.endOffset+len("YYYY-MM-DD"):], |
| 343 |
}, nil) |
| 344 |
filename := known.namePrefix + test.date + ".v1.count" |
| 345 |
if err := os.MkdirAll(u.dir.LocalDir(), 0777); err != nil { |
| 346 |
t.Fatal(err) |
| 347 |
} |
| 348 |
if err := os.WriteFile(filepath.Join(u.dir.LocalDir(), filename), contents, 0666); err != nil { |
| 349 |
t.Fatalf("writing count file for %s (%s): %v", test.name, filename, err) |
| 350 |
} |
| 351 |
for _, x := range test.locals { |
| 352 |
nm := fmt.Sprintf("local.%s.json", x) |
| 353 |
if err := os.WriteFile(filepath.Join(u.dir.LocalDir(), nm), []byte{}, 0666); err != nil { |
| 354 |
t.Fatalf("%v writing local file %s", err, nm) |
| 355 |
} |
| 356 |
} |
| 357 |
for _, x := range test.readys { |
| 358 |
nm := fmt.Sprintf("%s.json", x) |
| 359 |
if err := os.WriteFile(filepath.Join(u.dir.LocalDir(), nm), []byte{}, 0666); err != nil { |
| 360 |
t.Fatalf("%v writing ready file %s", err, nm) |
| 361 |
} |
| 362 |
} |
| 363 |
if len(test.uploads) > 0 { |
| 364 |
os.MkdirAll(u.dir.UploadDir(), 0777) |
| 365 |
} |
| 366 |
for _, x := range test.uploads { |
| 367 |
nm := fmt.Sprintf("%s.json", x) |
| 368 |
if err := os.WriteFile(filepath.Join(u.dir.UploadDir(), nm), []byte{}, 0666); err != nil { |
| 369 |
t.Fatalf("%v writing upload %s", err, nm) |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
u.Run() |
| 375 |
|
| 376 |
|
| 377 |
var cfiles, rfiles, lfiles, ufiles int |
| 378 |
fis, err := os.ReadDir(u.dir.LocalDir()) |
| 379 |
if err != nil { |
| 380 |
t.Errorf("%v reading localdir %s", err, u.dir.LocalDir()) |
| 381 |
return 0 |
| 382 |
} |
| 383 |
for _, f := range fis { |
| 384 |
switch { |
| 385 |
case strings.HasSuffix(f.Name(), ".v1.count"): |
| 386 |
cfiles++ |
| 387 |
case f.Name() == "weekends": |
| 388 |
case strings.HasPrefix(f.Name(), "local."): |
| 389 |
lfiles++ |
| 390 |
case strings.HasSuffix(f.Name(), ".json"): |
| 391 |
rfiles++ |
| 392 |
default: |
| 393 |
t.Errorf("for %s, unexpected local file %s", test.name, f.Name()) |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
var logcnt int |
| 398 |
logs, err := os.ReadDir(u.dir.DebugDir()) |
| 399 |
if err == nil { |
| 400 |
logcnt = len(logs) |
| 401 |
} |
| 402 |
if logcnt != 1 { |
| 403 |
t.Errorf("expected 1 log file, got %d", logcnt) |
| 404 |
} |
| 405 |
|
| 406 |
fis, err = os.ReadDir(u.dir.UploadDir()) |
| 407 |
if err != nil { |
| 408 |
t.Errorf("%v reading uploaddir %s", err, u.dir.UploadDir()) |
| 409 |
return 0 |
| 410 |
} |
| 411 |
ufiles = len(fis) |
| 412 |
if test.wantCounts != cfiles { |
| 413 |
t.Errorf("%s: got %d countfiles, wanted %d", test.name, cfiles, test.wantCounts) |
| 414 |
} |
| 415 |
if test.wantReady != rfiles { |
| 416 |
t.Errorf("%s: got %d ready files, wanted %d", test.name, rfiles, test.wantReady) |
| 417 |
} |
| 418 |
if test.wantLocal != lfiles { |
| 419 |
t.Errorf("%s: got %d localfiles, wanted %d", test.name, lfiles, test.wantLocal) |
| 420 |
} |
| 421 |
if test.wantUploadeds != ufiles { |
| 422 |
t.Errorf("%s: got %d uploaded files, wanted %d", test.name, ufiles, test.wantUploadeds) |
| 423 |
} |
| 424 |
return ufiles - len(test.uploads) |
| 425 |
} |