-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #372 from rmmh/g8r-testgrid
Link build results to testgrid (fixes #272).
- Loading branch information
Showing
11 changed files
with
530 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#!/usr/bin/env python | ||
# Copyright 2016 The Kubernetes Authors All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
''' | ||
A tiny, minimal protobuf2 parser that's able to extract enough information | ||
to be useful. | ||
''' | ||
|
||
import cStringIO as StringIO | ||
|
||
|
||
def parse_protobuf(data, schema=None): | ||
''' | ||
Do a simple parse of a protobuf2 given minimal type information. | ||
Args: | ||
data: a string containing the encoded protocol buffer. | ||
schema: a dict containing information about each field number. | ||
The keys are field numbers, and the values represent: | ||
- str: the name of the field | ||
- dict: schema to recursively decode an embedded message. | ||
May contain a 'name' key to name the field. | ||
Returns: | ||
dict: mapping from fields to values. The fields may be strings instead of | ||
numbers if schema named them, and the value will *always* be | ||
a list of values observed for that key. | ||
''' | ||
if schema is None: | ||
schema = {} | ||
|
||
buf = StringIO.StringIO(data) | ||
|
||
def read_varint(): | ||
out = 0 | ||
shift = 0 | ||
c = 0x80 | ||
while c & 0x80: | ||
c = ord(buf.read(1)) | ||
out = out | ((c & 0x7f) << shift) | ||
shift += 7 | ||
return out | ||
|
||
values = {} | ||
|
||
while buf.tell() < len(data): | ||
key = read_varint() | ||
wire_type = key & 0b111 | ||
field_number = key >> 3 | ||
field_name = field_number | ||
if wire_type == 0: | ||
value = read_varint() | ||
elif wire_type == 1: # 64-bit | ||
value = buf.read(8) | ||
elif wire_type == 2: # length-delim | ||
length = read_varint() | ||
value = buf.read(length) | ||
if isinstance(schema.get(field_number), basestring): | ||
field_name = schema[field_number] | ||
elif field_number in schema: | ||
# yes, I'm using dynamic features of a dynamic language. | ||
# pylint: disable=redefined-variable-type | ||
value = parse_protobuf(value, schema[field_number]) | ||
field_name = schema[field_number].get('name', field_name) | ||
elif wire_type == 5: # 32-bit | ||
value = buf.read(4) | ||
else: | ||
raise ValueError('unhandled wire type %d' % wire_type) | ||
values.setdefault(field_name, []).append(value) | ||
|
||
return values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright 2016 The Kubernetes Authors All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
|
||
import pb_glance | ||
|
||
|
||
def tostr(data): | ||
if isinstance(data, list): | ||
return ''.join(c if isinstance(c, str) else chr(c) for c in data) | ||
return data | ||
|
||
|
||
class PBGlanceTest(unittest.TestCase): | ||
def expect(self, data, expected, types=None): | ||
result = pb_glance.parse_protobuf(tostr(data), types) | ||
self.assertEqual(result, expected) | ||
|
||
def test_basic(self): | ||
self.expect( | ||
[0, 1, # varint | ||
0, 0x96, 1, # multi-byte varint | ||
(1<<3)|1, 'abcdefgh', # 64-bit | ||
(2<<3)|2, 5, 'value', # length-delimited (string) | ||
(3<<3)|5, 'abcd', # 32-bit | ||
], | ||
{ | ||
0: [1, 150], | ||
1: ['abcdefgh'], | ||
2: ['value'], | ||
3: ['abcd'], | ||
}) | ||
|
||
def test_embedded(self): | ||
self.expect([2, 2, 3<<3, 1], {0: [{3: [1]}]}, {0: {}}) | ||
|
||
def test_field_names(self): | ||
self.expect([2, 2, 'hi'], {'greeting': ['hi']}, {0: 'greeting'}) | ||
|
||
def test_embedded_names(self): | ||
self.expect( | ||
[2, 4, (3<<3)|2, 2, 'hi'], | ||
{'msg': [{'greeting': ['hi']}]}, | ||
{0: {'name': 'msg', 3: 'greeting'}}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.