1#! /usr/bin/env python3
2#
3# BitBake Toaster Implementation
4#
5# Copyright (C) 2016 Intel Corporation
6#
7# SPDX-License-Identifier: GPL-2.0-only
8#
9
10# Tests were part of openembedded-core oe selftest Authored by: Lucian Musat
11# Ionut Chisanovici, Paul Eggleton and Cristian Iorga
12
13"""
14Test toaster backend by playing build event log files
15using toaster-eventreplay script
16"""
17
18import os
19
20from subprocess import getstatusoutput
21from pathlib import Path
22
23from django.test import TestCase
24
25from orm.models import Target_Installed_Package, Package, Build
26
27class EventReplay(TestCase):
28    """Base class for eventreplay test cases"""
29
30    def setUp(self):
31        """
32        Setup build environment:
33            - set self.script to toaster-eventreplay path
34            - set self.eventplay_dir to the value of EVENTPLAY_DIR env variable
35        """
36        bitbake_dir = Path(__file__.split('lib/toaster')[0])
37        self.script = bitbake_dir /  'bin' / 'toaster-eventreplay'
38        self.assertTrue(self.script.exists(), "%s doesn't exist")
39        self.eventplay_dir = os.getenv("EVENTREPLAY_DIR")
40        self.assertTrue(self.eventplay_dir,
41                        "Environment variable EVENTREPLAY_DIR is not set")
42
43    def _replay(self, eventfile):
44        """Run toaster-eventplay <eventfile>"""
45        eventpath = Path(self.eventplay_dir) / eventfile
46        status, output = getstatusoutput('%s %s' % (self.script, eventpath))
47        if status:
48            print(output)
49
50        self.assertEqual(status, 0)
51
52class CoreImageMinimalEventReplay(EventReplay):
53    """Replay core-image-minimal events"""
54
55    def test_installed_packages(self):
56        """Test if all required packages have been installed"""
57
58        self._replay('core-image-minimal.events')
59
60        # test installed packages
61        packages = sorted(Target_Installed_Package.objects.\
62                          values_list('package__name', flat=True))
63        self.assertEqual(packages, ['base-files', 'base-passwd', 'busybox',
64                                    'busybox-hwclock', 'busybox-syslog',
65                                    'busybox-udhcpc', 'eudev', 'glibc',
66                                    'init-ifupdown', 'initscripts',
67                                    'initscripts-functions', 'kernel-base',
68                                    'kernel-module-uvesafb', 'libkmod',
69                                    'modutils-initscripts', 'netbase',
70                                    'packagegroup-core-boot', 'run-postinsts',
71                                    'sysvinit', 'sysvinit-inittab',
72                                    'sysvinit-pidof', 'udev-cache',
73                                    'update-alternatives-opkg',
74                                    'update-rc.d', 'util-linux-libblkid',
75                                    'util-linux-libuuid', 'v86d', 'zlib'])
76
77class ZlibEventReplay(EventReplay):
78    """Replay zlib events"""
79
80    def test_replay_zlib(self):
81        """Test if zlib build and package are in the database"""
82        self._replay("zlib.events")
83
84        self.assertEqual(Build.objects.last().target_set.last().target, "zlib")
85        self.assertTrue('zlib' in Package.objects.values_list('name', flat=True))
86