github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/gubernator/pb_glance_test.py (about)

     1  # Copyright 2016 The Kubernetes 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 unittest
    16  
    17  import pb_glance
    18  
    19  
    20  def tostr(data):
    21      if isinstance(data, list):
    22          return ''.join(c if isinstance(c, str) else chr(c) for c in data)
    23      return data
    24  
    25  
    26  class PBGlanceTest(unittest.TestCase):
    27      def expect(self, data, expected, types=None):
    28          result = pb_glance.parse_protobuf(tostr(data), types)
    29          self.assertEqual(result, expected)
    30  
    31      def test_basic(self):
    32          self.expect(
    33              [0, 1,                  # varint
    34               0, 0x96, 1,            # multi-byte varint
    35               (1<<3)|1, 'abcdefgh',  # 64-bit
    36               (2<<3)|2, 5, 'value',  # length-delimited (string)
    37               (3<<3)|5, 'abcd',      # 32-bit
    38              ],
    39              {
    40                  0: [1, 150],
    41                  1: ['abcdefgh'],
    42                  2: ['value'],
    43                  3: ['abcd'],
    44              })
    45  
    46      def test_embedded(self):
    47          self.expect([2, 2, 3<<3, 1], {0: [{3: [1]}]}, {0: {}})
    48  
    49      def test_field_names(self):
    50          self.expect([2, 2, 'hi'], {'greeting': ['hi']}, {0: 'greeting'})
    51  
    52      def test_embedded_names(self):
    53          self.expect(
    54              [2, 4, (3<<3)|2, 2, 'hi'],
    55              {'msg': [{'greeting': ['hi']}]},
    56              {0: {'name': 'msg', 3: 'greeting'}})