62
65
# a huge score making it impossible to win.
65
index = get_index('index_09.json')
68
index = get_index('scores.index_03.json')
66
69
candidates = get_candidates(index, 600)
67
70
# There are three paths. The scores are as above.
68
71
scores = self.scorer.score(candidates)
69
72
self.assertEqual(scores, [300, 200, 9401])
70
winner = self.scorer.choose(candidates)
73
winner = self.scorer.choose(candidates, 'devel')
71
74
self.assertEqual(len(winner), 3)
72
75
self.assertEqual([image.version for image in winner],
73
76
[1200, 1201, 1304])
76
# There's only one description per image so order doesn't matter.
77
descriptions.extend(image.descriptions.values())
78
self.assertEqual(descriptions, ['Full B', 'Delta B.1', 'Delta B.2'])
77
self.assertEqual(descriptions(winner),
78
['Full B', 'Delta B.1', 'Delta B.2'])
80
80
def test_tied_candidates(self):
81
81
# LP: #1206866 - TypeError when two candidate paths scored equal.
83
# index_17.json was captured from real data causing the traceback.
84
index = get_index('index_17.json')
83
# index_04.json was captured from real data causing the traceback.
84
index = get_index('scores.index_04.json')
85
85
candidates = get_candidates(index, 1)
86
path = self.scorer.choose(candidates)
86
path = self.scorer.choose(candidates, 'devel')
87
87
self.assertEqual(len(path), 1)
88
88
self.assertEqual(path[0].version, 1800)
91
class TestPhasedUpdates(unittest.TestCase):
93
self.scorer = WeightedScorer()
95
def test_inside_phase_gets_update(self):
96
# When the final image on an update path has a phase percentage higher
97
# than the device percentage, the candidate path is okay. In this
98
# case, the `Full B` has phase of 50%.
99
index = get_index('scores.index_05.json')
100
candidates = get_candidates(index, 100)
101
with patch('systemimage.scores.phased_percentage', return_value=22):
102
winner = self.scorer.choose(candidates, 'devel')
105
descriptions.extend(image.descriptions.values())
106
self.assertEqual(descriptions, ['Full B', 'Delta B.1', 'Delta B.2'])
108
def test_outside_phase_gets_update(self):
109
# When the final image on an update path has a phase percentage lower
110
# than the device percentage, the scorer falls back to the next
112
index = get_index('scores.index_05.json')
113
candidates = get_candidates(index, 100)
114
with patch('systemimage.scores.phased_percentage', return_value=66):
115
winner = self.scorer.choose(candidates, 'devel')
116
self.assertEqual(descriptions(winner),
117
['Full A', 'Delta A.1', 'Delta A.2'])
119
def test_equal_phase_gets_update(self):
120
# When the final image on an update path has a phase percentage exactly
121
# equal to the device percentage, the candidate path is okay. In this
122
# case, the `Full B` has phase of 50%.
123
index = get_index('scores.index_05.json')
124
candidates = get_candidates(index, 100)
125
with patch('systemimage.scores.phased_percentage', return_value=50):
126
winner = self.scorer.choose(candidates, 'devel')
127
self.assertEqual(descriptions(winner),
128
['Full B', 'Delta B.1', 'Delta B.2'])
130
def test_pulled_update(self):
131
# When the final image on an update path has a phase percentage of
132
# zero, then regardless of the device's percentage, the candidate path
133
# is not okay. In this case, the `Full B` has phase of 0%.
134
index = get_index('scores.index_01.json')
135
candidates = get_candidates(index, 100)
136
with patch('systemimage.scores.phased_percentage', return_value=0):
137
winner = self.scorer.choose(candidates, 'devel')
138
self.assertEqual(descriptions(winner),
139
['Full A', 'Delta A.1', 'Delta A.2'])
141
def test_pulled_update_insanely_negative_randint(self):
142
# When the final image on an update path has a phase percentage of
143
# zero, then regardless of the device's percentage (even if randint
144
# returned some insane value), the candidate path is not okay. In this
145
# case, the `Full B` has phase of 0%.
146
index = get_index('scores.index_01.json')
147
candidates = get_candidates(index, 100)
148
with patch('systemimage.scores.phased_percentage', return_value=-100):
149
winner = self.scorer.choose(candidates, 'devel')
150
self.assertEqual(descriptions(winner),
151
['Full A', 'Delta A.1', 'Delta A.2'])
153
def test_pulled_update_insanely_positive_randint(self):
154
# When the final image on an update path has a phase percentage of
155
# zero, then regardless of the device's percentage (even if randint
156
# returned some insane value), the candidate path is not okay. In this
157
# case, the `Full B` has phase of 0%.
158
index = get_index('scores.index_01.json')
159
candidates = get_candidates(index, 100)
160
with patch('systemimage.scores.phased_percentage', return_value=1000):
161
winner = self.scorer.choose(candidates, 'devel')
162
self.assertEqual(len(winner), 0)
165
class TestVersionDetail(unittest.TestCase):
167
self.scorer = WeightedScorer()
169
def test_version_detail(self):
170
# The index.json file has three paths for updates, but only one is
171
# selected. The winning path lands on an image with a version_detail
173
index = get_index('scores.index_06.json')
174
candidates = get_candidates(index, 600)
175
scores = self.scorer.score(candidates)
176
self.assertEqual(scores, [300, 200, 9401])
177
winner = self.scorer.choose(candidates, 'devel')
178
self.assertEqual(len(winner), 3)
179
self.assertEqual([image.version for image in winner],
181
self.assertEqual(descriptions(winner),
182
['Full B', 'Delta B.1', 'Delta B.2'])
183
self.assertEqual(winner[-1].version_detail,
184
"ubuntu=105,raw-device=205,version=305")
186
def test_no_version_detail(self):
187
# The index.json file has three paths for updates, but only one is
188
# selected. The winning path lands on an image without a
189
# version_detail key.
190
index = get_index('scores.index_07.json')
191
candidates = get_candidates(index, 600)
192
scores = self.scorer.score(candidates)
193
self.assertEqual(scores, [300, 200, 9401])
194
winner = self.scorer.choose(candidates, 'devel')
195
self.assertEqual(len(winner), 3)
196
self.assertEqual([image.version for image in winner],
198
self.assertEqual(descriptions(winner),
199
['Full B', 'Delta B.1', 'Delta B.2'])
200
self.assertEqual(winner[-1].version_detail, '')