OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 3 # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 # that can be found in the LICENSE file. |
| 5 |
| 6 import logging |
| 7 import os |
| 8 import sys |
| 9 import unittest |
| 10 |
| 11 ROOT_DIR = os.path.dirname(os.path.abspath(os.path.join( |
| 12 __file__.decode(sys.getfilesystemencoding()), |
| 13 os.pardir, os.pardir))) |
| 14 sys.path.insert(0, ROOT_DIR) |
| 15 |
| 16 from libs.luci_context import luci_context |
| 17 |
| 18 class TestLuciContext(unittest.TestCase): |
| 19 def setUp(self): |
| 20 self.ek = luci_context._ENV_KEY |
| 21 # Makes all logged messages go into unittest's buffer to be revealed on test |
| 22 # failure. |
| 23 logging.root.handlers[0].stream = sys.stdout |
| 24 |
| 25 def tearDown(self): |
| 26 self.assertNotIn( |
| 27 self.ek, os.environ, '%s in environ (%r)! Possible leak in test?' % ( |
| 28 self.ek, os.environ.get(self.ek))) |
| 29 luci_context._CUR_CONTEXT = None |
| 30 |
| 31 def test_ok(self): |
| 32 self.assertFalse(luci_context._check_ok('hi')) |
| 33 self.assertFalse(luci_context._check_ok({'hi': 'there'})) |
| 34 self.assertFalse(luci_context._check_ok({'hi': 'there', |
| 35 'ok': {'thing': 'true'}})) |
| 36 self.assertTrue(luci_context._check_ok({'ok': {'thing': 'true'}})) |
| 37 |
| 38 def test_initial_load_dne(self): |
| 39 self.assertDictEqual(luci_context.read_full(), {}) |
| 40 self.assertDictEqual(luci_context._CUR_CONTEXT, {}) |
| 41 |
| 42 def nope(): |
| 43 raise Exception('I SHOULD NOT BE CALLED') |
| 44 og_load = luci_context._initial_load |
| 45 luci_context._initial_load = nope |
| 46 try: |
| 47 self.assertIsNone(luci_context.read('section')) |
| 48 finally: |
| 49 luci_context._initial_load = og_load |
| 50 |
| 51 def test_initial_load_not_json(self): |
| 52 with luci_context._tf("not json", data_raw=True) as name: |
| 53 os.environ[self.ek] = name |
| 54 try: |
| 55 self.assertDictEqual(luci_context.read_full(), {}) |
| 56 self.assertDictEqual(luci_context._CUR_CONTEXT, {}) |
| 57 finally: |
| 58 del os.environ[self.ek] |
| 59 |
| 60 def test_initial_load_cannot_read(self): |
| 61 with luci_context._tf({'something': {'data': True}}) as name: |
| 62 os.chmod(name, 0) |
| 63 os.environ[self.ek] = name |
| 64 try: |
| 65 self.assertDictEqual(luci_context.read_full(), {}) |
| 66 self.assertDictEqual(luci_context._CUR_CONTEXT, {}) |
| 67 finally: |
| 68 del os.environ[self.ek] |
| 69 |
| 70 def test_initial_load_not_dict(self): |
| 71 with luci_context._tf('hi') as name: |
| 72 os.environ[self.ek] = name |
| 73 try: |
| 74 self.assertDictEqual(luci_context.read_full(), {}) |
| 75 self.assertDictEqual(luci_context._CUR_CONTEXT, {}) |
| 76 finally: |
| 77 del os.environ[self.ek] |
| 78 |
| 79 def test_initial_load_not_subsection_dict(self): |
| 80 with luci_context._tf({'something': 'string'}) as name: |
| 81 os.environ[self.ek] = name |
| 82 try: |
| 83 self.assertDictEqual(luci_context.read_full(), {}) |
| 84 self.assertDictEqual(luci_context._CUR_CONTEXT, {}) |
| 85 finally: |
| 86 del os.environ[self.ek] |
| 87 |
| 88 def test_initial_load_win(self): |
| 89 with luci_context.write(something={'data': True}): |
| 90 self.assertDictEqual(luci_context.read_full(), |
| 91 {'something': {'data': True}}) |
| 92 self.assertDictEqual(luci_context._CUR_CONTEXT, |
| 93 {'something': {'data': True}}) |
| 94 self.assertDictEqual(luci_context.read('something'), {'data': True}) |
| 95 |
| 96 def test_nested(self): |
| 97 w = luci_context.write |
| 98 r = luci_context.read |
| 99 with w(something={'data': True}): |
| 100 self.assertIsNone(r('other')) |
| 101 self.assertDictEqual(r('something'), {'data': True}) |
| 102 |
| 103 with w(other={'not': 10}, something=None): |
| 104 self.assertIsNone(r('something')) |
| 105 self.assertDictEqual(r('other'), {'not': 10}) |
| 106 |
| 107 self.assertIsNone(r('other')) |
| 108 self.assertDictEqual(r('something'), {'data': True}) |
| 109 |
| 110 self.assertIsNone(r('other')) |
| 111 self.assertIsNone(r('something')) |
| 112 |
| 113 def test_stage(self): |
| 114 path = None |
| 115 with luci_context.stage(something={'data': True}) as path: |
| 116 with open(path, 'r') as f: |
| 117 self.assertEqual('{"something": {"data": true}}', f.read()) |
| 118 # The file is gone outside 'with' block. |
| 119 self.assertFalse(os.path.exists(path)) |
| 120 |
| 121 |
| 122 if __name__ == '__main__': |
| 123 # Pop it out of the environment to make sure we start clean. |
| 124 logging.basicConfig() |
| 125 os.environ.pop(luci_context._ENV_KEY, None) |
| 126 unittest.main(buffer=True) |
OLD | NEW |