github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/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              Stub(kubernetes_bazel, 'check', self.fake_check),
    62              Stub(kubernetes_bazel, 'check_output', self.fake_check),
    63              Stub(kubernetes_bazel, 'query', self.fake_query),
    64              Stub(kubernetes_bazel, 'get_version', self.fake_version),
    65              Stub(kubernetes_bazel, 'clean_file_in_dir', self.fake_clean),
    66          ]
    67  
    68      def tearDown(self):
    69          for stub in self.boiler:
    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(_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/... //c/...'
   128              ])
   129          kubernetes_bazel.main(args)
   130  
   131          call = self.callstack[-2]
   132          self.assertIn('//b/...', call)
   133          self.assertIn('//c/...', call)
   134  
   135      def test_expand_arg(self):
   136          """Make sure flags are expanded properly."""
   137          args = kubernetes_bazel.parse_args([
   138              '--test-args=--foo',
   139              '--test-args=--bar',
   140              '--test=//b/... //c/...'
   141              ])
   142          kubernetes_bazel.main(args)
   143  
   144          call = self.callstack[-2]
   145          self.assertIn('--foo', call)
   146          self.assertIn('--bar', call)
   147          self.assertIn('//b/...', call)
   148          self.assertIn('//c/...', call)
   149  
   150  
   151      def test_all_bazel(self):
   152          """Make sure all commands starts with bazel except for coarse."""
   153          args = kubernetes_bazel.parse_args([
   154              '--build=//a',
   155              '--test=//b',
   156              '--release=//c'
   157              ])
   158          kubernetes_bazel.main(args)
   159  
   160          for call in self.callstack[:-2]:
   161              self.assertTrue(call.startswith('bazel'), call)
   162  
   163      def test_install(self):
   164          """Make sure install is called as 1st scenario call."""
   165          with tempfile.NamedTemporaryFile(delete=False) as fp:
   166              install = fp.name
   167          args = kubernetes_bazel.parse_args([
   168              '--install=%s' % install,
   169              ])
   170          kubernetes_bazel.main(args)
   171  
   172          self.assertIn(install, self.callstack[0])
   173  
   174      def test_install_fail(self):
   175          """Make sure install fails if path does not exist."""
   176          args = kubernetes_bazel.parse_args([
   177              '--install=foo',
   178              ])
   179          with self.assertRaises(ValueError):
   180              kubernetes_bazel.main(args)
   181  
   182      def test_affected(self):
   183          """--test=affected will work."""
   184          args = kubernetes_bazel.parse_args([
   185              '--affected',
   186              ])
   187          with self.assertRaises(ValueError):
   188              kubernetes_bazel.main(args)
   189  
   190          with Stub(kubernetes_bazel, 'get_changed', self.fake_changed_valid):
   191              with Stub(os, 'getenv', self.fake_sha):
   192                  kubernetes_bazel.main(args)
   193                  test = self.callstack[-2]
   194                  self.assertIn('//foo', test)
   195                  self.assertIn('//bar', test)
   196  
   197                  build = self.callstack[-3]
   198                  self.assertIn('//foo', build)
   199                  self.assertIn('//bar', build)
   200  
   201      def test_affected_empty(self):
   202          """if --affected returns nothing, then nothing should be triggered"""
   203          args = kubernetes_bazel.parse_args([
   204              '--affected',
   205              ])
   206          with Stub(kubernetes_bazel, 'get_changed', self.fake_changed_empty):
   207              with Stub(os, 'getenv', self.fake_sha):
   208                  kubernetes_bazel.main(args)
   209                  # trigger empty build
   210                  self.assertIn('bazel build', self.callstack)
   211                  # nothing to test
   212                  for call in self.callstack:
   213                      self.assertNotIn('bazel test', call)
   214  
   215      def test_affected_filter(self):
   216          """--test=affected will work."""
   217          args = kubernetes_bazel.parse_args([
   218              '--affected',
   219              '--build=//foo',
   220              '--test=//foo',
   221              ])
   222          with Stub(kubernetes_bazel, 'get_changed', self.fake_changed_valid):
   223              with Stub(os, 'getenv', self.fake_sha):
   224                  kubernetes_bazel.main(args)
   225                  test = self.callstack[-2]
   226                  self.assertIn('//foo', test)
   227                  self.assertNotIn('//bar', test)
   228  
   229                  build = self.callstack[-3]
   230                  self.assertIn('//foo', build)
   231                  self.assertNotIn('//bar', build)
   232  
   233  
   234  if __name__ == '__main__':
   235      unittest.main()