2010/06/28

memo: writing new autotest test case.

This is my memo about writing autotest test case.
At least, it requires control file, python file and test code.

This is a test code directories.
[masami@moon]~/autotest-0.12.0/client/tests/pwrite_test% ls . src
.:
.  ..  control  pwrite_test.py  pwrite_test.py.bk  pwrite_test.pyc  src

src:
.  ..  Makefile  pwrite_test.c

This is a control file. It defines some information e.g. test type, author and so forth.
AUTHOR = "your name "
EXPERIMENTAL = 'True'  # Not really, but this is only for testing autotest...
                       # not testing _with_ autotest.
NAME = "pwrite_test"
TEST_TYPE = "client"
TEST_CLASS = "General"
TEST_CATEGORY = "Functional"
TIME = "SHORT"
DOC = """\
Test program for pwrite(2) and pread(2).
"""

job.run_test('pwrite_test')
job.run_test() calls your python script which implements test.test class.

This is a python code. this file name is pwrite_test.py.
In the setup(), it changes current directory from somewhere to "src" directory. src directory contains your my test code then call make command to create test program.

import os
from autotest_lib.client.bin import test, utils


class pwrite_test(test.test):
    """
    @author: your name (your email address)
    """
    version = 1
    preserve_srcdir = True

    def setup(self):
        os.chdir(self.srcdir)
        utils.system('make')
        utils.system('make create_test_file')

    def initialize(self):
        self.job.require_gcc()
        self.results = None


    def run_once(self, num_groups=90):
        pwrite_test_bin = os.path.join(self.srcdir, 'pwrite_test')
        cmd = '%s' % (pwrite_test_bin)
        self.results = utils.system(cmd)
    
    def cleanup(self):
        os.chdir(self.srcdir)
        utils.system('make clean')
        os.remove(os.path.join(self.srcdir, '.version'))

This is a test code.
#include 
#include 
#include 
#include 
#include 
#include 
#include 

static const char filename[] = "/home/masami/autotest-0.12.0/client/tests/pwrite_test/src/test.txt";
static const int offset = 6;
static const int read_byte = 5;

int main(int argc, char **argv)
{
 char buf[16];
 int fd, ret;
 off_t off;

 fd = open(filename, O_RDWR);
 if (fd < 0) {
  printf("file open error\n");
  return -1;
 }
 
 errno = 0;

 // Read data from file.
 pread(fd, &buf, read_byte, offset);
 if (errno) {
  printf("pread faild\n");
  return -1;
 }

 // Read data shuold be "world".
 if (strcmp(buf, "world")) {
  printf("read buf isn't \"world\"\n");
  return -1;
 }

 off = lseek(fd, 0, SEEK_CUR);
 // cursor should be zero.
 if (off) {
  printf("cursor isn't 0\n");
  return -1;
 }

 strcpy(buf, "WORLD");
 errno = 0;
 ret = pwrite(fd, buf, strlen(buf), 6);
 if (errno) {
  printf("pwrite faild\n");
  return -1;
 }
 
 pread(fd, &buf, read_byte, offset);
 // Read data shuold be "world".
 if (strcmp(buf, "WORLD")) {
  printf("read buf isn't \"world\"\n");
  return -1;
 }

 off = lseek(fd, 0, SEEK_CUR);
 // cursor should be zero.
 if (off) {
  printf("cursor isn't 0\n");
  return -1;
 }
 
 close(fd);

 printf("Done.\n");

 return 0;
}
Then, you can run your test code.
[masami@moon]~/autotest-0.12.0/client% sudo ./bin/autotest tests/pwrite_test/control
23:35:06 INFO | Writing results to /home/masami/autotest-0.12.0/client/results/default
23:35:06 INFO | Initializing the state engine
23:35:06 INFO | Symlinking init scripts
23:35:08 INFO | START ---- ---- timestamp=1277735708 localtime=Jun 28 23:35:08 
23:35:08 INFO |  START pwrite_test pwrite_test timestamp=1277735708 localtime=Jun 28 23:35:08 
23:35:10 INFO | Test started. Number of iterations: 1
23:35:10 INFO | Executing iteration 1 of 1
23:35:10 INFO | Dropping caches between iterations
23:35:10 INFO | Test finished after 1 iterations.
23:35:12 INFO |   GOOD pwrite_test pwrite_test timestamp=1277735712 localtime=Jun 28 23:35:12 completed successfully
23:35:12 INFO |  END GOOD pwrite_test pwrite_test timestamp=1277735712 localtime=Jun 28 23:35:12 
23:35:12 INFO | END GOOD ---- ---- timestamp=1277735712 localtime=Jun 28 23:35:12

No comments:

Post a Comment