Skip to content

Commit

Permalink
Merge "Use proper 'open' mocking in unit tests"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Jan 17, 2015
2 parents dabfdce + 3abb64c commit f9651c2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 48 deletions.
6 changes: 2 additions & 4 deletions tests/unit/benchmark/scenarios/vm/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@ def setUp(self):
".bench_utils.wait_for")
self.useFixture(self.wait_for)

@mock.patch('__builtin__.open')
@mock.patch('__builtin__.open', side_effect=mock.mock_open(), create=True)
def test_run_action(self, mock_open):
mock_ssh = mock.MagicMock()
mock_file_handle = mock.MagicMock()
mock_open.return_value = mock_file_handle
vm_scenario = utils.VMScenario()
vm_scenario.run_action(mock_ssh, 'interpreter', 'script')
mock_ssh.execute.assert_called_once_with('interpreter',
stdin=mock_file_handle)
stdin=mock_open.side_effect())

def test_wait_for_ssh(self):
ssh = mock.MagicMock()
Expand Down
29 changes: 10 additions & 19 deletions tests/unit/cmd/commands/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ def test_invalid_results(self, mock_get):
return_value=None)
@mock.patch("rally.cmd.commands.task.os.path.realpath",
side_effect=lambda p: "realpath_%s" % p)
@mock.patch("rally.cmd.commands.task.open", create=True)
@mock.patch("rally.cmd.commands.task.open",
side_effect=mock.mock_open(), create=True)
@mock.patch("rally.cmd.commands.task.plot")
@mock.patch("rally.cmd.commands.task.webbrowser")
@mock.patch("rally.cmd.commands.task.objects.Task.get")
Expand All @@ -340,9 +341,6 @@ def test_report_one_uuid(self, mock_get, mock_web, mock_plot, mock_open,
mock_results = mock.Mock(return_value=data)
mock_get.return_value = mock.Mock(get_results=mock_results)
mock_plot.plot.return_value = "html_report"
mock_write = mock.Mock()
mock_open.return_value.__enter__.return_value = (
mock.Mock(write=mock_write))

def reset_mocks():
for m in mock_get, mock_web, mock_plot, mock_open:
Expand All @@ -351,7 +349,7 @@ def reset_mocks():
mock_open.assert_called_once_with("/tmp/%s.html" % task_id, "w+")
mock_plot.plot.assert_called_once_with(results)

mock_write.assert_called_once_with("html_report")
mock_open.side_effect().write.assert_called_once_with("html_report")
mock_get.assert_called_once_with(task_id)

reset_mocks()
Expand All @@ -363,7 +361,8 @@ def reset_mocks():
return_value=None)
@mock.patch("rally.cmd.commands.task.os.path.realpath",
side_effect=lambda p: "realpath_%s" % p)
@mock.patch("rally.cmd.commands.task.open", create=True)
@mock.patch("rally.cmd.commands.task.open",
side_effect=mock.mock_open(), create=True)
@mock.patch("rally.cmd.commands.task.plot")
@mock.patch("rally.cmd.commands.task.webbrowser")
@mock.patch("rally.cmd.commands.task.objects.Task.get")
Expand Down Expand Up @@ -394,9 +393,6 @@ def test_report_bunch_uuids(self, mock_get, mock_web, mock_plot, mock_open,
mock_results = mock.Mock(return_value=data)
mock_get.return_value = mock.Mock(get_results=mock_results)
mock_plot.plot.return_value = "html_report"
mock_write = mock.Mock()
mock_open.return_value.__enter__.return_value = (
mock.Mock(write=mock_write))

def reset_mocks():
for m in mock_get, mock_web, mock_plot, mock_open:
Expand All @@ -405,7 +401,7 @@ def reset_mocks():
mock_open.assert_called_once_with("/tmp/1_test.html", "w+")
mock_plot.plot.assert_called_once_with(results)

mock_write.assert_called_once_with("html_report")
mock_open.side_effect().write.assert_called_once_with("html_report")
expected_get_calls = [mock.call(task) for task in tasks]
mock_get.assert_has_calls(expected_get_calls, any_order=True)

Expand Down Expand Up @@ -439,11 +435,9 @@ def test_report_one_file(self, mock_plot, mock_open, mock_os,
data)

mock_plot.plot.return_value = "html_report"
mock_write = mock.Mock()
mock_read = mock.MagicMock(return_value=results)
mock_open.side_effect = mock.mock_open(read_data=results)

mock_json_load.return_value = results
mock_open.return_value.__enter__.return_value = (
mock.Mock(write=mock_write, read=mock_read))

def reset_mocks():
for m in mock_plot, mock_open, mock_json_load, mock_validate:
Expand All @@ -454,7 +448,7 @@ def reset_mocks():
mock_open.assert_has_calls(expected_open_calls, any_order=True)
mock_plot.plot.assert_called_once_with(results)

mock_write.assert_called_once_with("html_report")
mock_open.side_effect().write.assert_called_once_with("html_report")

@mock.patch("rally.cmd.commands.task.os.path.exists", return_value=True)
@mock.patch("rally.cmd.commands.task.json.load")
Expand All @@ -468,11 +462,8 @@ def test_report_exceptions(self, mock_open, mock_json_load,
"load_duration": 0.1,
"full_duration": 1.2}}]

mock_write = mock.Mock()
mock_read = mock.MagicMock(return_value=results)
mock_open.side_effect = mock.mock_open(read_data=results)
mock_json_load.return_value = results
mock_open.return_value.__enter__.return_value = (
mock.Mock(write=mock_write, read=mock_read))

ret = self.task.report(tasks="/tmp/task.json",
out="/tmp/tmp.hsml")
Expand Down
34 changes: 17 additions & 17 deletions tests/unit/cmd/commands/test_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,29 +158,30 @@ def test_results_verification_not_found(self, mock_db_result_get):

mock_db_result_get.assert_called_once_with(verification_uuid)

@mock.patch("rally.cmd.commands.verify.open", create=True)
@mock.patch("rally.cmd.commands.verify.open",
side_effect=mock.mock_open(), create=True)
@mock.patch("rally.db.verification_result_get", return_value={"data": {}})
def test_results_with_output_json_and_output_file(self,
mock_db_result_get,
mock_open):
mock_open.return_value = mock.MagicMock()
mock_open.side_effect = mock.mock_open()
verification_uuid = "94615cd4-ff45-4123-86bd-4b0741541d09"
self.verify.results(verification_uuid, output_file="results",
output_html=False, output_json=True)

mock_db_result_get.assert_called_once_with(verification_uuid)
mock_open.assert_called_once_with("results", "wb")
fake_file = mock_open.return_value.__enter__.return_value
fake_file.write.assert_called_once_with("{}")
mock_open.side_effect().write.assert_called_once_with("{}")

@mock.patch("rally.cmd.commands.verify.open", create=True)
@mock.patch("rally.cmd.commands.verify.open",
side_effect=mock.mock_open(), create=True)
@mock.patch("rally.db.verification_result_get")
@mock.patch("rally.verification.tempest.json2html.HtmlOutput")
def test_results_with_output_html_and_output_file(self,
mock_html,
mock_db_result_get,
mock_open):
mock_open.return_value = mock.MagicMock()

verification_uuid = "7140dd59-3a7b-41fd-a3ef-5e3e615d7dfa"
fake_data = {}
results = {"data": fake_data}
Expand All @@ -193,8 +194,7 @@ def test_results_with_output_html_and_output_file(self,
mock_db_result_get.assert_called_once_with(verification_uuid)
mock_html.assert_called_once_with(fake_data)
mock_open.assert_called_once_with("results", "wb")
fake_file = mock_open.return_value.__enter__.return_value
fake_file.write.assert_called_once_with("html_report")
mock_open.side_effect().write.assert_called_once_with("html_report")

@mock.patch("rally.db.verification_result_get",
return_value={"data": {"test_cases": {}}})
Expand Down Expand Up @@ -224,7 +224,8 @@ def test_compare_verification_not_found(self, mock_db_result_get):

mock_db_result_get.assert_called_once_with(uuid1)

@mock.patch("rally.cmd.commands.verify.open", create=True)
@mock.patch("rally.cmd.commands.verify.open",
side_effect=mock.mock_open(), create=True)
@mock.patch("rally.db.verification_result_get",
return_value={"data": {"test_cases": {}}})
def test_compare_with_output_csv_and_output_file(self,
Expand All @@ -242,10 +243,10 @@ def test_compare_with_output_csv_and_output_file(self,
mock.call(uuid2)]
mock_db_result_get.assert_has_calls(calls, True)
mock_open.assert_called_once_with("results", "wb")
fake_file = mock_open.return_value.__enter__.return_value
fake_file.write.assert_called_once_with(fake_string)
mock_open.side_effect().write.assert_called_once_with(fake_string)

@mock.patch("rally.cmd.commands.verify.open", create=True)
@mock.patch("rally.cmd.commands.verify.open",
side_effect=mock.mock_open(), create=True)
@mock.patch("rally.db.verification_result_get",
return_value={"data": {"test_cases": {}}})
def test_compare_with_output_json_and_output_file(self,
Expand All @@ -262,10 +263,10 @@ def test_compare_with_output_json_and_output_file(self,
mock.call(uuid2)]
mock_db_result_get.assert_has_calls(calls, True)
mock_open.assert_called_once_with("results", "wb")
fake_file = mock_open.return_value.__enter__.return_value
fake_file.write.assert_called_once_with(fake_json_string)
mock_open.side_effect().write.assert_called_once_with(fake_json_string)

@mock.patch("rally.cmd.commands.verify.open", create=True)
@mock.patch("rally.cmd.commands.verify.open",
side_effect=mock.mock_open(), create=True)
@mock.patch("rally.db.verification_result_get")
@mock.patch(("rally.verification.tempest."
"compare2html.create_report"), return_value="")
Expand All @@ -289,5 +290,4 @@ def test_compare_with_output_html_and_output_file(self,
mock_compare2html_create.assert_called_once_with(fake_data)

mock_open.assert_called_once_with("results", "wb")
fake_file = mock_open.return_value.__enter__.return_value
fake_file.write.assert_called_once_with("")
mock_open.side_effect().write.assert_called_once_with("")
13 changes: 5 additions & 8 deletions tests/unit/verification/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ def _remove_default_section(self, items):

@mock.patch("rally.verification.tempest.config.requests")
@mock.patch("rally.verification.tempest.config.os.rename")
@mock.patch("six.moves.builtins.open")
@mock.patch("six.moves.builtins.open", side_effect=mock.mock_open(),
create=True)
def test__load_img_success(self, mock_open, mock_rename, mock_requests):
mock_result = mock.MagicMock()
mock_result.status_code = 200
mock_requests.get.return_value = mock_result
mock_file = mock.MagicMock()
mock_open.return_value = mock_file
self.conf_generator._load_img()
cirros_url = ("http://download.cirros-cloud.net/%s/%s" %
(CONF.image.cirros_version,
Expand Down Expand Up @@ -298,16 +297,14 @@ def test__set_service_available_horizon(self, mock_requests):
self.assertEqual(self.conf_generator.conf.get(
"service_available", "horizon"), "True")

@mock.patch('six.moves.builtins.open')
@mock.patch('six.moves.builtins.open', side_effect=mock.mock_open(),
create=True)
def test_write_config(self, mock_open):
self.conf_generator.conf = mock.Mock()
mock_file = mock.MagicMock()
mock_open.return_value = mock_file
file_name = '/path/to/fake/conf'

self.conf_generator.write_config(file_name)

mock_open.assert_called_once_with(file_name, 'w+')
self.conf_generator.conf.write.assert_called_once_with(
mock_file.__enter__())
mock_file.__exit__.assert_called_once_with(None, None, None)
mock_open.side_effect())

0 comments on commit f9651c2

Please sign in to comment.