|
| 1 | +import sys |
| 2 | +import types |
| 3 | +import logging |
| 4 | +import builtins |
| 5 | +import pytest |
| 6 | + |
| 7 | +import gardenlinux.features.cname_main as cname_main |
| 8 | + |
| 9 | + |
| 10 | +def test_main_happy(monkeypatch, capsys): |
| 11 | + # Arrange |
| 12 | + argv = ["prog", "--arch", "amd64", "--version", "1.0-abc123", "flav-amd64"] |
| 13 | + monkeypatch.setattr(sys, "argv", argv) |
| 14 | + |
| 15 | + class FakeCName: |
| 16 | + def __init__(self, cname, arch=None, version=None): |
| 17 | + self.arch = arch |
| 18 | + self.flavor = "flav" |
| 19 | + self.version_and_commit_id = "1.0-abc123" |
| 20 | + |
| 21 | + monkeypatch.setattr(cname_main, "CName", FakeCName) |
| 22 | + |
| 23 | + class FakeGraph: |
| 24 | + in_degree = lambda self: [("f1", 0)] |
| 25 | + edges = [("f1", "f2")] |
| 26 | + |
| 27 | + class FakeParser: |
| 28 | + def __init__(self, *a, **k): |
| 29 | + pass |
| 30 | + |
| 31 | + def filter(self, *a, **k): |
| 32 | + return FakeGraph() |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def sort_graph_nodes(graph): |
| 36 | + return ["f1", "f2"] |
| 37 | + |
| 38 | + monkeypatch.setattr(cname_main, "Parser", FakeParser) |
| 39 | + |
| 40 | + # Act |
| 41 | + cname_main.main() |
| 42 | + |
| 43 | + # Assert |
| 44 | + out = capsys.readouterr().out |
| 45 | + assert "f1" in out |
| 46 | + assert "amd64" in out |
| 47 | + |
| 48 | + |
| 49 | +def test_main_version_from_file(monkeypatch, capsys): |
| 50 | + # Arrange |
| 51 | + argv = ["prog", "--arch", "amd64", "flav-amd64"] |
| 52 | + monkeypatch.setattr(sys, "argv", argv) |
| 53 | + |
| 54 | + monkeypatch.setattr( |
| 55 | + cname_main, |
| 56 | + "get_version_and_commit_id_from_files", |
| 57 | + lambda root: ("2.0", "abcdef12"), |
| 58 | + ) |
| 59 | + |
| 60 | + class FakeCName: |
| 61 | + def __init__(self, cname, arch=None, version=None): |
| 62 | + self.arch = arch |
| 63 | + self.flavor = "flav" |
| 64 | + self.version_and_commit_id = version |
| 65 | + |
| 66 | + monkeypatch.setattr(cname_main, "CName", FakeCName) |
| 67 | + |
| 68 | + class FakeParser: |
| 69 | + def __init__(self, *a, **k): |
| 70 | + pass |
| 71 | + |
| 72 | + def filter(self, *a, **k): |
| 73 | + return types.SimpleNamespace(in_degree=lambda: [("f1", 0)], edges=[]) |
| 74 | + |
| 75 | + @staticmethod |
| 76 | + def sort_graph_nodes(graph): |
| 77 | + return ["f1"] |
| 78 | + |
| 79 | + monkeypatch.setattr(cname_main, "Parser", FakeParser) |
| 80 | + |
| 81 | + # Act |
| 82 | + cname_main.main() |
| 83 | + |
| 84 | + # Assert |
| 85 | + assert "2.0-abcdef12" in capsys.readouterr().out |
| 86 | + |
| 87 | + |
| 88 | +def test_cname_main_version_file_missing_warns(monkeypatch, caplog): |
| 89 | + """ |
| 90 | + Check if a warning is logged when it fails to read version and commit id files. |
| 91 | +
|
| 92 | + Specifically, this test simulates a scenario where the helper function |
| 93 | + `get_version_and_commit_id_from_files` raises a RuntimeError, which would occur |
| 94 | + if the expected version or commit files are missing or unreadable. |
| 95 | + """ |
| 96 | + # Arrange |
| 97 | + argv = ["prog", "--arch", "amd64", "flav-amd64"] |
| 98 | + monkeypatch.setattr(sys, "argv", argv) |
| 99 | + |
| 100 | + # Patch version fatch function to raise RuntimeError (Simulates missing files) |
| 101 | + def raise_runtime(_): |
| 102 | + raise RuntimeError("missing") |
| 103 | + |
| 104 | + monkeypatch.setattr( |
| 105 | + cname_main, "get_version_and_commit_id_from_files", raise_runtime |
| 106 | + ) |
| 107 | + |
| 108 | + # Patch CName to control attributes |
| 109 | + class FakeCName: |
| 110 | + def __init__(self, cname, arch=None, version=None): |
| 111 | + self.arch = arch |
| 112 | + self.flavor = "flav" |
| 113 | + self.version_and_commit_id = version |
| 114 | + |
| 115 | + monkeypatch.setattr(cname_main, "CName", FakeCName) |
| 116 | + |
| 117 | + # Patch Parser for minimal valid graph |
| 118 | + class FakeParser: |
| 119 | + def __init__(self, *a, **k): |
| 120 | + pass |
| 121 | + |
| 122 | + # Return object with in_degree method returning a node with zero dependencies |
| 123 | + def filter(self, *a, **k): |
| 124 | + return types.SimpleNamespace(in_degree=lambda: [("f1", 0)], edges=[]) |
| 125 | + |
| 126 | + @staticmethod |
| 127 | + def sort_graph_nodes(graph): |
| 128 | + return ["f1"] |
| 129 | + |
| 130 | + monkeypatch.setattr(cname_main, "Parser", FakeParser) |
| 131 | + |
| 132 | + # Capture any logs with WARNING level |
| 133 | + caplog.set_level(logging.WARNING) |
| 134 | + |
| 135 | + # Act |
| 136 | + cname_main.main() |
| 137 | + |
| 138 | + # Assert |
| 139 | + assert "Failed to parse version information" in caplog.text |
| 140 | + |
| 141 | + |
| 142 | +def test_cname_main_invalid_cname(monkeypatch): |
| 143 | + """ |
| 144 | + Test if AssertionError is raised with an invalid or malformed cname. |
| 145 | + """ |
| 146 | + # Arrange |
| 147 | + argv = ["prog", "--arch", "amd64", "--version", "1.0", "INVALID@NAME"] |
| 148 | + monkeypatch.setattr(sys, "argv", argv) |
| 149 | + |
| 150 | + # Act / Assert |
| 151 | + with pytest.raises(AssertionError): |
| 152 | + cname_main.main() |
| 153 | + |
| 154 | + |
| 155 | +def test_cname_main_missing_arch_in_cname(monkeypatch): |
| 156 | + """ |
| 157 | + Test if an assertion error is raised when the arch argument is missing. |
| 158 | + """ |
| 159 | + # Arrange |
| 160 | + argv = ["prog", "--version", "1.0", "flav"] |
| 161 | + monkeypatch.setattr(sys, "argv", argv) |
| 162 | + |
| 163 | + class FakeCName: |
| 164 | + def __init__(self, cname, arch=None, version=None): |
| 165 | + self.arch = None # Force missing arch |
| 166 | + self.flavor = "flav" |
| 167 | + self.version_and_commit_id = "1.0-abc" |
| 168 | + |
| 169 | + monkeypatch.setattr(cname_main, "CName", FakeCName) |
| 170 | + |
| 171 | + # Act / Assert |
| 172 | + with pytest.raises(AssertionError): |
| 173 | + cname_main.main() |
0 commit comments