|
1 | 1 | from pathlib import Path |
2 | | -from typing import Any |
| 2 | +from typing import Any, Optional |
| 3 | +from unittest.mock import MagicMock, patch |
3 | 4 |
|
4 | 5 | import pytest |
5 | 6 | from operatorcert.operator_repo import Repo |
|
10 | 11 | check_validate_schema_bundle_release_config, |
11 | 12 | check_network_policy_presence, |
12 | 13 | check_operator_version_directory_name, |
| 14 | + check_replaces_availability, |
13 | 15 | ) |
14 | 16 | from tests.utils import bundle_files, create_files |
15 | 17 |
|
@@ -752,3 +754,91 @@ def test_check_operator_version_directory_name( |
752 | 754 | assert { |
753 | 755 | (x.__class__, x.reason) for x in check_operator_version_directory_name(bundle) |
754 | 756 | } == expected_results |
| 757 | + |
| 758 | + |
| 759 | +@pytest.mark.parametrize( |
| 760 | + "bundle_version_annotation,replaces_version_annotation,replaces_csv_value,ocp_range,expected", |
| 761 | + [ |
| 762 | + pytest.param(None, None, None, [], set(), id="No replaces"), |
| 763 | + pytest.param(None, None, "hello.v0.0.1", [], set(), id="No annotations"), |
| 764 | + pytest.param( |
| 765 | + "v4.10", "v4.10", "hello.v0.0.1", [], set(), id="Same annotations" |
| 766 | + ), |
| 767 | + pytest.param( |
| 768 | + "v4.15", |
| 769 | + "v4.15,v4.16", |
| 770 | + "hello.v0.0.1", |
| 771 | + [["v4.15", "v4.16"], ["v4.15", "v4.16"]], |
| 772 | + set(), |
| 773 | + id="Different annotation, versions match", |
| 774 | + ), |
| 775 | + pytest.param( |
| 776 | + "v4.15", |
| 777 | + "v4.16", |
| 778 | + "hello.v0.0.1", |
| 779 | + [["v4.15", "v4.16"], ["v4.16"]], |
| 780 | + { |
| 781 | + Fail( |
| 782 | + "Replaces bundle Bundle(hello/0.0.1) ['v4.16'] does not support " |
| 783 | + "the same OCP versions as bundle Bundle(hello/0.0.2) ['v4.15', 'v4.16']. " |
| 784 | + "In order to fix this issue, align the OCP version range to match the " |
| 785 | + "range of the replaced bundle. " |
| 786 | + "This can be done by setting the `com.redhat.openshift.versions` annotation " |
| 787 | + "in the `metadata/annotations.yaml` file.\n" |
| 788 | + "`Bundle(hello/0.0.2)` - `v4.15`\n" |
| 789 | + "`Bundle(hello/0.0.1)` - `v4.16`" |
| 790 | + ) |
| 791 | + }, |
| 792 | + id="Different annotation, different version", |
| 793 | + ), |
| 794 | + pytest.param( |
| 795 | + None, |
| 796 | + None, |
| 797 | + "hello.v0.0.5", |
| 798 | + [], |
| 799 | + { |
| 800 | + Fail( |
| 801 | + "Bundle(hello/0.0.2) attempts to replace version '0.0.5' which" |
| 802 | + " does not exist. Available versions: ['0.0.1', '0.0.2']" |
| 803 | + ) |
| 804 | + }, |
| 805 | + id="Nonexistent replaces version", |
| 806 | + ), |
| 807 | + ], |
| 808 | +) |
| 809 | +@patch("operatorcert.static_tests.common.bundle.utils.get_ocp_supported_versions") |
| 810 | +def test_check_replaces_availability( |
| 811 | + mock_get_ocp_supported_versions: MagicMock, |
| 812 | + bundle_version_annotation: str, |
| 813 | + replaces_version_annotation: str, |
| 814 | + replaces_csv_value: Optional[str], |
| 815 | + ocp_range: Any, |
| 816 | + expected: Any, |
| 817 | + tmp_path: Path, |
| 818 | +) -> None: |
| 819 | + bundle_annotation = { |
| 820 | + "com.redhat.openshift.versions": bundle_version_annotation, |
| 821 | + } |
| 822 | + replaces_bundle_annotation = { |
| 823 | + "com.redhat.openshift.versions": replaces_version_annotation, |
| 824 | + } |
| 825 | + csv = {"spec": {"replaces": replaces_csv_value}} if replaces_csv_value else {} |
| 826 | + create_files( |
| 827 | + tmp_path, |
| 828 | + bundle_files("hello", "0.0.1", annotations=replaces_bundle_annotation), |
| 829 | + bundle_files( |
| 830 | + "hello", |
| 831 | + "0.0.2", |
| 832 | + annotations=bundle_annotation, |
| 833 | + csv=csv, |
| 834 | + ), |
| 835 | + ) |
| 836 | + |
| 837 | + mock_get_ocp_supported_versions.side_effect = ocp_range |
| 838 | + |
| 839 | + repo = Repo(tmp_path) |
| 840 | + operator = repo.operator("hello") |
| 841 | + bundle = operator.bundle("0.0.2") |
| 842 | + errors = list(check_replaces_availability(bundle)) |
| 843 | + |
| 844 | + assert set(errors) == expected |
0 commit comments