ironic-inspector/ironic_inspector_ramdisk/test/test_main.py
Dmitry Tantsur d6404d2f99 Rename discoverd -> inspector
As agreed on the summit I'm renaming the python modules
and doing some adjustments:
* This is a breaking change, so version is bumped to 2.0.0
* Used this chance to split conf options over proper sections
* RELEASES.rst is gone; it's too hard to keep it up-to-date;
  anyway git does better job at doing history
* Dropped deprecated option ports_for_inactive_interfaces
* Dropped old /v1/discover endpoint and associated client call
* No longer set on_discovery and newly_discovered in Node.extra
  (deprecated since 1.0.0, superseded by the get status API)
* Default firewall chain name is "ironic-inspector" and
  is configurable

Notes:
* Some links will be updated after real move.
* Stable branches will probably use the old name.
* Some usage of discovery word is left in context of
  "discovered data"
* DIB element will probably be deprecated, so leaving it
  alone for now.
* Some usages of word "discovery" in the README will be updated
  later to make this patch a bit smaller
* Ramdisk code will be moved to IPA, so not touching it too much

Change-Id: I59f1f5bfb1248ab69973dab845aa028df493054e
2015-05-29 09:06:56 +02:00

137 lines
5.6 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import mock
import requests
from ironic_inspector_ramdisk import discover
from ironic_inspector_ramdisk import main
from ironic_inspector_ramdisk.test import test_discover
FAKE_ARGS = test_discover.get_fake_args()
class TestParseArgs(unittest.TestCase):
def test(self):
args = ['http://url']
parsed_args = main.parse_args(args)
self.assertEqual('http://url', parsed_args.callback_url)
def test_log_files(self):
args = ['-L', 'log1', '-L', 'log2', 'url']
parsed_args = main.parse_args(args)
self.assertEqual(['log1', 'log2'],
parsed_args.system_log_file)
@mock.patch.object(main, 'setup_logging', lambda args: None)
@mock.patch.object(main, 'parse_args', return_value=FAKE_ARGS,
autospec=True)
@mock.patch.object(discover, 'setup_ipmi_credentials', autospec=True)
@mock.patch.object(discover, 'call_inspector', autospec=True,
return_value={})
@mock.patch.object(discover, 'collect_logs', autospec=True)
@mock.patch.object(discover, 'discover_hardware', autospec=True)
class TestMain(unittest.TestCase):
def test_success(self, mock_discover, mock_logs, mock_callback,
mock_setup_ipmi, mock_parse):
mock_logs.return_value = 'LOG'
main.main()
# FIXME(dtantsur): mock does not copy arguments, so the 2nd argument
# actually is not what we expect ({})
mock_discover.assert_called_once_with(FAKE_ARGS, mock.ANY, mock.ANY)
mock_logs.assert_called_once_with(FAKE_ARGS)
mock_callback.assert_called_once_with(FAKE_ARGS, {'logs': 'LOG'},
mock.ANY)
self.assertFalse(mock_setup_ipmi.called)
def test_discover_fails(self, mock_discover, mock_logs, mock_callback,
mock_setup_ipmi, mock_parse):
mock_logs.return_value = 'LOG'
mock_discover.side_effect = RuntimeError('boom')
self.assertRaisesRegexp(SystemExit, '1', main.main)
mock_discover.assert_called_once_with(FAKE_ARGS, mock.ANY, mock.ANY)
mock_logs.assert_called_once_with(FAKE_ARGS)
mock_callback.assert_called_once_with(FAKE_ARGS, {'logs': 'LOG'},
mock.ANY)
failures = mock_callback.call_args[0][2]
self.assertIn('boom', failures.get_error())
def test_collect_logs_fails(self, mock_discover, mock_logs, mock_callback,
mock_setup_ipmi, mock_parse):
mock_logs.side_effect = RuntimeError('boom')
main.main()
mock_discover.assert_called_once_with(FAKE_ARGS, mock.ANY, mock.ANY)
mock_logs.assert_called_once_with(FAKE_ARGS)
mock_callback.assert_called_once_with(FAKE_ARGS, {}, mock.ANY)
def test_callback_fails(self, mock_discover, mock_logs, mock_callback,
mock_setup_ipmi, mock_parse):
mock_logs.return_value = 'LOG'
mock_callback.side_effect = requests.HTTPError('boom')
self.assertRaisesRegexp(SystemExit, '1', main.main)
mock_discover.assert_called_once_with(FAKE_ARGS, mock.ANY, mock.ANY)
mock_logs.assert_called_once_with(FAKE_ARGS)
mock_callback.assert_called_once_with(FAKE_ARGS, {'logs': 'LOG'},
mock.ANY)
def test_callback_fails2(self, mock_discover, mock_logs, mock_callback,
mock_setup_ipmi, mock_parse):
mock_logs.return_value = 'LOG'
mock_callback.side_effect = RuntimeError('boom')
self.assertRaisesRegexp(SystemExit, '1', main.main)
mock_discover.assert_called_once_with(FAKE_ARGS, mock.ANY, mock.ANY)
mock_logs.assert_called_once_with(FAKE_ARGS)
mock_callback.assert_called_once_with(FAKE_ARGS, {'logs': 'LOG'},
mock.ANY)
def test_setup_ipmi(self, mock_discover, mock_logs, mock_callback,
mock_setup_ipmi, mock_parse):
mock_logs.return_value = 'LOG'
mock_callback.return_value = {'ipmi_setup_credentials': True}
main.main()
mock_discover.assert_called_once_with(FAKE_ARGS, mock.ANY, mock.ANY)
mock_logs.assert_called_once_with(FAKE_ARGS)
mock_callback.assert_called_once_with(FAKE_ARGS, {'logs': 'LOG'},
mock.ANY)
mock_setup_ipmi.assert_called_once_with(mock_callback.return_value)
def test_setup_ipmi_fails(self, mock_discover, mock_logs, mock_callback,
mock_setup_ipmi, mock_parse):
mock_logs.return_value = 'LOG'
mock_callback.return_value = {'ipmi_setup_credentials': True}
mock_setup_ipmi.side_effect = RuntimeError('boom')
self.assertRaisesRegexp(SystemExit, '1', main.main)
mock_discover.assert_called_once_with(FAKE_ARGS, mock.ANY, mock.ANY)
mock_logs.assert_called_once_with(FAKE_ARGS)
mock_callback.assert_called_once_with(FAKE_ARGS, {'logs': 'LOG'},
mock.ANY)
mock_setup_ipmi.assert_called_once_with(mock_callback.return_value)