2021-07-29 13:57:40 +03:00
from collections import defaultdict
2020-04-18 21:26:31 +03:00
from contextlib import ExitStack
from pathlib import Path
2020-04-20 14:36:30 +03:00
from unittest import TestCase
2020-04-18 21:26:31 +03:00
from unittest import mock
import os
import shutil
2021-07-29 13:57:40 +03:00
from parameterized import parameterized , parameterized_class # type: ignore
import yaml
2020-04-20 14:36:30 +03:00
from cloud_build import CB
2020-04-18 21:26:31 +03:00
from tests . call import Call
2021-07-29 13:57:40 +03:00
def change ( origin , new , key , value ) :
with open ( origin ) as f :
cfg = yaml . safe_load ( f )
with open ( new , ' w ' ) as f :
yaml . safe_dump ( cfg | { key : value } , f )
def get_class_name ( cls , num , params_dict ) :
result = cls . __name__
if branch := params_dict [ ' branch ' ] :
result = f ' { result } _ { parameterized . to_safe_name ( branch ) } '
if arch := params_dict [ ' arch ' ] :
result = f ' { result } _ { parameterized . to_safe_name ( arch ) } '
return f ' { result } _ { num } '
@parameterized_class ( [
{ ' branch ' : ' branch ' , ' arch ' : ' ' } ,
{ ' branch ' : ' branch ' , ' arch ' : ' arch ' } ,
{ ' branch ' : ' ' , ' arch ' : ' arch ' } ,
{ ' branch ' : ' ' , ' arch ' : ' ' } ,
] , class_name_func = get_class_name )
2020-04-20 14:36:30 +03:00
class TestIntegrationImages ( TestCase ) :
2020-04-18 21:26:31 +03:00
def setUp ( self ) :
2021-07-29 13:57:40 +03:00
self . _images = self . __class__ . _images
self . images_dir = self . __class__ . images_dir
2020-04-18 21:26:31 +03:00
@classmethod
def setUpClass ( cls ) :
cls . work_dir = Path ( ' /tmp/cloud-build ' )
2021-07-29 13:57:40 +03:00
os . makedirs ( cls . work_dir / ' external_files/p9/x86_64 ' , exist_ok = True )
( cls . work_dir / ' external_files/p9/x86_64/README ' ) . write_text ( ' README ' )
config = cls . work_dir / ' config.yaml '
branch = cls . branch
arch = cls . arch
remote = Path ( ' /tmp/cloud-build/images ' )
if branch :
remote = remote / ' {branch} '
if arch :
remote = remote / ' {arch} '
remote = ( remote / ' cloud ' ) . as_posix ( )
change (
' tests/test_integration_images.yaml ' ,
config ,
' remote ' ,
remote ,
)
2020-04-18 21:26:31 +03:00
with ExitStack ( ) as stack :
stack . enter_context ( mock . patch ( ' subprocess.call ' , Call ( ) ) )
2020-04-20 17:27:33 +03:00
cloud_build = CB (
2021-07-29 13:57:40 +03:00
config = config ,
2020-04-20 18:55:00 +03:00
data_dir = ( cls . work_dir / ' cloud_build ' ) . as_posix ( ) ,
2020-04-20 17:27:33 +03:00
)
2021-04-10 01:03:47 +03:00
cloud_build . create_images ( no_tests = True )
2020-04-18 21:26:31 +03:00
cloud_build . copy_external_files ( )
cloud_build . sign ( )
2021-04-10 01:41:07 +03:00
cloud_build . sync ( create_remote_dirs = True )
2020-04-18 21:26:31 +03:00
images_dir = cls . work_dir / ' images '
2021-07-29 13:57:40 +03:00
cls . images_dir = images_dir
images = defaultdict ( lambda : defaultdict ( list ) )
if branch :
for branch in os . listdir ( images_dir ) :
if arch :
for arch in os . listdir ( images_dir / branch ) :
images [ branch ] [ arch ] = os . listdir (
images_dir / branch / arch / ' cloud '
)
else :
images [ branch ] [ ' arch ' ] = os . listdir (
images_dir / branch / ' cloud '
)
elif arch :
for arch in os . listdir ( images_dir ) :
images [ ' branch ' ] [ arch ] = os . listdir (
images_dir / arch / ' cloud '
)
else :
images [ ' branch ' ] [ ' arch ' ] = os . listdir (
images_dir / ' cloud '
)
cls . _images = images
def image_path ( self , branch , arch , image ) - > Path :
images_dir = self . images_dir
if self . branch : # type: ignore
images_dir = images_dir / branch
if self . arch : # type: ignore
images_dir = images_dir / arch
return images_dir / ' cloud ' / image
def images ( self , branch , arch ) - > list :
if not self . branch : # type: ignore
branch = ' branch '
if not self . arch : # type: ignore
arch = ' arch '
return self . _images [ branch ] [ arch ]
def images_lists ( self ) - > list :
result = [ ]
for branch_value in self . _images . values ( ) :
for arch_value in branch_value . values ( ) :
result . append ( arch_value )
return result
2020-04-18 21:26:31 +03:00
@classmethod
def tearDownClass ( cls ) :
shutil . rmtree ( cls . work_dir , ignore_errors = True )
def test_arch_ppc64le ( self ) :
self . assertIn ( ' alt-p9-rootfs-minimal-ppc64le.tar.xz ' ,
2021-07-29 13:57:40 +03:00
self . images ( ' p9 ' , ' ppc64le ' ) )
2020-04-18 21:26:31 +03:00
def test_branches ( self ) :
2021-07-29 13:57:40 +03:00
if self . branch :
self . assertCountEqual ( self . _images . keys ( ) ,
[ ' Sisyphus ' , ' p9 ' , ' p8 ' ] )
else :
self . assertCountEqual ( self . _images . keys ( ) , [ ' branch ' ] )
2020-04-18 21:26:31 +03:00
2021-08-08 20:35:21 +03:00
def test_build_cloud_img ( self ) :
self . assertIn ( ' alt-p9-cloud-x86_64.img ' , self . images ( ' p9 ' , ' x86_64 ' ) )
def test_rename_regex_cloud ( self ) :
2021-07-29 13:57:40 +03:00
self . assertIn ( ' alt-p9-cloud-x86_64.qcow2 ' , self . images ( ' p9 ' , ' x86_64 ' ) )
2020-04-18 21:26:31 +03:00
def test_exclude_arches ( self ) :
2021-07-29 13:57:40 +03:00
self . assertNotIn ( ' alt-p8-cloud-x86_64.qcow2 ' ,
self . images ( ' p8 ' , ' x86_64 ' ) )
2020-04-18 21:26:31 +03:00
def test_exclude_branches ( self ) :
2021-07-29 13:57:40 +03:00
self . assertNotIn ( ' alt-p9-cloud-ppc64le.qcow2 ' ,
self . images ( ' p9 ' , ' ppc64le ' ) )
2020-04-18 21:26:31 +03:00
def test_external_files ( self ) :
2021-07-29 13:57:40 +03:00
self . assertIn ( ' README ' , self . images ( ' p9 ' , ' x86_64 ' ) )
2020-04-18 21:26:31 +03:00
2020-10-30 14:48:12 +03:00
def test_verification_files ( self ) :
2021-07-29 13:57:40 +03:00
for images_list in self . images_lists ( ) :
self . assertIn ( ' SHA256SUMS ' , images_list )
self . assertIn ( ' SHA256SUMS.gpg ' , images_list )
number_of_images = len ( self . image_path (
' p9 ' ,
' x86_64 ' ,
' SHA256SUMS ' ,
) . read_text ( ) . splitlines ( ) )
index = bool ( self . branch ) * 2 + bool ( self . arch )
expected_numbers = [ 52 , 17 , 21 , 7 ]
self . assertEqual ( number_of_images , expected_numbers [ index ] )
2020-10-30 14:48:12 +03:00
2020-04-18 21:26:31 +03:00
def test_number_of_images ( self ) :
2021-07-29 13:57:40 +03:00
number_of_images = sum ( len ( lst ) for lst in self . images_lists ( ) )
index = bool ( self . branch ) * 2 + bool ( self . arch )
expected_numbers = [ 56 , 72 , 64 , 96 ]
self . assertEqual ( number_of_images , expected_numbers [ index ] )