def get_dockerfile_packages(): '''Given a Dockerfile return an approximate image object. This is mosty guess work and shouldn't be relied on for accurate information. Add Notice messages indicating as such: 1. Create an image with a placeholder repotag 2. For each RUN command, create a package list 3. Create layer objects with incremental integers and add the package list to that layer with a Notice about parsing 4. Return stub image''' stub_image = Image('easteregg:cookie') layer_count = 0 for inst in docker.docker_commands: if inst[0] == 'RUN': layer_count = layer_count + 1 layer = ImageLayer(layer_count) install_commands, msg = common.filter_install_commands(inst[1]) if msg: layer.origins.add_notice_to_origins( inst[1], Notice(msg, 'info')) pkg_names = [] for command in install_commands: pkg_names.append(common.get_installed_package_names(command)) for pkg_name in pkg_names: pkg = Package(pkg_name) # shell parser does not parse version pins yet # when that is enabled, Notices for no versions need to be # added here layer.add_package(pkg) return stub_image
def setUp(self): self.layer = ImageLayer('123abc', 'path/to/tar')
class TestClassImageLayer(unittest.TestCase): def setUp(self): self.layer = ImageLayer('123abc', 'path/to/tar') def tearDown(self): del self.layer def testInstance(self): self.assertEqual(self.layer.diff_id, '123abc') self.assertEqual(self.layer.tar_file, 'path/to/tar') self.assertFalse(self.layer.packages) self.assertFalse(self.layer.created_by) self.assertRaises(AttributeError, setattr, self.layer, 'diff_id', '456def') self.assertRaises(AttributeError, setattr, self.layer, 'tar_file', 'some/other/path') self.layer.created_by = 'some string' self.assertEqual(self.layer.created_by, 'some string') 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 testToDict(self): p1 = Package('x') self.layer.add_package(p1) a_dict = self.layer.to_dict() self.assertTrue(a_dict['']) 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')