From 812c36823e085416afab7a35233cb8ff704d2e22 Mon Sep 17 00:00:00 2001 From: Chad Smith Date: Thu, 13 Jul 2017 10:45:32 -0600 Subject: 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. --- tests/unittests/test_datasource/test_gce.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'tests/unittests/test_datasource/test_gce.py') diff --git a/tests/unittests/test_datasource/test_gce.py b/tests/unittests/test_datasource/test_gce.py index 6fd1341d..3e8398b3 100644 --- a/tests/unittests/test_datasource/test_gce.py +++ b/tests/unittests/test_datasource/test_gce.py @@ -163,8 +163,9 @@ class TestDataSourceGCE(test_helpers.HttprettyTestCase): self.assertEqual(True, r) self.assertEqual('bar', self.ds.availability_zone) - def test_get_data_returns_false_if_not_on_gce(self): - self.m_platform_reports_gce.return_value = False + @mock.patch('cloudinit.sources.DataSourceGCE.platform_reports_gce') + def test_get_data_returns_false_if_not_on_gce(self, m_platform_gce): + m_platform_gce.return_value = False self.assertEqual(False, self.ds.get_data()) -- cgit v1.2.3 From 7b13279109d9eb3d1b102bb5ae8900c3d8423537 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 13 Jul 2017 12:59:16 -0400 Subject: tests: fix usage of mock in GCE test. The usage of mock in this test was simply invalid and only worked by happenstance. --- tests/unittests/test_datasource/test_gce.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'tests/unittests/test_datasource/test_gce.py') diff --git a/tests/unittests/test_datasource/test_gce.py b/tests/unittests/test_datasource/test_gce.py index 3e8398b3..ad608bec 100644 --- a/tests/unittests/test_datasource/test_gce.py +++ b/tests/unittests/test_datasource/test_gce.py @@ -72,11 +72,11 @@ class TestDataSourceGCE(test_helpers.HttprettyTestCase): self.ds = DataSourceGCE.DataSourceGCE( settings.CFG_BUILTIN, None, helpers.Paths({})) - self.m_platform_reports_gce = mock.patch( - 'cloudinit.sources.DataSourceGCE.platform_reports_gce', - return_value=True) - self.m_platform_reports_gce.start() - self.addCleanup(self.m_platform_reports_gce.stop) + ppatch = self.m_platform_reports_gce = mock.patch( + 'cloudinit.sources.DataSourceGCE.platform_reports_gce') + self.m_platform_reports_gce = ppatch.start() + self.m_platform_reports_gce.return_value = True + self.addCleanup(ppatch.stop) super(TestDataSourceGCE, self).setUp() def test_connection(self): @@ -163,10 +163,12 @@ class TestDataSourceGCE(test_helpers.HttprettyTestCase): self.assertEqual(True, r) self.assertEqual('bar', self.ds.availability_zone) - @mock.patch('cloudinit.sources.DataSourceGCE.platform_reports_gce') - def test_get_data_returns_false_if_not_on_gce(self, m_platform_gce): - m_platform_gce.return_value = False - self.assertEqual(False, self.ds.get_data()) + @mock.patch("cloudinit.sources.DataSourceGCE.GoogleMetadataFetcher") + def test_get_data_returns_false_if_not_on_gce(self, m_fetcher): + self.m_platform_reports_gce.return_value = False + ret = self.ds.get_data() + self.assertEqual(False, ret) + m_fetcher.assert_not_called() # vi: ts=4 expandtab -- cgit v1.2.3