|
20 | 20 | _resolve_inst_key, |
21 | 21 | _attach_memory_usage, |
22 | 22 | _attach_capacity_scalar, |
| 23 | + _accumulate_commands, |
| 24 | + _normalize_location, |
23 | 25 | ) |
24 | 26 |
|
25 | 27 |
|
@@ -556,6 +558,82 @@ def test_attach_capacity_scalar_uses_resolve_inst_key(self): |
556 | 558 | table["my-project/memorystore-valkey"]["abc123"]["MaxMemory"], 10000000 |
557 | 559 | ) |
558 | 560 |
|
| 561 | + def test_normalize_location_zone_form(self): |
| 562 | + """GCP zone string is split into (region, zone)""" |
| 563 | + self.assertEqual( |
| 564 | + _normalize_location("us-central1-a"), |
| 565 | + ("us-central1", "us-central1-a"), |
| 566 | + ) |
| 567 | + |
| 568 | + def test_normalize_location_region_form(self): |
| 569 | + """Region-shaped string is passed through as region, zone blank""" |
| 570 | + self.assertEqual(_normalize_location("us-east4"), ("us-east4", "")) |
| 571 | + |
| 572 | + def test_normalize_location_multi_segment_zone(self): |
| 573 | + """Multi-word region prefix (e.g. asia-northeast1) is handled""" |
| 574 | + self.assertEqual( |
| 575 | + _normalize_location("asia-northeast1-c"), |
| 576 | + ("asia-northeast1", "asia-northeast1-c"), |
| 577 | + ) |
| 578 | + |
| 579 | + def test_normalize_location_empty_and_none(self): |
| 580 | + """Empty string and None return ('', '')""" |
| 581 | + self.assertEqual(_normalize_location(""), ("", "")) |
| 582 | + self.assertEqual(_normalize_location(None), ("", "")) |
| 583 | + |
| 584 | + def _make_cmd_ts(self, resource_labels, cmd="GET"): |
| 585 | + mock_ts = MagicMock() |
| 586 | + mock_ts.resource.labels = resource_labels |
| 587 | + mock_ts.metric.labels = {"cmd": cmd} |
| 588 | + mock_point = MagicMock() |
| 589 | + mock_point.interval.start_time.timestamp.return_value = 1000.0 |
| 590 | + mock_point.value.int64_value = 1 |
| 591 | + mock_point.value.double_value = 0 |
| 592 | + mock_ts.points = [mock_point] |
| 593 | + return mock_ts |
| 594 | + |
| 595 | + def test_accumulate_commands_cluster_location_splits_into_region_and_zone(self): |
| 596 | + """Redis Cluster: location=<zone> must split into Region + Zone""" |
| 597 | + table = {} |
| 598 | + ts = self._make_cmd_ts( |
| 599 | + {"cluster_id": "c1", "shard_id": "s0", "location": "us-central1-a"} |
| 600 | + ) |
| 601 | + _accumulate_commands([ts], table, "Redis Cluster", "proj") |
| 602 | + entry = table["proj/c1"]["s0"] |
| 603 | + self.assertEqual(entry["Region"], "us-central1") |
| 604 | + self.assertEqual(entry["Zone"], "us-central1-a") |
| 605 | + |
| 606 | + def test_accumulate_commands_standalone_keeps_region_and_explicit_zone(self): |
| 607 | + """Redis standalone: explicit region + zone labels survive untouched""" |
| 608 | + table = {} |
| 609 | + ts = self._make_cmd_ts( |
| 610 | + { |
| 611 | + "instance_id": "projects/proj/locations/us-central1/instances/r1", |
| 612 | + "node_id": "n0", |
| 613 | + "region": "us-central1", |
| 614 | + "zone": "us-central1-a", |
| 615 | + } |
| 616 | + ) |
| 617 | + _accumulate_commands([ts], table, "Redis", "proj") |
| 618 | + entry = table["projects/proj/locations/us-central1/instances/r1"]["n0"] |
| 619 | + self.assertEqual(entry["Region"], "us-central1") |
| 620 | + self.assertEqual(entry["Zone"], "us-central1-a") |
| 621 | + |
| 622 | + def test_accumulate_commands_standalone_region_only_leaves_zone_blank(self): |
| 623 | + """Redis standalone: region-only label leaves Zone blank (regex no match)""" |
| 624 | + table = {} |
| 625 | + ts = self._make_cmd_ts( |
| 626 | + { |
| 627 | + "instance_id": "projects/proj/locations/us-east4/instances/r1", |
| 628 | + "node_id": "n0", |
| 629 | + "region": "us-east4", |
| 630 | + } |
| 631 | + ) |
| 632 | + _accumulate_commands([ts], table, "Redis", "proj") |
| 633 | + entry = table["projects/proj/locations/us-east4/instances/r1"]["n0"] |
| 634 | + self.assertEqual(entry["Region"], "us-east4") |
| 635 | + self.assertEqual(entry["Zone"], "") |
| 636 | + |
559 | 637 |
|
560 | 638 | if __name__ == "__main__": |
561 | 639 | unittest.main() |
0 commit comments