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 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_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_revision_no_acrhive(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) self.assert_attempt(True, 'Config directory not found. Imported as empty')
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) 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') 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') 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) self.assertFalse(gitiles.get_archive.called) self.assert_attempt(True, 'Up-to-date')