nebraska: Prepare unittests for move to chromite.
Also fix new nebraska lint errors.
BUG=chromium:1003986
TEST=./run_unittests
copy into chromite, soft link to scripts/wrapper.py, and run unittest
cros flash --debug --clear-cache ssh://100.90.29.199 xBuddy://remote/kevin-release/R79-12591.0.0
Change-Id: I1aa4f96ac31b26b243762fcb60df34ff17d6d07f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/1862214
Tested-by: Achuith Bhandarkar <[email protected]>
Commit-Queue: Achuith Bhandarkar <[email protected]>
Reviewed-by: Amin Hassani <[email protected]>
diff --git a/nebraska/nebraska.py b/nebraska/nebraska.py
index cbde3ca..d31533f 100755
--- a/nebraska/nebraska.py
+++ b/nebraska/nebraska.py
@@ -22,11 +22,11 @@
import threading
import traceback
-from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from datetime import datetime, time
from xml.dom import minidom
from xml.etree import ElementTree
+from six.moves import BaseHTTPServer
from six.moves import http_client
from six.moves import urllib
@@ -168,7 +168,7 @@
# Filter out the None elements into a set.
unique_attrs = set(x for x in all_attrs if x is not None)
- if len(unique_attrs) == 0:
+ if not unique_attrs:
raise NebraskaErrorInvalidRequest('"{}" attribute should appear in at '
'least one app.'.format(attribute))
if len(unique_attrs) > 1:
@@ -627,9 +627,8 @@
# Omaha response contains the hex value of that hash. So here convert the
# value from base64 to hex so nebraska can send the correct version to the
# client. See b/131762584.
- self.sha256_hex = base64.b64decode(
- app_data[self.SHA256_HEX_KEY]).encode('hex')
self.sha256 = app_data[self.SHA256_HEX_KEY]
+ self.sha256_hex = base64.b16encode(base64.b64decode(self.sha256))
self.url = None # Determined per-request.
def __str__(self):
@@ -761,7 +760,7 @@
self._server_thread = None
self._created_runtime_root = False
- class NebraskaHandler(BaseHTTPRequestHandler):
+ class NebraskaHandler(BaseHTTPServer.BaseHTTPRequestHandler):
"""HTTP request handler for Omaha requests."""
def _SendResponse(self, content_type, response, code=http_client.OK):
@@ -861,8 +860,8 @@
def Start(self):
"""Starts the nebraska server."""
- self._httpd = HTTPServer(('', self.GetPort()),
- NebraskaServer.NebraskaHandler)
+ self._httpd = BaseHTTPServer.HTTPServer(('', self.GetPort()),
+ NebraskaServer.NebraskaHandler)
self._port = self._httpd.server_port
if self._runtime_root:
diff --git a/nebraska/nebraska_unittest.py b/nebraska/nebraska_unittest.py
index 0a69476..6967526 100755
--- a/nebraska/nebraska_unittest.py
+++ b/nebraska/nebraska_unittest.py
@@ -18,6 +18,7 @@
from xml.etree import ElementTree
import mock
+from six.moves import builtins
from six.moves import http_client
import nebraska
@@ -148,8 +149,9 @@
nebraska_handler.path = 'http://test.com/update'
test_response = 'foobar'
- with mock.patch('nebraska.Nebraska.GetResponseToRequest') as response_mock:
- with mock.patch('nebraska.Request') as _:
+ with mock.patch.object(nebraska.Nebraska,
+ 'GetResponseToRequest') as response_mock:
+ with mock.patch.object(nebraska, 'Request'):
response_mock.return_value = test_response
nebraska_handler.do_POST()
@@ -163,8 +165,9 @@
nebraska_handler = MockNebraskaHandler()
nebraska_handler.path = 'http://test.com/update/?critical_update=True'
- with mock.patch('nebraska.Nebraska.GetResponseToRequest') as response_mock:
- with mock.patch('nebraska.Request') as _:
+ with mock.patch.object(nebraska.Nebraska,
+ 'GetResponseToRequest') as response_mock:
+ with mock.patch.object(nebraska, 'Request'):
nebraska_handler.do_POST()
response_mock.assert_called_once_with(mock.ANY, critical_update=True,
@@ -175,8 +178,9 @@
nebraska_handler = MockNebraskaHandler()
nebraska_handler.path = 'http://test.com/update/?no_update=True'
- with mock.patch('nebraska.Nebraska.GetResponseToRequest') as response_mock:
- with mock.patch('nebraska.Request') as _:
+ with mock.patch.object(nebraska.Nebraska,
+ 'GetResponseToRequest') as response_mock:
+ with mock.patch.object(nebraska, 'Request'):
nebraska_handler.do_POST()
response_mock.assert_called_once_with(mock.ANY, critical_update=False,
@@ -198,8 +202,8 @@
nebraska_handler = MockNebraskaHandler()
nebraska_handler.path = 'http://test.com/update'
- with mock.patch('nebraska.traceback') as traceback_mock:
- with mock.patch('nebraska.Request.ParseRequest') as parse_mock:
+ with mock.patch.object(nebraska, 'traceback') as traceback_mock:
+ with mock.patch.object(nebraska.Request, 'ParseRequest') as parse_mock:
parse_mock.side_effect = nebraska.NebraskaErrorInvalidRequest
nebraska_handler.do_POST()
@@ -212,8 +216,8 @@
nebraska_handler = MockNebraskaHandler()
nebraska_handler.path = 'http://test.com/update'
- with mock.patch('nebraska.traceback') as traceback_mock:
- with mock.patch('nebraska.Response') as response_mock:
+ with mock.patch.object(nebraska, 'traceback') as traceback_mock:
+ with mock.patch.object(nebraska, 'Response') as response_mock:
response_instance = response_mock.return_value
response_instance.GetXMLString.side_effect = Exception
nebraska_handler.do_POST()
@@ -247,17 +251,18 @@
nebraska_instance = nebraska.Nebraska(_PAYLOAD_ADDRESS, _PAYLOAD_ADDRESS)
server = nebraska.NebraskaServer(nebraska_instance, port=_NEBRASKA_PORT)
- with mock.patch('nebraska.HTTPServer') as server_mock:
- with mock.patch('nebraska.threading.Thread') as thread_mock:
+ with mock.patch.object(nebraska.BaseHTTPServer,
+ 'HTTPServer') as server_mock:
+ with mock.patch.object(nebraska.threading, 'Thread') as thread_mock:
server.Start()
server_mock.assert_called_once_with(
('', _NEBRASKA_PORT), nebraska.NebraskaServer.NebraskaHandler)
# pylint: disable=protected-access
- thread_mock.assert_has_calls((
+ thread_mock.assert_has_calls([
mock.call(target=server._httpd.serve_forever),
- mock.call().start()))
+ mock.call().start()])
def testStop(self):
"""Tests Stop."""
@@ -283,8 +288,8 @@
port_file = os.path.join(runtime_root, 'port')
pid_file = os.path.join(runtime_root, 'pid')
- with mock.patch('nebraska.HTTPServer') as _:
- with mock.patch('nebraska.threading.Thread') as _:
+ with mock.patch.object(nebraska.BaseHTTPServer, 'HTTPServer'):
+ with mock.patch.object(nebraska.threading, 'Thread'):
server.Start()
# Make sure files are created and written with correct values.
@@ -406,8 +411,8 @@
def testScanEmpty(self):
"""Tests Scan on an empty directory."""
- with mock.patch('nebraska.os.listdir') as listdir_mock:
- with mock.patch('nebraska.open') as open_mock:
+ with mock.patch('os.listdir') as listdir_mock:
+ with mock.patch.object(builtins, 'open') as open_mock:
listdir_mock.return_value = []
app_index = nebraska.AppIndex(_INSTALL_DIR)
app_index.Scan()
@@ -417,8 +422,8 @@
def testScanNoJson(self):
"""Tests Scan on a directory with no JSON files."""
- with mock.patch('nebraska.os.listdir') as listdir_mock:
- with mock.patch('nebraska.open') as open_mock:
+ with mock.patch('os.listdir') as listdir_mock:
+ with mock.patch.object(builtins, 'open') as open_mock:
listdir_mock.return_value = ['foo.bin', 'bar.bin', 'json']
app_index = nebraska.AppIndex(_INSTALL_DIR)
app_index.Scan()
@@ -429,8 +434,8 @@
def testScanMultiple(self):
"""Tests Scan on a directory with multiple appids."""
# Providing some mock properties and non-properties files.
- with mock.patch('nebraska.os.listdir') as listdir_mock:
- with mock.patch('nebraska.open') as open_mock:
+ with mock.patch('os.listdir') as listdir_mock:
+ with mock.patch.object(builtins, 'open') as open_mock:
listdir_mock.return_value = [
'foo_install.json',
'foo_update.json',
@@ -461,8 +466,8 @@
def testScanInvalidJson(self):
"""Tests Scan with invalid JSON files."""
# Providing some mock properties and non-properties files.
- with mock.patch('nebraska.os.listdir') as listdir_mock:
- with mock.patch('nebraska.open') as open_mock:
+ with mock.patch('os.listdir') as listdir_mock:
+ with mock.patch.object(builtins, 'open') as open_mock:
listdir_mock.return_value = [
'foo_install.json',
'foo_update.json',
@@ -489,8 +494,8 @@
def testScanInvalidApp(self):
"""Tests Scan on JSON files lacking required keys."""
# Providing some mock properties and non-properties files.
- with mock.patch('nebraska.os.listdir') as listdir_mock:
- with mock.patch('nebraska.open') as open_mock:
+ with mock.patch('os.listdir') as listdir_mock:
+ with mock.patch.object(builtins, 'open') as open_mock:
listdir_mock.return_value = [
'foo_install.json',
'foo_update.json',
@@ -517,8 +522,8 @@
def testContains(self):
"""Tests Constains() correctly finds matching AppData."""
# Providing some mock properties files.
- with mock.patch('nebraska.os.listdir') as listdir_mock:
- with mock.patch('nebraska.open') as open_mock:
+ with mock.patch('os.listdir') as listdir_mock:
+ with mock.patch.object(builtins, 'open') as open_mock:
listdir_mock.return_value = [
'foo.json',
]
@@ -546,8 +551,8 @@
def testContainsEmpty(self):
"""Tests Constains() correctly finds matching AppData with empty appid."""
# Providing some mock properties files.
- with mock.patch('nebraska.os.listdir') as listdir_mock:
- with mock.patch('nebraska.open') as open_mock:
+ with mock.patch('os.listdir') as listdir_mock:
+ with mock.patch.object(builtins, 'open') as open_mock:
listdir_mock.return_value = [
'foo.json',
'empty.json'