1import unittest 2import unit 3 4 5class TestUnitPythonEnvironment(unit.TestUnitApplicationPython): 6 def setUpClass(): 7 unit.TestUnit().check_modules('python') 8 9 def test_python_environment_name_null(self): 10 self.load('environment') 11 12 self.assertIn( 13 'error', 14 self.conf( 15 {"va\0r": "val1"}, 'applications/environment/environment' 16 ), 17 'name null', 18 ) 19 20 def test_python_environment_name_equals(self): 21 self.load('environment') 22 23 self.assertIn( 24 'error', 25 self.conf( 26 {"var=": "val1"}, 'applications/environment/environment' 27 ), 28 'name equals', 29 ) 30 31 def test_python_environment_value_null(self): 32 self.load('environment') 33 34 self.assertIn( 35 'error', 36 self.conf( 37 {"var": "\0val"}, 'applications/environment/environment' 38 ), 39 'value null', 40 ) 41 42 def test_python_environment_update(self): 43 self.load('environment') 44 45 self.conf({"var": "val1"}, 'applications/environment/environment') 46 47 self.assertEqual( 48 self.get( 49 headers={ 50 'Host': 'localhost', 51 'X-Variables': 'var', 52 'Connection': 'close', 53 } 54 )['body'], 55 'val1,', 56 'set', 57 ) 58 59 self.conf({"var": "val2"}, 'applications/environment/environment') 60 61 self.assertEqual( 62 self.get( 63 headers={ 64 'Host': 'localhost', 65 'X-Variables': 'var', 66 'Connection': 'close', 67 } 68 )['body'], 69 'val2,', 70 'update', 71 ) 72 73 def test_python_environment_replace(self): 74 self.load('environment') 75 76 self.conf({"var1": "val1"}, 'applications/environment/environment') 77 78 self.assertEqual( 79 self.get( 80 headers={ 81 'Host': 'localhost', 82 'X-Variables': 'var1', 83 'Connection': 'close', 84 } 85 )['body'], 86 'val1,', 87 'set', 88 ) 89 90 self.conf({"var2": "val2"}, 'applications/environment/environment') 91 92 self.assertEqual( 93 self.get( 94 headers={ 95 'Host': 'localhost', 96 'X-Variables': 'var1,var2', 97 'Connection': 'close', 98 } 99 )['body'], 100 'val2,', 101 'replace', 102 ) 103 104 def test_python_environment_clear(self): 105 self.load('environment') 106 107 self.conf( 108 {"var1": "val1", "var2": "val2"}, 109 'applications/environment/environment', 110 ) 111 112 self.assertEqual( 113 self.get( 114 headers={ 115 'Host': 'localhost', 116 'X-Variables': 'var1,var2', 117 'Connection': 'close', 118 } 119 )['body'], 120 'val1,val2,', 121 'set', 122 ) 123 124 self.conf({}, 'applications/environment/environment') 125 126 self.assertEqual( 127 self.get( 128 headers={ 129 'Host': 'localhost', 130 'X-Variables': 'var1,var2', 131 'Connection': 'close', 132 } 133 )['body'], 134 '', 135 'clear', 136 ) 137 138 def test_python_environment_replace_default(self): 139 self.load('environment') 140 141 pwd_default = self.get( 142 headers={ 143 'Host': 'localhost', 144 'X-Variables': 'PWD', 145 'Connection': 'close', 146 } 147 )['body'] 148 149 self.assertGreater(len(pwd_default), 1, 'get default') 150 151 self.conf({"PWD": "new/pwd"}, 'applications/environment/environment') 152 153 self.assertEqual( 154 self.get( 155 headers={ 156 'Host': 'localhost', 157 'X-Variables': 'PWD', 158 'Connection': 'close', 159 } 160 )['body'], 161 'new/pwd,', 162 'replace default', 163 ) 164 165 self.conf({}, 'applications/environment/environment') 166 167 self.assertEqual( 168 self.get( 169 headers={ 170 'Host': 'localhost', 171 'X-Variables': 'PWD', 172 'Connection': 'close', 173 } 174 )['body'], 175 pwd_default, 176 'restore default', 177 ) 178 179 180if __name__ == '__main__': 181 TestUnitPythonEnvironment.main() 182