blob: 265bd24df222f5247da2ad599ee8945688983b12 [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
Tom Wai-Hong Tamee2bc912011-02-17 12:58:58 +080092 def doPackUnpackZ(self, comp):
93 """Create, unpack, recreate with a given compression"""
94 rc, out, err = runprog(prog, '-z', comp, '-c', 'case_simple.yaml', 'FOO')
Bill Richardson61362d62011-02-14 10:28:03 -080095 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')
Tom Wai-Hong Tamee2bc912011-02-17 12:58:58 +080099 rc, out, err = runprog(prog, '-z', comp, '-c', 'config.yaml', 'BAR')
Bill Richardson61362d62011-02-14 10:28:03 -0800100 self.assertEqual(0, rc)
101 rc, out, err = runprog('/usr/bin/cmp', '../FOO', 'BAR')
102 self.assertEqual(0, rc)
103 os.chdir('..')
104
Tom Wai-Hong Tamee2bc912011-02-17 12:58:58 +0800105 def testPackUnpackZ1(self):
106 """Create, unpack, recreate with EFIv1 compression"""
107 self.doPackUnpackZ('1');
108
109 def testPackUnpackZ2(self):
110 """Create, unpack, recreate with LZMA compression"""
111 self.doPackUnpackZ('2');
112
Bill Richardson61362d62011-02-14 10:28:03 -0800113 def tearDown(self):
114 rc, out, err = runprog('/bin/rm', '-rf', './FOO_DIR', 'FOO')
115 self.assertEqual(0, rc)
116
Bill Richardson856e0722011-02-07 15:39:45 -0800117
Bill Richardsonf82f9412011-02-17 08:56:33 -0800118class TestReproducable(unittest.TestCase):
119
120 def setUp(self):
121 rc, out, err = runprog('/bin/rm', '-f', 'ORDER1', 'ORDER2')
122 self.assertEqual(0, rc)
123
124 def testReproduce(self):
125 """Equivalent yaml files should produce identical bmpblocks"""
126 rc, out, err = runprog(prog, '-c', 'case_order1.yaml', 'ORDER1')
127 self.assertEqual(0, rc)
128 rc, out, err = runprog(prog, '-c', 'case_order2.yaml', 'ORDER2')
129 self.assertEqual(0, rc)
130 rc, out, err = runprog('/usr/bin/cmp', 'ORDER1', 'ORDER2')
131 self.assertEqual(0, rc)
132
133 def tearDown(self):
134 rc, out, err = runprog('/bin/rm', '-f', 'ORDER1', 'ORDER2')
135 self.assertEqual(0, rc)
136
Bill Richardsonf456e832011-02-17 11:29:51 -0800137class TestReuse(unittest.TestCase):
138
139 def setUp(self):
140 rc, out, err = runprog('/bin/rm', '-rf', './FOO_DIR', 'FOO')
141 self.assertEqual(0, rc)
142
143 def testReuse(self):
144 """Reusing screens in the yaml file should be okay"""
145 rc, out, err = runprog(prog, '-c', 'case_reuse.yaml', 'FOO')
146 self.assertEqual(0, rc)
147 rc, out, err = runprog(prog, '-x', '-d', './FOO_DIR', 'FOO')
148 self.assertEqual(0, rc)
149 os.chdir('./FOO_DIR')
150 rc, out, err = runprog(prog, '-c', 'config.yaml', 'BAR')
151 self.assertEqual(0, rc)
152 rc, out, err = runprog('/usr/bin/cmp', '../FOO', 'BAR')
153 self.assertEqual(0, rc)
154 os.chdir('..')
155
156 def tearDown(self):
157 rc, out, err = runprog('/bin/rm', '-rf', './FOO_DIR', 'FOO')
158 self.assertEqual(0, rc)
159
Bill Richardsonf82f9412011-02-17 08:56:33 -0800160
Bill Richardson856e0722011-02-07 15:39:45 -0800161# Run these tests
162if __name__ == '__main__':
163 varname = 'BMPBLK'
164 if varname not in os.environ:
165 print('You must specify the path to bmpblk_utility in the $%s '
166 'environment variable.' % varname)
167 sys.exit(1)
168 prog = os.environ[varname]
169 print "Testing prog...", prog
170 unittest.main()
171