2020-04-20 19:58:00 +03:00
from unittest import TestCase
2020-04-21 22:30:07 +03:00
from unittest import mock
2020-04-20 19:58:00 +03:00
import shutil
import tempfile
import yaml
from cloud_build import CB
2020-04-21 22:30:07 +03:00
from cloud_build import Error , BuildError , MultipleBuildErrors
import tests . call as call
2020-04-20 19:58:00 +03:00
def update ( old_dict , kwargs ) :
new_dict = old_dict . copy ( )
for key , value in kwargs . items ( ) :
if value is None :
del new_dict [ key ]
else :
new_dict [ key ] = value
return new_dict
class TestErrors ( TestCase ) :
def setUp ( self ) :
kwargs = { }
kwargs [ ' data_dir ' ] = tempfile . mkdtemp ( prefix = ' cloud_build ' )
self . kwargs = kwargs
def tearDown ( self ) :
shutil . rmtree ( self . kwargs [ ' data_dir ' ] )
def test_read_config_not_found ( self ) :
regex = ' config file.*No such file or directory '
self . kwargs . update ( config = ' /var/empty/cb_conf.yaml ' )
self . assertRaisesRegex ( Error , regex , CB , * * self . kwargs )
def test_read_config_permission_denied ( self ) :
regex = ' config file.*Permission denied '
self . kwargs . update ( config = ' /root/cb_conf.yaml ' )
self . assertRaisesRegex ( Error , regex , CB , * * self . kwargs )
def test_required_parameters_in_config ( self ) :
config = tempfile . mktemp ( prefix = ' cb_conf ' )
with open ( ' tests/minimal_config.yaml ' ) as f :
cfg = yaml . safe_load ( f )
parameter = ' key '
for parameter in [ ' remote ' , ' key ' , ' images ' , ' branches ' ] :
with open ( config , ' w ' ) as f :
yaml . safe_dump ( update ( cfg , { parameter : None } ) , f )
regex = f ' parameter.* { parameter } '
self . kwargs . update ( config = config )
self . assertRaisesRegex ( Error , regex , CB , * * self . kwargs )
def test_run_already_running ( self ) :
self . kwargs . update ( config = ' tests/minimal_config.yaml ' )
cb = CB ( * * self . kwargs ) # noqa F841
regex = ' already running '
self . assertRaisesRegex ( Error , regex , CB , * * self . kwargs )
2020-04-21 22:30:07 +03:00
2020-05-15 14:17:43 +03:00
def test_try_build_all_zero_rc ( self ) :
2020-04-21 22:30:07 +03:00
def cond ( args ) :
return args [ 1 ] . endswith ( ' aarch64 ' )
ds = { ' make ' : [ call . return_d ( 0 , cond = cond ) , call . nop_d ( cond = cond ) ] }
with mock . patch ( ' subprocess.call ' , call . Call ( decorators = ds ) ) :
cloud_build = CB (
config = ' tests/test_try_build_all.yaml ' ,
data_dir = self . kwargs [ ' data_dir ' ] ,
no_tests = True ,
create_remote_dirs = True ,
)
regex = r ' build.*: '
self . assertRaisesRegex (
2020-05-15 14:17:43 +03:00
MultipleBuildErrors ,
regex ,
cloud_build . create_images
)
def test_try_build_all_non_zero_rc ( self ) :
def cond ( args ) :
return args [ 1 ] . endswith ( ' aarch64 ' )
ds = { ' make ' : [ call . return_d ( 2 , cond = cond ) , call . nop_d ( cond = cond ) ] }
with mock . patch ( ' subprocess.call ' , call . Call ( decorators = ds ) ) :
cloud_build = CB (
config = ' tests/test_try_build_all.yaml ' ,
data_dir = self . kwargs [ ' data_dir ' ] ,
no_tests = True ,
create_remote_dirs = True ,
)
regex = r ' build.*: '
self . assertRaisesRegex (
2020-04-21 22:30:07 +03:00
MultipleBuildErrors ,
regex ,
cloud_build . create_images
)
def test_not_try_build_all ( self ) :
def cond ( args ) :
return args [ 1 ] . endswith ( ' aarch64 ' )
ds = { ' make ' : [ call . return_d ( 0 , cond = cond ) , call . nop_d ( cond = cond ) ] }
with mock . patch ( ' subprocess.call ' , call . Call ( decorators = ds ) ) :
cloud_build = CB (
config = ' tests/test_not_try_build_all.yaml ' ,
data_dir = self . kwargs [ ' data_dir ' ] ,
no_tests = True ,
create_remote_dirs = True ,
)
regex = r ' build.*aarch64 '
self . assertRaisesRegex (
BuildError ,
regex ,
cloud_build . create_images
)
2020-04-22 17:08:51 +03:00
def test_rebuild_after_format ( self ) :
regex = ' years.*rebuild_after '
self . kwargs . update ( config = ' tests/test_rebuild_after_format.yaml ' )
self . assertRaisesRegex ( Error , regex , CB , * * self . kwargs )
2020-07-03 16:02:45 +03:00
def test_bad_size ( self ) :
2021-03-03 20:50:02 +03:00
with mock . patch ( ' subprocess.call ' , call . Call ( ) ) :
regex = ' Bad size.* '
cloud_build = CB (
config = ' tests/test_bad_size.yaml ' ,
data_dir = self . kwargs [ ' data_dir ' ] ,
no_tests = True ,
)
self . assertRaisesRegex ( Error , regex , cloud_build . create_images )