1#! /usr/bin/env python
2# ex:ts=4:sw=4:sts=4:et
3# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4#
5# BitBake Toaster Implementation
6#
7# Copyright (C) 2016 Intel Corporation
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License version 2 as
11# published by the Free Software Foundation.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License along
19# with this program; if not, write to the Free Software Foundation, Inc.,
20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22# Tests were part of openembedded-core oe selftest Authored by: Lucian Musat
23# Ionut Chisanovici, Paul Eggleton and Cristian Iorga
24
25"""
26Test toaster backend by playing build event log files
27using toaster-eventreplay script
28"""
29
30import os
31
32from subprocess import getstatusoutput
33from pathlib import Path
34
35from django.test import TestCase
36
37from orm.models import Target_Installed_Package, Package, Build
38
39class EventReplay(TestCase):
40    """Base class for eventreplay test cases"""
41
42    def setUp(self):
43        """
44        Setup build environment:
45            - set self.script to toaster-eventreplay path
46            - set self.eventplay_dir to the value of EVENTPLAY_DIR env variable
47        """
48        bitbake_dir = Path(__file__.split('lib/toaster')[0])
49        self.script = bitbake_dir /  'bin' / 'toaster-eventreplay'
50        self.assertTrue(self.script.exists(), "%s doesn't exist")
51        self.eventplay_dir = os.getenv("EVENTREPLAY_DIR")
52        self.assertTrue(self.eventplay_dir,
53                        "Environment variable EVENTREPLAY_DIR is not set")
54
55    def _replay(self, eventfile):
56        """Run toaster-eventplay <eventfile>"""
57        eventpath = Path(self.eventplay_dir) / eventfile
58        status, output = getstatusoutput('%s %s' % (self.script, eventpath))
59        if status:
60            print(output)
61
62        self.assertEqual(status, 0)
63
64class CoreImageMinimalEventReplay(EventReplay):
65    """Replay core-image-minimal events"""
66
67    def test_installed_packages(self):
68        """Test if all required packages have been installed"""
69
70        self._replay('core-image-minimal.events')
71
72        # test installed packages
73        packages = sorted(Target_Installed_Package.objects.\
74                          values_list('package__name', flat=True))
75        self.assertEqual(packages, ['base-files', 'base-passwd', 'busybox',
76                                    'busybox-hwclock', 'busybox-syslog',
77                                    'busybox-udhcpc', 'eudev', 'glibc',
78                                    'init-ifupdown', 'initscripts',
79                                    'initscripts-functions', 'kernel-base',
80                                    'kernel-module-uvesafb', 'libkmod',
81                                    'modutils-initscripts', 'netbase',
82                                    'packagegroup-core-boot', 'run-postinsts',
83                                    'sysvinit', 'sysvinit-inittab',
84                                    'sysvinit-pidof', 'udev-cache',
85                                    'update-alternatives-opkg',
86                                    'update-rc.d', 'util-linux-libblkid',
87                                    'util-linux-libuuid', 'v86d', 'zlib'])
88
89class ZlibEventReplay(EventReplay):
90    """Replay zlib events"""
91
92    def test_replay_zlib(self):
93        """Test if zlib build and package are in the database"""
94        self._replay("zlib.events")
95
96        self.assertEqual(Build.objects.last().target_set.last().target, "zlib")
97        self.assertTrue('zlib' in Package.objects.values_list('name', flat=True))
98