Bill Richardson | 856e072 | 2011-02-07 15:39:45 -0800 | [diff] [blame] | 1 | #!/usr/bin/python -tt |
| 2 | |
| 3 | """Unit tests for bmpblk_utility. |
| 4 | """ |
| 5 | |
| 6 | import os |
| 7 | import sys |
| 8 | import subprocess |
| 9 | import unittest |
| 10 | |
| 11 | def runprog(*args): |
| 12 | """Runs specified program and args, returns (exitcode, stdout, stderr).""" |
| 13 | p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 14 | out, err = p.communicate() |
| 15 | return (p.returncode, out, err) |
| 16 | |
| 17 | |
| 18 | class TestBmpBlock(unittest.TestCase): |
| 19 | |
| 20 | def testNoArgs(self): |
| 21 | """Running with no args should print usage and fail.""" |
| 22 | rc, out, err = runprog(prog) |
| 23 | self.assertNotEqual(0, rc) |
| 24 | self.assertTrue(out.count("Usage:")) |
| 25 | |
| 26 | def testMissingBmp(self): |
| 27 | """Missing a bmp specified in the yaml is an error.""" |
| 28 | rc, out, err = runprog(prog, '-c', '-C', 'case_nobmp.yaml', 'FOO') |
| 29 | self.assertNotEqual(0, rc) |
| 30 | self.assertTrue(err.count("No such file or directory")) |
| 31 | |
| 32 | def testInvalidBmp(self): |
| 33 | """A .bmp file that isn't really a BMP should fail.""" |
| 34 | rc, out, err = runprog(prog, '-c', '-C', 'case_badbmp.yaml', 'FOO') |
| 35 | self.assertNotEqual(0, rc) |
| 36 | self.assertTrue(err.count("Unsupported image format")) |
| 37 | |
| 38 | |
| 39 | # Run these tests |
| 40 | if __name__ == '__main__': |
| 41 | varname = 'BMPBLK' |
| 42 | if varname not in os.environ: |
| 43 | print('You must specify the path to bmpblk_utility in the $%s ' |
| 44 | 'environment variable.' % varname) |
| 45 | sys.exit(1) |
| 46 | prog = os.environ[varname] |
| 47 | print "Testing prog...", prog |
| 48 | unittest.main() |
| 49 | |