Skip to content

Commit

Permalink
SmartOS: fixes for python3 reading from serial device.
Browse files Browse the repository at this point in the history
We were hitting exceptions when writing to the SmartOS serial console and, once
that was fixed, we were hanging permanently waiting for b"." == "." to be true.

This fixes both of those issues.
  • Loading branch information
smoser committed Mar 17, 2015
2 parents 516af9b + c8a7b44 commit ad820a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
5 changes: 3 additions & 2 deletions cloudinit/sources/DataSourceSmartOS.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ def query_data(noun, seed_device, seed_timeout, strip=False, default=None,
return False

ser = get_serial(seed_device, seed_timeout)
ser.write("GET %s\n" % noun.rstrip())
request_line = "GET %s\n" % noun.rstrip()
ser.write(request_line.encode('ascii'))
status = str(ser.readline()).rstrip()
response = []
eom_found = False
Expand All @@ -329,7 +330,7 @@ def query_data(noun, seed_device, seed_timeout, strip=False, default=None,
return default

while not eom_found:
m = ser.readline()
m = ser.readline().decode('ascii')
if m.rstrip() == ".":
eom_found = True
else:
Expand Down
15 changes: 10 additions & 5 deletions tests/unittests/test_datasource/test_smartos.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import stat
import uuid

import six


MOCK_RETURNS = {
'hostname': 'test-host',
Expand Down Expand Up @@ -78,24 +80,27 @@ def isOpen(self):
return True

def write(self, line):
line = line.replace('GET ', '')
if not isinstance(line, six.binary_type):
raise TypeError("Should be writing binary lines.")
line = line.decode('ascii').replace('GET ', '')
self.last = line.rstrip()

def readline(self):
if self.new:
self.new = False
if self.last in self.mockdata:
return 'SUCCESS\n'
line = 'SUCCESS\n'
else:
return 'NOTFOUND %s\n' % self.last
line = 'NOTFOUND %s\n' % self.last

if self.last in self.mockdata:
elif self.last in self.mockdata:
if not self.mocked_out:
self.mocked_out = [x for x in self._format_out()]

if len(self.mocked_out) > self.count:
self.count += 1
return self.mocked_out[self.count - 1]
line = self.mocked_out[self.count - 1]
return line.encode('ascii')

def _format_out(self):
if self.last in self.mockdata:
Expand Down

0 comments on commit ad820a9

Please sign in to comment.