def testIsCacheStaleWithMalformedCache(self):
        vc = version_checker.VersionChecker(cache_ttl_sec=20,
                                            current_version='1.2.0')
        malformed_caches = ({
            'last_checked_version': '1.2',
            'last_check': 3000,
            'current_version': '1.2.0'
        }, {
            'last_checked_version': '1.2',
            'current_version': '1.2.0'
        }, {
            'last_checked_version': '1.2.x',
            'last_check': 3000,
            'current_version': '1.2.0'
        })

        for cache in malformed_caches:
            self.assertTrue(vc._IsCacheStale(cache, current_time=1))
            self.assertTrue(vc._IsCacheStale(cache, current_time=2000))
            self.assertTrue(vc._IsCacheStale(cache, current_time=2999))
            self.assertTrue(vc._IsCacheStale(cache, current_time=3000))
            self.assertTrue(vc._IsCacheStale(cache, current_time=3005))
            self.assertTrue(vc._IsCacheStale(cache, current_time=3010))
            self.assertTrue(vc._IsCacheStale(cache, current_time=3019))
            self.assertTrue(vc._IsCacheStale(cache, current_time=3020))
            self.assertTrue(vc._IsCacheStale(cache, current_time=3021))
            self.assertTrue(vc._IsCacheStale(cache, current_time=4000))
            self.assertTrue(vc._IsCacheStale(cache, current_time=398457398))
    def testGetDownloadLink(self):
        def MockGetSystemWindows():
            return 'Windows'

        vc = version_checker.VersionChecker()
        vc._GetSystem = MockGetSystemWindows
        cache = {
            'last_checked_version': '1.3.0',
            'last_check': 3000.0,
            'current_version': '1.2.0'
        }
        self.assertEqual(vc._GetDownloadLink(cache), None)

        cache = {
            'last_checked_version': '1.3.0',
            'last_check': 3000.0,
            'current_version': '1.2.0',
            'last_tar_url': 'download/gcutil-1.3.0.tar.gz',
            'last_zip_url': 'download/gcutil-1.3.0.zip'
        }
        self.assertEqual(vc._GetDownloadLink(cache),
                         'download/gcutil-1.3.0.zip')

        def MockGetSystemLinux():
            return 'Linux'

        vc._GetSystem = MockGetSystemLinux
        self.assertEqual(vc._GetDownloadLink(cache),
                         'download/gcutil-1.3.0.tar.gz')
    def testIsCacheStale(self):
        vc = version_checker.VersionChecker(cache_ttl_sec=20,
                                            current_version='1.2.0')
        cache = {
            'last_checked_version': '1.2',
            'last_check': 3000.0,
            'current_version': '1.2.0'
        }

        self.assertTrue(vc._IsCacheStale(cache, current_time=1))
        self.assertTrue(vc._IsCacheStale(cache, current_time=2000))
        self.assertTrue(vc._IsCacheStale(cache, current_time=2999))
        self.assertFalse(vc._IsCacheStale(cache, current_time=3000))
        self.assertFalse(vc._IsCacheStale(cache, current_time=3005))
        self.assertFalse(vc._IsCacheStale(cache, current_time=3010))
        self.assertFalse(vc._IsCacheStale(cache, current_time=3019))
        self.assertTrue(vc._IsCacheStale(cache, current_time=3020))
        self.assertTrue(vc._IsCacheStale(cache, current_time=3021))
        self.assertTrue(vc._IsCacheStale(cache, current_time=4000))
        self.assertTrue(vc._IsCacheStale(cache, current_time=398457398))

        self.assertTrue(vc._IsCacheStale({}, current_time=1))
        self.assertTrue(vc._IsCacheStale({}, current_time=2000))
        self.assertTrue(vc._IsCacheStale({}, current_time=2999))
        self.assertTrue(vc._IsCacheStale({}, current_time=3000))
        self.assertTrue(vc._IsCacheStale({}, current_time=3005))
        self.assertTrue(vc._IsCacheStale({}, current_time=3010))
        self.assertTrue(vc._IsCacheStale({}, current_time=3019))
        self.assertTrue(vc._IsCacheStale({}, current_time=3020))
        self.assertTrue(vc._IsCacheStale({}, current_time=3021))
        self.assertTrue(vc._IsCacheStale({}, current_time=4000))
        self.assertTrue(vc._IsCacheStale({}, current_time=398457398))
 def testUpdateCache(self):
     vc = version_checker.VersionChecker(cache_ttl_sec=20,
                                         current_version='1.2.0')
     cache = {}
     vc._UpdateCache(cache,
                     http=VersionCheckerTests.MockHttp(),
                     current_time=123456.7)
     self.assertEqual(cache['current_version'], '1.2.0')
     self.assertEqual(cache['last_checked_version'], '1.3.0')
     self.assertEqual(cache['last_tar_url'], 'download/gcutil-1.3.0.tar.gz')
     self.assertEqual(cache['last_zip_url'], 'download/gcutil-1.3.0.zip')
     self.assertEqual(cache['last_check'], 123456.7)
    def testNewVersionExistsWithNonStaleCache(self):
        def MockReadCache():
            return {
                'last_checked_version': '1.3.0',
                'last_check': 3000.0,
                'current_version': '1.2.0',
                'last_tar_url': 'download/gcutil-1.3.0.tar.gz',
                'last_zip_url': 'download/gcutil-1.3.0.zip'
            }

        def MockIsCacheStale(cache):
            return False

        vc = version_checker.VersionChecker(current_version='1.2.0')
        vc._ReadCache = MockReadCache
        vc._IsCacheStale = MockIsCacheStale
        available, _, _ = vc._NewVersionExists()
        self.assertTrue(available)

        vc = version_checker.VersionChecker(current_version='1.3.0')
        vc._ReadCache = MockReadCache
        vc._IsCacheStale = MockIsCacheStale
        available, _, _ = vc._NewVersionExists()
        self.assertFalse(available)
    def testWriteToCache(self):
        handle, cache_file = tempfile.mkstemp()
        try:
            cache = {
                'last_checked_version': '1.2',
                'last_check': 3000,
                'last_tar_url': 'download/gcutil-1.2.tar.gz',
                'last_zip_url': 'download/gcutil-1.2.zip'
            }
            vc = version_checker.VersionChecker(cache_path=cache_file)
            vc._WriteToCache(cache)

            with os.fdopen(handle) as f:
                self.assertEqual(json.load(f), cache)
        finally:
            os.remove(cache_file)
    def testReadCacheWithMalformedJson(self):
        malformed_caches = (
            'MALFORMED JSON',
            '{ "last_checked_version": "1.2", "last_check": 3000.0, }',
            '{ "last_checked_version": "1.2", "last_check": 3000.0 }}',
            '{ "last_checked_version": "1.2", "last_check": 3000.0 }bad characters'
        )

        for malformed_cache in malformed_caches:
            handle, bad_cache_file = tempfile.mkstemp()
            try:
                with os.fdopen(handle, 'w') as f:
                    f.write(malformed_cache)

                vc = version_checker.VersionChecker(cache_path=bad_cache_file)
                self.assertEqual(vc._ReadCache(), {})
            finally:
                os.remove(bad_cache_file)
    def testReadCacheWithGoodCache(self):
        handle, cache_file = tempfile.mkstemp()
        try:
            with os.fdopen(handle, 'w') as f:
                f.write(
                    '{ "last_checked_version": "1.2", "last_check": 3000.0, '
                    ' "last_tar_url": "download/gcutil-1.2.tar.gz", '
                    ' "last_zip_url": "download/gcutil-1.2.zip" }')

            vc = version_checker.VersionChecker(cache_path=cache_file)
            self.assertEqual(
                vc._ReadCache(), {
                    'last_checked_version': '1.2',
                    'last_check': 3000,
                    'last_tar_url': 'download/gcutil-1.2.tar.gz',
                    'last_zip_url': 'download/gcutil-1.2.zip'
                })
        finally:
            os.remove(cache_file)
    def testNewVersionExistsWithStaleCache(self):
        def MockIsCacheStale(cache):
            return True

        def MockUpdateCache(cache):
            cache['last_checked_version'] = '1.3.0'
            cache['last_tar_url'] = 'download/gcutil-1.3.0.tar.gz'
            cache['last_zip_url'] = 'download/gcutil-1.3.0.zip'

        handle, cache_file = tempfile.mkstemp()
        try:
            with os.fdopen(handle, 'w') as f:
                f.write(
                    '{ "last_checked_version": "1.2.0", "last_check": 3000.0,'
                    '   "current_version": "1.2.0",'
                    '   "last_tar_url": "download/gcutil-1.2.0.tar.gz",'
                    '   "last_zip_url": "download/gcutil-1.2.0.zip"}')

                vc = version_checker.VersionChecker(cache_path=cache_file,
                                                    current_version='1.2.0')
                vc._IsCacheStale = MockIsCacheStale
                vc._UpdateCache = MockUpdateCache
                available, _, _ = vc._NewVersionExists()
                self.assertTrue(available)
                self.assertEqual(vc._ReadCache()['last_checked_version'],
                                 '1.3.0')

                def MockUpdateCache2(cache):
                    cache['last_checked_version'] = '1.2.0'

                vc._UpdateCache = MockUpdateCache2
                available, _, _ = vc._NewVersionExists()
                self.assertFalse(available)
                self.assertEqual(vc._ReadCache()['last_checked_version'],
                                 '1.2.0')
        finally:
            os.remove(cache_file)
Ejemplo n.º 10
0
def main(unused_argv):
    # The real work is performed by the appcommands.Run() method, which
    # first invokes this method, and then runs the specified command.

    # Set up early logging configuration
    format_string = '%(levelname)s: %(message)s'
    logging.basicConfig(stream=sys.stderr, format=format_string)

    # Next, register all the commands.
    address_cmds.AddCommands()
    basic_cmds.AddCommands()
    disk_cmds.AddCommands()
    disk_type_cmds.AddCommands()
    firewall_cmds.AddCommands()
    image_cmds.AddCommands()
    instance_cmds.AddCommands()
    machine_type_cmds.AddCommands()
    move_cmds.AddCommands()
    network_cmds.AddCommands()
    operation_cmds.AddCommands()
    project_cmds.AddCommands()
    route_cmds.AddCommands()
    snapshot_cmds.AddCommands()
    whoami.AddCommands()
    zone_cmds.AddCommands()

    target_pool_cmds.AddCommands()
    forwarding_rule_cmds.AddCommands()
    http_health_check_cmds.AddCommands()
    target_instance_cmds.AddCommands()

    region_cmds.AddCommands()

    # Registers the version checker.
    vc = version_checker.VersionChecker()
    atexit.register(vc.CheckForNewVersion)
 def testReadCacheWithNonExistentCacheFile(self):
     vc = version_checker.VersionChecker(cache_path='non-existent-file')
     self.assertEqual(vc._ReadCache(), {})