diff --git a/examples/autoinstall.yaml b/examples/autoinstall.yaml index 605c86ff..51aff9e2 100644 --- a/examples/autoinstall.yaml +++ b/examples/autoinstall.yaml @@ -41,7 +41,7 @@ snaps: - name: etcd channel: 3.2/stable updates: all -# timezone: Pacific/Guam +timezone: Pacific/Guam storage: config: - {type: disk, ptable: gpt, path: /dev/vdb, wipe: superblock, preserve: false, grub_device: true, id: disk-1} diff --git a/scripts/runtests.sh b/scripts/runtests.sh index 3e175ef4..4ee31e7b 100755 --- a/scripts/runtests.sh +++ b/scripts/runtests.sh @@ -55,8 +55,8 @@ python3 scripts/check-yaml-fields.py .subiquity/subiquity-curtin-install.conf \ storage.config[-1].options='"errors=remount-ro"' python3 scripts/check-yaml-fields.py <(python3 scripts/check-yaml-fields.py .subiquity/etc/cloud/cloud.cfg.d/99-installer.cfg datasource.None.userdata_raw) \ locale='"en_GB.UTF-8"' \ + timezone='"Pacific/Guam"' \ 'snap.commands=[snap install --channel=3.2/stable etcd]' - # timezone='"Pacific/Guam"' grep -q 'finish: subiquity/Install/install/postinstall/install_package1: SUCCESS: installing package1' \ .subiquity/subiquity-server-debug.log grep -q 'finish: subiquity/Install/install/postinstall/install_package2: SUCCESS: installing package2' \ diff --git a/subiquity/server/controllers/timezone.py b/subiquity/server/controllers/timezone.py index 5eb2cec5..64a0a611 100644 --- a/subiquity/server/controllers/timezone.py +++ b/subiquity/server/controllers/timezone.py @@ -31,8 +31,11 @@ def generate_possible_tzs(): return special_keys + real_tzs -def timedatectl_settz(tz): +def timedatectl_settz(app, tz): tzcmd = ['timedatectl', 'set-timezone', tz] + if app.opts.dry_run: + tzcmd = ['sleep', str(1/app.scale_factor)] + try: subprocess.run(tzcmd, universal_newlines=True) except subprocess.CalledProcessError as cpe: @@ -59,7 +62,7 @@ def timedatectl_gettz(): log.error('Failed to get live system timezone: %r', cpe) except IndexError: log.error('Failed to acquire system time zone') - log.debug('Failed to fine Time zone in timedatectl output') + log.debug('Failed to find Time zone in timedatectl output') return 'Etc/UTC' @@ -102,19 +105,20 @@ class TimeZoneController(SubiquityController): def set_system_timezone(self): if self.model.should_set_tz: - timedatectl_settz(self.model.timezone) + timedatectl_settz(self.app, self.model.timezone) async def GET(self) -> TimeZoneInfo: + # if someone POSTed before, return that if self.model.timezone: return TimeZoneInfo(self.model.timezone, self.model.got_from_geoip) - # a bare call to GET() is equivalent to autoinstall "timezone: geoip" - self.deserialize('geoip') - tz = self.model.timezone - if not tz: - tz = timedatectl_gettz() - return TimeZoneInfo(tz, self.model.got_from_geoip) + # GET requests geoip results + if self.app.geoip.timezone: + return TimeZoneInfo(self.app.geoip.timezone, True) + + # geoip wasn't ready for some reason, so ask the system + return TimeZoneInfo(timedatectl_gettz(), False) async def POST(self, tz: str): self.deserialize(tz) diff --git a/subiquity/tests/test_timezonecontroller.py b/subiquity/tests/test_timezonecontroller.py index fff2ba53..5fba2a47 100644 --- a/subiquity/tests/test_timezonecontroller.py +++ b/subiquity/tests/test_timezonecontroller.py @@ -81,7 +81,7 @@ class TestTimeZoneController(SubiTestCase): cloudconfig = {} if self.tzc.model.should_set_tz: cloudconfig = {'timezone': tz.timezone} - tdc_settz.assert_called_with(tz.timezone) + tdc_settz.assert_called_with(self.tzc.app, tz.timezone) self.assertEqual(cloudconfig, self.tzc.model.make_cloudconfig(), self.tzc.model) @@ -93,3 +93,17 @@ class TestTimeZoneController(SubiTestCase): for b in bads: with self.assertRaises(ValueError): self.tzc.deserialize(b) + + @mock.patch('subprocess.run') + @mock.patch('subiquity.server.controllers.timezone.timedatectl_gettz') + def test_set_tz_escape_dryrun(self, tdc_gettz, subprocess_run): + tdc_gettz.return_value = tz_utc + self.tzc.app.dry_run = True + self.tzc.deserialize('geoip') + self.assertEqual('sleep', subprocess_run.call_args.args[0][0]) + + @mock.patch('subiquity.server.controllers.timezone.timedatectl_settz') + def test_get_tz_should_not_set(self, tdc_settz): + run_coro(self.tzc.GET()) + self.assertFalse(self.tzc.model.should_set_tz) + tdc_settz.assert_not_called() diff --git a/subiquitycore/tests/mocks.py b/subiquitycore/tests/mocks.py index e27b0873..51572b33 100644 --- a/subiquitycore/tests/mocks.py +++ b/subiquitycore/tests/mocks.py @@ -39,4 +39,7 @@ def make_app(model=None): app.next_screen = mock.Mock() app.prev_screen = mock.Mock() app.hub = MessageHub() + app.opts = mock.Mock() + app.opts.dry_run = True + app.scale_factor = 1000 return app