fab-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Fab-user] unit testing


From: andrea crotti
Subject: Re: [Fab-user] unit testing
Date: Wed, 30 Jan 2013 13:34:27 +0000

Anyway I'm almost satisfied, now I can record the things that would be
run and then check things with them.
I wonder why the "cd" is not complaining, is it actually running on
the remote hosts behind my back?

def fake_run(cmd, seen=[]):
        seen.append(cmd)
        return sys.stdout.write(cmd + '\n')


def files_exists(exists=True):
        m = Mock()
        m.exists = lambda x: exists
        return m


SEEN = []

@patch('fabfile.WAZOKU_REPO', new='/tmp/wazoku')
@patch('fabfile.run', new=lambda cmd: fake_run(cmd, seen=SEEN))
class TestNewDeploy(unittest.TestCase):
        def setUp(self):
                global SEEN
                SEEN = []

        @patch('fabfile.files', new=files_exists(False))
        def test_first_run_works_file(self):
                if path.isdir(fabfile.WAZOKU_REPO):
                        rmtree(fabfile.WAZOKU_REPO)

                fabfile.deploy_head_new_pull(branch='staging')
                self.assertEqual(len(SEEN), 6)

        @patch('fabfile.files', new=files_exists(True))
        def test_second_run_works(self):
                fabfile.deploy_head_new_pull(branch='staging')
                self.assertEqual(len(SEEN), 5)


2013/1/30 andrea crotti <address@hidden>:
> Yes I tried that but than I need to setup some ssh keys to connect
> automatically to localhost (unless there is a smarter way to do that).
>
> 2013/1/30 Jasper van den Bosch <address@hidden>:
>> Yes, or just specify localhost as the host. We could think about making a
>> fully mocked version of the fabric API as a python package.
>>
>>
>> On 30 January 2013 14:11, andrea crotti <address@hidden> wrote:
>>>
>>> Well I suppose it's files.exists, and in fact adding the other mock works
>>>
>>> def files_exists():
>>>
>>>         m = Mock()
>>>
>>> m.exists = lambda x: True
>>>
>>> return m
>>>
>>> @patch('fabfile.files', new=files_exists())
>>>
>>> It would be nice to avoid mocking all the calls though..
>>> And maybe another useful thing would be to make something that should run
>>> on different servers run locally, I suppose I could simply do a
>>> run -> local
>>> translation, right?
>>>
>>> On 30 Jan 2013 12:58, "Jasper van den Bosch" <address@hidden> wrote:
>>>>
>>>> Hi andrea,
>>>>
>>>> I regularly unit-test fabric code. If you are mocking out run() etc, and
>>>> using the regular python executable, not fab, to start the tests, I don't
>>>> see why it would try to connect. Can you post more code, i.e. as a Gist?
>>>>
>>>> Jasper
>>>>
>>>> On 30 January 2013 13:30, andrea crotti <address@hidden>
>>>> wrote:
>>>>>
>>>>> I'm trying to unit test my fabfile, what I would like to do is the
>>>>> following:
>>>>> - set some conditions
>>>>> - get a trail of commands that would be run
>>>>> - check if that is correct
>>>>>
>>>>> Now the problem is that for any command it still wants to connect, so
>>>>> even if I mock out run, it still complains about the host missing for
>>>>> some reason.
>>>>>
>>>>> And then I have to find a way to record the sequence of commands
>>>>> instead of simply printing them out, any ideas about that?
>>>>> By the way, I was looking for documentation about unit testing and
>>>>> fabric but didn't find anything useful, is noone doing it?
>>>>>
>>>>> Test:
>>>>> def fake_run(cmd):
>>>>>         return sys.stdout.write(cmd)
>>>>>
>>>>> @patch('fabfile.env.hosts', new=['localhost'])
>>>>> @patch('fabfile.WAZOKU_REPO', new='/tmp/wazoku')
>>>>> @patch('fabfile.run', new=fake_run)
>>>>> class TestNewDeploy(unittest.TestCase):
>>>>>         def test_first_run_works_file(self):
>>>>>
>>>>>
>>>>> Function to test:
>>>>> @parallel
>>>>> def deploy_head_new_pull(branch='master'):
>>>>>         api_dir = "%s/company-api/api" % COMPANY_REPO
>>>>>         ve_dir = '%s/ve' % api_dir
>>>>>         if not files.exists(COMPANY_REPO):
>>>>>                 run(CLONE.format(branch, COMPANY_REPO))
>>>>>                 # create the symlink to make sure it points to the right
>>>>> place
>>>>>                 run('ln -sf %s/company-api $HOME/api' % COMPANY_REPO)
>>>>>                 run('virtualenv %s' % ve_dir)
>>>>>         else:
>>>>>                 with cd(COMPANY_API):
>>>>>                         run('git pull')
>>>>>                         run('git clean -f')
>>>>>
>>>>>         with settings(user='company'):
>>>>>                 with cd(COMPANY_REPO):
>>>>>                         run('source %s/bin/activate && pip install -r
>>>>> requirements.txt' % ve_dir)
>>>>>                         run('cp production_settings.py settings.py')
>>>>>
>>>>>         restart_api()
>>>>>
>>>>> _______________________________________________
>>>>> Fab-user mailing list
>>>>> address@hidden
>>>>> https://lists.nongnu.org/mailman/listinfo/fab-user
>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Jasper van den Bosch
>>>> ilogue.com/jasper
>>>>
>>>> I'd love to see your attachment, but please use OpenDocument, not a
>>>> proprietary format like docx. It's an international standard, endorsed by
>>>> the EU, and implemented in many office suites, such as LibreOffice, and
>>>> commercial products from Oracle, Sun, IBM and Microsoft (from 2007).
>>
>>
>>
>>
>> --
>> Jasper van den Bosch
>> ilogue.com/jasper
>>
>> I'd love to see your attachment, but please use OpenDocument, not a
>> proprietary format like docx. It's an international standard, endorsed by
>> the EU, and implemented in many office suites, such as LibreOffice, and
>> commercial products from Oracle, Sun, IBM and Microsoft (from 2007).



reply via email to

[Prev in Thread] Current Thread [Next in Thread]