root / joy / test / .svn / text-base / test_joy_msg_migration.py.svn-base @ ba6306a1
History | View | Annotate | Download (4.48 KB)
1 |
#!/usr/bin/env python |
---|---|
2 |
# Software License Agreement (BSD License) |
3 |
# |
4 |
# Copyright (c) 2008, Willow Garage, Inc. |
5 |
# All rights reserved. |
6 |
# |
7 |
# Redistribution and use in source and binary forms, with or without |
8 |
# modification, are permitted provided that the following conditions |
9 |
# are met: |
10 |
# |
11 |
# * Redistributions of source code must retain the above copyright |
12 |
# notice, this list of conditions and the following disclaimer. |
13 |
# * Redistributions in binary form must reproduce the above |
14 |
# copyright notice, this list of conditions and the following |
15 |
# disclaimer in the documentation and/or other materials provided |
16 |
# with the distribution. |
17 |
# * Neither the name of Willow Garage, Inc. nor the names of its |
18 |
# contributors may be used to endorse or promote products derived |
19 |
# from this software without specific prior written permission. |
20 |
# |
21 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
22 |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
23 |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
24 |
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
25 |
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
26 |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
27 |
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
28 |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
29 |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
31 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 |
# POSSIBILITY OF SUCH DAMAGE. |
33 |
# |
34 |
|
35 |
import roslib |
36 |
roslib.load_manifest('joy') |
37 |
|
38 |
import sys |
39 |
import struct |
40 |
|
41 |
import unittest |
42 |
|
43 |
import rostest |
44 |
import rosbag |
45 |
import rosbagmigration |
46 |
|
47 |
import re |
48 |
from cStringIO import StringIO |
49 |
import os |
50 |
|
51 |
import rospy |
52 |
|
53 |
|
54 |
|
55 |
migrator = rosbagmigration.MessageMigrator() |
56 |
|
57 |
|
58 |
def repack(x): |
59 |
return struct.unpack('<f',struct.pack('<f',x))[0] |
60 |
|
61 |
class TestJoyMsgsMigration(unittest.TestCase): |
62 |
|
63 |
# (*) Joy.saved |
64 |
|
65 |
########### Joy ############### |
66 |
|
67 |
|
68 |
def get_old_joy(self): |
69 |
joy_classes = self.load_saved_classes('Joy.saved') |
70 |
joy = joy_classes['joy/Joy'] |
71 |
return joy([0.1,0.2,0.3,0.4,0.5],[0,1,0,1,0]) |
72 |
|
73 |
def get_new_joy(self): |
74 |
from sensor_msgs.msg import Joy |
75 |
from roslib.msg import Header |
76 |
return Joy(Header(),[0.1,0.2,0.3,0.4,0.5],[0,1,0,1,0]) |
77 |
|
78 |
|
79 |
def test_joy(self): |
80 |
self.do_test('joy', self.get_old_joy, self.get_new_joy) |
81 |
|
82 |
########### Helper functions ########### |
83 |
|
84 |
def setUp(self): |
85 |
self.pkg_dir = roslib.packages.get_pkg_dir("joy") |
86 |
|
87 |
|
88 |
def load_saved_classes(self,saved_msg): |
89 |
f = open("%s/test/saved/%s"%(self.pkg_dir,saved_msg), 'r') |
90 |
|
91 |
type_line = f.readline() |
92 |
pat = re.compile(r"\[(.*)]:") |
93 |
type_match = pat.match(type_line) |
94 |
|
95 |
self.assertTrue(type_match is not None, "Full definition file malformed. First line should be: '[my_package/my_msg]:'") |
96 |
|
97 |
saved_type = type_match.groups()[0] |
98 |
saved_full_text = f.read() |
99 |
|
100 |
saved_classes = roslib.genpy.generate_dynamic(saved_type,saved_full_text) |
101 |
|
102 |
self.assertTrue(saved_classes is not None, "Could not generate class from full definition file.") |
103 |
self.assertTrue(saved_classes.has_key(saved_type), "Could not generate class from full definition file.") |
104 |
|
105 |
return saved_classes |
106 |
|
107 |
def do_test(self, name, old_msg, new_msg): |
108 |
# Name the bags |
109 |
oldbag = "%s/test/%s_old.bag"%(self.pkg_dir,name) |
110 |
newbag = "%s/test/%s_new.bag"%(self.pkg_dir,name) |
111 |
|
112 |
# Create an old message |
113 |
bag = rosbag.Bag(oldbag, 'w') |
114 |
bag.write("topic", old_msg(), roslib.rostime.Time()) |
115 |
bag.close() |
116 |
|
117 |
# Check and migrate |
118 |
res = rosbagmigration.checkbag(migrator, oldbag) |
119 |
self.assertTrue(not False in [m[1] == [] for m in res], 'Bag not ready to be migrated') |
120 |
res = rosbagmigration.fixbag(migrator, oldbag, newbag) |
121 |
self.assertTrue(res, 'Bag not converted successfully') |
122 |
|
123 |
# Pull the first message out of the bag |
124 |
topic, msg, t = rosbag.Bag(newbag).read_messages().next() |
125 |
|
126 |
# Reserialize the new message so that floats get screwed up, etc. |
127 |
m = new_msg() |
128 |
buff = StringIO() |
129 |
m.serialize(buff) |
130 |
m.deserialize(buff.getvalue()) |
131 |
|
132 |
# Strifying them helps make the comparison easier until I figure out why the equality operator is failing |
133 |
self.assertTrue(roslib.message.strify_message(msg) == roslib.message.strify_message(m)) |
134 |
# self.assertTrue(msgs[0][1] == m) |
135 |
|
136 |
#Cleanup |
137 |
os.remove(oldbag) |
138 |
os.remove(newbag) |
139 |
|
140 |
|
141 |
if __name__ == '__main__': |
142 |
rostest.unitrun('test_joy_msg', 'test_joy_msg_migration', TestJoyMsgsMigration, sys.argv) |