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
2021-06-21 15:25:14 +03:00
import os
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 = { }
2021-06-21 15:25:14 +03:00
kwargs [ ' data_dir ' ] = tempfile . mkdtemp ( prefix = ' cloud_build_data ' )
2020-04-20 19:58:00 +03:00
self . kwargs = kwargs
2021-06-21 15:25:14 +03:00
self . images_dir = tempfile . mkdtemp ( prefix = ' cloud_build_images ' )
self . config = None
2020-04-20 19:58:00 +03:00
def tearDown ( self ) :
shutil . rmtree ( self . kwargs [ ' data_dir ' ] )
2021-06-21 15:25:14 +03:00
shutil . rmtree ( self . images_dir )
if ( config := self . config ) is not None :
os . unlink ( config )
2020-04-20 19:58:00 +03:00
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 ) :
2021-06-21 15:25:14 +03:00
self . config = tempfile . mktemp ( prefix = ' cb_conf ' )
2020-04-20 19:58:00 +03:00
with open ( ' tests/minimal_config.yaml ' ) as f :
cfg = yaml . safe_load ( f )
2021-04-10 01:03:47 +03:00
for parameter in [ ' remote ' , ' images ' , ' branches ' ] :
2021-06-21 15:25:14 +03:00
with open ( self . config , ' w ' ) as f :
2020-04-20 19:58:00 +03:00
yaml . safe_dump ( update ( cfg , { parameter : None } ) , f )
regex = f ' parameter.* { parameter } '
2021-06-21 15:25:14 +03:00
self . kwargs . update ( config = self . config )
2020-04-20 19:58:00 +03:00
self . assertRaisesRegex ( Error , regex , CB , * * self . kwargs )
2022-02-09 01:19:03 +03:00
def test_override_required_parameters_in_config ( self ) :
self . config = tempfile . mktemp ( prefix = ' cb_conf ' )
config_override = {
' remote ' : ' /var/empty ' ,
' images ' : { } ,
' branches ' : { } ,
}
with open ( ' tests/minimal_config.yaml ' ) as f :
cfg = yaml . safe_load ( f )
for parameter in [ ' remote ' , ' images ' , ' branches ' ] :
with open ( self . config , ' w ' ) as f :
yaml . safe_dump ( update ( cfg , { parameter : None } ) , f )
self . kwargs . update ( config = self . config )
self . kwargs . update ( config_override = config_override )
CB ( * * self . kwargs )
2020-04-20 19:58:00 +03:00
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 ' ] ,
)
regex = r ' build.*: '
self . assertRaisesRegex (
2020-05-15 14:17:43 +03:00
MultipleBuildErrors ,
regex ,
2021-04-10 01:03:47 +03:00
cloud_build . create_images ,
no_tests = True
2020-05-15 14:17:43 +03:00
)
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 ' ] ,
)
regex = r ' build.*: '
self . assertRaisesRegex (
2020-04-21 22:30:07 +03:00
MultipleBuildErrors ,
regex ,
2021-04-10 01:03:47 +03:00
cloud_build . create_images ,
no_tests = True
2020-04-21 22:30:07 +03:00
)
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 ' ] ,
)
regex = r ' build.*aarch64 '
self . assertRaisesRegex (
BuildError ,
regex ,
2021-04-10 01:03:47 +03:00
cloud_build . create_images ,
no_tests = True
2020-04-21 22:30:07 +03:00
)
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 ' ] ,
)
2021-04-10 01:03:47 +03:00
self . assertRaisesRegex (
Error ,
regex ,
cloud_build . create_images ,
no_tests = True
)
def test_sign_requires_key ( self ) :
with mock . patch ( ' subprocess.call ' , call . Call ( ) ) :
regex = ' key.*config '
cloud_build = CB (
config = ' tests/minimal_config.yaml ' ,
data_dir = self . kwargs [ ' data_dir ' ] ,
)
cloud_build . create_images ( no_tests = True )
self . assertRaisesRegex ( Error , regex , cloud_build . sign )
2021-06-21 15:25:14 +03:00
2022-02-09 00:54:59 +03:00
def test_sign_override_key ( self ) :
with mock . patch ( ' subprocess.call ' , call . Call ( ) ) :
cloud_build = CB (
config = ' tests/minimal_config.yaml ' ,
data_dir = self . kwargs [ ' data_dir ' ] ,
config_override = { ' key ' : 0 } ,
)
cloud_build . create_images ( no_tests = True )
cloud_build . sign ( )
2021-06-21 15:25:14 +03:00
def test_skiped_build ( self ) :
with mock . patch ( ' subprocess.call ' ) :
cloud_build = CB (
config = ' tests/minimal_config.yaml ' ,
data_dir = self . kwargs [ ' data_dir ' ] ,
built_images_dir = self . images_dir ,
)
regex = r ' build.*skip '
self . assertRaisesRegex (
Error ,
regex ,
cloud_build . create_images ,
no_tests = True
)