Add basic integration tests

This commit is contained in:
Mikhail Gordeev 2020-04-18 21:26:31 +03:00
parent 13464ddb32
commit 2b5676a89a
4 changed files with 240 additions and 0 deletions

0
tests/__init__.py Normal file
View File

77
tests/call.py Normal file
View File

@ -0,0 +1,77 @@
from pathlib import Path
import datetime
import os
import random
import re
import subprocess
SUBPROCESS_CALL = subprocess.call
DEFAULT = object()
def error_call(args):
raise Exception(f'Not implemened call for `{args}`')
def git(args):
if args[1] == 'clone':
target = Path(args[3])
os.makedirs(target)
if target.name == 'mkimage-profiles':
os.makedirs(target / 'conf.d')
os.makedirs(target / 'features.in/build-ve/image-scripts.d')
else:
error_call(args)
return 0
def make(args):
for arg in args:
if arg.startswith('IMAGE_OUTDIR='):
out_dir = Path(arg.lstrip('IMAGE_OUTDIR='))
if arg.startswith('ARCH='):
arch = Path(arg.lstrip('ARCH='))
match = re.match(r'.*/([-\w]*)\.(.*)', args[-1])
target, kind = match.groups()
date = datetime.date.today().strftime('%Y%m%d')
image = out_dir / f'{target}-{date}-{arch}.{kind}'
image.write_bytes(
bytes(random.randint(0, 255) for x in range(random.randint(32, 128)))
)
return 0
def gpg(args):
Path(f'{args[-1]}.asc').touch()
return 0
def rsync(args):
return SUBPROCESS_CALL(args, stdout=subprocess.DEVNULL)
class Call():
def __init__(self, progs=None):
self.progs = {
'git': git,
'make': make,
'gpg2': gpg,
'rsync': rsync,
DEFAULT: error_call,
}
if progs is not None:
self.progs.update(progs)
def __call__(self, args):
rc = None
prog = args[0]
if prog in self.progs:
rc = self.progs[prog](args)
else:
rc = self.progs[DEFAULT](args)
return rc

View File

@ -0,0 +1,73 @@
#!/usr/bin/python3
from contextlib import ExitStack
from pathlib import Path
from types import SimpleNamespace
from unittest import mock
import os
import shutil
import unittest
from cloud_build.cloud_build import CB
from tests.call import Call
class TestCommon(unittest.TestCase):
def setUp(self):
self.images = TestCommon.images
@classmethod
def setUpClass(cls):
cls.work_dir = Path('/tmp/cloud-build')
os.makedirs(cls.work_dir / 'external_files/p9', exist_ok=True)
(cls.work_dir / 'external_files/p9/README').write_text('README')
with ExitStack() as stack:
stack.enter_context(mock.patch('subprocess.call', Call()))
stack.enter_context(mock.patch.dict(
'os.environ',
{'XDG_DATA_HOME': cls.work_dir.as_posix()}
))
cloud_build = CB(SimpleNamespace(
config='tests/test_integration_images.yaml',
no_tests=True,
create_remote_dirs=True,
))
cloud_build.create_images()
cloud_build.copy_external_files()
cloud_build.sign()
cloud_build.sync()
images_dir = cls.work_dir / 'images'
cls.images = {}
for branch in os.listdir(images_dir):
cls.images[branch] = os.listdir(images_dir / branch / 'cloud')
@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',
self.images['p9'])
def test_branches(self):
self.assertCountEqual(self.images.keys(), ['Sisyphus', 'p9', 'p8'])
def test_build_cloud(self):
self.assertIn('alt-p9-cloud-x86_64.qcow2', self.images['p9'])
def test_exclude_arches(self):
self.assertNotIn('alt-p8-cloud-x86_64.qcow2', self.images['p8'])
def test_exclude_branches(self):
self.assertNotIn('alt-p9-cloud-ppc64le.qcow2', self.images['p9'])
def test_external_files(self):
self.assertIn('README', self.images['p9'])
def test_number_of_images(self):
number_of_images = sum(len(self.images[b]) for b in self.images.keys())
self.assertEqual(number_of_images, 58)

View File

@ -0,0 +1,90 @@
---
remote: '/tmp/cloud-build/images/{branch}/cloud'
key: 0x00000000
log_level: debug
external_files: /tmp/cloud-build/external_files
bad_arches:
- armh
images:
opennebula:
target: vm/opennebula-systemd
kinds:
- qcow2
exclude_arches:
- aarch64
- armh
- ppc64le
cloud:
target: vm/cloud-systemd
kinds:
- img
- qcow2
exclude_arches:
- armh
- ppc64le
exclude_branches:
- p8
rootfs-minimal:
target: ve/docker
kinds:
- tar.xz
tests:
- method: docker
rootfs-systemd:
target: ve/systemd-base
kinds:
- tar.xz
tests:
- method: lxd
scripts:
- securetty
rootfs-sysvinit:
target: ve/base
kinds:
- tar.xz
tests:
- method: lxd
scripts:
- securetty
packages:
- glibc-gconv-modules
- glibc-locales
- tzdata
prerequisites:
- use/net-ssh
- use/net/dhcp
branches:
Sisyphus:
arches:
i586:
x86_64:
aarch64:
ppc64le:
armh:
repository_url: file:///space/ALT/{branch}-{arch}
p9:
arches:
i586:
x86_64:
aarch64:
ppc64le:
branding: alt-starterkit
p8:
arches:
i586:
x86_64:
branding: alt-starterkit
scripts:
securetty:
contents: |
#!/bin/sh
echo pts/0 >> /etc/securetty
global: no
number: 1
...