Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.notice_info = Notice("info")
self.notice_warn = Notice("warning", "warning")
self.notice_errr = Notice("error", "error")
self.notice_hint = Notice("hint", "hint")
self.notices = [
self.notice_info,
self.notice_warn,
self.notice_errr,
self.notice_hint
]
self.notice_origin = NoticeOrigin('origin_str')
def testIsEqual(self):
p = Package('p2')
p.pkg_license = 'Testpkg_license'
p.version = '1.0'
p.proj_url = 'TestUrl'
p.checksum = 'abcdef'
self.p2.pkg_license = 'Testpkg_license'
self.p2.version = '2.0'
self.p2.proj_url = 'TestUrl'
self.p2.checksum = 'abcdef'
self.assertFalse(self.p2.is_equal(p))
p.version = '2.0'
self.assertTrue(self.p2.is_equal(p))
def testToDict(self):
p1 = Package('x')
self.layer.add_package(p1)
a_dict = self.layer.to_dict()
self.assertEqual(a_dict['diff_id'], '123abc')
self.assertEqual(len(a_dict['packages']), 1)
self.assertEqual(a_dict['packages'][0]['name'], 'x')
self.assertEqual(a_dict['tar_file'], 'path/to/tar')
def testGetPackageNames(self):
p1 = Package('x')
self.layer.add_package(p1)
pkgs = self.layer.get_package_names()
self.assertEqual(pkgs[0], 'x')
def testToDictTemplate(self):
template1 = TestTemplate1()
template2 = TestTemplate2()
p1 = Package('x')
self.layer.add_package(p1)
dict1 = self.layer.to_dict(template1)
dict2 = self.layer.to_dict(template2)
self.assertEqual(len(dict1.keys()), 3)
self.assertEqual(dict1['layer.diff'], '123abc')
self.assertEqual(dict1['layer.tarfile'], 'path/to/tar')
self.assertEqual(len(dict1['layer.packages']), 1)
self.assertEqual(len(dict2.keys()), 4)
self.assertFalse(dict2['notes'])
self.assertFalse(dict2['layer.packages'][0]['notes'])
def load_image(self):
l1 = ImageLayer('123abc', 'path/to/tar')
self.name = 'testimage'
self.tag = 'testtag'
l1.add_package(Package('p1'))
l1.add_package(Package('p2'))
self._layers.append(l1)
def testFill(self):
p_dict = {'name': 'p1',
'version': '1.0',
'pkg_license': 'Apache 2.0',
'checksum': 'abcxyz'}
p = Package('p1')
p.fill(p_dict)
self.assertEqual(p.name, 'p1')
self.assertEqual(p.version, '1.0')
self.assertEqual(p.pkg_license, 'Apache 2.0')
self.assertEqual(p.checksum, 'abcxyz')
self.assertFalse(p.copyright)
self.assertFalse(p.proj_url)
self.assertEqual(len(p.origins.origins), 1)
self.assertEqual(p.origins.origins[0].origin_str, 'p1')
self.assertEqual(len(p.origins.origins[0].notices), 3)
self.assertEqual(p.origins.origins[0].notices[0].message,
"No metadata for key: copyright")
self.assertEqual(p.origins.origins[0].notices[0].level, 'warning')
self.assertEqual(p.origins.origins[0].notices[1].message,
"No metadata for key: proj_url")
self.assertEqual(p.origins.origins[0].notices[2].message,
def testAddPackage(self):
err = "Object type String, should be Package"
p1 = Package('x')
self.layer.add_package(p1)
self.assertEqual(len(self.layer.packages), 1)
with self.assertRaises(TypeError, msg=err):
self.layer.add_package("not_a_package")
def testRemovePackage(self):
p1 = Package('x')
p2 = Package('y')
self.layer.add_package(p1)
self.layer.add_package(p2)
self.assertTrue(self.layer.remove_package('y'))
self.assertFalse(self.layer.remove_package('y'))
def drop_into_layer(image_obj, layer_index):
"""Given the image object and the layer index, mount all the layers
upto the specified layer index and drop into a shell session"""
rootfs.set_up()
if layer_index == 0:
# mount only one layer
target = rootfs.mount_base_layer(
image_obj.layers[layer_index].tar_file)
else:
# mount all layers uptil the provided layer index
target = analyze.mount_overlay_fs(image_obj, layer_index)
# check if there is a shell
shell = check_shell()
if shell:
rootfs.prep_rootfs(target)
print("Done. Run 'sudo chroot . {}' to look around.".format(shell))
else:
print("A shell binary doesn't exist in the filesystem. You're on "
"your own.")
print("Working directory is: {}".format(get_mount_path()))
sys.exit(0)