Exemplo n.º 1
0
    def _test__get_forward_table(self, reallocation=False,
                                 insufficient_buffer=False,
                                 fail=False):
        if fail:
            e = self.assertRaises(exceptions.CloudInitError,
                                  self._check_raises_forward)
            msg = ('Unable to get IP forward table. Error: %s'
                   % mock.sentinel.error)
            self.assertEqual(msg, str(e))
        else:
            with self._network._get_forward_table() as table:
                pass
            pointer = self._ctypes_mock.POINTER(
                self._iphlpapi.Win32_MIB_IPFORWARDTABLE)
            expected_forward_table = self._ctypes_mock.cast(
                self._kernel32.HeapAlloc.return_value, pointer)
            self.assertEqual(expected_forward_table, table)

        heap_calls = [
            mock.call(self._kernel32.GetProcessHeap.return_value, 0,
                      self._ctypes_mock.cast.return_value)
        ]
        forward_calls = [
            mock.call(self._ctypes_mock.cast.return_value,
                      self._ctypes_mock.byref.return_value, 0),
        ]
        if insufficient_buffer:
            # We expect two calls for GetIpForwardTable
            forward_calls.append(forward_calls[0])
        if reallocation:
            heap_calls.append(heap_calls[0])
        self.assertEqual(heap_calls, self._kernel32.HeapFree.mock_calls)
        self.assertEqual(forward_calls,
                         self._iphlpapi.GetIpForwardTable.mock_calls)
Exemplo n.º 2
0
    def _test__get_forward_table(self,
                                 reallocation=False,
                                 insufficient_buffer=False,
                                 fail=False):
        if fail:
            e = self.assertRaises(exceptions.CloudInitError,
                                  self._check_raises_forward)
            msg = ('Unable to get IP forward table. Error: %s' %
                   mock.sentinel.error)
            self.assertEqual(msg, str(e))
        else:
            with self._network._get_forward_table() as table:
                pass
            pointer = self._ctypes_mock.POINTER(
                self._iphlpapi.Win32_MIB_IPFORWARDTABLE)
            expected_forward_table = self._ctypes_mock.cast(
                self._kernel32.HeapAlloc.return_value, pointer)
            self.assertEqual(expected_forward_table, table)

        heap_calls = [
            mock.call(self._kernel32.GetProcessHeap.return_value, 0,
                      self._ctypes_mock.cast.return_value)
        ]
        forward_calls = [
            mock.call(self._ctypes_mock.cast.return_value,
                      self._ctypes_mock.byref.return_value, 0),
        ]
        if insufficient_buffer:
            # We expect two calls for GetIpForwardTable
            forward_calls.append(forward_calls[0])
        if reallocation:
            heap_calls.append(heap_calls[0])
        self.assertEqual(heap_calls, self._kernel32.HeapFree.mock_calls)
        self.assertEqual(forward_calls,
                         self._iphlpapi.GetIpForwardTable.mock_calls)
Exemplo n.º 3
0
 def test_start_and_finish_success(self, report_start, report_finish):
     with reporting.ReportEventStack(name="myname", description="mydesc"):
         pass
     self.assertEqual([mock.call('myname', 'mydesc')],
                      report_start.call_args_list)
     self.assertEqual(
         [mock.call('myname', 'mydesc', reporting.status.SUCCESS)],
         report_finish.call_args_list)
Exemplo n.º 4
0
 def test_start_and_finish_success(self, report_start, report_finish):
     with reporting.ReportEventStack(name="myname", description="mydesc"):
         pass
     self.assertEqual(
         [mock.call('myname', 'mydesc')], report_start.call_args_list)
     self.assertEqual(
         [mock.call('myname', 'mydesc', reporting.status.SUCCESS)],
         report_finish.call_args_list)
Exemplo n.º 5
0
 def test_finish_exception_defaults_fail(self, report_start, report_finish):
     name = "myname"
     desc = "mydesc"
     try:
         with reporting.ReportEventStack(name, description=desc):
             raise ValueError("This didnt work")
     except ValueError:
         pass
     self.assertEqual([mock.call(name, desc)], report_start.call_args_list)
     self.assertEqual([mock.call(name, desc, reporting.status.FAIL)],
                      report_finish.call_args_list)
Exemplo n.º 6
0
 def test_finish_exception_defaults_fail(self, report_start, report_finish):
     name = "myname"
     desc = "mydesc"
     try:
         with reporting.ReportEventStack(name, description=desc):
             raise ValueError("This didnt work")
     except ValueError:
         pass
     self.assertEqual([mock.call(name, desc)], report_start.call_args_list)
     self.assertEqual(
         [mock.call(name, desc, reporting.status.FAIL)],
         report_finish.call_args_list)
Exemplo n.º 7
0
 def test_result_on_exception_used(self, report_start, report_finish):
     name = "myname"
     desc = "mydesc"
     try:
         with reporting.ReportEventStack(
                 name, desc, result_on_exception=reporting.status.WARN):
             raise ValueError("This didnt work")
     except ValueError:
         pass
     self.assertEqual([mock.call(name, desc)], report_start.call_args_list)
     self.assertEqual([mock.call(name, desc, reporting.status.WARN)],
                      report_finish.call_args_list)
Exemplo n.º 8
0
 def test_result_on_exception_used(self, report_start, report_finish):
     name = "myname"
     desc = "mydesc"
     try:
         with reporting.ReportEventStack(
                 name, desc, result_on_exception=reporting.status.WARN):
             raise ValueError("This didnt work")
     except ValueError:
         pass
     self.assertEqual([mock.call(name, desc)], report_start.call_args_list)
     self.assertEqual(
         [mock.call(name, desc, reporting.status.WARN)],
         report_finish.call_args_list)
Exemplo n.º 9
0
 def test_message_used_in_finish(self, report_finish):
     with reporting.ReportEventStack("myname", "mydesc",
                                     message="mymessage"):
         pass
     self.assertEqual(
         [mock.call("myname", "mymessage", reporting.status.SUCCESS)],
         report_finish.call_args_list)
Exemplo n.º 10
0
    def test__get_forward_table_insufficient_buffer_no_memory(self):
        self._kernel32.HeapAlloc.side_effect = (mock.sentinel.table_mem, None)
        self._iphlpapi.GetIpForwardTable.return_value = (
            self._iphlpapi.ERROR_INSUFFICIENT_BUFFER)

        self.assertRaises(exceptions.CloudInitError,
                          self._check_raises_forward)

        table = self._ctypes_mock.cast.return_value
        self._iphlpapi.GetIpForwardTable.assert_called_once_with(
            table, self._ctypes_mock.byref.return_value, 0)
        heap_calls = [
            mock.call(self._kernel32.GetProcessHeap.return_value, 0, table),
            mock.call(self._kernel32.GetProcessHeap.return_value, 0, table)
        ]
        self.assertEqual(heap_calls, self._kernel32.HeapFree.mock_calls)
Exemplo n.º 11
0
    def test__get_forward_table_insufficient_buffer_no_memory(self):
        self._kernel32.HeapAlloc.side_effect = (mock.sentinel.table_mem, None)
        self._iphlpapi.GetIpForwardTable.return_value = (
            self._iphlpapi.ERROR_INSUFFICIENT_BUFFER)

        self.assertRaises(exceptions.CloudInitError,
                          self._check_raises_forward)

        table = self._ctypes_mock.cast.return_value
        self._iphlpapi.GetIpForwardTable.assert_called_once_with(
            table,
            self._ctypes_mock.byref.return_value, 0)
        heap_calls = [
            mock.call(self._kernel32.GetProcessHeap.return_value, 0, table),
            mock.call(self._kernel32.GetProcessHeap.return_value, 0, table)
        ]
        self.assertEqual(heap_calls, self._kernel32.HeapFree.mock_calls)
Exemplo n.º 12
0
 def test_appropriate_logger_used(self, getLogger):
     event_type, event_name = 'test_type', 'test_name'
     event = reporting.ReportingEvent(event_type, event_name, 'description')
     reporting.LogHandler().publish_event(event)
     self.assertEqual(
         [mock.call(
             'cloudinit.reporting.{0}.{1}'.format(event_type, event_name))],
         getLogger.call_args_list)
Exemplo n.º 13
0
 def test_message_used_in_finish(self, report_finish):
     with reporting.ReportEventStack("myname",
                                     "mydesc",
                                     message="mymessage"):
         pass
     self.assertEqual(
         [mock.call("myname", "mymessage", reporting.status.SUCCESS)],
         report_finish.call_args_list)
Exemplo n.º 14
0
 def test_uses_non_type_parts_of_config_dict_as_kwargs(
         self, available_handlers):
     handler_type_name = 'test_handler'
     handler_cls = mock.Mock()
     available_handlers.registered_items = {handler_type_name: handler_cls}
     extra_kwargs = {'foo': 'bar', 'bar': 'baz'}
     handler_config = extra_kwargs.copy()
     handler_config.update({'type': handler_type_name})
     handler_name = 'my_test_handler'
     reporting.update_configuration({handler_name: handler_config})
     self.assertEqual(
         handler_cls.return_value, reporting.instantiated_handler_registry.
         registered_items[handler_name])
     self.assertEqual([mock.call(**extra_kwargs)],
                      handler_cls.call_args_list)
Exemplo n.º 15
0
 def test_uses_non_type_parts_of_config_dict_as_kwargs(
         self, available_handlers):
     handler_type_name = 'test_handler'
     handler_cls = mock.Mock()
     available_handlers.registered_items = {handler_type_name: handler_cls}
     extra_kwargs = {'foo': 'bar', 'bar': 'baz'}
     handler_config = extra_kwargs.copy()
     handler_config.update({'type': handler_type_name})
     handler_name = 'my_test_handler'
     reporting.add_configuration({handler_name: handler_config})
     self.assertEqual(
         handler_cls.return_value,
         reporting.instantiated_handler_registry.registered_items[
             handler_name])
     self.assertEqual([mock.call(**extra_kwargs)],
                      handler_cls.call_args_list)
Exemplo n.º 16
0
 def test_message_updatable(self, report_finish):
     with reporting.ReportEventStack("myname", "mydesc") as c:
         c.message = "all good"
     self.assertEqual(
         [mock.call("myname", "all good", reporting.status.SUCCESS)],
         report_finish.call_args_list)
Exemplo n.º 17
0
 def test_message_updatable(self, report_finish):
     with reporting.ReportEventStack("myname", "mydesc") as c:
         c.message = "all good"
     self.assertEqual(
         [mock.call("myname", "all good", reporting.status.SUCCESS)],
         report_finish.call_args_list)