|
| 1 | +# Copyright 2018, OpenCensus Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import sys |
| 16 | +import unittest |
| 17 | + |
| 18 | +from opencensus.trace.tracestate import Tracestate |
| 19 | +from opencensus.trace.propagation.tracestate_string_format \ |
| 20 | + import TracestateStringFormatter |
| 21 | + |
| 22 | +formatter = TracestateStringFormatter() |
| 23 | + |
| 24 | +class TestTracestate(unittest.TestCase): |
| 25 | + |
| 26 | + def test_ctor_no_arg(self): |
| 27 | + state = Tracestate() |
| 28 | + self.assertEqual(formatter.to_string(state), '') |
| 29 | + |
| 30 | + def test_ctor_with_dict(self): |
| 31 | + state = Tracestate({'foo': '1'}) |
| 32 | + self.assertEqual(formatter.to_string(state), 'foo=1') |
| 33 | + |
| 34 | + def test_cctor(self): |
| 35 | + state = Tracestate(formatter.from_string('foo=1,bar=2,baz=3')) |
| 36 | + self.assertEqual(formatter.to_string(state), 'foo=1,bar=2,baz=3') |
| 37 | + |
| 38 | + def test_all_allowed_chars(self): |
| 39 | + header = ''.join([ |
| 40 | + # key |
| 41 | + ''.join(map(chr, range(0x61, 0x7A + 1))), # lcalpha |
| 42 | + '0123456789', # DIGIT |
| 43 | + '_', |
| 44 | + '-', |
| 45 | + '*', |
| 46 | + '/', |
| 47 | + # "=" |
| 48 | + '=', |
| 49 | + # value |
| 50 | + ''.join(map(chr, range(0x20, 0x2B + 1))), |
| 51 | + ''.join(map(chr, range(0x2D, 0x3C + 1))), |
| 52 | + ''.join(map(chr, range(0x3E, 0x7E + 1))), |
| 53 | + ]) |
| 54 | + state = formatter.from_string(header) |
| 55 | + self.assertEqual(formatter.to_string(state), header) |
| 56 | + |
| 57 | + def test_delimiter(self): |
| 58 | + state = formatter.from_string('foo=1, \t bar=2') |
| 59 | + self.assertEqual(formatter.to_string(state), 'foo=1,bar=2') |
| 60 | + |
| 61 | + state = formatter.from_string('foo=1,\t \tbar=2') |
| 62 | + self.assertEqual(formatter.to_string(state), 'foo=1,bar=2') |
| 63 | + |
| 64 | + def test_get(self): |
| 65 | + state = Tracestate({'foo': '1'}) |
| 66 | + self.assertEqual(state['foo'], '1') |
| 67 | + |
| 68 | + def test_method_append(self): |
| 69 | + state = Tracestate() |
| 70 | + state.append('foo', '1') |
| 71 | + state.append('bar', '2') |
| 72 | + state.append('baz', '3') |
| 73 | + state.append('bar', '2') |
| 74 | + self.assertEqual(formatter.to_string(state), 'foo=1,baz=3,bar=2') |
| 75 | + |
| 76 | + def test_method_from_string(self): |
| 77 | + state = formatter.from_string('foo=1,bar=2,baz=3') |
| 78 | + self.assertEqual(formatter.to_string(state), 'foo=1,bar=2,baz=3') |
| 79 | + |
| 80 | + self.assertRaises(ValueError, lambda: formatter.from_string('#=#')) |
| 81 | + |
| 82 | + def test_method_get(self): |
| 83 | + state = formatter.from_string('foo=1, bar=2, baz=3') |
| 84 | + self.assertEqual(state.get('bar'), '2') |
| 85 | + |
| 86 | + def test_method_is_valid(self): |
| 87 | + state = Tracestate() |
| 88 | + |
| 89 | + # empty state not allowed |
| 90 | + self.assertFalse(state.is_valid()) |
| 91 | + |
| 92 | + state['foo'] = 'x' * 256 |
| 93 | + self.assertTrue(state.is_valid()) |
| 94 | + |
| 95 | + # exceeds 32 elements |
| 96 | + for i in range(0xa0, 0xa0 + 31): |
| 97 | + state['%x' % (i)] = 'E' |
| 98 | + self.assertEqual(len(state), 32) |
| 99 | + self.assertTrue(state.is_valid()) |
| 100 | + state['ff'] = 'E' |
| 101 | + self.assertFalse(state.is_valid()) |
| 102 | + |
| 103 | + def test_method_prepend(self): |
| 104 | + state = Tracestate() |
| 105 | + state.prepend('foo', '1') |
| 106 | + state.prepend('baz', '3') |
| 107 | + state.prepend('bar', '2') |
| 108 | + self.assertEqual(formatter.to_string(state), 'bar=2,baz=3,foo=1') |
| 109 | + |
| 110 | + # modified key-value pair MUST be moved to the beginning of the list |
| 111 | + state.prepend('foo', '1') |
| 112 | + self.assertEqual(formatter.to_string(state), 'foo=1,bar=2,baz=3') |
| 113 | + |
| 114 | + def test_pop(self): |
| 115 | + state = formatter.from_string('foo=1,bar=2,baz=3') |
| 116 | + state.popitem() |
| 117 | + self.assertEqual(formatter.to_string(state), 'foo=1,bar=2') |
| 118 | + state.popitem() |
| 119 | + self.assertEqual(formatter.to_string(state), 'foo=1') |
| 120 | + state.popitem() |
| 121 | + self.assertEqual(formatter.to_string(state), '') |
| 122 | + # raise KeyError exception while trying to pop from nothing |
| 123 | + self.assertRaises(KeyError, lambda: state.popitem()) |
| 124 | + |
| 125 | + def test_set(self): |
| 126 | + state = Tracestate({'bar': '0'}) |
| 127 | + state['foo'] = '1' |
| 128 | + state['bar'] = '2' |
| 129 | + state['baz'] = '3' |
| 130 | + self.assertEqual(formatter.to_string(state), 'bar=2,foo=1,baz=3') |
| 131 | + |
| 132 | + # key SHOULD be string |
| 133 | + self.assertRaises(ValueError, lambda: state.__setitem__(123, 'abc')) |
| 134 | + # value SHOULD NOT be empty string |
| 135 | + self.assertRaises(ValueError, lambda: state.__setitem__('', 'abc')) |
| 136 | + # key SHOULD start with a letter |
| 137 | + self.assertRaises(ValueError, lambda: state.__setitem__('123', 'abc')) |
| 138 | + # key SHOULD NOT have uppercase |
| 139 | + self.assertRaises(ValueError, lambda: state.__setitem__('FOO', 'abc')) |
| 140 | + |
| 141 | + # value SHOULD be string |
| 142 | + self.assertRaises(ValueError, lambda: state.__setitem__('foo', 123)) |
| 143 | + # value SHOULD NOT be empty string |
| 144 | + self.assertRaises(ValueError, lambda: state.__setitem__('foo', '')) |
| 145 | + |
| 146 | + state['foo'] = 'x' * 256 |
| 147 | + # throw if value exceeds 256 bytes |
| 148 | + self.assertRaises(ValueError, lambda: state.__setitem__('foo', 'x' * 257)) |
0 commit comments