2 # Copyright 2012 Google Inc. All Rights Reserved.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 """Unit tests for replay.
26 class MockOptions(dict):
27 """A dict with items that can be accessed as attributes."""
28 def __getattr__(self
, name
):
32 class OptionsWrapperTest(unittest
.TestCase
):
34 def testNoTrafficShapingByDefault(self
):
35 parser
= replay
.GetOptionParser()
36 options
, args
= 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
)
42 def testShapingProxyWithoutOptionsGivesEmptySettings(self
):
43 parser
= replay
.GetOptionParser()
44 options
, args
= 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
)
50 def testShapingProxyWithNetOption(self
):
51 parser
= replay
.GetOptionParser()
52 options
, args
= parser
.parse_args(['--shaping=proxy', '--net=cable'])
53 options
= replay
.OptionsWrapper(options
, parser
)
55 'down_bandwidth': '5Mbit/s', 'delay_ms': '28', 'up_bandwidth': '1Mbit/s'
57 self
.assertEqual({'delay_ms': '28'}, options
.shaping_dns
)
58 self
.assertEqual(expected_http
, options
.shaping_http
)
59 self
.assertEqual({}, options
.shaping_dummynet
)
61 def testNetOptionUsesDummynetByDefault(self
):
62 parser
= replay
.GetOptionParser()
63 options
, args
= parser
.parse_args(['--net=cable'])
64 options
= replay
.OptionsWrapper(options
, parser
)
66 'down_bandwidth': '5Mbit/s', 'delay_ms': '28', 'up_bandwidth': '1Mbit/s'
68 self
.assertEqual({}, options
.shaping_dns
)
69 self
.assertEqual({}, options
.shaping_http
)
70 self
.assertEqual(expected_dummynet
, options
.shaping_dummynet
)
72 def testPacketLossForDummynet(self
):
73 parser
= replay
.GetOptionParser()
74 options
, args
= parser
.parse_args(['--packet_loss_rate=12'])
75 options
= replay
.OptionsWrapper(options
, parser
)
76 self
.assertEqual({'packet_loss_rate': '12'}, options
.shaping_dummynet
)
78 def testIgnoredProxyShapingOptions(self
):
79 parser
= replay
.GetOptionParser()
80 options
, args
= 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
)
88 if __name__
== '__main__':