From 18d618f22d3693210e89b473277a80c218e06ec4 Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: Feb 23 2018 00:53:28 +0000 Subject: Tests: move a couple more mocks into the classes Since we actually use these mocks, it's cleaner to do them like this. Signed-off-by: Adam Williamson --- diff --git a/test_relvalconsumer.py b/test_relvalconsumer.py index 563bcb6..a806a4b 100644 --- a/test_relvalconsumer.py +++ b/test_relvalconsumer.py @@ -104,14 +104,6 @@ mock.patch('wikitcms.wiki.Wiki.login', autospec=True).start() # so does site init mock.patch('mwclient.client.Site.site_init', autospec=True).start() -# this we need to switch at Branch point, so name it. It hits the -# 'collections' API. -fakecurrpatcher = mock.patch('fedfind.helpers.get_current_release', _fakegetcurr1) -fakecurrpatcher.start() - -# This one we actually need to check sometimes, so name it -fakesubproc = mock.patch('subprocess.call', autospec=True, return_value=0).start() - # proper consumer init requires a fedmsg hub instance, we don't have # one and don't want to faff around faking one. CONSUMER = None @@ -142,13 +134,17 @@ MODBETA1 = 'Fedora-Modular-27-20171108.2' MODBRANCHED4 = 'Fedora-Modular-27-20171125.n.0' MODFINAL1 = 'Fedora-Modular-27-20171126.1' +# we don't really want to run size_check +@mock.patch('subprocess.call', autospec=True, return_value=0) +# get_current_release requires a network trip +@mock.patch('fedfind.helpers.get_current_release', _fakegetcurr1) # obviously we don't really want to create the event... @mock.patch('wikitcms.event.ValidationEvent.create', autospec=True) # ditto sending emails. @mock.patch('smtplib.SMTP', autospec=True) -class TestRelvalConsumer: - """All the tests are here. In a class so we can share mock patch - decorators. +class TestRelvalConsumerPreBranch: + """All the pre-branch tests that use the first fake_get_current + mock are here. In a class so we can share mock patch decorators. """ # this obviously hits mirrors; we have a couple of functions for faking # 'same' and 'different' package sets, for different tests @@ -161,7 +157,7 @@ class TestRelvalConsumer: # this always needs mocking, as whatever the 'current' event ought # to be in our fake release series. @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.ComposeEvent(None, '24', 'Final', '1.4')) - def test_rawhide1(self, fakesmtp, fakecreate): + def test_rawhide1(self, fakesmtp, fakecreate, fakesubproc): """Creating first Rawhide nightly for next release.""" CONSUMER.consume(_fakemsg(RAWHIDE1)) assert fakecreate.call_args[1]['check'] is True @@ -172,7 +168,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetdifferent) @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.NightlyEvent(None, '25', 'Rawhide', '20160601.n.0')) - def test_rawhide2(self, fakesmtp, fakecreate): + def test_rawhide2(self, fakesmtp, fakecreate, fakesubproc): """Another Rawhide nightly one day later should not produce an event, even if there are package differences. """ @@ -183,7 +179,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetsame) @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.NightlyEvent(None, '25', 'Rawhide', '20160601.n.0')) - def test_rawhide3_same(self, fakesmtp, fakecreate): + def test_rawhide3_same(self, fakesmtp, fakecreate, fakesubproc): """Another Rawhide nightly four days later should not produce an event if the packages are the same... """ @@ -193,7 +189,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetdifferent) @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.NightlyEvent(None, '25', 'Rawhide', '20160601.n.0')) - def test_rawhide3_different(self, fakesmtp, fakecreate): + def test_rawhide3_different(self, fakesmtp, fakecreate, fakesubproc): """...but the same four days later nightly should produce an event if the packages are different. """ @@ -202,23 +198,29 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetsame) @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.NightlyEvent(None, '25', 'Rawhide', '20160605.n.0')) - def test_rawhide4(self, fakesmtp, fakecreate): + def test_rawhide4(self, fakesmtp, fakecreate, fakesubproc): """Another Rawhide nightly 15 days later should produce an event even if the packages are the same. """ CONSUMER.consume(_fakemsg(RAWHIDE4)) assert fakecreate.call_args[1]['check'] is True - # BRANCH POINT +@mock.patch('subprocess.call', autospec=True, return_value=0) +@mock.patch('fedfind.helpers.get_current_release', _fakegetcurr2) +@mock.patch('wikitcms.event.ValidationEvent.create', autospec=True) +@mock.patch('smtplib.SMTP', autospec=True) +# BRANCH POINT, switch to the other get_current_release mock +class TestRelvalConsumerPostBranch: + """All the post-branch tests that use the other fake_get_current + mock are here. In a class so we can share mock patch decorators. + """ @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetsame) @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.NightlyEvent(None, '25', 'Rawhide', '20160620.n.0')) - def test_branched1(self, fakesmtp, fakecreate): + def test_branched1(self, fakesmtp, fakecreate, fakesubproc): """First Branched nightly shouldn't produce an event as it's only one day after the Rawhide nightly. """ - # switch the get_current_release mock to Branched mode - fakecurrpatcher.stop() mock.patch('fedfind.helpers.get_current_release', _fakegetcurr2).start() CONSUMER.consume(_fakemsg(BRANCHED1)) assert fakecreate.call_count == 0 @@ -226,7 +228,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetdifferent) @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.NightlyEvent(None, '25', 'Rawhide', '20160620.n.0')) - def test_branched2(self, fakesmtp, fakecreate): + def test_branched2(self, fakesmtp, fakecreate, fakesubproc): """Second branched compose some days later should create an event. """ CONSUMER.consume(_fakemsg(BRANCHED2)) @@ -242,7 +244,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Production.cid', ALPHA1) @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetsame) @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.NightlyEvent(None, '25', 'Branched', '20160629.n.0')) - def test_alpha1(self, fakesmtp, fakecreate): + def test_alpha1(self, fakesmtp, fakecreate, fakesubproc): """First Alpha candidate should produce an event even though it's for the same date and package set as BRANCHED2. """ @@ -258,7 +260,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetsame) @mock.patch('wikitcms.event.ComposeEvent.creation_date', '20160629') @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.ComposeEvent(None, '25', 'Alpha', '1.1')) - def test_alpha2(self, fakecompexists, fakesmtp, fakecreate): + def test_alpha2(self, fakecompexists, fakesmtp, fakecreate, fakesubproc): """Second Alpha candidate should produce an event even though it's for the same date and package set as ALPHA1 (somewhat unrealistic, but it's what ought to happen). @@ -270,7 +272,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetdifferent) @mock.patch('wikitcms.event.ComposeEvent.creation_date', '20160629') @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.ComposeEvent(None, '25', 'Alpha', '1.2')) - def test_branched3(self, fakecompexists, fakesmtp, fakecreate): + def test_branched3(self, fakecompexists, fakesmtp, fakecreate, fakesubproc): """Branched nightly shortly after candidate compose should not create event even if package set is different. """ @@ -282,7 +284,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetsame) @mock.patch('wikitcms.event.ComposeEvent.creation_date', '20160629') @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.ComposeEvent(None, '25', 'Alpha', '1.2')) - def test_branched4_same(self, fakecompexists, fakesmtp, fakecreate): + def test_branched4_same(self, fakecompexists, fakesmtp, fakecreate, fakesubproc): """Branched nightly a few days later with same package set should not create event... """ @@ -294,7 +296,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetolder) @mock.patch('wikitcms.event.ComposeEvent.creation_date', '20160629') @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.ComposeEvent(None, '25', 'Alpha', '1.2')) - def test_branched4_older(self, fakecompexists, fakesmtp, fakecreate): + def test_branched4_older(self, fakecompexists, fakesmtp, fakecreate, fakesubproc): """...nor should same few-days-later Branched nightly with *older* packages... """ @@ -306,7 +308,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetdifferent) @mock.patch('wikitcms.event.ComposeEvent.creation_date', '20160629') @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.ComposeEvent(None, '25', 'Alpha', '1.2')) - def test_branched4_newer(self, fakecompexists, fakesmtp, fakecreate): + def test_branched4_newer(self, fakecompexists, fakesmtp, fakecreate, fakesubproc): """...but same few-days-later Branched nightly with newer packages should create event. """ @@ -317,7 +319,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Production.cid', ALPHA3) @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetdifferent) @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.NightlyEvent(None, '25', 'Branched', '20160705.n.0')) - def test_alpha3(self, fakesmtp, fakecreate): + def test_alpha3(self, fakesmtp, fakecreate, fakesubproc): """Another Alpha candidate came along! Event should be created even though it's only been one day. """ @@ -328,7 +330,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetdifferent) @mock.patch('wikitcms.event.ComposeEvent.creation_date', '20160706') @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.ComposeEvent(None, '25', 'Alpha', '1.3')) - def test_branched5(self, fakecompexists, fakesmtp, fakecreate): + def test_branched5(self, fakecompexists, fakesmtp, fakecreate, fakesubproc): """OK, say Alpha 1.3 went out as Alpha. Now another Branched comes along. This isn't testing anything new but I wanna go from Branched to Beta then Beta to Final. We should get an event @@ -339,7 +341,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetdifferent) @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.NightlyEvent(None, '25', 'Branched', '20160710.n.0')) - def test_rawhide5(self, fakesmtp, fakecreate): + def test_rawhide5(self, fakesmtp, fakecreate, fakesubproc): """Meanwhile, a Rawhide compose shows up (this is a thing that happens all the time). We should NOT create any event for it - we never create events for the release after the next (i.e. for @@ -353,7 +355,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Production.cid', BETA1) @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetdifferent) @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.NightlyEvent(None, '25', 'Branched', '20160710.n.0')) - def test_beta1(self, fakesmtp, fakecreate): + def test_beta1(self, fakesmtp, fakecreate, fakesubproc): """OK, here comes Beta. We should get an event, of course.""" CONSUMER.consume(_fakemsg(BETA1)) assert fakecreate.call_args[1]['check'] is True @@ -364,7 +366,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetdifferent) @mock.patch('wikitcms.event.ComposeEvent.creation_date', '20160714') @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.ComposeEvent(None, '25', 'Beta', '1.1')) - def test_final1(self, fakecompexists, fakesmtp, fakecreate): + def test_final1(self, fakecompexists, fakesmtp, fakecreate, fakesubproc): """Finally, here comes Final! Testing going straight from one candidate compose to another milestone candidate compose, though this is unlikely ever to happen. We should get an event. @@ -378,7 +380,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetdifferent) @mock.patch('wikitcms.event.ComposeEvent.creation_date', '20160721') @mock.patch('wikitcms.wiki.Wiki.current_event', wikitcms.event.ComposeEvent(None, '25', 'RC', '1.1')) - def test_atomic1(self, fakecompexists, fakesmtp, fakecreate): + def test_atomic1(self, fakecompexists, fakesmtp, fakecreate, fakesubproc): """We may get two-week Atomic composes for the release starting up before it goes final; make sure we don't get events for these. """ @@ -388,7 +390,7 @@ class TestRelvalConsumer: @mock.patch('fedfind.release.Pungi4Release.get_package_nvras', _fakegetdifferent) @mock.patch('wikitcms.wiki.Wiki.current_modular_event', wikitcms.event.NightlyEvent(None, '27', 'Branched', '20171001.n.0', modular=True)) - def test_modular_branched1(self, fakesmtp, fakecreate): + def test_modular_branched1(self, fakesmtp, fakecreate, fakesubproc): """Now a Modular compose shows up! We should NOT get an event, because we don't do events for modular composes any more. """