mirror: use properties to get and set mirror uri

Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
This commit is contained in:
Olivier Gayot 2023-01-29 16:46:44 +01:00
parent 927df0b845
commit 23db4b597b
5 changed files with 33 additions and 31 deletions

View File

@ -55,19 +55,21 @@ class PrimarySection:
self.parent = parent
self.config = config
def get_mirror(self) -> str:
@property
def uri(self) -> str:
config = copy.deepcopy(self.parent.config)
config["primary"] = self.config
return get_mirror(config, "primary", self.parent.architecture)
def set_mirror(self, uri: str) -> None:
@uri.setter
def uri(self, uri: str) -> None:
config = get_arch_mirrorconfig(
{"primary": self.config},
"primary", self.parent.architecture)
config["uri"] = uri
def mirror_is_default(self) -> bool:
return self.get_mirror() == self.parent.default_mirror
return self.uri == self.parent.default_mirror
@classmethod
def new_from_default(cls, parent: "MirrorModel") -> "PrimarySection":
@ -94,7 +96,7 @@ class MirrorModel(object):
self.primary_staged: Optional[PrimarySection] = None
self.architecture = get_architecture()
self.default_mirror = self.primary_candidates[0].get_mirror()
self.default_mirror = self.primary_candidates[0].uri
def load_autoinstall_data(self, data):
if "disable_components" in data:
@ -132,8 +134,8 @@ class MirrorModel(object):
""" Set the URI of country-mirror candidates. """
for candidate in self.primary_candidates:
if candidate.mirror_is_default():
uri = candidate.get_mirror()
candidate.set_mirror(countrify_uri(uri, cc=cc))
uri = candidate.uri
candidate.uri = countrify_uri(uri, cc=cc)
def disable_components(self, comps, add: bool) -> None:
""" Add (or remove) a component (e.g., multiverse) from the list of
@ -148,14 +150,14 @@ class MirrorModel(object):
self.primary_candidates.clear()
for uri in uris:
section = PrimarySection.new_from_default(parent=self)
section.set_mirror(uri)
section.uri = uri
self.primary_candidates.append(section)
# NOTE: this is sometimes useful but it can be troublesome as well.
self.primary_staged = None
def assign_primary_elected(self, uri: str) -> None:
self.primary_elected = PrimarySection.new_from_default(parent=self)
self.primary_elected.set_mirror(uri)
self.primary_elected.uri = uri
def wants_geoip(self) -> bool:
""" Tell whether geoip results would be useful. """

View File

@ -64,16 +64,16 @@ class TestPrimarySection(unittest.TestCase):
primary = PrimarySection.new_from_default(parent=self.model)
self.assertEqual(primary.config, DEFAULT_PRIMARY_SECTION)
def test_get_mirror(self):
def test_get_uri(self):
self.model.architecture = "amd64"
primary = PrimarySection([{"uri": "http://myurl", "arches": "amd64"}],
parent=self.model)
self.assertEqual(primary.get_mirror(), "http://myurl")
self.assertEqual(primary.uri, "http://myurl")
def test_set_mirror(self):
def test_set_uri(self):
primary = PrimarySection.new_from_default(parent=self.model)
primary.set_mirror("http://mymirror.invalid/")
self.assertEqual(primary.get_mirror(), "http://mymirror.invalid/")
primary.uri = "http://mymirror.invalid/"
self.assertEqual(primary.uri, "http://mymirror.invalid/")
class TestMirrorModel(unittest.TestCase):
@ -85,17 +85,17 @@ class TestMirrorModel(unittest.TestCase):
def test_set_country(self):
self.model.set_country("CC")
self.assertIn(
self.candidate.get_mirror(),
self.candidate.uri,
[
"http://CC.archive.ubuntu.com/ubuntu",
"http://CC.ports.ubuntu.com/ubuntu-ports",
])
def test_set_country_after_set_mirror(self):
def test_set_country_after_set_uri(self):
candidate = self.model.primary_candidates[0]
candidate.set_mirror("http://mymirror.invalid/")
candidate.uri = "http://mymirror.invalid/"
self.model.set_country("CC")
self.assertEqual(candidate.get_mirror(), "http://mymirror.invalid/")
self.assertEqual(candidate.uri, "http://mymirror.invalid/")
def test_default_disable_components(self):
config = self.model.get_apt_config_staged()
@ -141,18 +141,18 @@ class TestMirrorModel(unittest.TestCase):
def test_replace_primary_candidates(self):
self.model.replace_primary_candidates(["http://single-valid"])
self.assertEqual(len(self.model.primary_candidates), 1)
self.assertEqual(self.model.primary_candidates[0].get_mirror(),
self.assertEqual(self.model.primary_candidates[0].uri,
"http://single-valid")
self.model.replace_primary_candidates(
["http://valid1", "http://valid2"])
self.assertEqual(len(self.model.primary_candidates), 2)
self.assertEqual(self.model.primary_candidates[0].get_mirror(),
self.assertEqual(self.model.primary_candidates[0].uri,
"http://valid1")
self.assertEqual(self.model.primary_candidates[1].get_mirror(),
self.assertEqual(self.model.primary_candidates[1].uri,
"http://valid2")
def test_assign_primary_elected(self):
self.model.assign_primary_elected("http://mymirror.valid")
self.assertEqual(self.model.primary_elected.get_mirror(),
self.assertEqual(self.model.primary_elected.uri,
"http://mymirror.valid")

View File

@ -325,7 +325,7 @@ class DryRunAptConfigurer(AptConfigurer):
async def apt_config_check_failure(self, output: io.StringIO) -> None:
""" Pretend that the execution of the apt-get update command results in
a failure. """
url = self.app.base_model.mirror.primary_staged.get_mirror()
url = self.app.base_model.mirror.primary_staged.uri
release = lsb_release(dry_run=True)["codename"]
host = url.split("/")[2]
@ -361,7 +361,7 @@ E: Some index files failed to download. They have been ignored,
async def apt_config_check_success(self, output: io.StringIO) -> None:
""" Pretend that the execution of the apt-get update command results in
a success. """
url = self.app.base_model.mirror.primary_staged.get_mirror()
url = self.app.base_model.mirror.primary_staged.uri
release = lsb_release(dry_run=True)["codename"]
output.write(f"""\
@ -388,7 +388,7 @@ Reading package lists...
self.MirrorCheckStrategy.SUCCESS: success,
self.MirrorCheckStrategy.RANDOM: random.choice([failure, success]),
}
mirror_url = self.app.base_model.mirror.primary_staged.get_mirror()
mirror_url = self.app.base_model.mirror.primary_staged.uri
strategy = strategies[self.get_mirror_check_strategy(mirror_url)]

View File

@ -191,7 +191,7 @@ class MirrorController(SubiquityController):
def serialize(self):
# TODO what to do with the candidates?
if self.model.primary_elected is not None:
return self.model.primary_elected.get_mirror()
return self.model.primary_elected.uri
return None
def deserialize(self, data):
@ -226,8 +226,8 @@ class MirrorController(SubiquityController):
async def GET(self) -> str:
# TODO farfetched
if self.model.primary_elected is not None:
return self.model.primary_elected.get_mirror()
return self.model.primary_candidates[0].get_mirror()
return self.model.primary_elected.uri
return self.model.primary_candidates[0].uri
async def POST(self, data: str):
log.debug(data)
@ -255,7 +255,7 @@ class MirrorController(SubiquityController):
assert False
output = io.StringIO()
self.mirror_check = MirrorCheck(
uri=self.model.primary_staged.get_mirror(),
uri=self.model.primary_staged.uri,
task=asyncio.create_task(self.run_mirror_testing(output)),
output=output)

View File

@ -178,20 +178,20 @@ class TestDRAptConfigurer(SubiTestCase):
async def test_run_apt_config_check_success(self):
output = io.StringIO()
self.app.dr_cfg.apt_mirror_check_default_strategy = "success"
self.candidate.set_mirror("http://default")
self.candidate.uri = "http://default"
await self.configurer.run_apt_config_check(output)
async def test_run_apt_config_check_failed(self):
output = io.StringIO()
self.app.dr_cfg.apt_mirror_check_default_strategy = "failure"
self.candidate.set_mirror("http://default")
self.candidate.uri = "http://default"
with self.assertRaises(AptConfigCheckError):
await self.configurer.run_apt_config_check(output)
async def test_run_apt_config_check_random(self):
output = io.StringIO()
self.app.dr_cfg.apt_mirror_check_default_strategy = "random"
self.candidate.set_mirror("http://default")
self.candidate.uri = "http://default"
with patch("subiquity.server.apt.random.choice",
return_value=self.configurer.apt_config_check_success):
await self.configurer.run_apt_config_check(output)