|
17 | 17 | from django.test import TestCase, override_settings |
18 | 18 |
|
19 | 19 | from operatingsystems.models import OSRelease, OSVariant |
| 20 | +from operatingsystems.utils import normalize_el_osrelease |
| 21 | + |
| 22 | + |
| 23 | +@override_settings( |
| 24 | + CELERY_TASK_ALWAYS_EAGER=True, |
| 25 | + CACHES={'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}} |
| 26 | +) |
| 27 | +class NormalizeELOSReleaseTests(TestCase): |
| 28 | + """Tests for normalize_el_osrelease() function. |
| 29 | +
|
| 30 | + This function normalizes EL-based distro names to major version only, |
| 31 | + ensuring consistent OSRelease naming across errata and reports. |
| 32 | +
|
| 33 | + Regression notes - OLD behavior that caused duplicate OSReleases: |
| 34 | + - 'rocky-linux-10.1' -> 'Rocky Linux 10.1' (should be 'Rocky Linux 10') |
| 35 | + - 'almalinux-10.1' -> 'Alma Linux 10.1' (should be 'Alma Linux 10') |
| 36 | + - 'Rocky Linux 10.1' -> passed through unchanged (should be 'Rocky Linux 10') |
| 37 | + - 'CentOS 7.9' -> passed through unchanged (should be 'CentOS 7') |
| 38 | + """ |
| 39 | + |
| 40 | + # =========================================== |
| 41 | + # REGRESSION TESTS - These were bugs before |
| 42 | + # =========================================== |
| 43 | + |
| 44 | + def test_regression_rocky_dash_format_minor_stripped(self): |
| 45 | + """REGRESSION: rocky-linux-10.1 was creating 'Rocky Linux 10.1' |
| 46 | +
|
| 47 | + Old behavior: version = osrelease_name.split('-')[2] -> '10.1' |
| 48 | + result = 'Rocky Linux 10.1' |
| 49 | + New behavior: major_version = '10.1'.split('.')[0] -> '10' |
| 50 | + result = 'Rocky Linux 10' |
| 51 | + """ |
| 52 | + # OLD (wrong): 'Rocky Linux 10.1' |
| 53 | + # NEW (correct): 'Rocky Linux 10' |
| 54 | + self.assertEqual(normalize_el_osrelease('rocky-linux-10.1'), 'Rocky Linux 10') |
| 55 | + self.assertNotEqual(normalize_el_osrelease('rocky-linux-10.1'), 'Rocky Linux 10.1') |
| 56 | + |
| 57 | + def test_regression_alma_dash_format_minor_stripped(self): |
| 58 | + """REGRESSION: almalinux-10.1 was creating 'Alma Linux 10.1' |
| 59 | +
|
| 60 | + Old behavior: version = osrelease_name.split('-')[1] -> '10.1' |
| 61 | + result = 'Alma Linux 10.1' |
| 62 | + New behavior: major_version = '10.1'.split('.')[0] -> '10' |
| 63 | + result = 'Alma Linux 10' |
| 64 | + """ |
| 65 | + # OLD (wrong): 'Alma Linux 10.1' |
| 66 | + # NEW (correct): 'Alma Linux 10' |
| 67 | + self.assertEqual(normalize_el_osrelease('almalinux-10.1'), 'Alma Linux 10') |
| 68 | + self.assertNotEqual(normalize_el_osrelease('almalinux-10.1'), 'Alma Linux 10.1') |
| 69 | + |
| 70 | + def test_regression_rocky_human_format_minor_stripped(self): |
| 71 | + """REGRESSION: 'Rocky Linux 10.1' was passed through unchanged |
| 72 | +
|
| 73 | + Old behavior: no handling, passed through as 'Rocky Linux 10.1' |
| 74 | + New behavior: normalized to 'Rocky Linux 10' |
| 75 | + """ |
| 76 | + # OLD (wrong): 'Rocky Linux 10.1' |
| 77 | + # NEW (correct): 'Rocky Linux 10' |
| 78 | + self.assertEqual(normalize_el_osrelease('Rocky Linux 10.1'), 'Rocky Linux 10') |
| 79 | + self.assertNotEqual(normalize_el_osrelease('Rocky Linux 10.1'), 'Rocky Linux 10.1') |
| 80 | + |
| 81 | + def test_regression_centos_minor_stripped(self): |
| 82 | + """REGRESSION: 'CentOS 7.9' was passed through unchanged |
| 83 | +
|
| 84 | + Old behavior: no handling for human-readable format |
| 85 | + New behavior: normalized to 'CentOS 7' |
| 86 | + """ |
| 87 | + # OLD (wrong): 'CentOS 7.9' |
| 88 | + # NEW (correct): 'CentOS 7' |
| 89 | + self.assertEqual(normalize_el_osrelease('CentOS 7.9'), 'CentOS 7') |
| 90 | + self.assertNotEqual(normalize_el_osrelease('CentOS 7.9'), 'CentOS 7.9') |
| 91 | + |
| 92 | + # =========================================== |
| 93 | + # STANDARD TESTS - Expected behavior |
| 94 | + # =========================================== |
| 95 | + |
| 96 | + def test_rocky_linux_with_minor_version(self): |
| 97 | + """Test Rocky Linux X.Y -> Rocky Linux X""" |
| 98 | + self.assertEqual(normalize_el_osrelease('Rocky Linux 10.1'), 'Rocky Linux 10') |
| 99 | + self.assertEqual(normalize_el_osrelease('Rocky Linux 9.3'), 'Rocky Linux 9') |
| 100 | + |
| 101 | + def test_rocky_linux_dash_format(self): |
| 102 | + """Test rocky-linux-X.Y -> Rocky Linux X""" |
| 103 | + self.assertEqual(normalize_el_osrelease('rocky-linux-10.1'), 'Rocky Linux 10') |
| 104 | + self.assertEqual(normalize_el_osrelease('rocky-linux-9.3'), 'Rocky Linux 9') |
| 105 | + |
| 106 | + def test_alma_linux_with_minor_version(self): |
| 107 | + """Test Alma Linux X.Y -> Alma Linux X""" |
| 108 | + self.assertEqual(normalize_el_osrelease('Alma Linux 10.1'), 'Alma Linux 10') |
| 109 | + self.assertEqual(normalize_el_osrelease('Alma Linux 9.3'), 'Alma Linux 9') |
| 110 | + |
| 111 | + def test_almalinux_dash_format(self): |
| 112 | + """Test almalinux-X.Y -> Alma Linux X""" |
| 113 | + self.assertEqual(normalize_el_osrelease('almalinux-10.1'), 'Alma Linux 10') |
| 114 | + self.assertEqual(normalize_el_osrelease('almalinux-9.3'), 'Alma Linux 9') |
| 115 | + |
| 116 | + def test_almalinux_no_space(self): |
| 117 | + """Test AlmaLinux X.Y -> AlmaLinux X""" |
| 118 | + self.assertEqual(normalize_el_osrelease('AlmaLinux 10.1'), 'AlmaLinux 10') |
| 119 | + |
| 120 | + def test_centos_with_minor_version(self): |
| 121 | + """Test CentOS X.Y -> CentOS X""" |
| 122 | + self.assertEqual(normalize_el_osrelease('CentOS 7.9'), 'CentOS 7') |
| 123 | + self.assertEqual(normalize_el_osrelease('CentOS 8.5'), 'CentOS 8') |
| 124 | + |
| 125 | + def test_rhel_with_minor_version(self): |
| 126 | + """Test RHEL X.Y -> RHEL X""" |
| 127 | + self.assertEqual(normalize_el_osrelease('RHEL 8.2'), 'RHEL 8') |
| 128 | + self.assertEqual(normalize_el_osrelease('RHEL 9.1'), 'RHEL 9') |
| 129 | + |
| 130 | + def test_red_hat_enterprise_linux_with_minor_version(self): |
| 131 | + """Test Red Hat Enterprise Linux X.Y -> Red Hat Enterprise Linux X""" |
| 132 | + self.assertEqual( |
| 133 | + normalize_el_osrelease('Red Hat Enterprise Linux 8.2'), |
| 134 | + 'Red Hat Enterprise Linux 8' |
| 135 | + ) |
| 136 | + |
| 137 | + def test_oracle_linux_with_minor_version(self): |
| 138 | + """Test Oracle Linux X.Y -> Oracle Linux X""" |
| 139 | + self.assertEqual(normalize_el_osrelease('Oracle Linux 8.1'), 'Oracle Linux 8') |
| 140 | + |
| 141 | + def test_amazon_linux_normalization(self): |
| 142 | + """Test Amazon Linux -> Amazon Linux 1""" |
| 143 | + self.assertEqual(normalize_el_osrelease('Amazon Linux'), 'Amazon Linux 1') |
| 144 | + self.assertEqual(normalize_el_osrelease('Amazon Linux AMI'), 'Amazon Linux 1') |
| 145 | + |
| 146 | + # =========================================== |
| 147 | + # NO-OP TESTS - Should remain unchanged |
| 148 | + # =========================================== |
| 149 | + |
| 150 | + def test_major_version_only_unchanged(self): |
| 151 | + """Test that major-version-only names are unchanged (no regression)""" |
| 152 | + self.assertEqual(normalize_el_osrelease('Rocky Linux 10'), 'Rocky Linux 10') |
| 153 | + self.assertEqual(normalize_el_osrelease('CentOS 7'), 'CentOS 7') |
| 154 | + self.assertEqual(normalize_el_osrelease('RHEL 9'), 'RHEL 9') |
| 155 | + self.assertEqual(normalize_el_osrelease('Alma Linux 9'), 'Alma Linux 9') |
| 156 | + |
| 157 | + def test_non_el_distros_unchanged(self): |
| 158 | + """Test that non-EL distros are unchanged (no false positives)""" |
| 159 | + self.assertEqual(normalize_el_osrelease('Ubuntu 22.04'), 'Ubuntu 22.04') |
| 160 | + self.assertEqual(normalize_el_osrelease('Debian 12'), 'Debian 12') |
| 161 | + self.assertEqual(normalize_el_osrelease('Fedora 39'), 'Fedora 39') |
| 162 | + self.assertEqual(normalize_el_osrelease('Arch Linux'), 'Arch Linux') |
| 163 | + self.assertEqual(normalize_el_osrelease('openSUSE Leap 15.5'), 'openSUSE Leap 15.5') |
20 | 164 |
|
21 | 165 |
|
22 | 166 | @override_settings( |
|
0 commit comments