@@ -305,9 +305,81 @@ def test_process_source(self, mock_get, mock_client,
305305 else :
306306 self .assertIsNotNone (get_result )
307307
308+ @mock .patch .dict (os .environ , clear = True )
309+ @mock .patch ('docker.APIClient' )
310+ def test_local_directory (self , mock_client ):
311+ tmpdir = tempfile .mkdtemp ()
312+ file_name = 'test.txt'
313+ file_path = os .path .join (tmpdir , file_name )
314+ saved_umask = os .umask (0o077 )
315+
316+ try :
317+ with open (file_path , 'w' ) as f :
318+ f .write ('Hello' )
319+
320+ self .dc = mock_client
321+ self .image .plugins = [{
322+ 'name' : 'fake-image-base-plugin-test' ,
323+ 'type' : 'local' ,
324+ 'enabled' : True ,
325+ 'source' : tmpdir }
326+ ]
327+ push_queue = mock .Mock ()
328+ builder = build .BuildTask (self .conf , self .image , push_queue )
329+ builder .run ()
330+ self .assertTrue (builder .success )
331+
332+ except IOError :
333+ print ('IOError' )
334+ else :
335+ os .remove (file_path )
336+ finally :
337+ os .umask (saved_umask )
338+ os .rmdir (tmpdir )
339+
308340 @mock .patch .dict (os .environ , clear = True )
309341 @mock .patch ('docker.APIClient' )
310342 def test_malicious_tar (self , mock_client ):
343+ tmpdir = tempfile .mkdtemp ()
344+ file_name = 'test.txt'
345+ archive_name = 'my_archive.tar'
346+ file_path = os .path .join (tmpdir , file_name )
347+ archive_path = os .path .join (tmpdir , archive_name )
348+ # Ensure the file is read/write by the creator only
349+ saved_umask = os .umask (0o077 )
350+
351+ try :
352+ with open (file_path , 'w' ) as f :
353+ f .write ('Hello' )
354+
355+ with tarfile .open (archive_path , 'w' ) as tar :
356+ tar .add (file_path , arcname = '../test.txt' )
357+
358+ self .dc = mock_client
359+ self .image .plugins = [{
360+ 'name' : 'fake-image-base-plugin-test' ,
361+ 'type' : 'local' ,
362+ 'enabled' : True ,
363+ 'source' : archive_path }
364+ ]
365+
366+ push_queue = mock .Mock ()
367+ builder = build .BuildTask (self .conf , self .image , push_queue )
368+ builder .run ()
369+ self .assertFalse (builder .success )
370+
371+ except IOError :
372+ print ('IOError' )
373+ else :
374+ os .remove (file_path )
375+ os .remove (archive_path )
376+ finally :
377+ os .umask (saved_umask )
378+ os .rmdir (tmpdir )
379+
380+ @mock .patch .dict (os .environ , clear = True )
381+ @mock .patch ('docker.APIClient' )
382+ def test_malicious_tar_gz (self , mock_client ):
311383 tmpdir = tempfile .mkdtemp ()
312384 file_name = 'test.txt'
313385 archive_name = 'my_archive.tar.gz'
0 commit comments