Clint Byrum | 5870cca | 2017-04-04 16:20:00 -0700 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
| 13 | import fixtures |
| 14 | import logging |
| 15 | import subprocess |
| 16 | import tempfile |
| 17 | import testtools |
James E. Blair | aa4199c | 2017-07-06 08:51:19 -0700 | [diff] [blame] | 18 | import time |
Tristan Cacqueray | 2438860 | 2017-06-11 23:40:57 +0000 | [diff] [blame] | 19 | import os |
Clint Byrum | 5870cca | 2017-04-04 16:20:00 -0700 | [diff] [blame] | 20 | |
| 21 | from zuul.driver import bubblewrap |
| 22 | from zuul.executor.server import SshAgent |
James E. Blair | aa4199c | 2017-07-06 08:51:19 -0700 | [diff] [blame] | 23 | from tests.base import iterate_timeout |
Clint Byrum | 5870cca | 2017-04-04 16:20:00 -0700 | [diff] [blame] | 24 | |
| 25 | |
| 26 | class TestBubblewrap(testtools.TestCase): |
| 27 | def setUp(self): |
| 28 | super(TestBubblewrap, self).setUp() |
| 29 | self.log_fixture = self.useFixture( |
| 30 | fixtures.FakeLogger(level=logging.DEBUG)) |
| 31 | self.useFixture(fixtures.NestedTempfile()) |
| 32 | |
| 33 | def test_bubblewrap_wraps(self): |
| 34 | bwrap = bubblewrap.BubblewrapDriver() |
James E. Blair | ce56ff9 | 2017-08-18 14:39:06 -0700 | [diff] [blame] | 35 | context = bwrap.getExecutionContext() |
Clint Byrum | 5870cca | 2017-04-04 16:20:00 -0700 | [diff] [blame] | 36 | work_dir = tempfile.mkdtemp() |
Clint Byrum | 5870cca | 2017-04-04 16:20:00 -0700 | [diff] [blame] | 37 | ssh_agent = SshAgent() |
| 38 | self.addCleanup(ssh_agent.stop) |
| 39 | ssh_agent.start() |
James E. Blair | ce56ff9 | 2017-08-18 14:39:06 -0700 | [diff] [blame] | 40 | po = context.getPopen(work_dir=work_dir, |
| 41 | ssh_auth_sock=ssh_agent.env['SSH_AUTH_SOCK']) |
James E. Blair | d6a71ca | 2017-08-18 14:15:05 -0700 | [diff] [blame] | 42 | self.assertTrue(po.fds[0] > 2) |
| 43 | self.assertTrue(po.fds[1] > 2) |
Clint Byrum | 5870cca | 2017-04-04 16:20:00 -0700 | [diff] [blame] | 44 | self.assertTrue(work_dir in po.command) |
Clint Byrum | 5870cca | 2017-04-04 16:20:00 -0700 | [diff] [blame] | 45 | # Now run /usr/bin/id to verify passwd/group entries made it in |
| 46 | true_proc = po(['/usr/bin/id'], stdout=subprocess.PIPE, |
| 47 | stderr=subprocess.PIPE) |
| 48 | (output, errs) = true_proc.communicate() |
| 49 | # Make sure it printed things on stdout |
| 50 | self.assertTrue(len(output.strip())) |
| 51 | # And that it did not print things on stderr |
| 52 | self.assertEqual(0, len(errs.strip())) |
| 53 | # Make sure the _r's are closed |
James E. Blair | d6a71ca | 2017-08-18 14:15:05 -0700 | [diff] [blame] | 54 | self.assertEqual([], po.fds) |
Tristan Cacqueray | 2438860 | 2017-06-11 23:40:57 +0000 | [diff] [blame] | 55 | |
| 56 | def test_bubblewrap_leak(self): |
| 57 | bwrap = bubblewrap.BubblewrapDriver() |
James E. Blair | ce56ff9 | 2017-08-18 14:39:06 -0700 | [diff] [blame] | 58 | context = bwrap.getExecutionContext() |
Tristan Cacqueray | 2438860 | 2017-06-11 23:40:57 +0000 | [diff] [blame] | 59 | work_dir = tempfile.mkdtemp() |
| 60 | ansible_dir = tempfile.mkdtemp() |
| 61 | ssh_agent = SshAgent() |
| 62 | self.addCleanup(ssh_agent.stop) |
| 63 | ssh_agent.start() |
James E. Blair | ce56ff9 | 2017-08-18 14:39:06 -0700 | [diff] [blame] | 64 | po = context.getPopen(work_dir=work_dir, |
| 65 | ansible_dir=ansible_dir, |
| 66 | ssh_auth_sock=ssh_agent.env['SSH_AUTH_SOCK']) |
James E. Blair | aa4199c | 2017-07-06 08:51:19 -0700 | [diff] [blame] | 67 | leak_time = 60 |
Tristan Cacqueray | 2438860 | 2017-06-11 23:40:57 +0000 | [diff] [blame] | 68 | # Use hexadecimal notation to avoid false-positive |
| 69 | true_proc = po(['bash', '-c', 'sleep 0x%X & disown' % leak_time]) |
| 70 | self.assertEqual(0, true_proc.wait()) |
| 71 | cmdline = "sleep\x000x%X\x00" % leak_time |
James E. Blair | aa4199c | 2017-07-06 08:51:19 -0700 | [diff] [blame] | 72 | for x in iterate_timeout(30, "process to exit"): |
| 73 | sleep_proc = [pid for pid in os.listdir("/proc") if |
| 74 | os.path.isfile("/proc/%s/cmdline" % pid) and |
| 75 | open("/proc/%s/cmdline" % pid).read() == cmdline] |
| 76 | if not sleep_proc: |
| 77 | break |
| 78 | time.sleep(1) |