-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_cname_main.py
More file actions
146 lines (109 loc) · 3.81 KB
/
test_cname_main.py
File metadata and controls
146 lines (109 loc) · 3.81 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import logging
import pytest
import sys
import types
import gardenlinux.features.cname_main as cname_main
from gardenlinux.features import CName
def test_main_happy(monkeypatch, capsys):
"""
Test the "Happy Path" of the main() function.
"""
# Arrange
argv = ["prog", "--arch", "amd64", "--version", "1.0-abc123", "flav-amd64"]
monkeypatch.setattr(sys, "argv", argv)
class FakeGraph:
in_degree = lambda self: [("f1", 0)]
edges = [("f1", "f2")]
class FakeParser:
def __init__(self, *a, **k):
pass
def filter(self, *a, **k):
return FakeGraph()
@staticmethod
def sort_graph_nodes(graph):
return ["f1", "f2"]
monkeypatch.setattr(cname_main, "Parser", FakeParser)
# Act
cname_main.main()
# Assert
out = capsys.readouterr().out
assert "f1" in out
assert "amd64" in out
def test_main_version_from_file(monkeypatch, capsys):
"""
"Happy Path" test for grabbing the version and commit id from file in main().
"""
# Arrange
argv = ["prog", "--arch", "amd64", "flav-amd64"]
monkeypatch.setattr(sys, "argv", argv)
monkeypatch.setattr(
cname_main,
"get_version_and_commit_id_from_files",
lambda root: ("2.0", "abcdef12"),
)
class FakeParser:
def __init__(self, *a, **k):
pass
def filter(self, *a, **k):
return types.SimpleNamespace(in_degree=lambda: [("f1", 0)], edges=[])
@staticmethod
def sort_graph_nodes(graph):
return ["f1"]
monkeypatch.setattr(cname_main, "Parser", FakeParser)
# Act
cname_main.main()
# Assert
assert "2.0-abcdef12" in capsys.readouterr().out
def test_cname_main_version_file_missing_warns(monkeypatch, caplog):
"""
Check if a warning is logged when it fails to read version and commit id files.
Specifically, this test simulates a scenario where the helper function
`get_version_and_commit_id_from_files` raises a RuntimeError, which would occur
if the expected version or commit files are missing or unreadable.
"""
# Arrange
argv = ["prog", "--arch", "amd64", "flav-amd64"]
monkeypatch.setattr(sys, "argv", argv)
# Patch version fatch function to raise RuntimeError (Simulates missing files)
def raise_runtime(_):
raise RuntimeError("missing")
monkeypatch.setattr(
cname_main, "get_version_and_commit_id_from_files", raise_runtime
)
# Patch Parser for minimal valid graph
class FakeParser:
def __init__(self, *a, **k):
pass
# Return object with in_degree method returning a node with zero dependencies
def filter(self, *a, **k):
return types.SimpleNamespace(in_degree=lambda: [("f1", 0)], edges=[])
@staticmethod
def sort_graph_nodes(graph):
return ["f1"]
monkeypatch.setattr(cname_main, "Parser", FakeParser)
# Capture any logs with WARNING level
caplog.set_level(logging.WARNING)
# Act
cname_main.main()
# Assert
assert "Failed to parse version information" in caplog.text
def test_cname_main_invalid_cname_raises(monkeypatch):
"""
Test if AssertionError is raised with an invalid or malformed cname.
"""
# Arrange
argv = ["prog", "--arch", "amd64", "--version", "1.0", "INVALID@NAME"]
monkeypatch.setattr(sys, "argv", argv)
# Act / Assert
with pytest.raises(AssertionError):
cname_main.main()
def test_cname_main_missing_arch_in_cname_raises(monkeypatch):
"""
Test if an assertion error is raised when the arch argument is missing.
"""
# Arrange
argv = ["prog", "--version", "1.0", "flav"]
monkeypatch.setattr(sys, "argv", argv)
# Act / Assert
with pytest.raises(AssertionError):
cname_main.main()