cron_servlet_test.py revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
1#!/usr/bin/env python 2# Copyright 2013 The Chromium Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import unittest 7 8from appengine_wrappers import GetAppVersion 9from app_yaml_helper import AppYamlHelper 10from cron_servlet import CronServlet 11from empty_dir_file_system import EmptyDirFileSystem 12from host_file_system_creator import HostFileSystemCreator 13from local_file_system import LocalFileSystem 14from mock_file_system import MockFileSystem 15from servlet import Request 16from test_branch_utility import TestBranchUtility 17from test_file_system import TestFileSystem 18from test_util import EnableLogging 19 20# NOTE(kalman): The ObjectStore created by the CronServlet is backed onto our 21# fake AppEngine memcache/datastore, so the tests aren't isolated. Of course, 22# if the host file systems have different identities, they will be, sort of. 23class _TestDelegate(CronServlet.Delegate): 24 def __init__(self, create_file_system): 25 self.file_systems = [] 26 # A callback taking a revision and returning a file system. 27 self._create_file_system = create_file_system 28 self._app_version = GetAppVersion() 29 30 def CreateBranchUtility(self, object_store_creator): 31 return TestBranchUtility.CreateWithCannedData() 32 33 def CreateHostFileSystemCreator(self, object_store_creator): 34 def constructor(branch=None, revision=None): 35 file_system = self._create_file_system(revision) 36 self.file_systems.append(file_system) 37 return file_system 38 return HostFileSystemCreator(object_store_creator, 39 constructor_for_test=constructor) 40 41 def CreateAppSamplesFileSystem(self, object_store_creator): 42 return EmptyDirFileSystem() 43 44 def GetAppVersion(self): 45 return self._app_version 46 47 # (non-Delegate method). 48 def SetAppVersion(self, app_version): 49 self._app_version = app_version 50 51class CronServletTest(unittest.TestCase): 52 @EnableLogging('info') 53 def testEverything(self): 54 # All these tests are dependent (see above comment) so lump everything in 55 # the one test. 56 delegate = _TestDelegate(lambda _: MockFileSystem(LocalFileSystem.Create())) 57 58 # Test that the cron runs successfully. 59 response = CronServlet(Request.ForTest('trunk'), 60 delegate_for_test=delegate).Get() 61 self.assertEqual(200, response.status) 62 63 # Save the file systems created, start with a fresh set for the next run. 64 first_run_file_systems = delegate.file_systems[:] 65 delegate.file_systems[:] = [] 66 67 # When re-running, all file systems should be Stat()d the same number of 68 # times, but the second round shouldn't have been re-Read() since the 69 # Stats haven't changed. 70 response = CronServlet(Request.ForTest('trunk'), 71 delegate_for_test=delegate).Get() 72 self.assertEqual(200, response.status) 73 74 self.assertEqual(len(first_run_file_systems), len(delegate.file_systems)) 75 for i, second_run_file_system in enumerate(delegate.file_systems): 76 self.assertTrue(*second_run_file_system.CheckAndReset( 77 read_count=0, 78 stat_count=first_run_file_systems[i].GetStatCount())) 79 80 def testSafeRevision(self): 81 test_data = { 82 'api': { 83 '_manifest_features.json': '{}' 84 }, 85 'docs': { 86 'examples': { 87 'examples.txt': 'examples.txt contents' 88 }, 89 'server2': { 90 'app.yaml': AppYamlHelper.GenerateAppYaml('2-0-8') 91 }, 92 'static': { 93 'static.txt': 'static.txt contents' 94 }, 95 'templates': { 96 'public': { 97 'apps': { 98 'storage.html': 'storage.html contents' 99 }, 100 'extensions': { 101 'storage.html': 'storage.html contents' 102 }, 103 }, 104 'json': { 105 'manifest.json': '{}', 106 'strings.json': '{}' 107 }, 108 } 109 } 110 } 111 112 updates = [] 113 114 def app_yaml_update(version): 115 return {'docs': {'server2': { 116 'app.yaml': AppYamlHelper.GenerateAppYaml(version) 117 }}} 118 def storage_html_update(update): 119 return {'docs': {'templates': {'public': {'apps': { 120 'storage.html': update 121 }}}}} 122 def static_txt_update(update): 123 return {'docs': {'static': { 124 'static.txt': update 125 }}} 126 127 app_yaml_path = 'docs/server2/app.yaml' 128 storage_html_path = 'docs/templates/public/apps/storage.html' 129 static_txt_path = 'docs/static/static.txt' 130 131 def create_file_system(revision=None): 132 '''Creates a MockFileSystem at |revision| by applying that many |updates| 133 to it. 134 ''' 135 mock_file_system = MockFileSystem(TestFileSystem(test_data)) 136 for update in updates[:revision]: 137 mock_file_system.Update(update) 138 return mock_file_system 139 140 delegate = _TestDelegate(create_file_system) 141 delegate.SetAppVersion('2-0-8') 142 143 file_systems = delegate.file_systems 144 145 # No updates applied yet. 146 CronServlet(Request.ForTest('trunk'), delegate_for_test=delegate).Get() 147 self.assertEqual(AppYamlHelper.GenerateAppYaml('2-0-8'), 148 file_systems[-1].ReadSingle(app_yaml_path)) 149 self.assertEqual('storage.html contents', 150 file_systems[-1].ReadSingle(storage_html_path)) 151 152 # Apply updates to storage.html. 153 updates.append(storage_html_update('interim contents')) 154 updates.append(storage_html_update('new contents')) 155 156 CronServlet(Request.ForTest('trunk'), delegate_for_test=delegate).Get() 157 self.assertEqual(AppYamlHelper.GenerateAppYaml('2-0-8'), 158 file_systems[-1].ReadSingle(app_yaml_path)) 159 self.assertEqual('new contents', 160 file_systems[-1].ReadSingle(storage_html_path)) 161 162 # Apply several updates to storage.html and app.yaml. The file system 163 # should be pinned at the version before app.yaml changed. 164 updates.append(storage_html_update('stuck here contents')) 165 166 double_update = storage_html_update('newer contents') 167 double_update.update(app_yaml_update('2-0-10')) 168 updates.append(double_update) 169 170 updates.append(storage_html_update('never gonna reach here')) 171 172 CronServlet(Request.ForTest('trunk'), delegate_for_test=delegate).Get() 173 self.assertEqual(AppYamlHelper.GenerateAppYaml('2-0-8'), 174 file_systems[-1].ReadSingle(app_yaml_path)) 175 self.assertEqual('stuck here contents', 176 file_systems[-1].ReadSingle(storage_html_path)) 177 178 # Further pushes to storage.html will keep it pinned. 179 updates.append(storage_html_update('y u not update!')) 180 181 CronServlet(Request.ForTest('trunk'), delegate_for_test=delegate).Get() 182 self.assertEqual(AppYamlHelper.GenerateAppYaml('2-0-8'), 183 file_systems[-1].ReadSingle(app_yaml_path)) 184 self.assertEqual('stuck here contents', 185 file_systems[-1].ReadSingle(storage_html_path)) 186 187 # Likewise app.yaml. 188 updates.append(app_yaml_update('2-1-0')) 189 190 CronServlet(Request.ForTest('trunk'), delegate_for_test=delegate).Get() 191 self.assertEqual(AppYamlHelper.GenerateAppYaml('2-0-8'), 192 file_systems[-1].ReadSingle(app_yaml_path)) 193 self.assertEqual('stuck here contents', 194 file_systems[-1].ReadSingle(storage_html_path)) 195 196 # And updates to other content won't happen either. 197 updates.append(static_txt_update('important content!')) 198 199 CronServlet(Request.ForTest('trunk'), delegate_for_test=delegate).Get() 200 self.assertEqual(AppYamlHelper.GenerateAppYaml('2-0-8'), 201 file_systems[-1].ReadSingle(app_yaml_path)) 202 self.assertEqual('stuck here contents', 203 file_systems[-1].ReadSingle(storage_html_path)) 204 self.assertEqual('static.txt contents', 205 file_systems[-1].ReadSingle(static_txt_path)) 206 207 # Lastly - when the app version changes, everything should no longer be 208 # pinned. 209 delegate.SetAppVersion('2-1-0') 210 CronServlet(Request.ForTest('trunk'), delegate_for_test=delegate).Get() 211 self.assertEqual(AppYamlHelper.GenerateAppYaml('2-1-0'), 212 file_systems[-1].ReadSingle(app_yaml_path)) 213 self.assertEqual('y u not update!', 214 file_systems[-1].ReadSingle(storage_html_path)) 215 self.assertEqual('important content!', 216 file_systems[-1].ReadSingle(static_txt_path)) 217 218if __name__ == '__main__': 219 unittest.main() 220