def test_init(self): """Test whether Jadolint is correctly initialized""" jadolint = Jadolint(JADOLINT_PATH, analysis=SMELLS) self.assertEqual(jadolint.analysis, SMELLS) jadolint = Jadolint(JADOLINT_PATH, analysis=DEPENDENCIES) self.assertEqual(jadolint.analysis, DEPENDENCIES)
def test_analyze_empty(self): """Test whether Jadolint returns empty results if the file isn't a Dockerfile""" jadolint = Jadolint(JADOLINT_PATH, analysis=DEPENDENCIES) kwargs = {'file_path': os.path.join(self.tmp_path, ANALYZER_TEST_FILE)} result = jadolint.analyze(**kwargs) self.assertIn('dependencies', result) self.assertListEqual(result['dependencies'], [])
def test_analyze_error(self, check_output_mock): """Test whether an exception is thrown in case of errors""" check_output_mock.side_effect = subprocess.CalledProcessError( -1, "command", output=b'output') jadolint = Jadolint(JADOLINT_PATH, analysis=DEPENDENCIES) kwargs = {'file_path': os.path.join(self.repo_path, DOCKERFILE_TEST)} with self.assertRaises(GraalError): _ = jadolint.analyze(**kwargs)
def test_analyze_dependencies(self): """Test whether Jadolint returns the expected fields data""" jadolint = Jadolint(JADOLINT_PATH, analysis=DEPENDENCIES) kwargs = {'file_path': os.path.join(self.tmp_path, DOCKERFILE_TEST)} result = jadolint.analyze(**kwargs) expected = [ 'debian stretch-slim', 'bash', 'locales', 'gcc', 'git', 'git-core', 'python3', 'python3-pip', 'python3-venv', 'python3-dev', 'python3-gdbm', 'mariadb-client', 'unzip', 'curl', 'wget', 'sudo', 'ssh' ] self.assertIn(DEPENDENCIES, result) self.assertListEqual(result[DEPENDENCIES], expected)
def test_analyze_smells(self): """Test whether Jadolint returns the expected fields data""" jadolint = Jadolint(JADOLINT_PATH, analysis=SMELLS) kwargs = {'file_path': os.path.join(self.tmp_path, DOCKERFILE_TEST)} result = jadolint.analyze(**kwargs) expected = [ 'Dockerfile 5 DL4000 MAINTAINER is deprecated', 'Dockerfile 5 DL4000 MAINTAINER is deprecated', 'Dockerfile 5 DL4000 MAINTAINER is deprecated', 'Dockerfile 5 DL4000 MAINTAINER is deprecated', 'Dockerfile 5 DL4000 MAINTAINER is deprecated', 'Dockerfile 13 DL3009 Delete the apt-get lists after installing something', 'Dockerfile 16 DL3008 Pin versions in apt-get install', 'Dockerfile 16 DL3009 Delete the apt-get lists after installing something', 'Dockerfile 16 DL3014 Use the -y switch', 'Dockerfile 16 DL3015 Avoid additional packages by specifying --no-install-recommends', 'Dockerfile 32 DL3009 Delete the apt-get lists after installing something', 'Dockerfile 34 DL3009 Delete the apt-get lists after installing something', 'Dockerfile 34 DL3009 Delete the apt-get lists after installing something', 'Dockerfile 37 DL3020 Use COPY instead of ADD for files and folders', 'Dockerfile 38 DL3009 Delete the apt-get lists after installing something', 'Dockerfile 40 DL3009 Delete the apt-get lists after installing something', 'Dockerfile 40 DL3009 Delete the apt-get lists after installing something', 'Dockerfile 40 DL3009 Delete the apt-get lists after installing something', 'Dockerfile 40 DL3009 Delete the apt-get lists after installing something', 'Dockerfile 40 DL3009 Delete the apt-get lists after installing something', 'Dockerfile 40 DL3009 Delete the apt-get lists after installing something', 'Dockerfile 55 DL3000 Use absolute WORKDIR', 'Dockerfile 57 DL3025 Use arguments JSON notation for CMD and ENTRYPOINT arguments' ] self.assertIn(SMELLS, result) for i in range(len(result[SMELLS])): self.assertRegex(result[SMELLS][i], expected[i])
class JadolintAnalyzer(Analyzer): """Class to obtain a list of smells extracted from Dockerfiles.""" def __init__(self, exec_path, analysis=SMELLS): self.analyzer = Jadolint(exec_path, analysis=analysis) def analyze(self, file_path): """Analyze the content of a Python project using Jadolint :param file_path: path of the target file :returns a dict containing the results of the analysis, like the one below { 'image_path': .. } """ kwargs = {'file_path': file_path} analysis = self.analyzer.analyze(**kwargs) return analysis
def test_init_error(self): """Test whether an error is thrown when the exec path is None""" with self.assertRaises(GraalError): Jadolint("unknown", DEPENDENCIES)
def __init__(self, exec_path, analysis=SMELLS): self.analyzer = Jadolint(exec_path, analysis=analysis)
def __init__(self, exec_path, analysis=DEPENDENCIES): self.analyzer = Jadolint(exec_path, analysis=analysis)