feat: add is_symlink() and set_timezone() util functions

This commit is contained in:
Lance Edgar 2024-11-20 20:52:29 -06:00
parent 4dede6072c
commit 26774bbcaf
2 changed files with 72 additions and 1 deletions

View file

@ -2,7 +2,7 @@
import os
from unittest import TestCase
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch, call
from wuttamess import util as mod
@ -15,6 +15,21 @@ class TestExists(TestCase):
c.run.assert_called_once_with('test -e /foo', warn=True)
class TestIsSymlink(TestCase):
def test_yes(self):
c = MagicMock()
c.run.return_value.failed = False
self.assertTrue(mod.is_symlink(c, '/foo'))
c.run.assert_called_once_with('test -L "$(echo /foo)"', warn=True)
def test_no(self):
c = MagicMock()
c.run.return_value.failed = True
self.assertFalse(mod.is_symlink(c, '/foo'))
c.run.assert_called_once_with('test -L "$(echo /foo)"', warn=True)
class TestMakoRenderer(TestCase):
def test_basic(self):
@ -24,3 +39,26 @@ class TestMakoRenderer(TestCase):
path = os.path.join(here, 'files', 'bar', 'baz')
rendered = renderer(path, vars={})
self.assertEqual(rendered, 'machine_is_live = True')
class TestSetTimezone(TestCase):
def test_symlink(self):
c = MagicMock()
with patch.object(mod, 'is_symlink') as is_symlink:
is_symlink.return_value = True
mod.set_timezone(c, 'America/Chicago')
c.run.assert_has_calls([
call("bash -c 'echo America/Chicago > /etc/timezone'"),
])
c.run.assert_called_with('ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime')
def test_not_symlink(self):
c = MagicMock()
with patch.object(mod, 'is_symlink') as is_symlink:
is_symlink.return_value = False
mod.set_timezone(c, 'America/Chicago')
c.run.assert_has_calls([
call("bash -c 'echo America/Chicago > /etc/timezone'"),
])
c.run.assert_called_with('cp /usr/share/zoneinfo/America/Chicago /etc/localtime')