def test_utils_block_read_filename(self): # FIXME: explain why this has to be a list status = [False] # Very simple callback (not really useful) def set_status(x): if x == '\n': status[0] = True # The ../data/one_length.bin file should only contain an empty line local_path = get_local_path('..', 'data', 'one_length.bin') self.assertTrue(os.path.exists(local_path)) self.assertFalse(status[0]) utils.block_read_filename(local_path, set_status) self.assertTrue(status[0]) status[0] = False # This file contains multiple lines but eventually an empty one local_path = get_local_path('..', 'data', 'two_lines.txt') self.assertTrue(os.path.exists(local_path)) self.assertFalse(status[0]) # Must read file in one-char-sized blocks for set_status to work utils.block_read_filename(local_path, set_status, block_size=1) self.assertTrue(status[0])
def test_utils_block_read_filename(self): # FIXME: explain why this has to be a list status = [False] # Very simple callback (not really useful) def set_status(x): if x == '\n': status[0] = True # The ../data/one_length.bin file should only contain an empty line local_path = get_local_path('..', 'data', 'one_length.bin') self.assertTrue(os.path.exists(local_path)) self.assertFalse(status[0]) utils.block_read_filename(local_path, set_status) self.assertTrue(status[0]) status[0] = False # This file contains multiple lines but eventually an empty one local_path = get_local_path('..', 'data', 'two_lines.txt') self.assertTrue(os.path.exists(local_path)) self.assertFalse(status[0]) # Must read file in one-char-sized blocks for set_status to work utils.block_read_filename(local_path, set_status, block_size=1) self.assertTrue(status[0])
def test_glancing_image_coreos(self): fn = 'coreos_production_qemu_image.img' imgfile = get_local_path('..', 'images', fn) chksumfile = get_local_path('..', 'images', 'coreos-MD5SUMS') with open(chksumfile, 'rb') as fin: md5 = fin.read(multihash.hash2len('md5')) self.assertTrue( glancing.main(['-v', '-d', imgfile, '-s', md5 + ':' + md5]))
def test_glancing_image_coreos(self): fn = 'coreos_production_qemu_image.img' imgfile = get_local_path('..', 'images', fn) chksumfile = get_local_path('..', 'images', 'coreos-MD5SUMS') with open(chksumfile, 'rb') as fin: md5 = fin.read(multihash.hash2len('md5')) self.assertTrue(glancing.main(['-v', '-d', imgfile, '-s', md5 + ':' + md5]))
def test_metadata_xml_button_vs_wget(self): fn_base = 'LHfKVPoHcv4oMirHU0KuOQc-TvI.%s.xml' fn = fn_base % 'button' xmlfile = get_local_path('..', 'stratuslab', fn) m = metadata.MetaStratusLabXml(xmlfile) md = m.get_metadata() self.assertEqual(set(md.keys()), good_keys_sl) fn = fn_base % 'wget' xmlfile = get_local_path('..', 'stratuslab', fn) m = metadata.MetaStratusLabXml(xmlfile) md = m.get_metadata() self.assertEqual(set(md.keys()), good_keys_sl)
def test_metadata_xml_button_vs_wget(self): fn_base = 'LHfKVPoHcv4oMirHU0KuOQc-TvI.%s.xml' fn = fn_base % 'button' xmlfile = get_local_path('..', 'stratuslab', fn) m = metadata.MetaStratusLabXml(xmlfile) md = m.get_metadata() self.assertEqual(set(md.keys()), good_keys_sl) fn = fn_base % 'wget' xmlfile = get_local_path('..', 'stratuslab', fn) m = metadata.MetaStratusLabXml(xmlfile) md = m.get_metadata() self.assertEqual(set(md.keys()), good_keys_sl)
def setUp(self): # pragma: no cover if os.path.exists(self.testdir): shutil.rmtree(self.testdir) os.mkdir(self.testdir) for fn in _TEST_FILES: local_path = get_local_path('..', 'data', fn) shutil.copy(local_path, self.testdir)
def test_glancing_metadata_bad_but_force(self): # Size & checksum mismatch: 375 MB -> 492 MB market_id = 'ME4iRTemHRwhABKV5AgrkQfDerA' mdfile = get_local_path('..', 'stratuslab', market_id + '.json') self.assertTrue(glancing.main(['-vf', '-n', test_name(), mdfile])) self.assertTrue(glance.glance_delete(test_name())) self.assertFalse(glancing.main(['-vf', '-n', test_name(), market_id]))
def test_glance_manager_mocked_ok_double(self): cln = ['glance', '--os-image-api-version', '1', 'image-delete', 'GLANCE_MANAGER_CIRROS_TESTING_IMAGE'] with utils.cleanup(cln): locpath = get_local_path('..', 'gm_list.txt') ret = glance_manager.main(['-v', '-l', locpath, '-l', locpath]) self.assertTrue(ret)
def test_multihash_doit(self): devnull_checksums = { 'md5': 'd41d8cd98f00b204e9800998ecf8427e', 'sha1': 'da39a3ee5e6b4b0d3255bfef95601890afd80709', 'sha224': 'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f', 'sha256': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'sha384': '38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b', 'sha512': 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e', } random_1M_checksums = {} for alg in devnull_checksums.keys(): # Just use the same algos fin_fn = get_local_path('..', 'data', alg.upper() + 'SUMS') with open(fin_fn, 'rb') as fin: for line in fin: if line.endswith('random_1M.bin\n'): random_1M_checksums[alg] = line[:multihash.hash2len(alg )] break expected = dict( zip(self.files_to_hash, [devnull_checksums, random_1M_checksums])) self.assertEqual(expected, self.computed)
def setUp(self): # pragma: no cover if os.path.exists(self.testdir): shutil.rmtree(self.testdir) os.mkdir(self.testdir) for fn in _TEST_FILES: local_path = get_local_path('..', 'data', fn) shutil.copy(local_path, self.testdir)
def test_glancing_metadata_bad_but_force(self): # Size & checksum mismatch: 375 MB -> 492 MB market_id = 'ME4iRTemHRwhABKV5AgrkQfDerA' mdfile = get_local_path('..', 'stratuslab', market_id + '.json') self.assertTrue(glancing.main(['-vf', '-n', test_name(), mdfile])) self.assertTrue(glance.glance_delete(test_name())) self.assertFalse(glancing.main(['-vf', '-n', test_name(), market_id]))
def test_glancing_metadata_cirros_import_no_cksum(self): # 12 MB mdfile = get_local_path('..', 'stratuslab', 'cirros_no_cksum.json') with devnull('stderr'): self.assertTrue( glancing.main(['-v', '-n', test_name(), mdfile, '-k']))
def test_glance_manager_mocked_ok_double(self): cln = [ 'glance', '--os-image-api-version', '1', 'image-delete', 'GLANCE_MANAGER_CIRROS_TESTING_IMAGE' ] with utils.cleanup(cln): locpath = get_local_path('..', 'gm_list.txt') ret = glance_manager.main(['-v', '-l', locpath, '-l', locpath]) self.assertTrue(ret)
def test_multihash_files(self): files = [os.devnull, 'random_1M.bin', 'random_5M.bin'] for fn in files: local_path = get_local_path('..', 'data', fn) mhs = multihash.multihash_serial_exec() mhp = multihash.multihash_hashlib() mhs.hash_file(local_path) mhp.hash_file(local_path) self.assertEquals(mhs.hexdigests(), mhp.hexdigests())
def test_metadata_cern(self): fn = 'test_image_list' jsonfile = get_local_path('..', 'CERN', fn) m = metadata.MetaCern(jsonfile, 'deadbabe-f00d-beef-cafe-b1ab1ab1a666') md = m.get_metadata() self.assertEqual(set(md.keys()), good_keys_cern) self.assertEqual( md['location'], 'http://download.cirros-cloud.net/0.3.4/' 'cirros-0.3.4-i386-disk.img')
def test_metadata_json(self): fn = 'PIDt94ySjKEHKKvWrYijsZtclxU.json' jsonfile = get_local_path('..', 'stratuslab', fn) m = metadata.MetaStratusLabJson(jsonfile) md = m.get_metadata() self.assertEqual(set(md.keys()), good_keys_sl) self.assertEqual(md['location'], 'http://appliances.stratuslab.eu/images/base/CentOS-' '7-Server-x86_64/1.1/CentOS-7-Server-x86_64.dsk.gz')
def test_glancing_metadata_cern_cirros_import(self): # 12 MB mdfile = get_local_path('..', 'CERN', 'test_image_list') self.assertTrue( glancing.main([ '-v', '-n', test_name(), '-c', mdfile, '-k', "deadbabe-f00d-beef-cafe-b1ab1ab1a666" ]))
def test_metadata_cern(self): fn = 'test_image_list' jsonfile = get_local_path('..', 'CERN', fn) m = metadata.MetaCern(jsonfile, 'deadbabe-f00d-beef-cafe-b1ab1ab1a666') md = m.get_metadata() self.assertEqual(set(md.keys()), good_keys_cern) self.assertEqual(md['location'], 'http://download.cirros-cloud.net/0.3.4/' 'cirros-0.3.4-i386-disk.img')
class GlancingCirrosImageTest(unittest.TestCase): _CIRROS_FILE = get_local_path('..', 'images', 'cirros-0.3.4-i386-disk.img') _CIRROS_MD5 = get_local_path('..', 'images', 'cirros-MD5SUMS') _CIRROS_SHA1 = get_local_path('..', 'images', 'cirros-SHA1SUMS') _CIRROS_CHK = '79b4436412283bb63c2cba4ac796bcd9' def test_glancing_file_dryrun_good_sum(self): self.assertTrue( glancing.main([ '-d', '-n', test_name(), self._CIRROS_FILE, '-S', self._CIRROS_MD5 ])) self.assertTrue( glancing.main([ '-d', '-n', test_name(), self._CIRROS_FILE, '-S', self._CIRROS_SHA1 ])) self.assertTrue( glancing.main([ '-d', '-n', test_name(), self._CIRROS_FILE, '-S', self._CIRROS_SHA1, '-S', self._CIRROS_MD5 ])) self.assertTrue( glancing.main([ '-d', '-n', test_name(), self._CIRROS_FILE, '-S', self._CIRROS_MD5, '-s', self._CIRROS_CHK ])) self.assertTrue( glancing.main([ '-d', '-n', test_name(), self._CIRROS_FILE, '-s', self._CIRROS_CHK, '-S', self._CIRROS_SHA1 ])) @glance_cleanup() @unittest.skipUnless(_GLANCE_OK, "glance not properly configured") def test_glancing_file_import_good_sum(self): self.assertTrue( glancing.main( ['-n', test_name(), self._CIRROS_FILE, '-S', self._CIRROS_MD5]))
def test_tutils_local_pythonpath(self): def count_endswith(alist, suffix): return len([x for x in alist if x.endswith(suffix)]) self.assertEqual(0, count_endswith(sys.path, 'TOTO')) tutils.local_pythonpath('TOTO') self.assertEqual(1, count_endswith(sys.path, 'TOTO')) tutils.local_pythonpath('TOTO') self.assertEqual(1, count_endswith(sys.path, 'TOTO')) sys.path.remove(tutils.get_local_path('TOTO')) self.assertEqual(0, count_endswith(sys.path, 'TOTO'))
def test_metadata_json(self): fn = 'PIDt94ySjKEHKKvWrYijsZtclxU.json' jsonfile = get_local_path('..', 'stratuslab', fn) m = metadata.MetaStratusLabJson(jsonfile) md = m.get_metadata() self.assertEqual(set(md.keys()), good_keys_sl) self.assertEqual( md['location'], 'http://appliances.stratuslab.eu/images/base/CentOS-' '7-Server-x86_64/1.1/CentOS-7-Server-x86_64.dsk.gz')
def test_tutils_local_pythonpath(self): def count_endswith(alist, suffix): return len([x for x in alist if x.endswith(suffix)]) self.assertEqual(0, count_endswith(sys.path, 'TOTO')) tutils.local_pythonpath('TOTO') self.assertEqual(1, count_endswith(sys.path, 'TOTO')) tutils.local_pythonpath('TOTO') self.assertEqual(1, count_endswith(sys.path, 'TOTO')) sys.path.remove(tutils.get_local_path('TOTO')) self.assertEqual(0, count_endswith(sys.path, 'TOTO'))
def test_metadata_xml(self): fn = 'Buh-tYElvOEvst1HDyTq_6v-1si.xml' xmlfile = get_local_path('..', 'stratuslab', fn) self.assertTrue(os.path.exists(xmlfile)) m = metadata.MetaStratusLabXml(xmlfile) self.assertTrue(m is not None) md = m.get_metadata() self.assertTrue(md is not None) self.assertEqual(set(md.keys()), good_keys_sl) self.assertEqual( md['location'], 'http://grand-est.fr/resources/CLOUD/' 'precise-server-cloudimg-amd64-disk1.img') self.assertEqual(m.get_name(), 'Ubuntu-12.04-x86_64')
def test_metadata_xml(self): fn = 'Buh-tYElvOEvst1HDyTq_6v-1si.xml' xmlfile = get_local_path('..', 'stratuslab', fn) self.assertTrue(os.path.exists(xmlfile)) m = metadata.MetaStratusLabXml(xmlfile) self.assertTrue(m is not None) md = m.get_metadata() self.assertTrue(md is not None) self.assertEqual(set(md.keys()), good_keys_sl) self.assertEqual(md['location'], 'http://grand-est.fr/resources/CLOUD/' 'precise-server-cloudimg-amd64-disk1.img') self.assertEqual(m.get_name(), 'Ubuntu-12.04-x86_64')
def test_glance_download(self): _RND1M_FILE = get_local_path('..', 'data', 'random_1M.bin') _DL_ED_FILE = os.path.join('/', 'tmp', self._IMG_NAME) if os.path.exists(_DL_ED_FILE): os.remove(_DL_ED_FILE) self.common_start(_RND1M_FILE) self.assertTrue(glance.glance_download(self._IMG_NAME, _DL_ED_FILE)) self.assertTrue(os.path.exists(_DL_ED_FILE)) self.assertTrue(utils.run(['cmp', _RND1M_FILE, _DL_ED_FILE])[0]) os.remove(_DL_ED_FILE) self.assertFalse(os.path.exists(_DL_ED_FILE)) self.assertFalse(glance.glance_download('', _DL_ED_FILE)) self.assertFalse(os.path.exists(_DL_ED_FILE))
def test_glance_download(self): _RND1M_FILE = get_local_path('..', 'data', 'random_1M.bin') _DL_ED_FILE = os.path.join('/', 'tmp', self._IMG_NAME) if os.path.exists(_DL_ED_FILE): os.remove(_DL_ED_FILE) self.common_start(_RND1M_FILE) self.assertTrue(glance.glance_download(self._IMG_NAME, _DL_ED_FILE)) self.assertTrue(os.path.exists(_DL_ED_FILE)) self.assertTrue(utils.run(['cmp', _RND1M_FILE, _DL_ED_FILE])[0]) os.remove(_DL_ED_FILE) self.assertFalse(os.path.exists(_DL_ED_FILE)) self.assertFalse(glance.glance_download('', _DL_ED_FILE)) self.assertFalse(os.path.exists(_DL_ED_FILE))
def test_glancing_metadata_heavies(self): market_ids = ( # 98 MB, size & checksum mismatch: 4 B -> 98 MB ('JcqGhHxmTRAEpHMmRF-xhSTM3TO', False, False, False), # 102 MB, does not exists any more on SL marketplace ('BtSKdXa2SvHlSVTvgFgivIYDq--', True, False, False), # 872 MB ('IzEOzeHK8-zpgSyAkhNiZujL4nZ', True, True, True), # Size & checksum mismatch: 375 MB -> 492 MB ('ME4iRTemHRwhABKV5AgrkQfDerA', False, False, False), ) for market_id, status_json, status_xml, status_market in market_ids: mdfile_base = get_local_path('..', 'stratuslab', market_id) self.assertEqual(status_json, glancing.main(['-d', mdfile_base + '.json']), mdfile_base + '.json') self.assertEqual(status_xml, glancing.main(['-d', mdfile_base + '.xml']), mdfile_base + '.xml') self.assertEqual(status_market, glancing.main(['-d', market_id]), market_id)
def test_glancing_metadata_heavies(self): market_ids = ( # 98 MB, size & checksum mismatch: 4 B -> 98 MB ('JcqGhHxmTRAEpHMmRF-xhSTM3TO', False, False, False), # 102 MB, does not exists any more on SL marketplace ('BtSKdXa2SvHlSVTvgFgivIYDq--', True, False, False), # 872 MB ('IzEOzeHK8-zpgSyAkhNiZujL4nZ', True, True, True), # Size & checksum mismatch: 375 MB -> 492 MB ('ME4iRTemHRwhABKV5AgrkQfDerA', False, False, False), ) for market_id, status_json, status_xml, status_market in market_ids: mdfile_base = get_local_path('..', 'stratuslab', market_id) self.assertEqual(status_json, glancing.main(['-d', mdfile_base + '.json']), mdfile_base + '.json') self.assertEqual(status_xml, glancing.main(['-d', mdfile_base + '.xml']), mdfile_base + '.xml') self.assertEqual(status_market, glancing.main(['-d', market_id]), market_id)
def test_multihash_doit(self): devnull_checksums = { 'md5': 'd41d8cd98f00b204e9800998ecf8427e', 'sha1': 'da39a3ee5e6b4b0d3255bfef95601890afd80709', 'sha224': 'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f', 'sha256': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'sha384': '38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b', 'sha512': 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e', } random_1M_checksums = {} for alg in devnull_checksums: # Just use the same algos fin_fn = get_local_path('..', 'data', alg.upper() + 'SUMS') with open(fin_fn, 'rb') as fin: for line in fin: if line.endswith('random_1M.bin\n'): random_1M_checksums[alg] = line[:multihash.hash2len(alg)] break expected = dict(zip(self.files_to_hash, [devnull_checksums, random_1M_checksums])) self.assertEqual(expected, self.computed)
def test_tutils_get_local_path(self): lp = tutils.get_local_path('toto', 'titi.txt') self.assertTrue(lp.endswith('/glancing/test/src/toto/titi.txt'))
def test_metadata_json_no_md_matching_query(self): fn = 'LHfKVPoHcv4oMirHU0KuOQc-TvI?media=json' jsonfile = get_local_path('..', 'stratuslab', fn) m = metadata.MetaStratusLabJson(jsonfile) md = m.get_metadata() self.assertIsNone(md)
def setUp(self): self.files_to_hash = [os.devnull, get_local_path('..', 'data', 'random_1M.bin')] self.computed = multihash.doit(self.files_to_hash)
def test_utils_block_read_filename_zero_size(self): local_path = get_local_path('..', 'data', 'two_lines.txt') self.assertTrue(os.path.exists(local_path)) with self.assertRaises(IOError): utils.block_read_filename(local_path, lambda x: None, block_size=0)
class TestGlancingImageTtylinuxBase(unittest.TestCase): _TTYLINUX_FILE = get_local_path('..', 'images', 'ttylinux-16.1-x86_64.img') _TTYLINUX_MD5 = '3d1b4804dcf2a613f0ed4a91b9ed2b98'
def setUp(self): gmf = lambda mpid, url: get_local_path('..', 'stratuslab', 'cirros.xml' ) glance_manager.get_meta_file = gmf
def test_glancing_metadata_cirros_import_bad_size(self): # 12 MB mdfile = get_local_path('..', 'stratuslab', 'cirros_bad_size.json') with devnull('stderr'): self.assertFalse(glancing.main(['-v', '-n', test_name(), mdfile])) self.assertTrue(glancing.main(['-f', '-n', test_name(), mdfile]))
def test_metadata_json_no_md_matching_query(self): fn = 'LHfKVPoHcv4oMirHU0KuOQc-TvI?media=json' jsonfile = get_local_path('..', 'stratuslab', fn) m = metadata.MetaStratusLabJson(jsonfile) md = m.get_metadata() self.assertIsNone(md)
def test_glancing_metadata_big(self): market_id = 'PIDt94ySjKEHKKvWrYijsZtclxU' mdfile = get_local_path('..', 'stratuslab', market_id + '.json') self.assertTrue(glancing.main(['-d', mdfile])) self.assertTrue(glancing.main(['-d', market_id]))
def setUp(self): gmf = lambda mpid, url: get_local_path('..', 'stratuslab', 'cirros.xml') glance_manager.get_meta_file = gmf
def test_tutils_get_local_path(self): lp = tutils.get_local_path('toto', 'titi.txt') self.assertTrue(lp.endswith('/glancing/test/src/toto/titi.txt'))
def test_utils_block_read_filename_zero_size(self): local_path = get_local_path('..', 'data', 'two_lines.txt') self.assertTrue(os.path.exists(local_path)) with self.assertRaises(IOError): utils.block_read_filename(local_path, lambda x: None, block_size=0)
def test_glancing_metadata_big(self): market_id = 'PIDt94ySjKEHKKvWrYijsZtclxU' mdfile = get_local_path('..', 'stratuslab', market_id + '.json') self.assertTrue(glancing.main(['-d', mdfile])) self.assertTrue(glancing.main(['-d', market_id]))
def test_glancing_metadata_cirros_import_bad_size(self): # 12 MB mdfile = get_local_path('..', 'stratuslab', 'cirros_bad_size.json') with devnull('stderr'): self.assertFalse(glancing.main(['-v', '-n', test_name(), mdfile])) self.assertTrue(glancing.main(['-f', '-n', test_name(), mdfile]))
def test_glancing_metadata_cirros_import_no_cksum(self): # 12 MB mdfile = get_local_path('..', 'stratuslab', 'cirros_no_cksum.json') with devnull('stderr'): self.assertTrue(glancing.main(['-v', '-n', test_name(), mdfile, '-k']))
def test_glancing_metadata_cern_cirros_import(self): # 12 MB mdfile = get_local_path('..', 'CERN', 'test_image_list') self.assertTrue(glancing.main(['-v', '-n', test_name(), '-c', mdfile, '-k', "deadbabe-f00d-beef-cafe-b1ab1ab1a666"]))