def test_branch_exceptions(self): """ This wil create conditions to exercise bad paths in the switch_branch function. """ # create bare repo that we can mess with and attempt an import bare_repo = os.path.abspath('{0}/{1}'.format(settings.TEST_ROOT, 'bare.git')) os.mkdir(bare_repo) self.addCleanup(shutil.rmtree, bare_repo) subprocess.check_output([ 'git', '--bare', 'init', ], stderr=subprocess.STDOUT, cwd=bare_repo) # Build repo dir repo_dir = self.GIT_REPO_DIR if not os.path.isdir(repo_dir): os.mkdir(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) rdir = '{0}/bare'.format(repo_dir) with self.assertRaisesRegexp(GitImportError, GitImportError.BAD_REPO): git_import.add_repo('file://{0}'.format(bare_repo), None, None) # Get logger for checking strings in logs output = StringIO.StringIO() test_log_handler = logging.StreamHandler(output) test_log_handler.setLevel(logging.DEBUG) glog = git_import.log glog.addHandler(test_log_handler) # Move remote so fetch fails shutil.move(bare_repo, '{0}/not_bare.git'.format(settings.TEST_ROOT)) try: git_import.switch_branch('master', rdir) except GitImportError: self.assertIn('Unable to fetch remote', output.getvalue()) shutil.move('{0}/not_bare.git'.format(settings.TEST_ROOT), bare_repo) output.truncate(0) # Replace origin with a different remote subprocess.check_output([ 'git', 'remote', 'rename', 'origin', 'blah', ], stderr=subprocess.STDOUT, cwd=rdir) with self.assertRaises(GitImportError): git_import.switch_branch('master', rdir) self.assertIn('Getting a list of remote branches failed', output.getvalue())
def import_mongo_course(self, gitloc, branch): """ Imports course using management command and captures logging output at debug level for display in template """ msg = u"" log.debug("Adding course using git repo %s", gitloc) # Grab logging output for debugging imports output = StringIO.StringIO() import_log_handler = logging.StreamHandler(output) import_log_handler.setLevel(logging.DEBUG) logger_names = [ "xmodule.modulestore.xml_importer", "dashboard.git_import", "xmodule.modulestore.xml", "xmodule.seq_module", ] loggers = [] for logger_name in logger_names: logger = logging.getLogger(logger_name) logger.setLevel(logging.DEBUG) logger.addHandler(import_log_handler) loggers.append(logger) error_msg = "" try: git_import.add_repo(gitloc, None, branch) except GitImportError as ex: error_msg = str(ex) ret = output.getvalue() # Remove handler hijacks for logger in loggers: logger.setLevel(logging.NOTSET) logger.removeHandler(import_log_handler) if error_msg: msg_header = error_msg color = "red" else: msg_header = _("Added Course") color = "blue" msg = u"<h4 style='color:{0}'>{1}</h4>".format(color, msg_header) msg += u"<pre>{0}</pre>".format(escape(ret)) return msg
def import_mongo_course(self, gitloc): """ Imports course using management command and captures logging output at debug level for display in template """ msg = u'' log.debug('Adding course using git repo {0}'.format(gitloc)) # Grab logging output for debugging imports output = StringIO.StringIO() import_log_handler = logging.StreamHandler(output) import_log_handler.setLevel(logging.DEBUG) logger_names = [ 'xmodule.modulestore.xml_importer', 'dashboard.git_import', 'xmodule.modulestore.xml', 'xmodule.seq_module', ] loggers = [] for logger_name in logger_names: logger = logging.getLogger(logger_name) logger.setLevel(logging.DEBUG) logger.addHandler(import_log_handler) loggers.append(logger) error_msg = '' try: git_import.add_repo(gitloc, None) except GitImportError as ex: error_msg = str(ex) ret = output.getvalue() # Remove handler hijacks for logger in loggers: logger.setLevel(logging.NOTSET) logger.removeHandler(import_log_handler) if error_msg: msg_header = error_msg color = 'red' else: msg_header = _('Added Course') color = 'blue' msg = u"<h4 style='color:{0}'>{1}</h4>".format(color, msg_header) msg += "<pre>{0}</pre>".format(escape(ret)) return msg
def test_branching(self): """ Exercise branching code of import """ repo_dir = self.GIT_REPO_DIR # Test successful import from command if not os.path.isdir(repo_dir): os.mkdir(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) # Checkout non existent branch with self.assertRaisesRegexp(GitImportError, GitImportError.REMOTE_BRANCH_MISSING): git_import.add_repo(self.TEST_REPO, repo_dir / "edx4edx_lite", "asdfasdfasdf") # Checkout new branch git_import.add_repo(self.TEST_REPO, repo_dir / "edx4edx_lite", self.TEST_BRANCH) def_ms = modulestore() # Validate that it is different than master self.assertIsNotNone(def_ms.get_course(self.TEST_BRANCH_COURSE)) # Attempt to check out the same branch again to validate branch choosing # works git_import.add_repo(self.TEST_REPO, repo_dir / "edx4edx_lite", self.TEST_BRANCH) # Delete to test branching back to master def_ms.delete_course(self.TEST_BRANCH_COURSE, ModuleStoreEnum.UserID.test) self.assertIsNone(def_ms.get_course(self.TEST_BRANCH_COURSE)) git_import.add_repo(self.TEST_REPO, repo_dir / "edx4edx_lite", "master") self.assertIsNone(def_ms.get_course(self.TEST_BRANCH_COURSE)) self.assertIsNotNone(def_ms.get_course(SlashSeparatedCourseKey.from_deprecated_string(self.TEST_COURSE)))
def test_add_repo(self): """ Various exit path tests for test_add_repo """ with self.assertRaises(GitImportErrorNoDir): git_import.add_repo(self.TEST_REPO, None, None) os.mkdir(self.git_repo_dir) self.addCleanup(shutil.rmtree, self.git_repo_dir) with self.assertRaises(GitImportErrorUrlBad): git_import.add_repo('foo', None, None) with self.assertRaises(GitImportErrorCannotPull): git_import.add_repo('file:///foobar.git', None, None) # Test git repo that exists, but is "broken" bare_repo = os.path.abspath('{0}/{1}'.format(settings.TEST_ROOT, 'bare.git')) os.mkdir(bare_repo) self.addCleanup(shutil.rmtree, bare_repo) subprocess.check_output(['git', '--bare', 'init', ], stderr=subprocess.STDOUT, cwd=bare_repo) with self.assertRaises(GitImportErrorBadRepo): git_import.add_repo('file://{0}'.format(bare_repo), None, None)
def test_add_repo(self): """ Various exit path tests for test_add_repo """ with self.assertRaisesRegexp(GitImportError, GitImportError.NO_DIR): git_import.add_repo(self.TEST_REPO, None, None) os.mkdir(self.GIT_REPO_DIR) self.addCleanup(shutil.rmtree, self.GIT_REPO_DIR) with self.assertRaisesRegexp(GitImportError, GitImportError.URL_BAD): git_import.add_repo('foo', None, None) with self.assertRaisesRegexp(GitImportError, GitImportError.CANNOT_PULL): git_import.add_repo('file:///foobar.git', None, None) # Test git repo that exists, but is "broken" bare_repo = os.path.abspath('{0}/{1}'.format(settings.TEST_ROOT, 'bare.git')) os.mkdir(bare_repo) self.addCleanup(shutil.rmtree, bare_repo) subprocess.check_output(['git', '--bare', 'init', ], stderr=subprocess.STDOUT, cwd=bare_repo) with self.assertRaisesRegexp(GitImportError, GitImportError.BAD_REPO): git_import.add_repo('file://{0}'.format(bare_repo), None, None)
def test_add_repo(self): """ Various exit path tests for test_add_repo """ with self.assertRaisesRegexp(GitImportError, GitImportError.NO_DIR): git_import.add_repo(self.TEST_REPO, None, None) os.mkdir(self.GIT_REPO_DIR) self.addCleanup(shutil.rmtree, self.GIT_REPO_DIR) with self.assertRaisesRegexp(GitImportError, GitImportError.URL_BAD): git_import.add_repo('foo', None, None) with self.assertRaisesRegexp(GitImportError, GitImportError.CANNOT_PULL): git_import.add_repo('file:///foobar.git', None, None) # Test git repo that exists, but is "broken" bare_repo = os.path.abspath('{0}/{1}'.format(settings.TEST_ROOT, 'bare.git')) os.mkdir(bare_repo) self.addCleanup(shutil.rmtree, bare_repo) subprocess.check_output([ 'git', '--bare', 'init', ], stderr=subprocess.STDOUT, cwd=bare_repo) with self.assertRaisesRegexp(GitImportError, GitImportError.BAD_REPO): git_import.add_repo('file://{0}'.format(bare_repo), None, None)
def test_add_repo(self): """ Various exit path tests for test_add_repo """ with self.assertRaises(GitImportErrorNoDir): git_import.add_repo(self.TEST_REPO, None, None) os.mkdir(self.git_repo_dir) self.addCleanup(shutil.rmtree, self.git_repo_dir) with self.assertRaises(GitImportErrorUrlBad): git_import.add_repo('foo', None, None) with self.assertRaises(GitImportErrorCannotPull): git_import.add_repo('file:///foobar.git', None, None) # Test git repo that exists, but is "broken" bare_repo = os.path.abspath('{0}/{1}'.format(settings.TEST_ROOT, 'bare.git')) os.mkdir(bare_repo) self.addCleanup(shutil.rmtree, bare_repo) subprocess.check_output([ 'git', '--bare', 'init', ], stderr=subprocess.STDOUT, cwd=bare_repo) with self.assertRaises(GitImportErrorBadRepo): git_import.add_repo('file://{0}'.format(bare_repo), None, None)
def test_detached_repo(self): """ Test repo that is in detached head state. """ repo_dir = self.GIT_REPO_DIR # Test successful import from command try: os.mkdir(repo_dir) except OSError: pass self.addCleanup(shutil.rmtree, repo_dir) git_import.add_repo(self.TEST_REPO, repo_dir / "edx4edx_lite", None) subprocess.check_output(["git", "checkout", "HEAD~2"], stderr=subprocess.STDOUT, cwd=repo_dir / "edx4edx_lite") with self.assertRaisesRegexp(GitImportError, GitImportError.CANNOT_PULL): git_import.add_repo(self.TEST_REPO, repo_dir / "edx4edx_lite", None)
def test_detached_repo(self): """ Test repo that is in detached head state. """ repo_dir = getattr(settings, 'GIT_REPO_DIR') # Test successful import from command try: os.mkdir(repo_dir) except OSError: pass self.addCleanup(shutil.rmtree, repo_dir) git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite') subprocess.check_output(['git', 'checkout', 'HEAD~2', ], stderr=subprocess.STDOUT, cwd=repo_dir / 'edx4edx_lite') with self.assertRaisesRegexp(GitImportError, GitImportError.CANNOT_PULL): git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite')
def test_detached_repo(self): """ Test repo that is in detached head state. """ repo_dir = self.git_repo_dir # Test successful import from command try: os.mkdir(repo_dir) except OSError: pass self.addCleanup(shutil.rmtree, repo_dir) git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', None) subprocess.check_output(['git', 'checkout', 'HEAD~2', ], stderr=subprocess.STDOUT, cwd=repo_dir / 'edx4edx_lite') with self.assertRaises(GitImportErrorCannotPull): git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', None)
def test_detached_repo(self): """ Test repo that is in detached head state. """ repo_dir = self.GIT_REPO_DIR # Test successful import from command try: os.mkdir(repo_dir) except OSError: pass self.addCleanup(shutil.rmtree, repo_dir) git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', None) subprocess.check_output(['git', 'checkout', 'HEAD~2', ], stderr=subprocess.STDOUT, cwd=repo_dir / 'edx4edx_lite') with self.assertRaisesRegexp(GitImportError, GitImportError.CANNOT_PULL): git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', None)
def test_branch_exceptions(self): """ This wil create conditions to exercise bad paths in the switch_branch function. """ # create bare repo that we can mess with and attempt an import bare_repo = os.path.abspath('{0}/{1}'.format(settings.TEST_ROOT, 'bare.git')) os.mkdir(bare_repo) self.addCleanup(shutil.rmtree, bare_repo) subprocess.check_output(['git', '--bare', 'init', ], stderr=subprocess.STDOUT, cwd=bare_repo) # Build repo dir repo_dir = self.git_repo_dir if not os.path.isdir(repo_dir): os.mkdir(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) rdir = '{0}/bare'.format(repo_dir) with self.assertRaises(GitImportErrorBadRepo): git_import.add_repo('file://{0}'.format(bare_repo), None, None) # Get logger for checking strings in logs output = StringIO.StringIO() test_log_handler = logging.StreamHandler(output) test_log_handler.setLevel(logging.DEBUG) glog = git_import.log glog.addHandler(test_log_handler) # Move remote so fetch fails shutil.move(bare_repo, '{0}/not_bare.git'.format(settings.TEST_ROOT)) try: git_import.switch_branch('master', rdir) except GitImportError: self.assertIn('Unable to fetch remote', output.getvalue()) shutil.move('{0}/not_bare.git'.format(settings.TEST_ROOT), bare_repo) output.truncate(0) # Replace origin with a different remote subprocess.check_output( ['git', 'remote', 'rename', 'origin', 'blah', ], stderr=subprocess.STDOUT, cwd=rdir ) with self.assertRaises(GitImportError): git_import.switch_branch('master', rdir) self.assertIn('Getting a list of remote branches failed', output.getvalue())
def test_branch_exceptions(self): """ This wil create conditions to exercise bad paths in the switch_branch function. """ # create bare repo that we can mess with and attempt an import bare_repo = os.path.abspath("{0}/{1}".format(settings.TEST_ROOT, "bare.git")) os.mkdir(bare_repo) self.addCleanup(shutil.rmtree, bare_repo) subprocess.check_output(["git", "--bare", "init"], stderr=subprocess.STDOUT, cwd=bare_repo) # Build repo dir repo_dir = self.GIT_REPO_DIR if not os.path.isdir(repo_dir): os.mkdir(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) rdir = "{0}/bare".format(repo_dir) with self.assertRaisesRegexp(GitImportError, GitImportError.BAD_REPO): git_import.add_repo("file://{0}".format(bare_repo), None, None) # Get logger for checking strings in logs output = StringIO.StringIO() test_log_handler = logging.StreamHandler(output) test_log_handler.setLevel(logging.DEBUG) glog = git_import.log glog.addHandler(test_log_handler) # Move remote so fetch fails shutil.move(bare_repo, "{0}/not_bare.git".format(settings.TEST_ROOT)) try: git_import.switch_branch("master", rdir) except GitImportError: self.assertIn("Unable to fetch remote", output.getvalue()) shutil.move("{0}/not_bare.git".format(settings.TEST_ROOT), bare_repo) output.truncate(0) # Replace origin with a different remote subprocess.check_output(["git", "remote", "rename", "origin", "blah"], stderr=subprocess.STDOUT, cwd=rdir) with self.assertRaises(GitImportError): git_import.switch_branch("master", rdir) self.assertIn("Getting a list of remote branches failed", output.getvalue())
def test_branching(self): """ Exercise branching code of import """ repo_dir = self.GIT_REPO_DIR # Test successful import from command if not os.path.isdir(repo_dir): os.mkdir(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) # Checkout non existent branch with self.assertRaisesRegexp(GitImportError, GitImportError.REMOTE_BRANCH_MISSING): git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', 'asdfasdfasdf') # Checkout new branch git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', self.TEST_BRANCH) def_ms = modulestore() # Validate that it is different than master self.assertIsNotNone(def_ms.get_course(self.TEST_BRANCH_COURSE)) # Attempt to check out the same branch again to validate branch choosing # works git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', self.TEST_BRANCH) # Delete to test branching back to master def_ms.delete_course(self.TEST_BRANCH_COURSE, ModuleStoreEnum.UserID.test) self.assertIsNone(def_ms.get_course(self.TEST_BRANCH_COURSE)) git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', 'master') self.assertIsNone(def_ms.get_course(self.TEST_BRANCH_COURSE)) self.assertIsNotNone( def_ms.get_course( SlashSeparatedCourseKey.from_deprecated_string( self.TEST_COURSE)))
def test_branching(self): """ Exercise branching code of import """ repo_dir = self.GIT_REPO_DIR # Test successful import from command if not os.path.isdir(repo_dir): os.mkdir(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) # Checkout non existent branch with self.assertRaisesRegexp(GitImportError, GitImportError.REMOTE_BRANCH_MISSING): git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', 'asdfasdfasdf') # Checkout new branch git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', self.TEST_BRANCH) def_ms = modulestore() # Validate that it is different than master self.assertIsNotNone(def_ms.get_course(self.TEST_BRANCH_COURSE)) # Attempt to check out the same branch again to validate branch choosing # works git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', self.TEST_BRANCH) # Delete to test branching back to master delete_course(def_ms, contentstore(), def_ms.get_course(self.TEST_BRANCH_COURSE).location, True) self.assertIsNone(def_ms.get_course(self.TEST_BRANCH_COURSE)) git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', 'master') self.assertIsNone(def_ms.get_course(self.TEST_BRANCH_COURSE)) self.assertIsNotNone(def_ms.get_course(self.TEST_COURSE))
def test_branching(self): """ Exercise branching code of import """ repo_dir = self.git_repo_dir # Test successful import from command if not os.path.isdir(repo_dir): os.mkdir(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) # Checkout non existent branch with self.assertRaises(GitImportErrorRemoteBranchMissing): git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', 'asdfasdfasdf') # Checkout new branch git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', self.TEST_BRANCH) def_ms = modulestore() # Validate that it is different than master self.assertIsNotNone(def_ms.get_course(self.TEST_BRANCH_COURSE)) # Attempt to check out the same branch again to validate branch choosing # works git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', self.TEST_BRANCH) # Delete to test branching back to master def_ms.delete_course(self.TEST_BRANCH_COURSE, ModuleStoreEnum.UserID.test) self.assertIsNone(def_ms.get_course(self.TEST_BRANCH_COURSE)) git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', 'master') self.assertIsNone(def_ms.get_course(self.TEST_BRANCH_COURSE)) self.assertIsNotNone(def_ms.get_course(CourseKey.from_string(self.TEST_COURSE)))