Browse Source

[2621] Fix session_test for python3.3

Or more specifically, make the tests not depend on dict ordering
Jelte Jansen 12 years ago
parent
commit
d8c2ea58e0
1 changed files with 76 additions and 40 deletions
  1. 76 40
      src/lib/python/isc/cc/tests/session_test.py

+ 76 - 40
src/lib/python/isc/cc/tests/session_test.py

@@ -19,6 +19,7 @@
 
 import unittest
 import os
+import json
 from isc.cc.session import *
 
 # our fake socket, where we can read and insert messages
@@ -73,8 +74,23 @@ class MySocket():
 
         result.extend(self.readsent(header_length))
         result.extend(self.readsent(data_length))
+
         return result
 
+    def readsentmsg_parsed(self):
+        length_buf = self.readsent(4)
+        length = struct.unpack('>I', length_buf)[0]
+        header_length_buf = self.readsent(2)
+        header_length = struct.unpack('>H', header_length_buf)[0]
+        data_length = length - 2 - header_length
+
+        env = json.loads(self.readsent(header_length).decode('utf-8'), strict=False)
+        if (data_length > 0):
+            msg = json.loads(self.readsent(data_length).decode('utf-8'), strict=False)
+        else:
+            msg = {}
+        return (env, msg)
+
     def recv(self, length):
         if len(self.recvqueue) == 0:
             if self._blocking:
@@ -208,25 +224,25 @@ class testSession(unittest.TestCase):
 
         # 'malformed' messages
         # shouldn't some of these raise exceptions?
-        #self.recv_and_compare(sess, 
+        #self.recv_and_compare(sess,
         #                      b'\x00',
         #                      None, None)
-        #self.recv_and_compare(sess, 
+        #self.recv_and_compare(sess,
         #                      b'\x00\x00\x00\x10',
         #                      None, None)
-        #self.recv_and_compare(sess, 
+        #self.recv_and_compare(sess,
         #                      b'\x00\x00\x00\x02\x00\x00',
         #                      None, None)
-        #self.recv_and_compare(sess, 
+        #self.recv_and_compare(sess,
         #                      b'\x00\x00\x00\x02\x00\x02',
         #                      None, None)
-        #self.recv_and_compare(sess, 
+        #self.recv_and_compare(sess,
         #                      b'',
         #                      None, None)
 
         # need to clear
         sess._socket.recvqueue = bytearray()
-        
+
         # 'queueing' system
         # sending message {'to': 'someone', 'reply': 1}, {"hello": "a"}
         #print("sending message {'to': 'someone', 'reply': 1}, {'hello': 'a'}")
@@ -240,7 +256,7 @@ class testSession(unittest.TestCase):
         self.assertEqual({'to': 'someone', 'reply': 1}, env)
         self.assertEqual({"hello": "a"}, msg)
         self.assertFalse(sess.has_queued_msgs())
-        
+
         # ask for a differe sequence number reply (that doesn't exist)
         # then ask for the one that is there
         self.assertFalse(sess.has_queued_msgs())
@@ -253,7 +269,7 @@ class testSession(unittest.TestCase):
         self.assertEqual({'to': 'someone', 'reply': 1}, env)
         self.assertEqual({"hello": "a"}, msg)
         self.assertFalse(sess.has_queued_msgs())
-        
+
         # ask for a differe sequence number reply (that doesn't exist)
         # then ask for any message
         self.assertFalse(sess.has_queued_msgs())
@@ -266,7 +282,7 @@ class testSession(unittest.TestCase):
         self.assertEqual({'to': 'someone', 'reply': 1}, env)
         self.assertEqual({"hello": "a"}, msg)
         self.assertFalse(sess.has_queued_msgs())
-        
+
         #print("sending message {'to': 'someone', 'reply': 1}, {'hello': 'a'}")
 
         # ask for a differe sequence number reply (that doesn't exist)
@@ -287,7 +303,7 @@ class testSession(unittest.TestCase):
         self.assertEqual({'to': 'someone'}, env)
         self.assertEqual({"hello": "b"}, msg)
         self.assertFalse(sess.has_queued_msgs())
-        
+
         # send a message, then one with specific reply value
         # ask for that specific message (get the second)
         # then ask for any message (get the first)
@@ -326,48 +342,56 @@ class testSession(unittest.TestCase):
     def test_group_subscribe(self):
         sess = MySession()
         sess.group_subscribe("mygroup")
-        sent = sess._socket.readsentmsg()
-        self.assertEqual(sent, b'\x00\x00\x00<\x00:{"group": "mygroup", "type": "subscribe", "instance": "*"}')
-        
+        sent = sess._socket.readsentmsg_parsed()
+        self.assertEqual(sent, ({"group": "mygroup", "type": "subscribe",
+                                 "instance": "*"}, {}))
+
         sess.group_subscribe("mygroup")
-        sent = sess._socket.readsentmsg()
-        self.assertEqual(sent, b'\x00\x00\x00<\x00:{"group": "mygroup", "type": "subscribe", "instance": "*"}')
-        
+        sent = sess._socket.readsentmsg_parsed()
+        self.assertEqual(sent, ({"group": "mygroup", "type": "subscribe",
+                                 "instance": "*"}, {}))
+
         sess.group_subscribe("mygroup", "my_instance")
-        sent = sess._socket.readsentmsg()
-        self.assertEqual(sent, b'\x00\x00\x00F\x00D{"group": "mygroup", "type": "subscribe", "instance": "my_instance"}')
+        sent = sess._socket.readsentmsg_parsed()
+        self.assertEqual(sent, ({"group": "mygroup", "type": "subscribe",
+                                 "instance": "my_instance"}, {}))
 
     def test_group_unsubscribe(self):
         sess = MySession()
         sess.group_unsubscribe("mygroup")
-        sent = sess._socket.readsentmsg()
-        self.assertEqual(sent, b'\x00\x00\x00>\x00<{"group": "mygroup", "type": "unsubscribe", "instance": "*"}')
-        
+        sent = sess._socket.readsentmsg_parsed()
+        self.assertEqual(sent, ({"group": "mygroup", "type": "unsubscribe",
+                                 "instance": "*"}, {}))
+
         sess.group_unsubscribe("mygroup")
-        sent = sess._socket.readsentmsg()
-        self.assertEqual(sent, b'\x00\x00\x00>\x00<{"group": "mygroup", "type": "unsubscribe", "instance": "*"}')
-        
+        sent = sess._socket.readsentmsg_parsed()
+        self.assertEqual(sent, ({"group": "mygroup", "type": "unsubscribe",
+                                 "instance": "*"}, {}))
+
         sess.group_unsubscribe("mygroup", "my_instance")
-        sent = sess._socket.readsentmsg()
-        self.assertEqual(sent, b'\x00\x00\x00H\x00F{"group": "mygroup", "type": "unsubscribe", "instance": "my_instance"}')
+        sent = sess._socket.readsentmsg_parsed()
+        self.assertEqual(sent, ({"group": "mygroup", "type": "unsubscribe",
+                                 "instance": "my_instance"}, {}))
 
     def test_group_sendmsg(self):
         sess = MySession()
         self.assertEqual(sess._sequence, 1)
 
         sess.group_sendmsg({ 'hello': 'a' }, "my_group")
-        sent = sess._socket.readsentmsg()
-        self.assertEqual(sent, b'\x00\x00\x00p\x00`{"from": "test_name", "seq": 2, "to": "*", "instance": "*", "group": "my_group", "type": "send"}{"hello": "a"}')
+        sent = sess._socket.readsentmsg_parsed()
+        self.assertEqual(sent, ({"from": "test_name", "seq": 2, "to": "*",
+                                 "instance": "*", "group": "my_group",
+                                 "type": "send"}, {"hello": "a"}))
         self.assertEqual(sess._sequence, 2)
 
         sess.group_sendmsg({ 'hello': 'a' }, "my_group", "my_instance")
-        sent = sess._socket.readsentmsg()
-        self.assertEqual(sent, b'\x00\x00\x00z\x00j{"from": "test_name", "seq": 3, "to": "*", "instance": "my_instance", "group": "my_group", "type": "send"}{"hello": "a"}')
+        sent = sess._socket.readsentmsg_parsed()
+        self.assertEqual(sent, ({"from": "test_name", "seq": 3, "to": "*", "instance": "my_instance", "group": "my_group", "type": "send"}, {"hello": "a"}))
         self.assertEqual(sess._sequence, 3)
-        
+
         sess.group_sendmsg({ 'hello': 'a' }, "your_group", "your_instance")
-        sent = sess._socket.readsentmsg()
-        self.assertEqual(sent, b'\x00\x00\x00~\x00n{"from": "test_name", "seq": 4, "to": "*", "instance": "your_instance", "group": "your_group", "type": "send"}{"hello": "a"}')
+        sent = sess._socket.readsentmsg_parsed()
+        self.assertEqual(sent, ({"from": "test_name", "seq": 4, "to": "*", "instance": "your_instance", "group": "your_group", "type": "send"}, {"hello": "a"}))
         self.assertEqual(sess._sequence, 4)
 
     def test_group_recvmsg(self):
@@ -377,13 +401,25 @@ class testSession(unittest.TestCase):
 
     def test_group_reply(self):
         sess = MySession()
-        sess.group_reply({ 'from': 'me', 'group': 'our_group', 'instance': 'other_instance', 'seq': 4}, {"hello": "a"})
-        sent = sess._socket.readsentmsg();
-        self.assertEqual(sent, b'\x00\x00\x00\x8b\x00{{"from": "test_name", "seq": 2, "to": "me", "instance": "other_instance", "reply": 4, "group": "our_group", "type": "send"}{"hello": "a"}')
-        
-        sess.group_reply({ 'from': 'me', 'group': 'our_group', 'instance': 'other_instance', 'seq': 9}, {"hello": "a"})
-        sent = sess._socket.readsentmsg();
-        self.assertEqual(sent, b'\x00\x00\x00\x8b\x00{{"from": "test_name", "seq": 3, "to": "me", "instance": "other_instance", "reply": 9, "group": "our_group", "type": "send"}{"hello": "a"}')
+        sess.group_reply({ 'from': 'me', 'group': 'our_group',
+                           'instance': 'other_instance', 'seq': 4},
+                         {"hello": "a"})
+        sent = sess._socket.readsentmsg_parsed();
+        self.assertEqual(sent, ({"from": "test_name", "seq": 2,
+                                 "to": "me", "instance": "other_instance",
+                                 "reply": 4, "group": "our_group",
+                                 "type": "send"},
+                                {"hello": "a"}))
+
+        sess.group_reply({ 'from': 'me', 'group': 'our_group',
+                           'instance': 'other_instance', 'seq': 9},
+                         {"hello": "a"})
+        sent = sess._socket.readsentmsg_parsed();
+        self.assertEqual(sent, ({"from": "test_name", "seq": 3,
+                                 "to": "me", "instance": "other_instance",
+                                 "reply": 9, "group": "our_group",
+                                 "type": "send"},
+                                {"hello": "a"}))
 
     def test_timeout(self):
         if "BIND10_TEST_SOCKET_FILE" not in os.environ: