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.
  1. AUTHOR = "your name <your email="" address="">"  
  2. EXPERIMENTAL = 'True'  # Not really, but this is only for testing autotest...  
  3.                        # not testing _with_ autotest.  
  4. NAME = "pwrite_test"  
  5. TEST_TYPE = "client"  
  6. TEST_CLASS = "General"  
  7. TEST_CATEGORY = "Functional"  
  8. TIME = "SHORT"  
  9. DOC = """\ 
  10. Test program for pwrite(2) and pread(2). 
  11. """  
  12.   
  13. job.run_test('pwrite_test')  
  14. </your>  
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.

  1. import os  
  2. from autotest_lib.client.bin import test, utils  
  3.   
  4.   
  5. class pwrite_test(test.test):  
  6.     """ 
  7.     @author: your name (your email address) 
  8.     """  
  9.     version = 1  
  10.     preserve_srcdir = True  
  11.   
  12.     def setup(self):  
  13.         os.chdir(self.srcdir)  
  14.         utils.system('make')  
  15.         utils.system('make create_test_file')  
  16.   
  17.     def initialize(self):  
  18.         self.job.require_gcc()  
  19.         self.results = None  
  20.   
  21.   
  22.     def run_once(self, num_groups=90):  
  23.         pwrite_test_bin = os.path.join(self.srcdir, 'pwrite_test')  
  24.         cmd = '%s' % (pwrite_test_bin)  
  25.         self.results = utils.system(cmd)  
  26.       
  27.     def cleanup(self):  
  28.         os.chdir(self.srcdir)  
  29.         utils.system('make clean')  
  30.         os.remove(os.path.join(self.srcdir, '.version'))  

This is a test code.
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <unistd.h>  
  4. #include <fcntl.h>  
  5. #include <sys stat.h="">  
  6. #include <sys types.h="">  
  7. #include <errno.h>  
  8.   
  9. static const char filename[] = "/home/masami/autotest-0.12.0/client/tests/pwrite_test/src/test.txt";  
  10. static const int offset = 6;  
  11. static const int read_byte = 5;  
  12.   
  13. int main(int argc, char **argv)  
  14. {  
  15.  char buf[16];  
  16.  int fd, ret;  
  17.  off_t off;  
  18.   
  19.  fd = open(filename, O_RDWR);  
  20.  if (fd < 0) {  
  21.   printf("file open error\n");  
  22.   return -1;  
  23.  }  
  24.    
  25.  errno = 0;  
  26.   
  27.  // Read data from file.  
  28.  pread(fd, &buf, read_byte, offset);  
  29.  if (errno) {  
  30.   printf("pread faild\n");  
  31.   return -1;  
  32.  }  
  33.   
  34.  // Read data shuold be "world".  
  35.  if (strcmp(buf, "world")) {  
  36.   printf("read buf isn't \"world\"\n");  
  37.   return -1;  
  38.  }  
  39.   
  40.  off = lseek(fd, 0, SEEK_CUR);  
  41.  // cursor should be zero.  
  42.  if (off) {  
  43.   printf("cursor isn't 0\n");  
  44.   return -1;  
  45.  }  
  46.   
  47.  strcpy(buf, "WORLD");  
  48.  errno = 0;  
  49.  ret = pwrite(fd, buf, strlen(buf), 6);  
  50.  if (errno) {  
  51.   printf("pwrite faild\n");  
  52.   return -1;  
  53.  }  
  54.    
  55.  pread(fd, &buf, read_byte, offset);  
  56.  // Read data shuold be "world".  
  57.  if (strcmp(buf, "WORLD")) {  
  58.   printf("read buf isn't \"world\"\n");  
  59.   return -1;  
  60.  }  
  61.   
  62.  off = lseek(fd, 0, SEEK_CUR);  
  63.  // cursor should be zero.  
  64.  if (off) {  
  65.   printf("cursor isn't 0\n");  
  66.   return -1;  
  67.  }  
  68.    
  69.  close(fd);  
  70.   
  71.  printf("Done.\n");  
  72.   
  73.  return 0;  
  74. }  
  75. </errno.h></sys></sys></fcntl.h></unistd.h></string.h></stdio.h>  
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