def testNewVersionExistsWithStaleCache(self):
        def MockIsCacheStale(cache):
            return True

        def MockUpdateCache(cache):
            cache['last_checked_version'] = '1.3.0'

        _, cache_file = tempfile.mkstemp()
        try:
            with open(cache_file, 'w') as f:
                f.write(
                    '{ "last_checked_version": "1.2.0", "last_check": 3000.0,'
                    '   "current_version": "1.2.0"}')

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

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

                vc._UpdateCache = MockUpdateCache2
                self.assertFalse(vc._NewVersionExists())
                self.assertEqual(vc._ReadCache()['last_checked_version'],
                                 '1.2.0')
        finally:
            os.remove(cache_file)
    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 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_check'], 123456.7)
    def testWriteToCache(self):
        _, cache_file = tempfile.mkstemp()
        try:
            cache = {'last_checked_version': '1.2', 'last_check': 3000}
            vc = version_checker.VersionChecker(cache_path=cache_file)
            vc._WriteToCache(cache)

            with open(cache_file) as f:
                self.assertEqual(json.load(f), cache)
        finally:
            os.remove(cache_file)
    def testNewVersionExistsWithNonStaleCache(self):
        def MockReadCache():
            return {
                'last_checked_version': '1.3.0',
                'last_check': 3000.0,
                'current_version': '1.2.0'
            }

        def MockIsCacheStale(cache):
            return False

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

        vc = version_checker.VersionChecker(current_version='1.3.0')
        vc._ReadCache = MockReadCache
        vc._IsCacheStale = MockIsCacheStale
        self.assertFalse(vc._NewVersionExists())
    def testReadCacheWithGoodCache(self):
        _, cache_file = tempfile.mkstemp()
        try:
            with open(cache_file, 'w') as f:
                f.write(
                    '{ "last_checked_version": "1.2", "last_check": 3000.0 }')

            vc = version_checker.VersionChecker(cache_path=cache_file)
            self.assertEqual(vc._ReadCache(), {
                'last_checked_version': '1.2',
                'last_check': 3000
            })
        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:
            _, bad_cache_file = tempfile.mkstemp()
            try:
                with open(bad_cache_file, '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 testReadCacheWithNonExistentCacheFile(self):
     vc = version_checker.VersionChecker(cache_path='non-existent-file')
     self.assertEqual(vc._ReadCache(), {})