Hatena::ブログ(Diary)

linuxとかテストとか RSSフィード

この日記のはてなブックマーク数

2011-12-02

Fedora Advent Calendar day 2

| 21:47

The beakerlib package

The beakerlib is a testing framework written in bash. It helps you to write a test case in bash. It provides several features such as Journaling, Logging, Assertion, and so on.

For example, QA:Testcase ClamAV would be implemented like this.

#!/bin/bash

# Include the BeakerLib environment
. /usr/share/beakerlib/beakerlib.sh

# Packaget to be tested
PACKAGE="clamav"

# Set the full test name
TEST="QA:Testcase ClamAV"

rlJournalStart
    rlPhaseStartTest
        grep "^Example" /etc/freshclam.conf
        if [ $? -eq 0 ]; then
            rlRun "sed -i 's/Example/#Example/' /etc/freshclam.conf" 0 "Comment out Example line"
        fi
    rlPhaseEnd

    rlPhaseStartTest
        rlRun "freshclam" 0 "Run freshclam"
    rlPhaseEnd

    rlPhaseStartSetup
        rlRun "curl -O http://www.eicar.org/download/eicar.com" 0 "Getting test virus"
    rlPhaseEnd

    rlPhaseStartTest
        # if virus is found, it returns 1
        rlRun "clamscan eicar.com" 1 "Scanning test virus"
    rlPhaseEnd

    rlPhaseStartCleanup
        rlRun "rm -f eicar.com" 0 "Remove test virus"
    rlPhaseEnd

rlJournalEnd

# Print the test report
rlJournalPrintText

As you can see there are three "rlPhaseStartSetup" block, and one "rlPhaseStartSetup" block, and one "rlPhaseStartCleanup" block. These block is the test steps in the wiki. It is no problems to only use rlPhaseStartSetup block instead of using setup or cleanup block but it seems not actual test case so I use setup and cleanup functions.

Anyway, running this script result like this.

[masami@rune:~/tests]$ sudo ./clamav-test.sh

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [   LOG    ] :: Test
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::



::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [   LOG    ] :: Test
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


ClamAV update process started at Fri Dec  2 21:41:26 2011
main.cvd is up to date (version: 54, sigs: 1044387, f-level: 60, builder: sven)
daily.cvd is up to date (version: 14063, sigs: 43201, f-level: 60, builder: arna
ud)
bytecode.cvd is up to date (version: 154, sigs: 38, f-level: 60, builder: edwin)

:: [   PASS   ] :: Run freshclam

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [   LOG    ] :: Setup
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    68  100    68    0     0     99      0 --:--:-- --:--:-- --:--:--   221
:: [   PASS   ] :: Getting test virus

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [   LOG    ] :: Test
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


eicar.com: Eicar-Test-Signature FOUND

----------- SCAN SUMMARY -----------
Known viruses: 1086364
Engine version: 0.97.3
Scanned directories: 0
Scanned files: 1
Infected files: 1
Data scanned: 0.00 MB
Data read: 0.00 MB (ratio 0.00:1)
Time: 2.287 sec (0 m 2 s)
:: [   PASS   ] :: Scanning test virus

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [   LOG    ] :: Cleanup
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:: [   PASS   ] :: Remove test virus
:: [21:41:31] ::  JOURNAL XML: /tmp/beakerlib-iMxU4xi/journal.xml
:: [21:41:31] ::  JOURNAL TXT: /tmp/beakerlib-iMxU4xi/journal.txt

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [   LOG    ] :: TEST PROTOCOL
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:: [   LOG    ] :: Test run ID   : iMxU4xi
:: [   LOG    ] :: Package       : clamav
:: [   LOG    ] :: Installed:    : clamav-0.97.3-1600.fc16.x86_64
:: [   LOG    ] :: Test started  : 2011-12-02 21:41:26 JST
:: [   LOG    ] :: Test finished : 2011-12-02 21:41:31 JST
:: [   LOG    ] :: Test name     : QA:Testcase ClamAV
:: [   LOG    ] :: Distro:       : Fedora release 16 (Verne)
:: [   LOG    ] :: Hostname      : rune
:: [   LOG    ] :: Architecture  : x86_64

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [   LOG    ] :: Test description
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


PURPOSE of /tests/clamav-test.sh
Description: Testing ClamAV package
Author: Masami Ichikawa <masami256@gmail.com>

This test script is implementing
https://fedoraproject.org/wiki/QA:Testcase_ClamAV test case using beakerlib.
It does all test steps in the test case.



::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [   LOG    ] :: Test
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:: [   LOG    ] :: Duration: 0s
:: [   LOG    ] :: Assertions: 0 good, 0 bad
:: [   PASS   ] :: RESULT: Test

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [   LOG    ] :: Test
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:: [   PASS   ] :: Run freshclam
:: [   LOG    ] :: Duration: 1s
:: [   LOG    ] :: Assertions: 1 good, 0 bad
:: [   PASS   ] :: RESULT: Test

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [   LOG    ] :: Setup
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:: [   PASS   ] :: Getting test virus
:: [   LOG    ] :: Duration: 1s
:: [   LOG    ] :: Assertions: 1 good, 0 bad
:: [   PASS   ] :: RESULT: Setup

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [   LOG    ] :: Test
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:: [   PASS   ] :: Scanning test virus
:: [   LOG    ] :: Duration: 3s
:: [   LOG    ] :: Assertions: 1 good, 0 bad
:: [   PASS   ] :: RESULT: Test

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [   LOG    ] :: Cleanup
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:: [   PASS   ] :: Remove test virus
:: [   LOG    ] :: Duration: 0s
:: [   LOG    ] :: Assertions: 1 good, 0 bad
:: [   PASS   ] :: RESULT: Cleanup
:: [   LOG    ] :: JOURNAL XML: /tmp/beakerlib-iMxU4xi/journal.xml
:: [   LOG    ] :: JOURNAL TXT: /tmp/beakerlib-iMxU4xi/journal.txt

In this log, you can see "PURPOSE of /tests/clamav-test.sh" line. This values came from PURPOSE file which use to write test purpose. You don't have to have the file but it would be nice to have it to understand your test purpose.

トラックバック - http://d.hatena.ne.jp/masami256/20111202/1322830054