| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
import pytest |
| 26 |
from unittest.mock import mock_open, patch |
| 27 |
from pathlib import Path |
| 28 |
from Tensile.Common.Utilities import isRhel8 |
| 29 |
|
| 30 |
@pytest.fixture |
| 31 |
def mock_openFile(): |
| 32 |
with patch("builtins.open", mock_open()) as mock: |
| 33 |
yield mock |
| 34 |
|
| 35 |
@pytest.fixture |
| 36 |
def mock_exists(): |
| 37 |
with patch.object(Path, "exists", return_value=True) as mock: |
| 38 |
yield mock |
| 39 |
|
| 40 |
@pytest.fixture |
| 41 |
def mock_notExists(): |
| 42 |
with patch.object(Path, "exists", return_value=False) as mock: |
| 43 |
yield mock |
| 44 |
|
| 45 |
|
| 46 |
def test_isRhel8_true(mock_exists, mock_openFile): |
| 47 |
mock_openFile.return_value.read.return_value = 'NAME="Red Hat Enterprise Linux" VERSION_ID="8.4"' |
| 48 |
assert isRhel8() is True |
| 49 |
mock_exists.assert_called_once_with() |
| 50 |
mock_openFile.assert_called_once_with(Path("/etc/os-release"), "r") |
| 51 |
|
| 52 |
def test_isRhel8_false_non_rhel(mock_exists, mock_openFile): |
| 53 |
mock_openFile.return_value.read.return_value = 'NAME="Ubuntu" VERSION_ID="20.04"' |
| 54 |
assert isRhel8() is False |
| 55 |
mock_exists.assert_called_once_with() |
| 56 |
mock_openFile.assert_called_once_with(Path("/etc/os-release"), "r") |
| 57 |
|
| 58 |
def test_isRhel8_false_new_version(mock_exists, mock_openFile): |
| 59 |
mock_openFile.return_value.read.return_value = 'NAME="Red Hat Enterprise Linux" VERSION_ID="9.0"' |
| 60 |
assert isRhel8() is False |
| 61 |
mock_exists.assert_called_once_with() |
| 62 |
mock_openFile.assert_called_once_with(Path("/etc/os-release"), "r") |
| 63 |
|
| 64 |
def test_isRhel8_true_with_warning(mock_exists, mock_openFile): |
| 65 |
mock_openFile.return_value.read.return_value = 'NAME="Red Hat Enterprise Linux" VERSION_ID="8.5"' |
| 66 |
assert isRhel8() is True |
| 67 |
mock_exists.assert_called_once_with() |
| 68 |
mock_openFile.assert_called_once_with(Path("/etc/os-release"), "r") |
| 69 |
|
| 70 |
def test_isRhel8_file_not_found(mock_notExists, mock_openFile): |
| 71 |
assert isRhel8() is False |
| 72 |
mock_notExists.assert_called_once_with() |
| 73 |
mock_openFile.assert_not_called() |