Merge "Fake more of the github3 api" into feature/zuulv3
diff --git a/tests/unit/test_streaming.py b/tests/unit/test_streaming.py
index 4bb541a..59dd8b0 100644
--- a/tests/unit/test_streaming.py
+++ b/tests/unit/test_streaming.py
@@ -82,7 +82,7 @@
         s = socket.create_connection((self.host, port))
         self.addCleanup(s.close)
 
-        req = '%s\n' % build_uuid
+        req = '%s\r\n' % build_uuid
         s.sendall(req.encode('utf-8'))
         self.test_streaming_event.set()
 
@@ -196,7 +196,7 @@
                 time.sleep(0.1)
 
         with socket.create_connection(gateway_address) as s:
-            msg = "%s\n" % build_uuid
+            msg = "%s\r\n" % build_uuid
             s.sendall(msg.encode('utf-8'))
             event.set()  # notify we are connected and req sent
             while True:
diff --git a/zuul/lib/fingergw.py b/zuul/lib/fingergw.py
index c89ed0f..746f552 100644
--- a/zuul/lib/fingergw.py
+++ b/zuul/lib/fingergw.py
@@ -66,6 +66,12 @@
         try:
             build_uuid = self.getCommand()
             port_location = self.rpc.get_job_log_stream_address(build_uuid)
+
+            if not port_location:
+                msg = 'Invalid build UUID %s' % build_uuid
+                self.request.sendall(msg.encode('utf-8'))
+                return
+
             self._fingerClient(
                 port_location['server'],
                 port_location['port'],
diff --git a/zuul/lib/log_streamer.py b/zuul/lib/log_streamer.py
index 5c894b4..c778812 100644
--- a/zuul/lib/log_streamer.py
+++ b/zuul/lib/log_streamer.py
@@ -56,8 +56,6 @@
             self.request.sendall(msg.encode("utf-8"))
             return
 
-        build_uuid = build_uuid.rstrip()
-
         # validate build ID
         if not re.match("[0-9A-Fa-f]+$", build_uuid):
             msg = 'Build ID %s is not valid' % build_uuid
diff --git a/zuul/lib/streamer_utils.py b/zuul/lib/streamer_utils.py
index 985f3c3..43bc286 100644
--- a/zuul/lib/streamer_utils.py
+++ b/zuul/lib/streamer_utils.py
@@ -60,7 +60,8 @@
                 ret = buffer.decode('utf-8')
                 x = ret.find('\n')
                 if x > 0:
-                    return ret[:x]
+                    # rstrip to remove any other unnecessary chars (e.g. \r)
+                    return ret[:x].rstrip()
             except UnicodeDecodeError:
                 pass
 
diff --git a/zuul/model.py b/zuul/model.py
index cc2fea7..16a701d 100644
--- a/zuul/model.py
+++ b/zuul/model.py
@@ -1386,6 +1386,8 @@
         build.build_set = self
 
     def removeBuild(self, build):
+        if build.job.name not in self.builds:
+            return
         self.tries[build.job.name] += 1
         del self.builds[build.job.name]