Skip to content

Commit

Permalink
Optimize/fix dispatcher tests to work on CLI (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
gblewis1 authored Nov 14, 2023
1 parent 20c1738 commit 93be742
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 148 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea/*
**/*.pyc
dist/
**/.vscode
**/*.swp
18 changes: 16 additions & 2 deletions inbm/dispatcher-agent/tests/unit/aota/test_aota.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@


class TestAOTA(TestCase):
@classmethod
def setUpClass(cls):
# Patch is started and will apply to all tests in this class
cls.mock_detect_os = patch('dispatcher.aota.factory.detect_os').start()
cls.mock_detect_os.return_value = 'Ubuntu'

cls.mock_verify = patch('dispatcher.aota.checker.verify_source').start()

@classmethod
def tearDownClass(cls):
# Patch is stopped, no longer in effect after all tests in this class
patch.stopall() # This will stop all active patches

@patch('os.rmdir')
@patch('dispatcher.aota.aota_command.get', return_value=Result(400, "Unable to download application package."))
Expand Down Expand Up @@ -597,24 +609,26 @@ def test_application_centos_driver_update_raise_error_not_in_container(self, che
uri="http://example.com", device_reboot="Yes")
self.assertRaises(AotaError, aota.run)

@patch('dispatcher.aota.application_command.CentOsApplication.cleanup')
@patch('dispatcher.aota.aota_command.get', return_value=Result(200, "OK"))
@patch('dispatcher.aota.checker.verify_source')
@patch('dispatcher.aota.application_command.AotaCommand.create_repository_cache_repo')
@patch('dispatcher.aota.factory.is_inside_container', return_value=True)
@patch('dispatcher.aota.factory.detect_os', return_value='CentOS')
def test_application_centos_driver_update_raise_error_if_inb_driver_folder_not_found(self, detect_os,
is_inside_container, create_repo, verify_source, get):
is_inside_container, create_repo, verify_source, get, mock_cleanup):
aota = self._build_aota(cmd='update', app_type='application',
uri="http://example.com", device_reboot="Yes")
self.assertRaises(AotaError, aota.run)

@patch('dispatcher.aota.application_command.CentOsApplication.cleanup')
@patch('dispatcher.aota.checker.check_resource')
@patch('dispatcher.aota.checker.verify_source')
@patch('dispatcher.aota.application_command.get', return_value=Result(200, "ok"))
@patch('inbm_common_lib.shell_runner.PseudoShellRunner.run', return_value=("", "", 0))
@patch('dispatcher.aota.factory.is_inside_container', return_value=True)
@patch('dispatcher.aota.factory.detect_os', return_value='CentOS')
def test_application_centos_driver_update_raise_error_if_file_is_not_rpm_type(self, detect_os, is_inside_container, run, get, mock_verify, mock_resource):
def test_application_centos_driver_update_raise_error_if_file_is_not_rpm_type(self, detect_os, is_inside_container, run, get, mock_verify, mock_resource, mock_cleanup):
aota = self._build_aota(cmd='update', app_type='application',
uri="https://example.com/sample/sample.deb")
with self.assertRaisesRegex(AotaError, "Invalid file type"):
Expand Down
8 changes: 4 additions & 4 deletions inbm/dispatcher-agent/tests/unit/sota/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,16 @@ def test_ubuntu_delete_snap(self, order, rc, err, mock_del_snap):

@unpack
@data((1, "0", ""), (2, "1", "err"))
@patch('dispatcher.sota.snapshot.dispatcher_state', autospec=True)
@patch("inbm_lib.trtl.Trtl.single_snapshot", return_value=('', 'ERROR'))
@patch("pickle.dump")
def test_ubuntu_snapshot(self, order, rc, err, mock_pickle_dump, mock_trtl_single_snapshot):
def test_ubuntu_snapshot(self, order, rc, err, mock_trtl_single_snapshot, mock_state):
factory = SotaOsFactory(
MockDispatcherCallbacks.build_mock_dispatcher_callbacks(), None).get_os('Ubuntu')
snapshot = factory.create_snapshotter("update", '1', False)
with patch('builtins.open', new_callable=mock_open()) as m:
if order == 1:
mock_trtl_single_snapshot.return_value = (rc, err)
snapshot.take_snapshot()
mock_pickle_dump.assert_called_once()
else:
self.assertRaises(SotaError, snapshot.take_snapshot)

Expand Down Expand Up @@ -80,7 +79,8 @@ def test_Ubuntu_snapshot_raises2(self, rc, err, mock_trtl_single_snapshot):

class TestUbuntuSnapshot(unittest.TestCase):

def test_take_snapshot_proceed_fail_publishes_error_succeeds(self):
@patch('dispatcher.sota.snapshot.dispatcher_state', autospec=True)
def test_take_snapshot_proceed_fail_publishes_error_succeeds(self, mock_state):
dispatcher_callbacks = Mock()
dispatcher_callbacks.broker_core = Mock()
trtl = Mock()
Expand Down
2 changes: 1 addition & 1 deletion inbm/dispatcher-agent/tests/unit/sota/test_sota.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def _build_mock_repo(num_files=0):
mem_repo.add("test" + str(i + 1) + ".rpm", b"0123456789")
return mem_repo

@patch("inbm_lib.detect_os.detect_os")
@patch("dispatcher.sota.sota.detect_os")
def test_check_do_not_raise_exception(self, mock_detect_os):
parsed_manifest = {'release_date': "1970-01-01"}
mock_detect_os.return_value = 'Ubuntu'
Expand Down
Loading

0 comments on commit 93be742

Please sign in to comment.