|
14 | 14 | package main |
15 | 15 |
|
16 | 16 | import ( |
| 17 | + "archive/tar" |
17 | 18 | "bytes" |
| 19 | + "compress/gzip" |
18 | 20 | "context" |
19 | 21 | "flag" |
20 | 22 | "fmt" |
| 23 | + "io" |
21 | 24 | "log" |
22 | 25 | "os" |
23 | 26 | "os/exec" |
@@ -483,11 +486,78 @@ func (s *CollectorSuite) TestResourceOption() { |
483 | 486 | if strings.TrimRight(bytes.NewBuffer(out).String(), "\n") != test.want { |
484 | 487 | s.Failf("Summary Check", "test %s\nresource %s\nnamespace %s\noutput is not as expected\nOutput: %s\nWanted: %s", test.name, resource, test.namespace, out, test.want) |
485 | 488 | } |
| 489 | + |
| 490 | + if test.want != "0" { |
| 491 | + err = validateSummaryByNamespace("cluster-dump.tar.gz", test.namespace) |
| 492 | + s.NoErrorf(err, "summary validation failed for namespace %s, resource %s", test.namespace, resource) |
| 493 | + } |
486 | 494 | } |
487 | 495 | }) |
488 | 496 | } |
489 | 497 | } |
490 | 498 |
|
| 499 | +func validateSummaryByNamespace(archivePath, namespace string) error { |
| 500 | + switch namespace { |
| 501 | + case "psmdb": |
| 502 | + return validatePSMDBSummary(archivePath, namespace) |
| 503 | + default: |
| 504 | + return nil |
| 505 | + } |
| 506 | +} |
| 507 | + |
| 508 | +func validatePSMDBSummary(archivePath, namespace string) error { |
| 509 | + file, err := os.Open(archivePath) |
| 510 | + if err != nil { |
| 511 | + return err |
| 512 | + } |
| 513 | + defer file.Close() |
| 514 | + |
| 515 | + gzr, err := gzip.NewReader(file) |
| 516 | + if err != nil { |
| 517 | + return err |
| 518 | + } |
| 519 | + defer gzr.Close() |
| 520 | + |
| 521 | + tarReader := tar.NewReader(gzr) |
| 522 | + validated := 0 |
| 523 | + |
| 524 | + for { |
| 525 | + header, err := tarReader.Next() |
| 526 | + if err == io.EOF { |
| 527 | + break |
| 528 | + } |
| 529 | + if err != nil { |
| 530 | + return err |
| 531 | + } |
| 532 | + |
| 533 | + if header.Typeflag != tar.TypeReg { |
| 534 | + continue |
| 535 | + } |
| 536 | + if !strings.HasSuffix(header.Name, "/summary.txt") { |
| 537 | + continue |
| 538 | + } |
| 539 | + if !strings.Contains(header.Name, "/"+namespace+"/") { |
| 540 | + continue |
| 541 | + } |
| 542 | + |
| 543 | + content, err := io.ReadAll(tarReader) |
| 544 | + if err != nil { |
| 545 | + return err |
| 546 | + } |
| 547 | + if !bytes.Contains(content, []byte("# Report On")) { |
| 548 | + return fmt.Errorf("summary file %s does not contain # Report On", header.Name) |
| 549 | + } |
| 550 | + |
| 551 | + validated++ |
| 552 | + } |
| 553 | + |
| 554 | + if validated == 0 { |
| 555 | + return fmt.Errorf("no summary.txt files found for namespace %s", namespace) |
| 556 | + } |
| 557 | + |
| 558 | + return nil |
| 559 | +} |
| 560 | + |
491 | 561 | func (s *CollectorSuite) TestPT_2453() { |
492 | 562 | testcmd := []string{"sh", "-c", "tar -tf cluster-dump.tar.gz --wildcards '*/summary.txt' 2>/dev/null | wc -l"} |
493 | 563 | tests := []struct { |
|
0 commit comments