1#!/usr/bin/env python 2# Copyright 2012 Google Inc. All Rights Reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16"""Unit tests for replay. 17 18Usage: 19$ ./replay_test.py 20""" 21 22import replay 23import unittest 24 25 26class MockOptions(dict): 27 """A dict with items that can be accessed as attributes.""" 28 def __getattr__(self, name): 29 return self[name] 30 31 32class OptionsWrapperTest(unittest.TestCase): 33 34 def testNoTrafficShapingByDefault(self): 35 parser = replay.GetParser() 36 options = parser.parse_args([]) 37 options = replay.OptionsWrapper(options, parser) 38 self.assertEqual({}, options.shaping_dns) 39 self.assertEqual({}, options.shaping_http) 40 self.assertEqual({}, options.shaping_dummynet) 41 42 def testShapingProxyWithoutOptionsGivesEmptySettings(self): 43 parser = replay.GetParser() 44 options = parser.parse_args(['--shaping=proxy']) 45 options = replay.OptionsWrapper(options, parser) 46 self.assertEqual({}, options.shaping_dns) 47 self.assertEqual({}, options.shaping_http) 48 self.assertEqual({}, options.shaping_dummynet) 49 50 def testShapingProxyWithNetOption(self): 51 parser = replay.GetParser() 52 options = parser.parse_args(['--shaping=proxy', '--net=cable']) 53 options = replay.OptionsWrapper(options, parser) 54 expected_http = { 55 'down_bandwidth': '5Mbit/s', 'delay_ms': '28', 'up_bandwidth': '1Mbit/s' 56 } 57 self.assertEqual({'delay_ms': '28'}, options.shaping_dns) 58 self.assertEqual(expected_http, options.shaping_http) 59 self.assertEqual({}, options.shaping_dummynet) 60 61 def testNetOptionUsesDummynetByDefault(self): 62 parser = replay.GetParser() 63 options = parser.parse_args(['--net=cable']) 64 options = replay.OptionsWrapper(options, parser) 65 expected_dummynet = { 66 'down_bandwidth': '5Mbit/s', 'delay_ms': '28', 'up_bandwidth': '1Mbit/s' 67 } 68 self.assertEqual({}, options.shaping_dns) 69 self.assertEqual({}, options.shaping_http) 70 self.assertEqual(expected_dummynet, options.shaping_dummynet) 71 72 def testPacketLossForDummynet(self): 73 parser = replay.GetParser() 74 options = parser.parse_args(['--packet_loss_rate=12']) 75 options = replay.OptionsWrapper(options, parser) 76 self.assertEqual({'packet_loss_rate': '12'}, options.shaping_dummynet) 77 78 def testIgnoredProxyShapingOptions(self): 79 parser = replay.GetParser() 80 options = parser.parse_args( 81 ['--packet_loss_rate=12', '--init_cwnd=10', '--shaping=proxy']) 82 options = replay.OptionsWrapper(options, parser) 83 self.assertEqual({}, options.shaping_dns) 84 self.assertEqual({}, options.shaping_http) 85 self.assertEqual({}, options.shaping_dummynet) 86 87 88if __name__ == '__main__': 89 unittest.main() 90