Merge ~chad.smith/cloud-init:gce-mock-test-leak into cloud-init:master

Proposed by Chad Smith on 2017-07-12
Status: Merged
Merge reported by: Chad Smith
Merged at revision: e0649d1ca2a8958829f56e24c4d8b08d6f16fd72
Proposed branch: ~chad.smith/cloud-init:gce-mock-test-leak
Merge into: cloud-init:master
Diff against target: 14 lines (+3/-2)
1 file modified
tests/unittests/test_datasource/test_gce.py (+3/-2)
Reviewer Review Type Date Requested Status
Scott Moser 2017-07-12 Approve on 2017-07-12
Server Team CI bot continuous-integration Approve on 2017-07-12
Review via email: mp+327325@code.launchpad.net

Commit Message

test_gce: Fix invalid mock of platform_reports_gce to return False

The mock of platform_reports_gce is created with a True return value in tests/unittests/test_datasource/test_gce.py:TestDataSourceGCE.setUp(). But, the final test_get_data_returns_false_if_not_on_gce incorrectly attempts to override the mocked return_value of True to False by setting self.m_platform_gce.return_value = False. But, since the mock is already initialized, the updated False is not honored. Instead we should use the patch decorator on the specific unit test to override the return_value of DataSourceGCE.platform_reports_gce to False.

A False from platform_reports_gce allows DataSourceGCE.get_data to immediately return False instead of trying to contact metadata.google.internal as the related bug references.

Description of the Change

test_gce: Fix invalid mock of platform_reports_gce to return False

The mock of platform_reports_gce is created with a True return value in tests/unittests/test_datasource/test_gce.py:TestDataSourceGCE.setUp(). But, the final test_get_data_returns_false_if_not_on_gce incorrectly attempts to override the mocked return_value of True to False by setting self.m_platform_gce.return_value = False. But, since the mock is already initialized, the updated False is not honored. Instead we should use the patch decorator on the specific unit test to override the return_value of DataSourceGCE.platform_reports_gce to False.

A False from platform_reports_gce allows DataSourceGCE.get_data to immediately return False instead of trying to contact metadata.google.internal as the related bug references.

LP:#1703935

To post a comment you must log in.

PASSED: Continuous integration, rev:e0649d1ca2a8958829f56e24c4d8b08d6f16fd72
https://jenkins.ubuntu.com/server/job/cloud-init-ci/37/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: CentOS 6 & 7: Build & Test
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/37/rebuild

review: Approve (continuous-integration)
Scott Moser (smoser) wrote :

I think i've used that general work flow other places. :-(

ie, initialize a mock in a setUp with one value and then assumed others can overrwrite the .return_value

the change looks fine here though.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/tests/unittests/test_datasource/test_gce.py b/tests/unittests/test_datasource/test_gce.py
2index 6fd1341..3e8398b 100644
3--- a/tests/unittests/test_datasource/test_gce.py
4+++ b/tests/unittests/test_datasource/test_gce.py
5@@ -163,8 +163,9 @@ class TestDataSourceGCE(test_helpers.HttprettyTestCase):
6 self.assertEqual(True, r)
7 self.assertEqual('bar', self.ds.availability_zone)
8
9- def test_get_data_returns_false_if_not_on_gce(self):
10- self.m_platform_reports_gce.return_value = False
11+ @mock.patch('cloudinit.sources.DataSourceGCE.platform_reports_gce')
12+ def test_get_data_returns_false_if_not_on_gce(self, m_platform_gce):
13+ m_platform_gce.return_value = False
14 self.assertEqual(False, self.ds.get_data())
15
16

Subscribers

People subscribed via source and target branches