def test_import_revision(self): self.mock_get_archive() gitiles_import._import_revision( 'config_set', gitiles.Location( hostname='localhost', project='project', treeish='luci/config', path='/', ), self.test_commit, False) expected_latest_revision_url = ( 'https://localhost/project/+/a1841f40264376d170269ee9473ce924b7c2c4e9' ) gitiles.get_archive.assert_called_once_with( 'localhost', 'project', 'a1841f40264376d170269ee9473ce924b7c2c4e9', '/', deadline=15) saved_config_set = storage.ConfigSet.get_by_id('config_set') self.assertIsNotNone(saved_config_set) self.assertEqual(saved_config_set.latest_revision, self.test_commit.sha) self.assertEqual(saved_config_set.location, 'https://localhost/project/+/luci/config') self.assertEqual(saved_config_set.latest_revision_url, expected_latest_revision_url) saved_revision = storage.Revision.get_by_id( self.test_commit.sha, parent=saved_config_set.key) self.assertIsNotNone(saved_revision) saved_file = storage.File.get_by_id('test_archive/x', parent=saved_revision.key) self.assertIsNotNone(saved_file) self.assertEqual(saved_file.content_hash, 'v1:587be6b4c3f93f93c489c0111bba5596147a26cb') self.assertEqual( saved_file.url, os.path.join(expected_latest_revision_url, 'test_archive/x')) saved_blob = storage.Blob.get_by_id(saved_file.content_hash) self.assertIsNotNone(saved_blob) self.assertEqual(saved_blob.content, 'x\n') self.assert_attempt(True, 'Imported') # Run second time, assert nothing is fetched from gitiles. ndb.Key(storage.ConfigSet, 'config_set').delete() gitiles.get_archive.reset_mock() gitiles_import._import_revision( 'config_set', gitiles.Location(hostname='localhost', project='project', treeish='master', path='/'), self.test_commit, False) self.assertFalse(gitiles.get_archive.called) self.assert_attempt(True, 'Up-to-date')
def test_location_neq(self): loc1 = gitiles.Location( hostname='localhost', project='project', treeish='treeish', path='/path') loc2 = gitiles.Location( hostname='localhost', project='project', treeish='treeish', path='/path') self.assertFalse(loc1.__ne__(loc2))
def test_import_revision(self): self.mock_get_archive() gitiles_import.import_revision( 'config_set', gitiles.Location( hostname='localhost', project='project', treeish='a1841f40264376d170269ee9473ce924b7c2c4e9', path='/', ), create_config_set=True) gitiles.get_archive.assert_called_once_with( 'localhost', 'project', 'a1841f40264376d170269ee9473ce924b7c2c4e9', '/', deadline=15) saved_config_set = storage.ConfigSet.get_by_id('config_set') self.assertIsNotNone(saved_config_set) self.assertEqual(saved_config_set.latest_revision, 'a1841f40264376d170269ee9473ce924b7c2c4e9') saved_revision = storage.Revision.get_by_id( 'a1841f40264376d170269ee9473ce924b7c2c4e9', parent=saved_config_set.key) self.assertIsNotNone(saved_revision) saved_file = storage.File.get_by_id('test_archive/x', parent=saved_revision.key) self.assertIsNotNone(saved_file) self.assertEqual(saved_file.content_hash, 'v1:587be6b4c3f93f93c489c0111bba5596147a26cb') saved_blob = storage.Blob.get_by_id(saved_file.content_hash) self.assertIsNotNone(saved_blob) self.assertEqual(saved_blob.content, 'x\n') # Run second time, assert nothing is fetched from gitiles. ndb.Key(storage.ConfigSet, 'config_set').delete() gitiles.get_archive.reset_mock() gitiles_import.import_revision( 'config_set', gitiles.Location( hostname='localhost', project='project', treeish='a1841f40264376d170269ee9473ce924b7c2c4e9', path='/'), create_config_set=True) self.assertFalse(gitiles.get_archive.called)
def test_revision_revision_exists(self): self.mock(gitiles, 'get_archive', mock.Mock()) with open(TEST_ARCHIVE_PATH, 'r') as test_archive_file: gitiles.get_archive.return_value = test_archive_file.read() loc = gitiles.Location(hostname='localhost', project='project', treeish='master', path='/') cs = storage.ConfigSet( id='config_set', latest_revision=None, location=str(loc), ) rev = storage.Revision( parent=cs.key, id='deadbeef', ) ndb.put_multi([cs, rev]) gitiles_import._import_revision('config_set', loc, self.test_commit, False) cs_fresh = cs.key.get() self.assertEqual(cs_fresh.latest_revision, self.test_commit.sha)
def test_import_invalid_revision(self): self.mock_get_archive() self.mock(notifications, 'notify_gitiles_rejection', mock.Mock()) def validate_config(config_set, filename, content, ctx): if filename == 'test_archive/x': ctx.error('bad config!') self.mock(validation, 'validate_config', validate_config) gitiles_import._import_revision( 'config_set', gitiles.Location( hostname='localhost', project='project', treeish='master', path='/'), self.test_commit) # Assert not saved. self.assertIsNone(storage.ConfigSet.get_by_id('config_set')) saved_attempt = self.assert_attempt(False, 'Validation errors') self.assertEqual(len(saved_attempt.validation_messages), 1) val_msg = saved_attempt.validation_messages[0] self.assertEqual(val_msg.severity, config.Severity.ERROR) self.assertEqual(val_msg.text, 'bad config!')
def _make_gitiles_location(cfg): """Returns gitiles.Location from service_config_pb2.GitilesLocation.""" loc = gitiles.Location.parse(url=cfg.repo) return gitiles.Location(hostname=loc.hostname, project=loc.project, treeish=cfg.ref, path='/' + cfg.path)
def test_import_ref(self): self.mock(gitiles_import, '_import_config_set', mock.Mock()) self.mock(projects, 'get_project', mock.Mock()) self.mock(projects, 'get_refs', mock.Mock()) projects.get_project.return_value = service_config_pb2.Project( id='chromium', gitiles_location=service_config_pb2.GitilesLocation( repo='https://localhost/chromium/src', ref='refs/heads/infra/config', ), ) projects.get_refs.return_value = { 'chromium': [ project_config_pb2.RefsCfg.Ref(name='refs/heads/release42', config_path='/my-configs'), ], } gitiles_import.import_ref('chromium', 'refs/heads/release42') gitiles_import._import_config_set.assert_called_once_with( 'projects/chromium/refs/heads/release42', gitiles.Location( hostname='localhost', project='chromium/src', treeish='refs/heads/release42', path='/my-configs', ), )
def test_import_revision_no_acrhive(self): self.mock(gitiles, 'get_archive', mock.Mock(return_value=None)) gitiles_import.import_revision( 'config_set', gitiles.Location(hostname='localhost', project='project', treeish='master', path='/'), 'a1841f40264376d170269ee9473ce924b7c2c4e9')
def _gitiles_url(configs_url, rev, path): """URL to a directory in gitiles -> URL to a file at concrete revision.""" try: location = gitiles.Location.parse(configs_url) return str( gitiles.Location(hostname=location.hostname, project=location.project, treeish=rev, path=posixpath.join(location.path, path))) except ValueError: # Not a gitiles URL, return as is. return configs_url
def test_import_revision_no_archive(self): self.mock_get_log() self.mock(gitiles, 'get_archive', mock.Mock(return_value=None)) gitiles_import._import_revision( 'config_set', gitiles.Location(hostname='localhost', project='project', treeish='master', path='/'), self.test_commit, False) self.assert_attempt(True, 'Config directory not found. Imported as empty')
def test_import_invalid_revision(self): self.mock_get_archive() self.mock(validation, 'validate_config', mock.Mock(return_value=False)) gitiles_import.import_revision( 'config_set', gitiles.Location(hostname='localhost', project='project', treeish='master', path='/'), 'a1841f40264376d170269ee9473ce924b7c2c4e9') # Assert not saved. self.assertIsNone(storage.ConfigSet.get_by_id('config_set'))
def test_import_service(self): self.mock(gitiles_import, '_import_config_set', mock.Mock()) conf = admin.GlobalConfig( services_config_storage_type=admin.ServiceConfigStorageType. GITILES, services_config_location='https://localhost/config') gitiles_import.import_service('luci-config', conf) gitiles_import._import_config_set.assert_called_once_with( 'services/luci-config', gitiles.Location( hostname='localhost', project='config', treeish='HEAD', path='/luci-config', ), )
def test_import_project(self): self.mock(gitiles_import, '_import_config_set', mock.Mock()) self.mock(projects, 'get_project', mock.Mock()) projects.get_project.return_value = service_config_pb2.Project( id='chromium', config_location=service_config_pb2.ConfigSetLocation( url='https://localhost/chromium/src/+/refs/heads/master', storage_type=service_config_pb2.ConfigSetLocation.GITILES, ), ) gitiles_import.import_project('chromium') gitiles_import._import_config_set.assert_called_once_with( 'projects/chromium', gitiles.Location( hostname='localhost', project='chromium/src', treeish='refs/heads/master', path='/', ), 'chromium')
def test_import_project(self): self.mock(gitiles_import, '_import_config_set', mock.Mock()) self.mock(projects, 'get_project', mock.Mock()) projects.get_project.return_value = service_config_pb2.Project( id='chromium', gitiles_location=service_config_pb2.GitilesLocation( repo='https://localhost/chromium/src', ref='refs/any/thing', path='infra/config/generated', ), ) gitiles_import.import_project('chromium') gitiles_import._import_config_set.assert_called_once_with( 'projects/chromium', gitiles.Location( hostname='localhost', project='chromium/src', treeish='refs/any/thing', path='/infra/config/generated', ), 'chromium')
def test_location_str_defaults_to_head(self): loc = gitiles.Location(hostname='localhost', project='project', treeish=None, path='/path') self.assertEqual(loc, 'https://localhost/project/+/HEAD/path')
def test_location_str_with_slash_path(self): loc = gitiles.Location(hostname='localhost', project='project', treeish='treeish', path='/') self.assertEqual(loc, 'https://localhost/project/+/treeish')