Ejemplo n.º 1
0
 def _vmngr(self):
     vmngr = getattr(self, '__vmngr', None)
     if vmngr is None:
         vmngr = VersionMgr(log=self._logger)
         [setattr(vmngr, n, c) for n, c in self._callbacks.items()]
         [vmngr.register(ev, val[0], val[1]) for ev, val in
          self._registries.items()]
         setattr(self, '__vmngr', vmngr)
     return vmngr
Ejemplo n.º 2
0
    def setUp(self):
        '''
        Given that nosetests test isolation is "incompatible" with w3af's
        kb, cf, etc. objects, and the tests written here are overwriting
        some classes that are loaded into sys.modules and then used in other
        code sections -and tests-, I need to clean the mess after I finish.

        @see: http://mousebender.wordpress.com/2006/12/07/test-isolation-in-nose/

        I haven't been able to fix this issue... so I'm skipping these two
        tests!
        '''
        self.vmgr = VersionMgr(W3AF_LOCAL_PATH, MagicMock(return_value=None))
Ejemplo n.º 3
0
    def setUp(self):
        """
        Given that nosetests test isolation is "incompatible" with w3af's
        kb, cf, etc. objects, and the tests written here are overwriting
        some classes that are loaded into sys.modules and then used in other
        code sections -and tests-, I need to clean the mess after I finish.

        @see: http://mousebender.wordpress.com/2006/12/07/test-isolation-in-nose/

        I haven't been able to fix this issue... so I'm skipping these two
        tests!
        """
        self.vmgr = VersionMgr(W3AF_LOCAL_PATH, MagicMock(return_value=None))
Ejemplo n.º 4
0
class TestVersionMgr(unittest.TestCase):
    def setUp(self):
        """
        Given that nosetests test isolation is "incompatible" with w3af's
        kb, cf, etc. objects, and the tests written here are overwriting
        some classes that are loaded into sys.modules and then used in other
        code sections -and tests-, I need to clean the mess after I finish.

        @see: http://mousebender.wordpress.com/2006/12/07/test-isolation-in-nose/

        I haven't been able to fix this issue... so I'm skipping these two
        tests!
        """
        self.vmgr = VersionMgr(W3AF_LOCAL_PATH, MagicMock(return_value=None))

    def test_no_need_update(self):
        vmgr = self.vmgr
        vmgr._start_cfg = StartUpConfig()
        vmgr._start_cfg._autoupd = False

        # Test no auto-update
        self.assertFalse(vmgr._has_to_update())

    def test_has_to_update(self):
        """
        Test [D]aily, [W]eekly and [M]onthly auto-update
        """
        SC = StartUpConfig
        vmgr = self.vmgr

        for freq, diffdays in ((SC.FREQ_DAILY, 1), (SC.FREQ_WEEKLY, 8), (SC.FREQ_MONTHLY, 34)):

            vmgr._start_cfg = start_cfg = StartUpConfig()
            start_cfg._autoupd = True
            start_cfg._freq = freq

            last_upd = datetime.date.today() - datetime.timedelta(days=diffdays)
            start_cfg._lastupd = last_upd

            self.assertTrue(vmgr._has_to_update())

    def test_added_new_dependencies(self):
        start = "cb751e941bfa2063ebcef711642ed5d22ff9db87"
        end = "9c5f5614412dce67ac13411e1eebd754b4c6fb6a"

        changelog = ChangeLog(start, end)

        self.assertTrue(self.vmgr._added_new_dependencies(changelog))

    def test_not_added_new_dependencies(self):
        start = "479f30c95873c3e4f8370ceb91f8aeb74794d047"
        end = "87924241bf70c2321bc9f567e3d2ce62ee264fee"

        changelog = ChangeLog(start, end)

        self.assertFalse(self.vmgr._added_new_dependencies(changelog))

    def test_update_not_required_not_forced(self):
        """
        Test that we don't perform any extra steps if the local installation
        was already updated today.
        """
        self.vmgr._start_cfg = start_cfg = StartUpConfig()
        start_cfg._autoupd = True
        start_cfg._freq = StartUpConfig.FREQ_DAILY

        last_upd = datetime.date.today() - datetime.timedelta(days=0)
        start_cfg._lastupd = last_upd

        on_update_check_mock = MagicMock()
        on_already_latest_mock = MagicMock()
        on_update_mock = MagicMock()

        self.vmgr.register(VersionMgr.ON_UPDATE_CHECK, on_update_check_mock, None)
        self.vmgr.register(VersionMgr.ON_ALREADY_LATEST, on_already_latest_mock, None)
        self.vmgr.register(VersionMgr.ON_UPDATE, on_update_mock, None)

        self.vmgr.update()

        self.assertEqual(on_update_check_mock.call_count, 0)
        self.assertEqual(on_already_latest_mock.call_count, 0)
        self.assertEqual(on_update_mock.call_count, 0)

    def test_update_required_not_forced(self):
        """
        Test that we check if we're on the latest version if the latest
        local installation update was 3 days ago and the frequency is set to
        daily.
        
        The local repository is in the latest version (git pull is run before)
        """
        git_client = GitClient(".")
        git_client.pull()

        self.vmgr._start_cfg = start_cfg = StartUpConfig()
        start_cfg._autoupd = True
        start_cfg._freq = StartUpConfig.FREQ_DAILY

        last_upd = datetime.date.today() - datetime.timedelta(days=3)
        start_cfg._lastupd = last_upd

        on_update_check_mock = MagicMock()
        on_already_latest_mock = MagicMock()
        on_update_mock = MagicMock()

        self.vmgr.register(VersionMgr.ON_UPDATE_CHECK, on_update_check_mock, None)
        self.vmgr.register(VersionMgr.ON_ALREADY_LATEST, on_already_latest_mock, None)
        self.vmgr.register(VersionMgr.ON_UPDATE, on_update_mock, None)

        self.vmgr.update()

        self.assertEqual(on_update_check_mock.call_count, 1)
        self.assertEqual(on_already_latest_mock.call_count, 1)
        self.assertEqual(on_update_mock.call_count, 0)

    def test_update_required_outdated_not_forced(self):
        """
        Test that we check if we're on the latest version if the latest
        local installation update was 3 days ago and the frequency is set to
        daily.
        
        The local repository is NOT in the latest version. A 'git reset --hard'
        is run at the beginning of this test to reset the repo to a revision
        before the latest one.
        """
        try:
            git_client = GitClient(".")
            head_id = git_client.get_local_head_id()
            one_before_head = git_client.get_parent_for_revision(head_id)
            git_client.reset_to_previous_state(one_before_head)

            self.vmgr._start_cfg = start_cfg = StartUpConfig()
            start_cfg._autoupd = True
            start_cfg._freq = StartUpConfig.FREQ_DAILY

            last_upd = datetime.date.today() - datetime.timedelta(days=3)
            start_cfg._lastupd = last_upd

            on_update_check_mock = MagicMock()
            on_already_latest_mock = MagicMock()
            on_update_mock = MagicMock()

            self.vmgr.register(VersionMgr.ON_UPDATE_CHECK, on_update_check_mock, None)
            self.vmgr.register(VersionMgr.ON_ALREADY_LATEST, on_already_latest_mock, None)
            self.vmgr.register(VersionMgr.ON_UPDATE, on_update_mock, None)

            self.vmgr.callback_onupdate_confirm = MagicMock(side_effect=[True])

            self.vmgr.update()

            self.assertEqual(on_update_check_mock.call_count, 1)
            self.assertEqual(on_already_latest_mock.call_count, 0)
            self.assertEqual(on_update_mock.call_count, 1)
        finally:
            git_client.pull()
Ejemplo n.º 5
0
class TestVersionMgr(unittest.TestCase):
    def setUp(self):
        '''
        Given that nosetests test isolation is "incompatible" with w3af's
        kb, cf, etc. objects, and the tests written here are overwriting
        some classes that are loaded into sys.modules and then used in other
        code sections -and tests-, I need to clean the mess after I finish.

        @see: http://mousebender.wordpress.com/2006/12/07/test-isolation-in-nose/

        I haven't been able to fix this issue... so I'm skipping these two
        tests!
        '''
        self.vmgr = VersionMgr(W3AF_LOCAL_PATH, MagicMock(return_value=None))

    def test_no_need_update(self):
        vmgr = self.vmgr
        vmgr._start_cfg = StartUpConfig()
        vmgr._start_cfg._autoupd = False

        # Test no auto-update
        self.assertFalse(vmgr._has_to_update())

    def test_has_to_update(self):
        '''
        Test [D]aily, [W]eekly and [M]onthly auto-update
        '''
        SC = StartUpConfig
        vmgr = self.vmgr

        for freq, diffdays in ((SC.FREQ_DAILY, 1), (SC.FREQ_WEEKLY, 8),
                               (SC.FREQ_MONTHLY, 34)):

            vmgr._start_cfg = start_cfg = StartUpConfig()
            start_cfg._autoupd = True
            start_cfg._freq = freq

            last_upd = datetime.date.today() - datetime.timedelta(
                days=diffdays)
            start_cfg._lastupd = last_upd

            self.assertTrue(vmgr._has_to_update())

    def test_added_new_dependencies(self):
        start = 'cb751e941bfa2063ebcef711642ed5d22ff9db87'
        end = '9c5f5614412dce67ac13411e1eebd754b4c6fb6a'

        changelog = ChangeLog(start, end)

        self.assertTrue(self.vmgr._added_new_dependencies(changelog))

    def test_not_added_new_dependencies(self):
        start = '479f30c95873c3e4f8370ceb91f8aeb74794d047'
        end = '87924241bf70c2321bc9f567e3d2ce62ee264fee'

        changelog = ChangeLog(start, end)

        self.assertFalse(self.vmgr._added_new_dependencies(changelog))

    def test_update_not_required_not_forced(self):
        '''
        Test that we don't perform any extra steps if the local installation
        was already updated today.
        '''
        self.vmgr._start_cfg = start_cfg = StartUpConfig()
        start_cfg._autoupd = True
        start_cfg._freq = StartUpConfig.FREQ_DAILY

        last_upd = datetime.date.today() - datetime.timedelta(days=0)
        start_cfg._lastupd = last_upd

        on_update_check_mock = MagicMock()
        on_already_latest_mock = MagicMock()
        on_update_mock = MagicMock()

        self.vmgr.register(VersionMgr.ON_UPDATE_CHECK, on_update_check_mock,
                           None)
        self.vmgr.register(VersionMgr.ON_ALREADY_LATEST,
                           on_already_latest_mock, None)
        self.vmgr.register(VersionMgr.ON_UPDATE, on_update_mock, None)

        self.vmgr.update()

        self.assertEqual(on_update_check_mock.call_count, 0)
        self.assertEqual(on_already_latest_mock.call_count, 0)
        self.assertEqual(on_update_mock.call_count, 0)

    def test_update_required_not_forced(self):
        '''
        Test that we check if we're on the latest version if the latest
        local installation update was 3 days ago and the frequency is set to
        daily.
        
        The local repository is in the latest version (git pull is run before)
        '''
        git_client = GitClient('.')
        git_client.pull()

        self.vmgr._start_cfg = start_cfg = StartUpConfig()
        start_cfg._autoupd = True
        start_cfg._freq = StartUpConfig.FREQ_DAILY

        last_upd = datetime.date.today() - datetime.timedelta(days=3)
        start_cfg._lastupd = last_upd

        on_update_check_mock = MagicMock()
        on_already_latest_mock = MagicMock()
        on_update_mock = MagicMock()

        self.vmgr.register(VersionMgr.ON_UPDATE_CHECK, on_update_check_mock,
                           None)
        self.vmgr.register(VersionMgr.ON_ALREADY_LATEST,
                           on_already_latest_mock, None)
        self.vmgr.register(VersionMgr.ON_UPDATE, on_update_mock, None)

        self.vmgr.update()

        self.assertEqual(on_update_check_mock.call_count, 1)
        self.assertEqual(on_already_latest_mock.call_count, 1)
        self.assertEqual(on_update_mock.call_count, 0)

    def test_update_required_outdated_not_forced(self):
        '''
        Test that we check if we're on the latest version if the latest
        local installation update was 3 days ago and the frequency is set to
        daily.
        
        The local repository is NOT in the latest version. A 'git reset --hard'
        is run at the beginning of this test to reset the repo to a revision
        before the latest one.
        '''
        try:
            git_client = GitClient('.')
            head_id = git_client.get_local_head_id()
            one_before_head = git_client.get_parent_for_revision(head_id)
            git_client.reset_to_previous_state(one_before_head)

            self.vmgr._start_cfg = start_cfg = StartUpConfig()
            start_cfg._autoupd = True
            start_cfg._freq = StartUpConfig.FREQ_DAILY

            last_upd = datetime.date.today() - datetime.timedelta(days=3)
            start_cfg._lastupd = last_upd

            on_update_check_mock = MagicMock()
            on_already_latest_mock = MagicMock()
            on_update_mock = MagicMock()

            self.vmgr.register(VersionMgr.ON_UPDATE_CHECK,
                               on_update_check_mock, None)
            self.vmgr.register(VersionMgr.ON_ALREADY_LATEST,
                               on_already_latest_mock, None)
            self.vmgr.register(VersionMgr.ON_UPDATE, on_update_mock, None)

            self.vmgr.callback_onupdate_confirm = MagicMock(side_effect=[
                True,
            ])

            self.vmgr.update()

            self.assertEqual(on_update_check_mock.call_count, 1)
            self.assertEqual(on_already_latest_mock.call_count, 0)
            self.assertEqual(on_update_mock.call_count, 1)
        finally:
            git_client.pull()