subprocess.Popen aped

subprocess.Popen aped

Published July 30, 2016 in Development, Tests and quality - Last update on July 30, 2016.

Lately, I worked on Django DBBackup, I was remaking the database command system: classes allowing launching of backup or restore commands through subprocess.Popen. There was no unit tests and major part of them are based on command line but contrary to what I though mock out Popen took me a some time.

What's the code to dry ?

    def run_command(self, command, stdin=None, env=None):
        cmd = shlex.split(command)
        stdout = SpooledTemporaryFile(max_size=10 * 1024 * 1024)
        stderr = SpooledTemporaryFile(max_size=10 * 1024 * 1024)
        full_env = self.env.copy()
        full_env.update(env or {})
        try:
            process = Popen(cmd, stdin=stdin, stdout=stdout, stderr=stderr,
                            env=full_env)
            process.wait()
            if process.poll():
                stderr.seek(0)
                raise exceptions.CommandConnectorError(
                    "Error running: {}\n{}".format(command, stderr.read()))
            stdout.seek(0)
            stderr.seek(0)
            return stdout, stderr
        except OSError as err:
            raise exceptions.CommandConnectorError(
                "Error running: {}\n{}".format(command, str(err)));

I want to pass all those lines and do not be affected by Popen's behavior, or in other words: make real unit test without to have the underlying system as a factor.

Are you a n00b ? There is true command...

true is Unix-Like and the OS whose we do not pronounce the name (Win****) may not have true/false/echo commands. More precisely, I must ape process.wait() and process.poll() behaviors. Below their documentation:

Popen.wait()

Wait for child process to terminate. Set and return returncode attribute. 

Popen.poll()

Check if child process has terminated. Set and return returncode attribute.

And the million of SO answers and blog post about ?

Most of reference about this problem have ways that I don't like:

  • Some configure Mock object manually inside tests (cf StackOverflow answer) and I want to configure only in decorator.
  • Other will use Popen.communicate(), I use Popen.poll(), the issue is relatively the same but they use autospec parameter.

My method is the following:

@patch('dbbackup.db.base.Popen', **{
    'return_value.wait.return_value': True,
    'return_value.poll.return_value': 0,
})
class PgDumpConnectorRunCommandTest(TestCase):
    def test_run_command(self, mock_popen):
        connector = PgDumpConnector()
        connector.create_dump()
        self.assertEqual(mock_popen.call_args[0][0][0], 'pg_dump');

I mock out Popen (only where I use it obviously) and with the keyword utility I define their attributes. The sublety is here, with classic form of argument (arg=value) I couldn't chain mock of attributes, the maxium would be 1 attribute and after we would set a MagicMock for customize subattributes.

Here we specify that the MagicMock returned by Popen.__init__ will have a wait (and poll) attribute, and this attribute will return True when launched.

Yes, even in Python I love One-Line.

References

Comments

  • online slots

    online slots on 01/11/2018 9:39 a.m. #

    online casino
    online slots
    real money casino
    casino games
    real money casino

  • GeorgeUtifs

    GeorgeUtifs on 01/11/2018 10:51 a.m. #

    autocad design suite premium 2014 download
    <a href="http://autocadtymafq.com/#">autocad download</a>
    autocad raster design 2011 download
    <a href=http://autocadtymafq.com/#>autocad student</a>
    online jobs in autocad drafting
    <a href="http://autocadbnaqk.com/#">autocad 2018</a>
    serial number autocad 2012 32 bit
    <a href=http://autocadbnaqk.com/#>auto cad</a>
    autocad objects download

  • NathanArord

    NathanArord on 01/11/2018 4:49 p.m. #

    viagra http://viagrarrr.com
    <a href=http://viagrarrr.com>viagra</a>
    <a href=http://viagrarrr.com">viagra</a>
    buy viagra http://viagrarrr.com
    <a href=http://viagrarrr.com>buy viagra</a>
    <a href=http://viagrarrr.com">buy viagra</a>
    cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>cialis</a>
    <a href="http://cialisrrr.com">cialis</a>
    buy cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>buy cialis</a>
    <a href="http://cialisrrr.com">buy cialis</a>

  • Lestertible

    Lestertible on 01/11/2018 6:35 p.m. #

    precio licencia autocad
    <a href="http://autocadtymafq.com/#">autocad</a>
    autocad mep 2011 download
    <a href=http://autocadtymafq.com/#>autocad lt</a>
    compare autocad products
    <a href="http://autocadbnaqk.com/#">auto cad</a>
    autocad structural detailing 2012 download
    <a href=http://autocadbnaqk.com/#>auto cad</a>
    buy old versions of autocad

  • CraigShots

    CraigShots on 01/11/2018 9:38 p.m. #

    viagra http://viagrarrr.com
    <a href=http://viagrarrr.com>viagra</a>
    <a href="http://viagrarrr.com">viagra</a>
    cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>cialis</a>
    <a href="http://cialisrrr.com">cialis</a>
    buy viagra http://viagrarrr.com
    <a href=http://viagrarrr.com>buy viagra</a>
    <a href="http://viagrarrr.com">buy viagra</a>
    buy cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>buy cialis</a>
    <a href="http://cialisrrr.com">buy cialis</a>

  • NathanArord

    NathanArord on 01/12/2018 4:24 a.m. #

    viagra http://viagrarrr.com
    <a href=http://viagrarrr.com>viagra</a>
    <a href=http://viagrarrr.com">viagra</a>
    buy viagra http://viagrarrr.com
    <a href=http://viagrarrr.com>buy viagra</a>
    <a href=http://viagrarrr.com">buy viagra</a>
    cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>cialis</a>
    <a href="http://cialisrrr.com">cialis</a>
    buy cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>buy cialis</a>
    <a href="http://cialisrrr.com">buy cialis</a>

  • CraigShots

    CraigShots on 01/12/2018 3:44 p.m. #

    viagra http://viagrarrr.com
    <a href=http://viagrarrr.com>viagra</a>
    <a href="http://viagrarrr.com">viagra</a>
    cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>cialis</a>
    <a href="http://cialisrrr.com">cialis</a>
    buy viagra http://viagrarrr.com
    <a href=http://viagrarrr.com>buy viagra</a>
    <a href="http://viagrarrr.com">buy viagra</a>
    buy cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>buy cialis</a>
    <a href="http://cialisrrr.com">buy cialis</a>

  • PeterRox

    PeterRox on 01/12/2018 5:30 p.m. #

    viagra http://viagrarrr.com
    <a href=http://viagrarrr.com>viagra</a>
    <a href=http://viagrarrr.com">viagra</a>
    buy viagra http://viagrarrr.com
    <a href=http://viagrarrr.com>buy viagra</a>
    <a href=http://viagrarrr.com">buy viagra</a>
    cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>cialis</a>
    <a href="http://cialisrrr.com">cialis</a>
    buy cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>buy cialis</a>
    <a href="http://cialisrrr.com">buy cialis</a>

  • Debradyday

    Debradyday on 01/12/2018 7:45 p.m. #

    loans no credit
    <a href=http://paydayhjkfg.com/>payday loans no credit</a>
    <a href="http://paydayhjkfg.com/">loans no credit</a>
    loans for bad credit

  • MauriceFep

    MauriceFep on 01/13/2018 9:50 a.m. #

    levitra http://levitrarrr.com
    <a href=http://levitrarrr.com>levitra</a>
    <a href=http://levitrarrr.com">levitra</a>
    buy levitra http://levitrarrr.com
    <a href=http://levitrarrr.com>buy levitra</a>
    <a href=http://levitrarrr.com">buy levitra</a>
    cialis http://cialis24h.us
    <a href=http://cialis24h.us>cialis</a>
    <a href="http://cialis24h.us">cialis</a>
    buy cialis http://cialis24h.us
    <a href=http://cialis24h.us>buy cialis</a>
    <a href="http://cialis24h.us">buy cialis</a>

  • Williamgoowl

    Williamgoowl on 01/13/2018 1:38 p.m. #

    viagra http://viagrarrr.com
    <a href=http://viagrarrr.com>viagra</a>
    <a href=http://viagrarrr.com">viagra</a>
    buy viagra http://viagrarrr.com
    <a href=http://viagrarrr.com>buy viagra</a>
    <a href=http://viagrarrr.com">buy viagra</a>
    cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>cialis</a>
    <a href="http://cialisrrr.com">cialis</a>
    buy cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>buy cialis</a>
    <a href="http://cialisrrr.com">buy cialis</a>

  • EdwardWaw

    EdwardWaw on 01/13/2018 9:53 p.m. #

    cheap viagra for sale uk
    <a href="http://viagrayityjg.com/#">buy viagra online</a>
    price comparison of viagra cialis levitra
    <a href=http://viagrayityjg.com/#>order viagra online</a>
    spot fake viagra pill
    <a href="http://viagramhkdyl.com/#">viagra cheap</a>
    generic viagra suppliers australia
    <a href=http://viagramhkdyl.com/#>viagra prices</a>
    viagra for sale in india

  • Ronaldlon

    Ronaldlon on 01/14/2018 1:56 a.m. #

    levitra http://levitrarrr.com
    <a href=http://levitrarrr.com>levitra</a>
    <a href=http://levitrarrr.com">levitra</a>
    buy levitra http://levitrarrr.com
    <a href=http://levitrarrr.com>buy levitra</a>
    <a href=http://levitrarrr.com">buy levitra</a>
    cialis online http://cialisrrr.com
    <a href=http://cialisrrr.com>cialis online</a>
    <a href="http://cialisrrr.com">cialis online</a>
    cheap cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>cheap cialis</a>
    <a href="http://cialisrrr.com">cheap cialis</a>
    sildenafil http://viagrarrr.com
    <a href=http://viagrarrr.com>sildenafil</a>
    <a href="http://viagrarrr.com">sildenafil</a>
    viagra generic http://viagrarrr.com
    <a href=http://viagrarrr.com>viagra generic</a>
    <a href="http://viagrarrr.com">viagra generic</a>

  • WilliamRiz

    WilliamRiz on 01/14/2018 3:49 a.m. #

    viagra http://viagrarrr.com
    <a href=http://viagrarrr.com>viagra</a>
    <a href="http://viagrarrr.com">viagra</a>
    cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>cialis</a>
    <a href="http://cialisrrr.com">cialis</a>
    buy viagra http://viagrarrr.com
    <a href=http://viagrarrr.com>buy viagra</a>
    <a href="http://viagrarrr.com">buy viagra</a>
    buy cialis http://cialisrrr.com
    <a href=http://cialisrrr.com>buy cialis</a>
    <a href="http://cialisrrr.com">buy cialis</a>
    viagra generic http://viagrarrr.com
    <a href=http://viagrarrr.com>viagra generic</a>
    <a href="http://viagrarrr.com">viagra generic</a>
    cialis generic http://cialisrrr.com
    <a href=http://cialisrrr.com>cialis generic</a>
    <a href="http://cialisrrr.com">cialis generic</a>

  • casino games list

    casino games list on 01/14/2018 10:15 a.m. #

    play casinos
    new usa online casinos
    casino games roulette
    casino online

  • JamesWrari

    JamesWrari on 01/14/2018 10:19 p.m. #

    generic viagra is it legal
    <a href="http://viagrayityjg.com/#">buy viagra online</a>
    cheapest price for generic viagra
    <a href=http://viagrayityjg.com/#>viagra online</a>
    best site to buy generic viagra
    <a href="http://viagramhkdyl.com/#">viagra cheap</a>
    is buying viagra online legit
    <a href=http://viagramhkdyl.com/#>best price viagra</a>
    cheap cialis viagra online

  • regime dukan dieta proteinas

    regime dukan dieta proteinas on 01/16/2018 5:34 p.m. #

    What's up, for all time i used to check webpage posts here early in the morning, as i love to learn more and more.

  • payday loans bad credit

    payday loans bad credit on 01/17/2018 8 a.m. #

    payday loans in az
    online loans
    payday loans bad credit
    payday personal loan

  • Jessegog

    Jessegog on 01/17/2018 10:38 a.m. #

    buy viagra in durban
    <a href="http://viagrayirib.com/#">viagra without a prescription</a>
    generic viagra soft tablets
    <a href=http://viagrayirib.com/#>viagra without script</a>
    generic viagra with overnight delivery
    <a href="http://viagrahkfy.com/#">viagra coupons 2018</a>
    efeitos viagra 100mg
    <a href=http://viagrahkfy.com/#>viagra coupons</a>
    quanto custa viagra generico

Post your comment

Comment as . Log out.