(t *testing.T)
| 369 | } |
| 370 | |
| 371 | func TestPerformUpdate_FullFlow(t *testing.T) { |
| 372 | // Create a fake "current binary" |
| 373 | dir := t.TempDir() |
| 374 | binaryPath := filepath.Join(dir, "chief") |
| 375 | os.WriteFile(binaryPath, []byte("old binary"), 0o755) |
| 376 | |
| 377 | // New binary content |
| 378 | newContent := []byte("new binary v0.6.0") |
| 379 | h := sha256.Sum256(newContent) |
| 380 | expectedHash := hex.EncodeToString(h[:]) |
| 381 | |
| 382 | binaryName := fmt.Sprintf("chief-%s-%s", runtime.GOOS, runtime.GOARCH) |
| 383 | |
| 384 | // Set up download server |
| 385 | downloadSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 386 | if r.URL.Path == "/binary" { |
| 387 | w.Write(newContent) |
| 388 | } else if r.URL.Path == "/checksum" { |
| 389 | fmt.Fprintf(w, "%s %s\n", expectedHash, binaryName) |
| 390 | } |
| 391 | })) |
| 392 | defer downloadSrv.Close() |
| 393 | |
| 394 | release := Release{ |
| 395 | TagName: "v0.6.0", |
| 396 | Assets: []Asset{ |
| 397 | {Name: binaryName, BrowserDownloadURL: downloadSrv.URL + "/binary"}, |
| 398 | {Name: binaryName + ".sha256", BrowserDownloadURL: downloadSrv.URL + "/checksum"}, |
| 399 | }, |
| 400 | } |
| 401 | releaseSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 402 | json.NewEncoder(w).Encode(release) |
| 403 | })) |
| 404 | defer releaseSrv.Close() |
| 405 | |
| 406 | // We can't easily test PerformUpdate because it calls os.Executable() |
| 407 | // Instead, test the components used by PerformUpdate individually |
| 408 | // (CheckForUpdate, findAssets, downloadToTemp, verifyChecksum are all tested above) |
| 409 | |
| 410 | // Test the download + checksum flow manually |
| 411 | tmpFile, err := downloadToTemp(downloadSrv.URL+"/binary", dir) |
| 412 | if err != nil { |
| 413 | t.Fatalf("download failed: %v", err) |
| 414 | } |
| 415 | defer os.Remove(tmpFile) |
| 416 | |
| 417 | if err := verifyChecksum(tmpFile, downloadSrv.URL+"/checksum"); err != nil { |
| 418 | t.Fatalf("checksum failed: %v", err) |
| 419 | } |
| 420 | |
| 421 | // Verify file content |
| 422 | data, err := os.ReadFile(tmpFile) |
| 423 | if err != nil { |
| 424 | t.Fatalf("reading downloaded file: %v", err) |
| 425 | } |
| 426 | if string(data) != string(newContent) { |
| 427 | t.Errorf("downloaded content mismatch") |
| 428 | } |
nothing calls this directly
no test coverage detected