blob: ced270ee0cc908c42cb7a83480177b71494b0c25 [file] [log] [blame]
Bill Richardson856e0722011-02-07 15:39:45 -08001#!/usr/bin/python -tt
Bill Richardson794d4d42011-02-10 19:13:10 -08002#
3# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
Bill Richardson856e0722011-02-07 15:39:45 -08006
7"""Unit tests for bmpblk_utility.
8"""
9
10import os
11import sys
12import subprocess
13import unittest
14
15def runprog(*args):
16 """Runs specified program and args, returns (exitcode, stdout, stderr)."""
17 p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
18 out, err = p.communicate()
19 return (p.returncode, out, err)
20
21
Bill Richardson61362d62011-02-14 10:28:03 -080022class TestFailures(unittest.TestCase):
Bill Richardson856e0722011-02-07 15:39:45 -080023
24 def testNoArgs(self):
25 """Running with no args should print usage and fail."""
26 rc, out, err = runprog(prog)
27 self.assertNotEqual(0, rc)
Bill Richardson794d4d42011-02-10 19:13:10 -080028 self.assertTrue(err.count("missing BMPBLOCK name"))
29 self.assertTrue(out.count("bmpblk_utility"))
Bill Richardson856e0722011-02-07 15:39:45 -080030
31 def testMissingBmp(self):
32 """Missing a bmp specified in the yaml is an error."""
Bill Richardson794d4d42011-02-10 19:13:10 -080033 rc, out, err = runprog(prog, '-c', 'case_nobmp.yaml', 'FOO')
Bill Richardson856e0722011-02-07 15:39:45 -080034 self.assertNotEqual(0, rc)
35 self.assertTrue(err.count("No such file or directory"))
36
37 def testInvalidBmp(self):
38 """A .bmp file that isn't really a BMP should fail."""
Bill Richardson794d4d42011-02-10 19:13:10 -080039 rc, out, err = runprog(prog, '-c', 'case_badbmp.yaml', 'FOO')
Bill Richardson856e0722011-02-07 15:39:45 -080040 self.assertNotEqual(0, rc)
41 self.assertTrue(err.count("Unsupported image format"))
42
Bill Richardson61362d62011-02-14 10:28:03 -080043 def testBadCompression(self):
44 """Wrong compression types should fail."""
45 rc, out, err = runprog(prog, '-z', '99', '-c', 'case_simple.yaml', 'FOO')
46 self.assertNotEqual(0, rc)
47 self.assertTrue(err.count("compression type"))
48
49
50class TestOverWrite(unittest.TestCase):
51
52 def setUp(self):
53 rc, out, err = runprog('/bin/rm', '-rf', './FOO_DIR', 'FOO')
54 self.assertEqual(0, rc)
55
56 def testOverwrite(self):
57 """Create, unpack, unpack again, with and without -f"""
58 rc, out, err = runprog(prog, '-c', 'case_simple.yaml', 'FOO')
59 self.assertEqual(0, rc)
60 rc, out, err = runprog(prog, '-x', '-d', './FOO_DIR', 'FOO')
61 self.assertEqual(0, rc)
62 rc, out, err = runprog(prog, '-x', '-d', './FOO_DIR', 'FOO')
63 self.assertNotEqual(0, rc)
64 self.assertTrue(err.count("File exists"))
65 rc, out, err = runprog(prog, '-x', '-d', './FOO_DIR', '-f', 'FOO')
66 self.assertEqual(0, rc)
67
68 def tearDown(self):
69 rc, out, err = runprog('/bin/rm', '-rf', './FOO_DIR', 'FOO')
70 self.assertEqual(0, rc)
71
72
73class TestPackUnpack(unittest.TestCase):
74
75 def setUp(self):
76 rc, out, err = runprog('/bin/rm', '-rf', './FOO_DIR', 'FOO')
77 self.assertEqual(0, rc)
78
79 def testPackUnpack(self):
80 """Create, unpack, recreate without compression"""
81 rc, out, err = runprog(prog, '-c', 'case_simple.yaml', 'FOO')
82 self.assertEqual(0, rc)
83 rc, out, err = runprog(prog, '-x', '-d', './FOO_DIR', 'FOO')
84 self.assertEqual(0, rc)
85 os.chdir('./FOO_DIR')
86 rc, out, err = runprog(prog, '-c', 'config.yaml', 'BAR')
87 self.assertEqual(0, rc)
88 rc, out, err = runprog('/usr/bin/cmp', '../FOO', 'BAR')
89 self.assertEqual(0, rc)
90 os.chdir('..')
91
92 def testPackUnpackZ(self):
93 """Create, unpack, recreate with explicit compression"""
94 rc, out, err = runprog(prog, '-z', '1', '-c', 'case_simple.yaml', 'FOO')
95 self.assertEqual(0, rc)
96 rc, out, err = runprog(prog, '-x', '-d', './FOO_DIR', 'FOO')
97 self.assertEqual(0, rc)
98 os.chdir('./FOO_DIR')
99 rc, out, err = runprog(prog, '-z', '1', '-c', 'config.yaml', 'BAR')
100 self.assertEqual(0, rc)
101 rc, out, err = runprog('/usr/bin/cmp', '../FOO', 'BAR')
102 self.assertEqual(0, rc)
103 os.chdir('..')
104
105 def tearDown(self):
106 rc, out, err = runprog('/bin/rm', '-rf', './FOO_DIR', 'FOO')
107 self.assertEqual(0, rc)
108
Bill Richardson856e0722011-02-07 15:39:45 -0800109
110# Run these tests
111if __name__ == '__main__':
112 varname = 'BMPBLK'
113 if varname not in os.environ:
114 print('You must specify the path to bmpblk_utility in the $%s '
115 'environment variable.' % varname)
116 sys.exit(1)
117 prog = os.environ[varname]
118 print "Testing prog...", prog
119 unittest.main()
120