DockerfileParser { public static List<DockerCommand> dockerfileToCommandList( File dockerfile ) throws IOException { List<DockerCommand> result = new ArrayList<>(); FileInputStream in = new FileInputStream( dockerfile ); Logger logger = Logger.getLogger( DockerfileParser.class.getName()); BufferedReader br = null; try { br = new BufferedReader( new InputStreamReader( in, StandardCharsets.UTF_8 )); String line; while((line = br.readLine()) != null) { DockerCommand cmd = DockerCommand.guess(line); if( cmd != null ) result.add( cmd ); else logger.fine("Ignoring unsupported Docker instruction: " + line ); } } finally { Utils.closeQuietly( br ); Utils.closeQuietly( in ); } return result; } private  DockerfileParser(); static List<DockerCommand> dockerfileToCommandList( File dockerfile );  }
@Test public void dockerfileToCommandListTest_1() throws Exception { File testFile = TestUtils.findTestFile( "/dockerfiles/dockerfile1" ); List<DockerfileParser.DockerCommand> ldc = DockerfileParser.dockerfileToCommandList( testFile ); Assert.assertEquals( 9, ldc.size()); Assert.assertEquals( DockerfileParser.DockerCommandType.RUN, ldc.get( 0 ).type ); Assert.assertEquals( "apt-get update", ldc.get( 0 ).argument ); Assert.assertEquals( DockerfileParser.DockerCommandType.RUN, ldc.get( 1 ).type ); Assert.assertEquals( "apt-key adv --keyserver hkp: Assert.assertEquals( DockerfileParser.DockerCommandType.RUN, ldc.get( 2 ).type ); Assert.assertEquals( "echo 'deb http: Assert.assertEquals( DockerfileParser.DockerCommandType.ADD, ldc.get( 3 ).type ); Assert.assertEquals( "rabbitmq-env.conf /etc/rabbitmq/rabbitmq-env.conf", ldc.get( 3 ).argument ); Assert.assertEquals( DockerfileParser.DockerCommandType.COPY, ldc.get( 4 ).type ); Assert.assertEquals( "/ttee/tree/toto.txt /usr/local/deploy/", ldc.get( 4 ).argument ); Assert.assertEquals( DockerfileParser.DockerCommandType.RUN, ldc.get( 5 ).type ); Assert.assertEquals( "apt-get update", ldc.get( 5 ).argument ); Assert.assertEquals( DockerfileParser.DockerCommandType.RUN, ldc.get( 6 ).type ); Assert.assertEquals( "apt-get install -y mongodb-10gen", ldc.get( 6 ).argument ); Assert.assertEquals( DockerfileParser.DockerCommandType.RUN, ldc.get( 7 ).type ); Assert.assertEquals( "mkdir -p /data/db", ldc.get( 7 ).argument ); Assert.assertEquals( DockerfileParser.DockerCommandType.COPY, ldc.get( 8 ).type ); Assert.assertEquals( "ext1.jar /usr/local/deploy/", ldc.get( 8 ).argument ); }
@Test public void dockerfileToCommandListTest_2() throws Exception { File testFile = TestUtils.findTestFile( "/dockerfiles/dockerfile2" ); List<DockerfileParser.DockerCommand> ldc = DockerfileParser.dockerfileToCommandList( testFile ); Assert.assertEquals( 4, ldc.size()); Assert.assertEquals( DockerfileParser.DockerCommandType.RUN, ldc.get( 0 ).type ); Assert.assertEquals( "apt-get update", ldc.get( 0 ).argument ); Assert.assertEquals( DockerfileParser.DockerCommandType.RUN, ldc.get( 1 ).type ); Assert.assertEquals( "apt-get install -y python python-pip wget", ldc.get( 1 ).argument ); Assert.assertEquals( DockerfileParser.DockerCommandType.RUN, ldc.get( 2 ).type ); Assert.assertEquals( "pip install Flask", ldc.get( 2 ).argument ); Assert.assertEquals( DockerfileParser.DockerCommandType.ADD, ldc.get( 3 ).type ); Assert.assertEquals( "hello.py /home/hello.py", ldc.get( 3 ).argument ); }
@Test public void dockerfileToCommandListTest_empty() throws Exception { File testFile = this.folder.newFile(); List<DockerfileParser.DockerCommand> ldc = DockerfileParser.dockerfileToCommandList( testFile ); Assert.assertEquals( 0, ldc.size()); }