github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/scenarios/kubernetes_bazel_test.py (about) 1 #!/usr/bin/env python 2 3 # Copyright 2017 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain 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, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 # Need to figure out why this only fails on travis 18 # pylint: disable=too-few-public-methods 19 20 """Test for kubernetes_bazel.py""" 21 22 import os 23 import string 24 import tempfile 25 import unittest 26 27 import kubernetes_bazel 28 29 30 def fake_pass(*_unused, **_unused2): 31 """Do nothing.""" 32 pass 33 34 def fake_bomb(*a, **kw): 35 """Always raise.""" 36 raise AssertionError('Should not happen', a, kw) 37 38 39 class Stub(object): 40 """Replace thing.param with replacement until exiting with.""" 41 def __init__(self, thing, param, replacement): 42 self.thing = thing 43 self.param = param 44 self.replacement = replacement 45 self.old = getattr(thing, param) 46 setattr(thing, param, self.replacement) 47 48 def __enter__(self, *a, **kw): 49 return self.replacement 50 51 def __exit__(self, *a, **kw): 52 setattr(self.thing, self.param, self.old) 53 54 55 class ScenarioTest(unittest.TestCase): # pylint: disable=too-many-public-methods 56 """Tests for bazel scenario.""" 57 callstack = [] 58 59 def setUp(self): 60 self.boiler = { 61 'check': Stub(kubernetes_bazel, 'check', self.fake_check), 62 'check_output': Stub(kubernetes_bazel, 'check_output', self.fake_check), 63 'query': Stub(kubernetes_bazel.Bazel, 'query', self.fake_query), 64 'get_version': Stub(kubernetes_bazel, 'get_version', self.fake_version), 65 'clean_file_in_dir': Stub(kubernetes_bazel, 'clean_file_in_dir', self.fake_clean), 66 } 67 68 def tearDown(self): 69 for _, stub in self.boiler.items(): 70 with stub: # Leaving with restores things 71 pass 72 self.callstack[:] = [] 73 74 def fake_check(self, *cmd): 75 """Log the command.""" 76 self.callstack.append(string.join(cmd)) 77 78 @staticmethod 79 def fake_sha(key, default): 80 """fake base and pull sha.""" 81 if key == 'PULL_BASE_SHA': 82 return '12345' 83 if key == 'PULL_PULL_SHA': 84 return '67890' 85 return os.getenv(key, default) 86 87 @staticmethod 88 def fake_version(): 89 """return a fake version""" 90 return 'v1.0+abcde' 91 92 @staticmethod 93 def fake_query(_self, _kind, selected, changed): 94 """Simple filter selected by changed.""" 95 if changed == []: 96 return changed 97 if not changed: 98 return selected 99 if not selected: 100 return changed 101 102 ret = [] 103 for pkg in selected: 104 if pkg in changed: 105 ret.append(pkg) 106 107 return ret 108 109 @staticmethod 110 def fake_changed_valid(_base, _pull): 111 """Return fake affected targets.""" 112 return ['//foo', '//bar'] 113 114 @staticmethod 115 def fake_changed_empty(_base, _pull): 116 """Return fake affected targets.""" 117 return [] 118 119 @staticmethod 120 def fake_clean(_dirname, _filename): 121 """Don't clean""" 122 pass 123 124 def test_expand(self): 125 """Make sure flags are expanded properly.""" 126 args = kubernetes_bazel.parse_args([ 127 '--build=//b/... -//b/bb/... //c/...' 128 ]) 129 kubernetes_bazel.main(args) 130 131 call = self.callstack[-2] 132 self.assertIn('//b/...', call) 133 self.assertIn('-//b/bb/...', call) 134 self.assertIn('//c/...', call) 135 136 137 def test_query(self): 138 """Make sure query is constructed properly.""" 139 args = kubernetes_bazel.parse_args([ 140 '--build=//b/... -//b/bb/... //c/...' 141 ]) 142 # temporarily un-stub query 143 with Stub(kubernetes_bazel.Bazel, 'query', self.boiler['query'].old): 144 def check_query(*cmd): 145 self.assertIn( 146 'kind(.*_binary, rdeps(//b/... -//b/bb/... +//c/..., //...))' 147 ' except attr(\'tags\', \'manual\', //...)', 148 cmd 149 ) 150 return '//b/aa/...\n//c/...' 151 with Stub(kubernetes_bazel, 'check_output', check_query): 152 kubernetes_bazel.main(args) 153 154 def test_expand_arg(self): 155 """Make sure flags are expanded properly.""" 156 args = kubernetes_bazel.parse_args([ 157 '--test-args=--foo', 158 '--test-args=--bar', 159 '--test=//b/... //c/...' 160 ]) 161 kubernetes_bazel.main(args) 162 163 call = self.callstack[-2] 164 self.assertIn('--foo', call) 165 self.assertIn('--bar', call) 166 self.assertIn('//b/...', call) 167 self.assertIn('//c/...', call) 168 169 170 def test_all_bazel(self): 171 """Make sure all commands starts with bazel except for coarse.""" 172 args = kubernetes_bazel.parse_args([ 173 '--build=//a', 174 '--test=//b', 175 '--release=//c' 176 ]) 177 kubernetes_bazel.main(args) 178 179 for call in self.callstack[:-2]: 180 self.assertTrue(call.startswith('bazel'), call) 181 182 def test_install(self): 183 """Make sure install is called as 1st scenario call.""" 184 with tempfile.NamedTemporaryFile(delete=False) as fp: 185 install = fp.name 186 args = kubernetes_bazel.parse_args([ 187 '--install=%s' % install, 188 ]) 189 kubernetes_bazel.main(args) 190 191 self.assertIn(install, self.callstack[0]) 192 193 def test_install_fail(self): 194 """Make sure install fails if path does not exist.""" 195 args = kubernetes_bazel.parse_args([ 196 '--install=foo', 197 ]) 198 with self.assertRaises(ValueError): 199 kubernetes_bazel.main(args) 200 201 def test_affected(self): 202 """--test=affected will work.""" 203 args = kubernetes_bazel.parse_args([ 204 '--affected', 205 ]) 206 with self.assertRaises(ValueError): 207 kubernetes_bazel.main(args) 208 209 with Stub(kubernetes_bazel, 'get_changed', self.fake_changed_valid): 210 with Stub(os, 'getenv', self.fake_sha): 211 kubernetes_bazel.main(args) 212 test = self.callstack[-2] 213 self.assertIn('//foo', test) 214 self.assertIn('//bar', test) 215 216 build = self.callstack[-3] 217 self.assertIn('//foo', build) 218 self.assertIn('//bar', build) 219 220 def test_affected_empty(self): 221 """if --affected returns nothing, then nothing should be triggered""" 222 args = kubernetes_bazel.parse_args([ 223 '--affected', 224 ]) 225 with Stub(kubernetes_bazel, 'get_changed', self.fake_changed_empty): 226 with Stub(os, 'getenv', self.fake_sha): 227 kubernetes_bazel.main(args) 228 # trigger empty build 229 self.assertIn('bazel build', self.callstack) 230 # nothing to test 231 for call in self.callstack: 232 self.assertNotIn('bazel test', call) 233 234 def test_affected_filter(self): 235 """--test=affected will work.""" 236 args = kubernetes_bazel.parse_args([ 237 '--affected', 238 '--build=//foo', 239 '--test=//foo', 240 ]) 241 with Stub(kubernetes_bazel, 'get_changed', self.fake_changed_valid): 242 with Stub(os, 'getenv', self.fake_sha): 243 kubernetes_bazel.main(args) 244 test = self.callstack[-2] 245 self.assertIn('//foo', test) 246 self.assertNotIn('//bar', test) 247 248 build = self.callstack[-3] 249 self.assertIn('//foo', build) 250 self.assertNotIn('//bar', build) 251 252 253 if __name__ == '__main__': 254 unittest.main()