def test_download_tarball_download_directory_does_not_exist(self): """ if the folder in which the tarball is to be stored does not exist, it should be created. """ mock_resource = self.mox.CreateMockAnything() mock_file = self.mox.CreateMockAnything() mock_http_message = self.mox.CreateMockAnything() mock_contents = self.mox.CreateMockAnything() self.mox.StubOutWithMock(Updater, "get_tarball_url") self.mox.StubOutWithMock(urllib2, "urlopen") self.mox.StubOutWithMock(__builtin__, "open") self.mox.StubOutWithMock(os.path, "isdir") self.mox.StubOutWithMock(os, "mkdir") os.path.isdir(self.__update_dir).AndReturn(False) os.mkdir(self.__update_dir) Updater.get_tarball_url(self.__repo).AndReturn(self.__tarball_url) urllib2.urlopen(self.__tarball_url).AndReturn(mock_resource) mock_resource.info().AndReturn(mock_http_message) mock_http_message.get("Content-Disposition").AndReturn(self.__content_string) __builtin__.open(self.__target_filename, "wb").AndReturn(mock_file) mock_file.__enter__().AndReturn(mock_file) mock_file.write(mock_resource.read().AndReturn(mock_contents)) mock_file.__exit__(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()) self.mox.ReplayAll() updater = Updater(repo = self.__repo, update_dir = self.__update_dir) self.assertEquals(self.__target_filename, updater.download_tarball())
def test_download_tarball(self): """ download_tarball should download the tarball found at get_tarball_url, to get_target_directory """ mock_resource = self.mox.CreateMockAnything() mock_file = self.mox.CreateMockAnything() mock_http_message = self.mox.CreateMockAnything() mock_contents = self.mox.CreateMockAnything() self.mox.StubOutWithMock(Updater, "get_tarball_url") self.mox.StubOutWithMock(urllib2, "urlopen") self.mox.StubOutWithMock(__builtin__, "open") self.mox.StubOutWithMock(os.path, "isdir") os.path.isdir(self.__update_dir).AndReturn(True) Updater.get_tarball_url(self.__repo).AndReturn(self.__tarball_url) urllib2.urlopen(self.__tarball_url).AndReturn(mock_resource) mock_resource.info().AndReturn(mock_http_message) mock_http_message.get("Content-Disposition").AndReturn(self.__content_string) __builtin__.open(self.__target_filename, "wb").AndReturn(mock_file) mock_file.__enter__().AndReturn(mock_file) mock_file.write(mock_resource.read().AndReturn(mock_contents)) mock_file.__exit__(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()) self.mox.ReplayAll() updater = Updater(repo = self.__repo, update_dir = self.__update_dir) self.assertEquals(self.__target_filename, updater.download_tarball())
def test_download_tarball_invalid_url(self): """ if urlopen raises URLError, None shall be returned from download_tarball. """ self.mox.StubOutWithMock(Updater, "get_tarball_url") self.mox.StubOutWithMock(urllib2, "urlopen") self.mox.StubOutWithMock(os.path, "isdir") os.path.isdir(self.__update_dir).AndReturn(True) Updater.get_tarball_url(self.__repo).AndReturn(self.__tarball_url) urllib2.urlopen(self.__tarball_url).AndRaise(urllib2.URLError("Nah-ah")) self.mox.ReplayAll() updater = Updater(repo = self.__repo, update_dir = self.__update_dir) self.assertEquals(None, updater.download_tarball())
def test_download_tarball_no_handler(self): """ if urlopen returns None (i.e. no handler), None shall be returned from download_tarball. """ self.mox.StubOutWithMock(Updater, "get_tarball_url") self.mox.StubOutWithMock(urllib2, "urlopen") self.mox.StubOutWithMock(os.path, "isdir") os.path.isdir(self.__update_dir).AndReturn(True) Updater.get_tarball_url(self.__repo).AndReturn(self.__tarball_url) urllib2.urlopen(self.__tarball_url).AndReturn(None) self.mox.ReplayAll() updater = Updater(repo = self.__repo, update_dir = self.__update_dir) self.assertEquals(None, updater.download_tarball())