def testEncodersInMultipleRepos(self): test_tools.EmptyWorkDirectory() context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) context.cache = cache my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) other_dir = os.path.join(encoder_configuration.conf.sysdir(), 'multirepo_test') os.mkdir(other_dir) other_cache = encoder.EncodingDiskCache(context, scoredir='multirepo_test') encoder_configuration.conf.override_scorepath_for_test([other_dir]) other_cache.StoreEncoder(my_encoder) encoders = cache.AllEncoderFilenames(only_workdir=True) self.assertEquals(0, len(encoders)) encoders = cache.AllEncoderFilenames(only_workdir=False) self.assertEquals(1, len(encoders)) fetched_encoder = encoder.Encoder(context, filename=encoders[0]) self.assertEquals(my_encoder.parameters.ToString(), fetched_encoder.parameters.ToString()) my_encoding = encoder.Encoding( my_encoder, 123, encoder.Videofile('x/foo_640_480_20.yuv')) testresult = {'foo': 'bar'} my_encoding.result = testresult other_cache.StoreEncoding(my_encoding) # With a specified directory, we should find it in only one place. self.assertTrue(other_cache.ReadEncodingResult(my_encoding)) # Without a specified directory, we should find it on the searchpath. self.assertTrue(cache.ReadEncodingResult(my_encoding)) # Without a searchpath, we shouldn't find it in the default cache. encoder_configuration.conf.override_scorepath_for_test([]) self.assertFalse(cache.ReadEncodingResult(my_encoding))
def testReadResultEncodedInAst(self): # We've changed the storage format for results from AST to JSON. # This test verifies that AST formatted results are still readable. context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) my_encoding = encoder.Encoding( my_encoder, 123, encoder.Videofile('x/foo_640_480_20.yuv')) testresult = {'foo': 'bar'} my_encoding.result = testresult # The following code is a copy of cache.StoreEncoding, with the # encoding step changed. dirname = '%s/%s/%s' % (cache.workdir, my_encoding.encoder.Hashname(), cache.context.codec.SpeedGroup( my_encoding.bitrate)) videoname = my_encoding.videofile.basename with open('%s/%s.result' % (dirname, videoname), 'w') as resultfile: resultfile.write(str(my_encoding.result)) my_encoding.result = None result = cache.ReadEncodingResult(my_encoding) self.assertEquals(result, testresult)
def testBrokenStoredEncoding(self): context = StorageOnlyContext() other_dir = os.path.join(encoder_configuration.conf.sysdir(), 'broken_files') os.mkdir(other_dir) cache = encoder.EncodingDiskCache(context, scoredir='broken_files') # This particular test needs the context to know about the cache. context.cache = cache my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) # Cache should start off empty. self.assertFalse(cache.AllScoredEncodingsForEncoder(my_encoder)) videofile = encoder.Videofile('x/foo_640_480_20.yuv') my_encoding = encoder.Encoding(my_encoder, 123, videofile) testresult = {'foo': 'bar'} my_encoding.result = testresult cache.StoreEncoding(my_encoding) # TODO(hta): Expose the filename generation as a function for testing. with open( os.path.join( cache.workdir, my_encoding.encoder.Hashname(), cache.context.codec.SpeedGroup(my_encoding.bitrate), '%s.result' % my_encoding.videofile.basename), 'w') as scorefile: scorefile.write('stuff that is not valid json') result = cache.AllScoredEncodingsForEncoder(my_encoder) self.assertFalse(result) self.assertEquals(1, len(cache.bad_encodings))
def testStoreFetchEncoder(self): context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) new_encoder_data = cache.ReadEncoderParameters(my_encoder.Hashname()) self.assertEquals(new_encoder_data, my_encoder.parameters)
def testAllEncoderFilenames(self): context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) files = cache.AllEncoderFilenames() self.assertEquals(0, len(files)) my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) files = cache.AllEncoderFilenames() self.assertEquals(1, len(files)) self.assertEquals(my_encoder.Hashname(), files[0])
def testStoreFetchEncoder(self): context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) new_encoder_data = cache.ReadEncoderParameters( os.path.join(cache.WorkDir(), my_encoder.Hashname())) self.assertEquals(new_encoder_data, my_encoder.parameters) # Using only the hashname should work too. new_encoder_data = cache.ReadEncoderParameters(my_encoder.Hashname()) self.assertEquals(new_encoder_data, my_encoder.parameters)
def testSearchPathForScores(self): # The score path starts out empty from setUp. cache = encoder.EncodingDiskCache(StorageOnlyContext()) path = cache.SearchPathForScores() self.assertEquals(1, len(path), msg='Not just one element when scorepath is empty') encoder_configuration.conf.override_scorepath_for_test(['foo']) path = cache.SearchPathForScores() self.assertEquals( 2, len(path), msg='Not just 2 elements when scorepath has 1 element')
def testStoreFetchEncoding(self): context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) my_encoding = encoder.Encoding( my_encoder, 123, encoder.Videofile('x/foo_640_480_20.yuv')) testresult = {'foo': 'bar'} my_encoding.result = testresult cache.StoreEncoding(my_encoding) my_encoding.result = None result = cache.ReadEncodingResult(my_encoding) self.assertEquals(result, testresult)
def testReadResultFromAlternateDir(self): context = StorageOnlyContext() otherdir_path = os.path.join(encoder_configuration.conf.sysdir(), 'otherdir') os.mkdir(otherdir_path) cache = encoder.EncodingDiskCache(context) other_cache = encoder.EncodingDiskCache(context, scoredir='otherdir') my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) videofile = encoder.Videofile('x/foo_640_480_20.yuv') my_encoding = encoder.Encoding(my_encoder, 123, videofile) testresult = {'foo': 'bar'} my_encoding.result = testresult cache.StoreEncoding(my_encoding) my_encoding.result = None result = other_cache.ReadEncodingResult(my_encoding) self.assertIsNone(result) shutil.rmtree(otherdir_path) shutil.copytree(encoder_configuration.conf.workdir(), otherdir_path) result = other_cache.ReadEncodingResult(my_encoding) self.assertEquals(result, testresult)
def testReadResultFromAlternateDir(self): context = StorageOnlyContext() otherdir = os.path.join(os.environ['CODEC_WORKDIR'], 'otherdir') cache = encoder.EncodingDiskCache(context) my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) videofile = encoder.Videofile('x/foo_640_480_20.yuv') my_encoding = encoder.Encoding(my_encoder, 123, videofile) testresult = {'foo': 'bar'} my_encoding.result = testresult cache.StoreEncoding(my_encoding) my_encoding.result = None result = cache.ReadEncodingResult(my_encoding, scoredir=otherdir) self.assertIsNone(result) shutil.copytree(os.environ['CODEC_WORKDIR'], otherdir) result = cache.ReadEncodingResult(my_encoding, scoredir=otherdir) self.assertEquals(result, testresult)
def testStoreMultipleEncodings(self): context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) # This particular test needs the context to know about the cache. context.cache = cache my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) videofile = encoder.Videofile('x/foo_640_480_20.yuv') my_encoding = encoder.Encoding(my_encoder, 123, videofile) testresult = {'foo': 'bar'} my_encoding.result = testresult cache.StoreEncoding(my_encoding) my_encoding = encoder.Encoding(my_encoder, 246, videofile) my_encoding.result = testresult cache.StoreEncoding(my_encoding) result = cache.AllScoredRates(my_encoder, videofile) self.assertEquals(2, len(result)) result = cache.AllScoredEncodings(123, videofile) self.assertEquals(1, len(result))
def testAllScoredEncodingsForEncoder(self): context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) # This particular test needs the context to know about the cache. context.cache = cache my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) # Cache should start off empty. self.assertFalse(cache.AllScoredEncodingsForEncoder(my_encoder)) videofile = encoder.Videofile('x/foo_640_480_20.yuv') my_encoding = encoder.Encoding(my_encoder, 123, videofile) testresult = {'foo': 'bar'} my_encoding.result = testresult cache.StoreEncoding(my_encoding) result = cache.AllScoredEncodingsForEncoder(my_encoder) self.assertTrue(result) self.assertEquals(1, len(result)) # The resulting videofile should have a basename = filename, # because synthesizing filenames from result files loses directory # information. self.assertEquals('foo_640_480_20.yuv', result[0].videofile.filename)
def testStoreMultipleEncodings(self): # This test is sensitive to old data left around. # The FileUsingCodecTest base class takes care of giving it an # empty directory at test start. context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) context.cache = cache # This particular test needs the link. my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) videofile = encoder.Videofile('x/foo_640_480_20.yuv') my_encoding = encoder.Encoding(my_encoder, 123, videofile) testresult = {'foo': 'bar'} my_encoding.result = testresult cache.StoreEncoding(my_encoding) my_encoding = encoder.Encoding(my_encoder, 246, videofile) my_encoding.result = testresult cache.StoreEncoding(my_encoding) result = cache.AllScoredRates(my_encoder, videofile) self.assertEquals(2, len(result)) result = cache.AllScoredEncodings(123, videofile) self.assertEquals(1, len(result))
def testInit(self): cache = encoder.EncodingDiskCache(StorageOnlyContext()) self.assertTrue(cache)