Michael Still | 89e4c6d | 2014-01-09 17:38:24 +1100 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 2 | # |
| 3 | # Copyright 2014 Rackspace Australia |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
| 17 | |
| 18 | import fixtures |
| 19 | import logging |
| 20 | import os |
| 21 | import testtools |
| 22 | |
| 23 | from turbo_hipster.lib import utils |
| 24 | |
| 25 | |
| 26 | class TestExecuteToLog(testtools.TestCase): |
| 27 | def test_makes_dir(self): |
| 28 | tempdir = self.useFixture(fixtures.TempDir()).path |
| 29 | self.assertFalse(os.path.exists(os.path.join(tempdir, 'foo'))) |
| 30 | utils.execute_to_log('echo yay', |
| 31 | os.path.join(tempdir, 'foo', 'banana.log'), |
| 32 | watch_logs=[]) |
| 33 | self.assertTrue(os.path.exists(os.path.join(tempdir, 'foo'))) |
| 34 | |
| 35 | def test_logging_works(self): |
| 36 | # Setup python logging to do what we need |
| 37 | logging.basicConfig(format='%(asctime)s %(name)s %(message)s', |
| 38 | level=logging.DEBUG) |
| 39 | |
| 40 | tempdir = self.useFixture(fixtures.TempDir()).path |
| 41 | log_path = os.path.join(tempdir, 'banana.log') |
| 42 | |
| 43 | utils.execute_to_log('echo yay', log_path, watch_logs=[]) |
| 44 | self.assertTrue(os.path.exists(log_path)) |
| 45 | |
| 46 | with open(log_path) as f: |
| 47 | d = f.read() |
| 48 | print d |
| 49 | |
| 50 | self.assertNotEqual('', d) |
Joshua Hesketh | 96052bf | 2014-04-05 19:48:06 +1100 | [diff] [blame] | 51 | self.assertEqual(4, len(d.split('\n'))) |
Michael Still | 89e4c6d | 2014-01-09 17:38:24 +1100 | [diff] [blame] | 52 | self.assertNotEqual(-1, d.find('yay')) |
| 53 | self.assertNotEqual(-1, d.find('[script exit code = 0]')) |
| 54 | |
| 55 | def test_timeout(self): |
| 56 | # Setup python logging to do what we need |
| 57 | logging.basicConfig(format='%(asctime)s %(name)s %(message)s', |
| 58 | level=logging.DEBUG) |
| 59 | |
| 60 | tempdir = self.useFixture(fixtures.TempDir()).path |
| 61 | log_path = os.path.join(tempdir, 'banana.log') |
| 62 | |
| 63 | utils.execute_to_log('/bin/sleep 30', log_path, watch_logs=[], |
| 64 | timeout=0.1) |
| 65 | self.assertTrue(os.path.exists(log_path)) |
| 66 | |
| 67 | with open(log_path) as f: |
| 68 | d = f.read() |
| 69 | print d |
| 70 | |
| 71 | self.assertNotEqual('', d) |
| 72 | self.assertNotEqual(-1, d.find('[timeout]')) |
| 73 | self.assertNotEqual(-1, d.find('[script exit code = -9]')) |