class TestStatusReport(object):
    @patch('os.path.exists')
    @patch('azure_li_services.status_report.Path.create')
    def setup(self, mock_Path_create, mock_exists):
        mock_exists.return_value = False
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            self.report = StatusReport('some_service')
            file_handle = mock_open.return_value.__enter__.return_value
            mock_open.assert_called_once_with(
                '/var/lib/azure_li_services/some_service.report.yaml', 'w')
            assert file_handle.write.call_args_list == [
                call('some_service'),
                call(':'),
                call('\n'),
                call('  '),
                call('reboot'),
                call(':'),
                call(' '),
                call('false'),
                call('\n'),
                call('  '),
                call('success'),
                call(':'),
                call(' '),
                call('false'),
                call('\n')
            ]

    def test_set_success(self):
        with patch('builtins.open', create=True) as mock_open:
            self.report.set_success()
            mock_open.assert_called_once_with(
                '/var/lib/azure_li_services/some_service.report.yaml', 'w')
            assert self.report.status['some_service']['success'] is True

    def test_set_failed(self):
        with patch('builtins.open', create=True) as mock_open:
            self.report.set_failed()
            mock_open.assert_called_once_with(
                '/var/lib/azure_li_services/some_service.report.yaml', 'w')
            assert self.report.status['some_service']['success'] is False

    def test_set_reboot_required(self):
        with patch('builtins.open', create=True) as mock_open:
            self.report.set_reboot_required()
            mock_open.assert_called_once_with(
                '/var/lib/azure_li_services/some_service.report.yaml', 'w')
            assert self.report.status['some_service']['reboot'] is True

    def test_get_systemd_service(self):
        assert self.report.get_systemd_service() is None

    def test_load(self):
        self.report.status_file = '../data/some_service.report.yaml'
        self.report.load()
        assert self.report.get_state() is True
        assert self.report.get_reboot() is True
Ejemplo n.º 2
0
 def get_service_reports():
     from azure_li_services.status_report import StatusReport
     service_reports = []
     unit_to_service_map = {
         'config_lookup': 'azure-li-config-lookup',
         'user': '******',
         'install': 'azure-li-install',
         'network': 'azure-li-network',
         'call': 'azure-li-call',
         'machine_constraints': 'azure-li-machine-constraints',
         'system_setup': 'azure-li-system-setup',
         'storage': 'azure-li-storage'
     }
     unit_to_service_map_ordered = OrderedDict(
         sorted(unit_to_service_map.items()))
     for unit, service in unit_to_service_map_ordered.items():
         report = StatusReport(unit,
                               init_state=False,
                               systemd_service_name=service)
         report.load()
         service_reports.append(report)
     return service_reports